반응형

출처: http://cbiscuit.info/114

Java API for CVS
전 우주적으로 가장 활발하게 사용하는 버전컨트롤 도구는 아마도 CVS일지도 모른다아니면 MS SourceSafe 정도보다 강력한 상용 형상관리 도구들이 있지만 발주처 빵빵하고 대규모 프로젝트 아니면 만져볼 기회 조차 없는 것이 현실이다또한, 이런 형상관리 도구는 대부분 Open API를 제공한다지금 몸 담고 있는 프로젝트에서도 CVS 써버를 4대로 구성하여 아주 빵빵하게(?) 돌리고 있는데, 이 마저도 느리고 모자라다고 난리다. 가끔 출몰하는 좀비 cvs.exe때문인데, 그 원인은 아직 밝혀내지 못 하고 있다혹시 아시는 분은 바로 댓글 달아주시면 감사하겠다.

아무튼 이야기가 시작하자마자 바로 옆으로 샛는데, 본론은 Java API를 이용한 CVS 핸들링이다.   CVS와 연계하여 시스템적으로 무엇인가 처리해야 할 일이 있으면 아주 유용하게 쓸 수 있어 소개한다


jar 파일 다운 받는 곳
: 여기
ㅁ 관련 싸이트는 : 여기

사용법은 대충 이렇다.

public class CVSUtil {
  
    private static Log log = LogFactory.getLog(CVSUtil.class);
   
    public static void main(String[] args) {
        String destDir = "
체크아웃 받을 디렉토리";
        //
) D:\\cvs_repository\\test  <- \\ 두개 주의 하삼
!
        String location = "CVSROOT";
        //
) :pserver:anoncvs@localhost:/cvs
        String module = "CVS Module";
        //
) /MyModule
       
        CVSUtil.checkout( destDir, location, module);
    }
   
    public static void checkout(String destDir, String location, String module) {
        CVSRoot cvsroot = CVSRoot.parse(location);
        GlobalOptions globalOptions = new GlobalOptions();
        globalOptions.setCVSRoot(cvsroot.toString());
       
        PServerConnection con = new PServerConnection(cvsroot);
       
        Client client = new Client(con, new StandardAdminHandler());
        client.setLocalPath(destDir);
       
        CheckoutCommand checkout = new CheckoutCommand(true, module);
        try {
            client.executeCommand(checkout, globalOptions);
        } catch (Exception e) {
            log.error("", e);
        }
    }
}


다양한 cvs커맨드를 날릴수 있는 api 사용예제이다. .... 몇일을 인터넷을 뒤져서 겨우 찾은....^^

java cvs functions
Checkout from CVS


public void checkoutFromCVS(String userName, String password, String hostName, String repository) throws ExecutionException{
     PServerConnection c = null;
     try {
      logger.info("entering checkOutImages");
     
      String encryptedPassword = StandardScrambler.getInstance().scramble(password);
     
      CVSRoot root = CVSRoot.parse(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);
      c = (PServerConnection)ConnectionFactory.getConnection(root);
      
       c.setEncodedPassword(encryptedPassword);     
      
       logger.info("open connection....");
       c.open();     
       logger.info("after open connection");
      
       Client client = new Client(c, new StandardAdminHandler()); 
       GlobalOptions options = new GlobalOptions();
       options.setCVSRoot(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);
      
       client.ensureConnection();
      
       CheckoutCommand checkout = new CheckoutCommand(true,SC_MODULE);
       checkout.setPruneDirectories(true);
       File f = new File(SC_CVS_HOME);
       if(!f.exists())
        f.mkdirs();
      
       client.setLocalPath(SC_CVS_HOME);
       client.getEventManager().addCVSListener(new LogCVSListener());
      
       boolean success = client.executeCommand(checkout,options);
       logger.info("cmd checkout execute success:"+success);
       //client.executeCommand(uc, options);
  } catch (CommandAbortedException e) {
   logger.error("command aborted ex:"+e.getMessage());
   throw new ExecutionException(e.getMessage());
  } catch (AuthenticationException e) {
   logger.error("authenticate ex:"+e.getMessage());
   throw new ExecutionException(e.getMessage());
  }catch (CommandException e) {
   logger.error("command ex:"+e.getCause());
   throw new ExecutionException(e.getMessage());
  }   finally {
   if(null != c) {
    try {
     c.close();
    } catch (IOException e) {
     logger.error(e.getMessage());
    }
   }
  }
    }
 



Add to CVS


public void addToCVS(List filesToAdd, File fileToAdd, String userName, String password, String hostName, String repository)throws ExecutionException{
     logger.info("entering addToCVS");
     PServerConnection c = null;
     try {
      String[] children;
       // It is also possible to filter the list of returned files.
       // This example does not return any files that end with `.txt'.
       FilenameFilter filter = new FilenameFilter() {
           public boolean accept(File dir, String name) {
            if(dir.getName().equals("CVS")){
                return false;
               }

               return true;
           }
       };
      children = fileToAdd.list(filter);    
       File f = null;
     
          for (int i=0; i < children.length; i++) {
          
           f = new File(fileToAdd.getAbsoluteFile()+"/"+children[i]);
          
           try {
      logger.info("file:"+f.getCanonicalFile());
      logger.info("is file:"+f.isFile()+" is dir:"+f.isDirectory());
      if(f.isDirectory() ) {
        if(!f.getName().equals("CVS")){
        logger.info("recursive...");      
        filesToAdd.add(f);
        addToCVS(filesToAdd, f, userName, password, hostName, repository);     
        }
      }else {
       filesToAdd.add(f);
      }
           }catch(IOException e) {
            throw new ExecutionException(e.getMessage());
           }
          }
     
          if(filesToAdd.size() == 0) {
           logger.info("nothing to add....");
           return;
          }
         
      String encryptedPassword = StandardScrambler.getInstance().scramble(password);
     
      CVSRoot root = CVSRoot.parse(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);
      c = (PServerConnection)ConnectionFactory.getConnection(root);
      
       c.setEncodedPassword(encryptedPassword);     
      
       logger.info("open connection....");
       c.open();     
       logger.info("after open connection");
      
       Client client = new Client(c, new StandardAdminHandler());       
            
       GlobalOptions options = new GlobalOptions();
       options.setCVSRoot(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);          
      
      
       AddCommand add = new AddCommand();           
       add.setMessage("adding images for site portal...");
       add.setKeywordSubst(KeywordSubstitutionOptions.BINARY);
       logger.info("file path:"+fileToAdd.getPath());
       logger.info("file parent:"+fileToAdd.getParent());
       client.setLocalPath(SC_DIR_TO_SAVE);
       client.ensureConnection();
      
       add.setFiles((File[])filesToAdd.toArray(new File[filesToAdd.size()]));
       client.getEventManager().addCVSListener(new LogCVSListener());
      
       logger.info("executing command...");     
       boolean success = client.executeCommand(add,options);
       logger.info("cmd add execute success:"+success);
                                   
  } catch (CommandAbortedException e) {
   logger.error("command aborted ex:"+e.getMessage());
   throw new ExecutionException(e.getMessage());
  } catch (AuthenticationException e) {
   logger.error("authenticate ex:"+e.getMessage());
   throw new ExecutionException(e.getMessage());
  }catch (CommandException e) {
   logger.error("command ex:"+e.getMessage());
   throw new ExecutionException(e.getMessage());
 
  }finally {
 
   if(null != c) {
    try {
     c.close();
    } catch (IOException e) {
     logger.error(e.getMessage());
    }
   }
  }
     logger.info("leaving addToCVS");
    }
   



Commit to CVS


    public void commitToCVS(File fileToCommit, String userName, String password, String hostName, String repository) throws ExecutionException{
     PServerConnection c = null;
     try {
      logger.info("entering commitToCVS");
     
      String encryptedPassword = StandardScrambler.getInstance().scramble(password);
     
      CVSRoot root = CVSRoot.parse(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);
      c = (PServerConnection)ConnectionFactory.getConnection(root);
      
       c.setEncodedPassword(encryptedPassword);     
      
       logger.info("open connection....");
       c.open();     
       logger.info("after open connection");
      
       Client client = new Client(c, new StandardAdminHandler()); 
       GlobalOptions options = new GlobalOptions();
       options.setCVSRoot(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);
      
      
       client.ensureConnection();
                              
       client.getEventManager().addCVSListener(new BasicListener());
      
       //File file = new File(SC_CVS_HOME+"\\"+SC_MODULE+"\\resize_needed\\"+SC_TESTER);
       File afile[] = new File[] { fileToCommit };
      
       CommitCommand commit = new CommitCommand();      
             commit.setMessage("auto commit adding to cvs...");
             commit.setFiles(afile);
            
            
             commit.setForceCommit(true);
            
       client.getEventManager().addCVSListener(new LogCVSListener());
       client.setLocalPath(SC_DIR_TO_SAVE);
      
       logger.info("executing command...");
       boolean success = client.executeCommand(commit,options);
       logger.info("cmd commit execute success:"+success);

       //client.executeCommand(uc, options);
   }catch (CommandAbortedException e) {
    logger.error("command aborted ex:"+e.getMessage());
    throw new ExecutionException(e.getMessage());
   }catch (AuthenticationException e) {
    logger.error("authenticate ex:"+e.getMessage());
    throw new ExecutionException(e.getMessage());
   }catch (CommandException e) {
    logger.error("command ex:"+e.getCause());
    throw new ExecutionException(e.getMessage());
   }   finally {
    if(null != c) {
     try {
      c.close();
     } catch (IOException e) {
      logger.error(e.getMessage());
     }
    }
   }
   logger.info("leaving commitToCVS");
    }



InnerClass LogCVSListener


static public class LogCVSListener implements CVSListener {
              protected CVSCommandResult result = new CVSCommandResult();
              protected Logger logger  = Logger.getLogger(LogCVSListener.class.getName());
              public LogCVSListener() {
              }
             
              /***
               * @return Returns the result.
               */
              public CVSCommandResult getResult() {
                  return this.result;
              }
             
              /*
               * (non-Javadoc)
               *
               * @see org.netbeans.lib.cvsclient.event.CVSListener#messageSent(org.netbeans.lib.cvsclient.event.MessageEvent)
               */
              public void messageSent(MessageEvent e) {
                  if (e.isError()) {
                      logger.error(e.getMessage());
                      // permet de stocker les traces
                      result.getTrace().append(e.getMessage() + "\n");
                  } else {
                      logger.info(e.getMessage());
                  }
              }
     
              /*
               * (non-Javadoc)
               *
               * @see org.netbeans.lib.cvsclient.event.CVSListener#messageSent(org.netbeans.lib.cvsclient.event.BinaryMessageEvent)
               */
              public void messageSent(BinaryMessageEvent e) {
                  logger.info(e.getMessage());
              }
     
              /*
               * (non-Javadoc)
               *
               * @see org.netbeans.lib.cvsclient.event.CVSListener#fileAdded(org.netbeans.lib.cvsclient.event.FileAddedEvent)
               */
              public void fileAdded(FileAddedEvent e) {
                  logger.debug("File has been added.");
                  result.getFileAdded().add(e.getFilePath());
              }
     
              /* (non-Javadoc)
               * @see org.netbeans.lib.cvsclient.event.CVSListener#fileToRemove(org.netbeans.lib.cvsclient.event.FileToRemoveEvent)
               */
              public void fileToRemove(FileToRemoveEvent e) {
                  logger.debug("File to remove: " + e.getFilePath());
                  //result.getFileToRemove().add(e.getFilePath());
                 
              }
     
              /*
               * (non-Javadoc)
               *
               * @see org.netbeans.lib.cvsclient.event.CVSListener#fileRemoved(org.netbeans.lib.cvsclient.event.FileRemovedEvent)
               */
              public void fileRemoved(FileRemovedEvent e) {
                  logger.debug("File is removed: " + e.getFilePath());
              }
     
              /*
               * (non-Javadoc)
               *
               * @see org.netbeans.lib.cvsclient.event.CVSListener#fileUpdated(org.netbeans.lib.cvsclient.event.FileUpdatedEvent)
               */
              public void fileUpdated(FileUpdatedEvent e) {
                  logger.debug("File has been updated");
                  result.getFileUpdated().add(e.getFilePath());
              }
     
              /*
               * (non-Javadoc)
               *
               * @see org.netbeans.lib.cvsclient.event.CVSListener#fileInfoGenerated(org.netbeans.lib.cvsclient.event.FileInfoEvent)
               */
              public void fileInfoGenerated(FileInfoEvent fileInfoEvent) {
                  logger.debug("File status information has been received");
                  // Avec le diffInfo, possibilité de gestion plus fine des différences.
                  // DiffInformation diffInfo =
                  // ((SimpleDiffBuilder)fileInfoEvent.getSource()).createDiffInformation();
                  result.getFileInfo().add(fileInfoEvent.getInfoContainer());
              }
     
              /*
               * (non-Javadoc)
               *
               * @see org.netbeans.lib.cvsclient.event.CVSListener#commandTerminated(org.netbeans.lib.cvsclient.event.TerminationEvent)
               */
              public void commandTerminated(TerminationEvent e) {
                  if (e.isError()) {
                      logger.error("Server responses has error");
                      result.setError(true);
                  } else {
                      logger.debug("Server responses has OK");
                  }
              }
     
              /*
               * (non-Javadoc)
               *
               * @see org.netbeans.lib.cvsclient.event.CVSListener#moduleExpanded(org.netbeans.lib.cvsclient.event.ModuleExpansionEvent)
               */
              public void moduleExpanded(ModuleExpansionEvent e) {
                  // ne fait rien
              }
     
          }


+ Recent posts