public class CVSManager {
private static CVSManager instance;
public String type = ""; //접속타입
public String userID = ""; //아이디
public String userPassword = ""; //패스워드
public String host =""; // IP
public String port = ""; // PORT
public String repoPath = ""; //리파지토리 Path
public String cvsFolderPath = ""; // 로컬 CVS위치
public String cvsModuleName = "";
private boolean isWrite =false;
public HashMap logList = new HashMap();
public String command = "";
public boolean isCollection;
public static CVSManager getInstance() {
if (instance == null) {
instance = new CVSManager();
System.setProperty("javacvs.multiple_commands_warning", "false");
return instance;
}
else {
return instance;
}
}
public String getServerLabel(String type,String user,String password,String host,String port,String repoPath){
String s = ":";
if(!type.equals(""))
s = s + type;
if(!user.equals(""))
s = s + ":" +user;
if(!password.equals(""))
s = s + ":" +password;
if(!host.equals(""))
s = s + "@" +host;
if(!port.equals(""))
s = s + ":" +port;
if(!repoPath.equals(""))
s = s +repoPath;
return s;
}
public boolean isConnection(String location){
try {
PServerConnection server = cvs_Open(location);
server.open();
server.close();
if(server!=null)
return true;
else
return false;
} catch (Exception e){
errorController(e);
return false;
}
}
public PServerConnection cvs_Open(String location){
try {
CVSRoot cvsroot = CVSRoot.parse(location);
GlobalOptions globalOptions = new GlobalOptions();
globalOptions.setCVSRoot(cvsroot.toString());
PServerConnection con = new PServerConnection(cvsroot);
type = "pserver";
userID = cvsroot.getUserName();
userPassword = cvsroot.getPassword();
host = cvsroot.getHostName();
port = String.valueOf(cvsroot.getPort());
repoPath = cvsroot.getRepository();
return con;
} catch (Exception e){
errorController(e);
return null;
}
}
public PServerConnection cvs_Open(){
try {
CVSRoot cvsroot = CVSRoot.parse(getServerLabel(type,userID,userPassword,host,port,repoPath));
GlobalOptions globalOptions = new GlobalOptions();
globalOptions.setCVSRoot(cvsroot.toString());
PServerConnection con = new PServerConnection(cvsroot);
ProjectManager.getInstance().writeConfig();
return con;
} catch (Exception e){
errorController(e);
return null;
}
}
public boolean isMatching(File f1,File f2){
try {
FileInputStream fis = new FileInputStream(f1);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
FileInputStream fis2 = new FileInputStream(f2);
BufferedInputStream bis2 = new BufferedInputStream(fis2);
DataInputStream dis2 = new DataInputStream(bis2);
while(dis.available()!= 0){
if(!dis.readLine().equals(dis2.readLine())){
dis.close();
bis.close();
fis.close();
dis2.close();
bis2.close();
fis2.close();
return false;
}
}
dis.close();
bis.close();
fis.close();
dis2.close();
bis2.close();
fis2.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
public void histroy(String file){
CVSRoot cvsroot = CVSRoot.parse(getServerLabel(type,userID,userPassword,host,port,repoPath));
GlobalOptions globalOptions = new GlobalOptions();
globalOptions.setCVSRoot(cvsroot.toString());
PServerConnection con = new PServerConnection(cvsroot);
Client client = new Client(con, new StandardAdminHandler());
System.out.println(cvsFolderPath+File.separator+cvsModuleName);
client.setLocalPath(cvsFolderPath+File.separator+cvsModuleName);
client.getEventManager().addCVSListener(new N3BasicListener());
File f = new File(file);
File[] afile = new File[1];
afile[0]= f;
LogCommand verCommand = new LogCommand();
verCommand.setFiles(afile);
System.out.println(verCommand.getCVSCommand());
try {
// client.executeCommand(update, globalOptions);
setCollection(true);
client.executeCommand(verCommand, globalOptions);
setCollection(false);
historyToken();
con.close();
} catch (Exception e) {
errorController(e);
}
}
public void download(String file,String ver,String writePath) {
CVSRoot cvsroot = CVSRoot.parse(getServerLabel(type,userID,userPassword,host,port,repoPath));
GlobalOptions globalOptions = new GlobalOptions();
globalOptions.setCVSRoot(cvsroot.toString());
PServerConnection con = new PServerConnection(cvsroot);
Client client = new Client(con, new StandardAdminHandler());
System.out.println(cvsFolderPath+File.separator+cvsModuleName);
client.setLocalPath(cvsFolderPath+File.separator+cvsModuleName);
client.getEventManager().addCVSListener(new N3BasicListener());
UpdateCommand update = new UpdateCommand();
File f = new File(file);
File[] afile = new File[1];
afile[0]= f;
update.setFiles(afile);
update.setPipeToOutput(true);
update.setUpdateByRevision(ver);
System.out.println(update.getCVSCommand());
try {
setCollection(true);
client.executeCommand(update, globalOptions);
setCollection(false);
File writeFile = new File(writePath);
FileWriter fileWriter = new FileWriter(writeFile);
BufferedWriter buffer = new BufferedWriter(fileWriter);
if(command.indexOf("VERS: ")>=0){
ver = command.substring(command.lastIndexOf("VERS: ")+"VERS: ".length(),command.lastIndexOf("***************")).replaceAll("\n", "");
}
if(command.indexOf("model Package (")>=0){//파일 다운받는 신호 이 다음부터 파일 표시됨
command = command.substring(command.indexOf("model Package ("),command.lastIndexOf(";")+1);
if(command.indexOf("property ID_CVS_FILE_VER=")>=0){
String text = command.substring(command.indexOf("property ID_CVS_FILE_VER="));
text = text.substring(0,text.indexOf("PROPERTY_N3EOF"));
command= command.replaceAll(text, "property ID_CVS_FILE_VER="+ver);
}
}
buffer.write(command);
buffer.flush();
buffer.close();
fileWriter.close();
con.close();
} catch (Exception e) {
errorController(e);
}
}
public void update(String file) {
setCollection(true);
CVSRoot cvsroot = CVSRoot.parse(getServerLabel(type,userID,userPassword,host,port,repoPath));
GlobalOptions globalOptions = new GlobalOptions();
globalOptions.setCVSRoot(cvsroot.toString());
PServerConnection con = new PServerConnection(cvsroot);
Client client = new Client(con, new StandardAdminHandler());
System.out.println(cvsFolderPath+File.separator+cvsModuleName);
client.setLocalPath(cvsFolderPath+File.separator+cvsModuleName);
client.getEventManager().addCVSListener(new N3BasicListener());
UpdateCommand update = new UpdateCommand();
File f = new File(file);
File[] afile = new File[1];
afile[0]= f;
update.setFiles(afile);
update.setPipeToOutput(true);
System.out.println(update.getCVSCommand());
try {
client.executeCommand(update, globalOptions);
setCollection(false);
con.close();
updateToken();
} catch (Exception e) {
errorController(e);
}
}
public void importCommand(String module,String rTage,String vTag,String log) {
CVSRoot cvsroot = CVSRoot.parse(getServerLabel(type,userID,userPassword,host,port,repoPath));
GlobalOptions globalOptions = new GlobalOptions();
globalOptions.setCVSRoot(cvsroot.toString());
PServerConnection con = new PServerConnection(cvsroot);
Client client = new Client(con, new StandardAdminHandler());
client.getEventManager().addCVSListener(new BasicListener());
client.setLocalPath(cvsFolderPath+File.separator+cvsModuleName);
ImportCommand importCommand = new ImportCommand();
importCommand.setReleaseTag(rTage);
importCommand.setVendorTag(vTag);
importCommand.setLogMessage(log);
importCommand.setModule(module);
importCommand.setImportDirectory(cvsFolderPath);
System.out.println(importCommand.getCVSCommand());
try {
client.executeCommand(importCommand, globalOptions);
} catch (Exception e) {
errorController(e);
}
}
public boolean isCVSFolder(){
File f = new File(cvsFolderPath +File.separator + cvsModuleName +File.separator +"CVS");
if(f.exists())
return true;
return false;
}
public void checkout() {
setCollection(true);
CVSRoot cvsroot = CVSRoot.parse(getServerLabel(type,userID,userPassword,host,port,repoPath));
GlobalOptions globalOptions = new GlobalOptions();
globalOptions.setCVSRoot(cvsroot.toString());
PServerConnection con = new PServerConnection(cvsroot);
Client client = new Client(con, new StandardAdminHandler());
client.getEventManager().addCVSListener(new BasicListener());
client.setLocalPath(cvsFolderPath);
CheckoutCommand checkout = new CheckoutCommand(true, cvsModuleName);
try {
client.executeCommand(checkout, globalOptions);
} catch (Exception e) {
errorController(e);
}
setCollection(false);
}
public void commit(String file,String msg) {
setCollection(true);
CVSRoot cvsroot = CVSRoot.parse(getServerLabel(type,userID,userPassword,host,port,repoPath));
GlobalOptions globalOptions = new GlobalOptions();
globalOptions.setCVSRoot(cvsroot.toString());
PServerConnection con = new PServerConnection(cvsroot);
Client client = new Client(con, new StandardAdminHandler());
System.out.println(cvsFolderPath+File.separator+cvsModuleName);
client.setLocalPath(cvsFolderPath+File.separator+cvsModuleName);
CommitCommand commitComand = new CommitCommand();
AddCommand addComand = new AddCommand();
client.getEventManager().addCVSListener(new N3BasicListener());
File f = new File(file);
File[] afile = new File[1];
afile[0]= f;
addComand.setFiles(afile);
commitComand.setFiles(afile);
commitComand.setMessage("<MSG START>"+msg+"</MSG END>");
commitComand.setForceCommit(true);
try {
con.open();
boolean success = client.executeCommand(addComand, globalOptions);
client.executeCommand(commitComand, globalOptions);
commitToken();
con.close();
} catch (Exception e) {
errorController(e);
}
setCollection(false);
}
public boolean isWrite() {
return isWrite;
}
public void setWrite(boolean isWrite) {
this.isWrite = isWrite;
}
public boolean isCollection() {
return isCollection;
}
public void setCollection(boolean isCollection) {
if(isCollection){
command = "";
isWrite = false;
logList.clear();
}
this.isCollection = isCollection;
}
public void commitToken(){
if(ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected()!=null && ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected() instanceof PackageTreeModel){
PackageTreeModel packageTreeModel = (PackageTreeModel)ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected();
FinalPackageModel finalPackageModel = (FinalPackageModel)packageTreeModel.getRefModel();
String ver = "";
if(command.indexOf("new revision: ")>=0){//파일 다운받는 신호 이 다음부터 파일 표시됨
ver = command.substring(command.indexOf("new revision: ")+"new revision: ".length(),command.indexOf("; previous revision:"));
}else if(command.indexOf("version number ")>=0){
ver = command.substring(command.indexOf("version number ")+"version number ".length(),command.indexOf("\n"));
}else if(command.indexOf("initial revision: ")>=0){
ver = command.substring(command.indexOf("initial revision: ")+"initial revision: ".length(),command.lastIndexOf("done"));
}
finalPackageModel.setCvsFileVer(ver.replaceAll("\n", ""));
ProjectManager.getInstance().getModelBrowser().refresh(packageTreeModel);
}
}
public void updateToken(){
String ver = "";
if(command.indexOf("VERS: ")>=0){
ver = command.substring(command.lastIndexOf("VERS: ")+"VERS: ".length(),command.lastIndexOf("***************")).replaceAll("\n", "");
}
if(command.indexOf("model Package (")>=0){//파일 다운받는 신호 이 다음부터 파일 표시됨
command = command.substring(command.lastIndexOf("model Package ("));
String text = command.substring(command.indexOf("property ID_CVS_FILE_VER="));
text = text.substring(0,text.indexOf("PROPERTY_N3EOF"));
command= command.replaceAll(text, "property ID_CVS_FILE_VER="+ver);
if(ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected()!=null && ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected() instanceof PackageTreeModel){
PackageTreeModel packageTreeModel = (PackageTreeModel)ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected();
FinalPackageModel finalPackageModel = (FinalPackageModel)packageTreeModel.getRefModel();
try{
File writeFile = new File(packageTreeModel.getLinkPath()+ver);
FileWriter fileWriter = new FileWriter(writeFile);
BufferedWriter buffer = new BufferedWriter(fileWriter);
buffer.write(command);
buffer.flush();
buffer.close();
fileWriter.close();
File f1 = new File(packageTreeModel.getLinkPath());
if(writeFile.exists()){
if(CVSManager.getInstance().isMatching(f1,writeFile)){
writeFile.delete();
}
else{
if(Double.parseDouble(ver) > Double.parseDouble(finalPackageModel.getCvsFileVer())){
MessageBox dialog = new MessageBox(ProjectManager.getInstance().window.getShell(),SWT.ICON_QUESTION|SWT.YES|SWT.NO);
dialog.setText("Message");
dialog.setMessage(N3Messages.DIALOG_CVS_DOWNLOAD_IS_DOWN_VER);
int p=dialog.open();
switch(p) {
case IDialogConstants.FINISH_ID:
System.out.println("Scrip Wizard Finish!!");
break;
case SWT.NO:
return;
case SWT.CANCEL:
break;
}
f1.delete();
writeFile.renameTo(f1);
}else{
MessageBox dialog = new MessageBox(ProjectManager.getInstance().window.getShell(),SWT.ICON_QUESTION|SWT.YES|SWT.NO);
dialog.setText("Message");
dialog.setMessage(N3Messages.DIALOG_CVS_DOWNLOAD_IS_UP_VER);
int p=dialog.open();
switch(p) {
case IDialogConstants.FINISH_ID:
System.out.println("Scrip Wizard Finish!!");
break;
case SWT.NO:
return;
case SWT.CANCEL:
break;
}
f1.delete();
writeFile.renameTo(f1);
}
UMLTreeParentModel umlTreeParentModel = (UMLTreeParentModel)packageTreeModel.getParent();
ProjectManager.getInstance().deleteUMLModel(packageTreeModel);
ProjectManager.getInstance().removeUMLNode(packageTreeModel.getParent(), packageTreeModel);
ProjectManager.getInstance().getModelBrowser().select(packageTreeModel);
TeamProjectManager.getInstance().load(umlTreeParentModel, f1.getParent(), f1.getName());
CVSManager.getInstance().setWrite(false);
}
}
}catch(Exception e){
errorController(e);
}
}
}
}
public void verToken(){
if(ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected()!=null
&& ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected().getRefModel() !=null
&& ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected().getRefModel()instanceof FinalPackageModel){
FinalPackageModel packageModel = (FinalPackageModel)ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected().getRefModel();
PackageTreeModel packageTreeModel =(PackageTreeModel)ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected();
if(command.indexOf("head: ")>=0){
String text = command.substring(command.indexOf("head: ")+"head: ".length(),command.indexOf("branch:")).replaceAll("\n","").trim();
// packageModel.setCvsFileVer(String.valueOf(value));
ProjectManager.getInstance().getModelBrowser().refresh(ProjectManager.getInstance().getModelBrowser().getUMLTreeModelSelected());
}else if(command.indexOf("initial revision: ")>=0){
command = command.replaceAll("initial revision: ", "");
}else if(command.indexOf("cvs server: use `cvs add' to create an entry ")>=0){
packageModel.setCvsFileVer("1.1");
}
ProjectManager.getInstance().getModelBrowser().refresh(packageTreeModel);
}
}
public void historyToken(){
command = command.replaceAll("----------------------------\n", "N3Token");
String[] stk = command.split("N3Token");
for(int i = 1; i < stk.length; i ++){
java.util.Properties properties = new java.util.Properties();
String revisin = stk[i].substring(stk[i].indexOf("revision")+"revision".length(),stk[i].indexOf("date")).replaceAll("\n", "");
properties.put("revision", revisin);
stk[i]=stk[i].substring(stk[i].indexOf("date"));
properties.put("date", stk[i].substring(stk[i].indexOf("date")+"date: ".length(),stk[i].indexOf(";")));
stk[i]=stk[i].substring(stk[i].indexOf(";")+1);
stk[i]=stk[i].substring(stk[i].indexOf("<MSG START>")+"<MSG START>".length(),stk[i].indexOf("</MSG END>"));
properties.put("desc",stk[i]);
System.out.println("");
logList.put(revisin,properties);
}
}
public void errorController(Exception e){
ProjectManager.getInstance().showMessage(e.getMessage());
e.printStackTrace();
}
public class N3BasicListener extends BasicListener {
public void messageSent(MessageEvent arg0) {
// TODO Auto-generated method stub
super.messageSent(arg0);
String msg = arg0.getMessage();
if(CVSManager.getInstance().isCollection()){//파일 다운받는 신호 이 다음부터 파일 표시됨
if(CVSManager.getInstance().command.equals("")){
CVSManager.getInstance().command = CVSManager.getInstance().command+msg;
}else{
CVSManager.getInstance().command = CVSManager.getInstance().command+"\n"+msg;
}
}
}
public void messageSent(BinaryMessageEvent e) {
// TODO Auto-generated method stub
super.messageSent(e);
}
}