Example #1
1
 private String getCIJobPath(ApplicationInfo appInfo) {
   StringBuilder builder = new StringBuilder(Utility.getProjectHome());
   builder.append(appInfo.getAppDirName());
   builder.append(File.separator);
   builder.append(FOLDER_DOT_PHRESCO);
   builder.append(File.separator);
   builder.append(CI_JOB_INFO_NAME);
   return builder.toString();
 }
Example #2
0
  private void setSvnCredential(CIJob job) throws JDOMException, IOException {
    S_LOGGER.debug("Entering Method CIManagerImpl.setSvnCredential");
    try {
      String jenkinsTemplateDir = Utility.getJenkinsTemplateDir();
      String credentialFilePath = jenkinsTemplateDir + job.getRepoType() + HYPHEN + CREDENTIAL_XML;
      if (debugEnabled) {
        S_LOGGER.debug("credentialFilePath ... " + credentialFilePath);
      }
      File credentialFile = new File(credentialFilePath);

      SvnProcessor processor = new SvnProcessor(credentialFile);

      //			DataInputStream in = new DataInputStream(new FileInputStream(credentialFile));
      //			while (in.available() != 0) {
      //				System.out.println(in.readLine());
      //			}
      //			in.close();

      processor.changeNodeValue("credentials/entry//userName", job.getUserName());
      processor.changeNodeValue("credentials/entry//password", job.getPassword());
      processor.writeStream(new File(Utility.getJenkinsHome() + File.separator + job.getName()));

      // jenkins home location
      String jenkinsJobHome = System.getenv(JENKINS_HOME);
      StringBuilder builder = new StringBuilder(jenkinsJobHome);
      builder.append(File.separator);

      processor.writeStream(new File(builder.toString() + CI_CREDENTIAL_XML));
    } catch (Exception e) {
      S_LOGGER.error(
          "Entered into the catch block of CIManagerImpl.setSvnCredential "
              + e.getLocalizedMessage());
    }
  }
Example #3
0
 // 获得文件长度
 public long getFileSize() {
   int nFileLength = -1;
   try {
     URL url = new URL(siteInfoBean.getSSiteURL());
     HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
     httpConnection.setRequestProperty("User-Agent", "NetFox");
     int responseCode = httpConnection.getResponseCode();
     if (responseCode >= 400) {
       processErrorCode(responseCode);
       return -2; // -2 represent access is error
     }
     String sHeader;
     for (int i = 1; ; i++) {
       // DataInputStream in = new
       // DataInputStream(httpConnection.getInputStream ());
       // Utility.log(in.readLine());
       sHeader = httpConnection.getHeaderFieldKey(i);
       if (sHeader != null) {
         if (sHeader.equals("Content-Length")) {
           nFileLength = Integer.parseInt(httpConnection.getHeaderField(sHeader));
           break;
         }
       } else break;
     }
   } catch (IOException e) {
     e.printStackTrace();
   } catch (Exception e) {
     e.printStackTrace();
   }
   Utility.log(nFileLength);
   return nFileLength;
 }
Example #4
0
 int getStatus(Exception e) {
   if (e instanceof JavaScriptException) {
     JavaScriptException je = (JavaScriptException) e;
     Object value = ((ScriptableObject) je.getValue()).get("message", null);
     if (value != null) {
       String status = Utility.find(value.toString(), "^\\d\\d\\d\\b");
       if (status != null) return Integer.parseInt(status);
     }
   }
   return 500;
 }
Example #5
0
  private void setMailCredential(CIJob job) {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method CIManagerImpl.setMailCredential");
    }
    try {
      String jenkinsTemplateDir = Utility.getJenkinsTemplateDir();
      String mailFilePath = jenkinsTemplateDir + MAIL + HYPHEN + CREDENTIAL_XML;
      if (debugEnabled) {
        S_LOGGER.debug("configFilePath ... " + mailFilePath);
      }
      File mailFile = new File(mailFilePath);

      SvnProcessor processor = new SvnProcessor(mailFile);

      //			DataInputStream in = new DataInputStream(new FileInputStream(mailFile));
      //			while (in.available() != 0) {
      //				System.out.println(in.readLine());
      //			}
      //			in.close();

      // Mail have to go with jenkins running email address
      InetAddress ownIP = InetAddress.getLocalHost();
      processor.changeNodeValue(
          CI_HUDSONURL,
          HTTP_PROTOCOL
              + PROTOCOL_POSTFIX
              + ownIP.getHostAddress()
              + COLON
              + job.getJenkinsPort()
              + FORWARD_SLASH
              + CI
              + FORWARD_SLASH);
      processor.changeNodeValue("smtpAuthUsername", job.getSenderEmailId());
      processor.changeNodeValue("smtpAuthPassword", job.getSenderEmailPassword());
      processor.changeNodeValue("adminAddress", job.getSenderEmailId());

      // jenkins home location
      String jenkinsJobHome = System.getenv(JENKINS_HOME);
      StringBuilder builder = new StringBuilder(jenkinsJobHome);
      builder.append(File.separator);

      processor.writeStream(new File(builder.toString() + CI_MAILER_XML));
    } catch (Exception e) {
      S_LOGGER.error(
          "Entered into the catch block of CIManagerImpl.setMailCredential "
              + e.getLocalizedMessage());
    }
  }
Example #6
0
 public void run() {
   // 获得文件长度
   // 分割文件
   // 实例FileSplitterFetch
   // 启动FileSplitterFetch线程
   // 等待子线程返回
   try {
     if (bFirst) {
       nFileLength = getFileSize();
       if (nFileLength == -1) {
         System.err.println("File Length is not known!");
       } else if (nFileLength == -2) {
         System.err.println("File is not access!");
       } else {
         for (int i = 0; i < nStartPos.length; i++) { // 分割
           nStartPos[i] = (long) (i * (nFileLength / nStartPos.length));
         }
         for (int i = 0; i < nEndPos.length - 1; i++) { // 分割
           nEndPos[i] = nStartPos[i + 1];
         }
         nEndPos[nEndPos.length - 1] = nFileLength;
       }
     }
     // 启动子线程
     fileSplitterFetch = new FileSplitterFetch[nStartPos.length];
     for (int i = 0; i < nStartPos.length; i++) {
       fileSplitterFetch[i] =
           new FileSplitterFetch(
               siteInfoBean.getSSiteURL(),
               siteInfoBean.getSFilePath() + File.separator + siteInfoBean.getSFileName(),
               nStartPos[i],
               nEndPos[i],
               i);
       Utility.log("Thread " + i + " , nStartPos = " + nStartPos[i] + ", nEndPos = " + nEndPos[i]);
       fileSplitterFetch[i].start();
     }
     /*
      * fileSplitterFetch[nPos.length-1] = new
      * FileSplitterFetch(siteInfoBean.getSSiteURL(),
      * siteInfoBean.getSFilePath()+ File.separator +
      * siteInfoBean.getSFileName(),nPos[nPos.length-1],nFileLength,nPos.length-1);
      * Utility.log("Thread " + (nPos.length-1) + " , nStartPos = " +
      * nPos[nPos.length-1] + ", nEndPos = " + nFileLength);
      * fileSplitterFetch[nPos.length-1].start(); 等待子线程结束 int count = 0;
      * 是否结束while循环
      */
     boolean breakWhile = false;
     while (!bStop) {
       write_nPos();
       Utility.sleep(500);
       breakWhile = true;
       for (int i = 0; i < nStartPos.length; i++) {
         if (!fileSplitterFetch[i].bDownOver) {
           breakWhile = false;
           break;
         }
       }
       if (breakWhile) break;
       // count++;
       // if(count>4)
       // siteStop();
     }
     System.err.println("文件下载结束!");
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Example #7
0
  private CIJobStatus configureJob(CIJob job, String jobType) throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method CIManagerImpl.createJob(CIJob job)");
    }
    try {
      cli = getCLI(job);
      List<String> argList = new ArrayList<String>();
      argList.add(jobType);
      argList.add(job.getName());

      String jenkinsTemplateDir = Utility.getJenkinsTemplateDir();
      String configFilePath = jenkinsTemplateDir + job.getRepoType() + HYPHEN + CONFIG_XML;
      if (debugEnabled) {
        S_LOGGER.debug("configFilePath ...  " + configFilePath);
      }

      File configFile = new File(configFilePath);
      ConfigProcessor processor = new ConfigProcessor(configFile);
      customizeNodes(processor, job);

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      if (debugEnabled) {
        S_LOGGER.debug("argList " + argList.toString());
      }
      int result = cli.execute(argList, processor.getConfigAsStream(), System.out, baos);

      String message = "Job created successfully";
      if (result == -1) {
        byte[] byteArray = baos.toByteArray();
        message = new String(byteArray);
      }
      if (debugEnabled) {
        S_LOGGER.debug("message " + message);
      }
      // when svn is selected credential value has to set
      if (SVN.equals(job.getRepoType())) {
        setSvnCredential(job);
      }

      setMailCredential(job);
      return new CIJobStatus(result, message);
    } catch (IOException e) {
      throw new PhrescoException(e);
    } catch (JDOMException e) {
      throw new PhrescoException(e);
    } finally {
      if (cli != null) {
        try {
          cli.close();
        } catch (IOException e) {
          if (debugEnabled) {
            S_LOGGER.error(e.getLocalizedMessage());
          }
        } catch (InterruptedException e) {
          if (debugEnabled) {
            S_LOGGER.error(e.getLocalizedMessage());
          }
        }
      }
    }
  }