/******************************* Copyright notice ******************************* Copyright (C)2002 by Andrei Loskutov <loskutov@gmx.de>. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ********************************************************************************/ package adiag.connection; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; //import adiag.gui.App; import adiag.model.Const; import adiag.model.Version; /** * The direct connection to Perforce-Prozess. * @author Andrei Loskutov */ public class P4CommandConnector extends Connector { /** the instance of P4 Process */ private Process p; /** the in/err process streams */ private BufferedReader in, err; /** The list of temporary diff files to delete they after work */ private ArrayList tempDiffFiles = new ArrayList(); /** temporary directory for files for Diff - Operation */ private String temp_dir; /** * Construktor * @param app current GUI, for error feedback etc */ public P4CommandConnector(Configuration app) { super(app); } /** * close connector and all probably opened streams * @throws IOException if something goes wrong */ public void close() throws IOException { if(in != null){ in.close(); } if(err != null){ err.close(); } if(p != null){ p.destroy(); } File f = null; // cleanup temp files while(!tempDiffFiles.isEmpty()){ f = new File("" + tempDiffFiles.remove(0)); //System.out.println("try to delete temporary file:" + f); if(f != null && f.exists()){ if(f.delete()){ //System.out.println("temporary file deleted!"); } else { //System.out.println("temporary file not deleted!"); } } } } /** * diff given versions * @param a the first version * @param b the second version * @throws Exception if something goes wrong */ public void diff(Version a, Version b) throws Exception { if(rt == null){ rt = Runtime.getRuntime(); } if(temp_dir == null){ temp_dir = System.getProperty("java.io.tmpdir", file_out_dir); //System.out.println("java.io.tmpdir is:" + temp_dir); String sep = System.getProperty("file.separator", "\\"); if(temp_dir!= null && !temp_dir.endsWith(sep)){ temp_dir += sep; } } // create temp file names String versionA = temp_dir + a.getAbbreviatedPath(b.getPath()).replace('/', '-') + "#" + a.getVersNumber(); String versionB = temp_dir + b.getAbbreviatedPath(a.getPath()).replace('/', '-') + "#" + b.getVersNumber(); String [] cmd = new String[6]; cmd[0] = "p4"; cmd[1] = "print"; cmd[2] = "-o"; cmd[3] = versionA; cmd[4] = "-q"; cmd[5] = a.getPath() + "#" + a.getVersNumber(); System.out.println(cmd[0] + " " + cmd[1] +" " + cmd[2] +" " + cmd[3]+" " + cmd[4] + " " + cmd[5] ); Process p1 = rt.exec(cmd, null); BufferedReader in1 = new BufferedReader(new InputStreamReader(p1.getInputStream())); BufferedReader err1 = new BufferedReader(new InputStreamReader(p1.getErrorStream())); String is, es = null; while((is = in1.readLine()) != null || (es = err1.readLine()) != null){ if(is != null) { System.out.println(is); } if(es != null) { System.out.println(es); } } in1.close(); err1.close(); //p.waitFor(); //System.out.println("p4 print version A ok"); if(!tempDiffFiles.contains(versionA)){ tempDiffFiles.add(versionA); } cmd[0] = "p4"; cmd[1] = "print"; cmd[2] = "-o"; cmd[3] = versionB; cmd[4] = "-q"; cmd[5] = b.getPath() + "#" + b.getVersNumber(); System.out.println(cmd[0] + " " + cmd[1] +" " + cmd[2] +" " + cmd[3]+" " + cmd[4] + " " + cmd[5] ); Process p2 = rt.exec(cmd, null); BufferedReader in2 = new BufferedReader(new InputStreamReader(p2.getInputStream())); BufferedReader err2 = new BufferedReader(new InputStreamReader(p2.getErrorStream())); while((is = in2.readLine()) != null || (es = err2.readLine()) != null){ if(is != null) { System.out.println(is); } if(es != null) { System.out.println(es); } } in2.close(); err2.close(); //p.waitFor(); //System.out.println("p4 print version B ok"); if(!tempDiffFiles.contains(versionB)){ tempDiffFiles.add(versionB); } cmd = new String[3]; cmd[0] = getDiffPath(); cmd[1] = versionA ; cmd[2] = versionB; try { rt.exec(cmd, null); // Process p = //System.out.println("p4 examdiff ok"); } catch(Exception e) { conf.error("Diff command failed:\n" + "'diff' tool executable may be not in path or path is wrong\n" + "Please ensure, that diff tool is in your PATH\n" + "or check your current diff tool path for misspelling:\n'" + getDiffPath() + "'", e ); } } /** * read native P4 Log * @param apath initial paths for requested files/directories * @throws Exception if something goes wrong */ protected void readP4Out(String [] paths) throws Exception { if(paths == null || paths.length ==0){ return; } String[] cmd = new String[4 + paths.length]; PropertySetter setter = conf.getPropsSetter(); String p4exe = conf.getProperty(Const.PROPERTY_P4_EXEC).trim(); if(!Const.P4_EXE_FILE.equals(p4exe)){ if(p4exe.length() == 0){ setter.setProperty(Const.PROPERTY_P4_EXEC, Const.P4_EXE_FILE, conf); p4exe = Const.P4_EXE_FILE; } } cmd[0] = p4exe; cmd[1] = "filelog"; cmd[2] = "-l"; cmd[3] = "-i"; System.arraycopy(paths, 0, cmd, 4, paths.length); String[] pre_cmds = new String[12]; int i = 0; pre_cmds[i++] = cmd[0]; pre_cmds[i++] = "-s"; String port, user, client, passwd; port = conf.getProperty(Const.PROPERTY_P4_PORT).trim(); user = conf.getProperty(Const.PROPERTY_P4_USER).trim(); client = conf.getProperty(Const.PROPERTY_P4_CLIENT).trim(); passwd = conf.getProperty(Const.PROPERTY_P4_PASSWD).trim(); if (!port.equals("")) { pre_cmds[i++] = "-p"; pre_cmds[i++] = port; } if (!user.equals("")) { pre_cmds[i++] = "-u"; pre_cmds[i++] = user; } if (!client.equals("")) { pre_cmds[i++] = "-c"; pre_cmds[i++] = client; } if (!passwd.equals("")) { pre_cmds[i++] = "-P"; pre_cmds[i++] = passwd; } String [] new_cmd = new String[(i + cmd.length) - 1]; for (int j = 0; j < (i + cmd.length) - 1; j++) { if (j < i) { new_cmd[j] = pre_cmds[j]; } else { new_cmd[j] = cmd[(j - i) + 1]; } } if(rt == null){ rt = Runtime.getRuntime(); } //System.out.println("p4 will be called with these parameters:"); StringBuffer sb = new StringBuffer(); for (int j = 0; j < new_cmd.length; j++) { sb.append(new_cmd[j]).append(" "); } // info on more then 10 files has no sense if(paths.length <= 10){ System.out.println(sb.toString()); } else { System.out.println("execute 'p4.exe -s filelog -l -i' with " + paths.length + " files"); } try { p = rt.exec(new_cmd, null); } catch (IOException e) { // we can have here problem with operationg system, because command // line can have limited length! System.out.println("previos p4 command line is may be too long:"); System.out.println("previos p4 command line length: " + sb.length()); e.printStackTrace(); // also we split our long command line into two subsets int length1 = paths.length/2; int length2 = paths.length - length1; String [] paths1 = new String [length1]; String [] paths2 = new String [length2]; System.arraycopy(paths, 0, paths1, 0, length1); System.arraycopy(paths, 0, paths2, 0, length2); readP4Out(paths1); readP4Out(paths2); return; } BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); String apath = paths[0]; if(apath == null){ return; } // replace given path with this one from p4 log // this is important for local file path in new p4 client programm, // that is differ to depot file path, if it feature set on by user apath = in.readLine(); out.println(apath); if(apath!=null && apath.startsWith("info:")){ apath = apath.substring(apath.indexOf(":") + 1).trim(); //app.setPath(apath); paths[0] = apath; } else { if(apath != null && apath.startsWith("error:")){ Version v = getVersion(paths[0], 0, true); v.addDescriptionLine(apath); v.setReceived(true); } } if(apath == null){ return; } LineReader in2 = new P4CommandLineReader(in); parseP4FileLog(apath, in2); } } /** * Wrapper for the BufferedReader * @author Andy the Great */ class P4CommandLineReader implements LineReader { BufferedReader reader; /** * Constructor */ public P4CommandLineReader(BufferedReader reader) { super(); this.reader = reader; } /** (non-Javadoc) * @see LineReader#readLine() */ public String readLine() throws IOException { return this.reader.readLine(); } /** (non-Javadoc) * @see LineReader#close() */ public void close() throws IOException { this.reader.close(); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#5 | 2735 | andrei_loskutov | sources are now included to binary distribution at http://andrei.gmxhome.de/perforce/index.html | ||
#4 | 2554 | andrei_loskutov |
#25.12.2002 23:29 Revision 1.3.0.4 beta 1) Some small refactoring 2) First integration with P4eclipse: properties etc, thanks Boris Pr�ssmann 5) Better integration to Eclipse: use Eclipse internal compare tool for diff. 6) Fix some minor bugs. #14.12.2002 23:15 Revision 1.3.0.3 beta 1) Fix endless loop bug if p4 path was not set. 2) Fix never getting focus bug after error message shown 3) Fix painting bug on JDK 1.4.1 (must be verified!!!) 4) Some small refactoring 5) Fix error handling. 6) Fix some minor bugs. |
||
#3 | 2552 | andrei_loskutov | 1.3.0.4 changes | ||
#2 | 2460 | andrei_loskutov |
1) Change configuration: always reed properties 2) Change password store to hex format/password field 3) Create standalone/not standalone state for new branchview eclipse wrapper 4) Refactor all main classes to remove cyclic package dependency 5) Add workaround to fix new JDK 1.4.1 Swing bugs on JScrollPane/JViewPort 6) Add dispose methods to panel/connector 7) Minor internal GUI fixes/refactorings |
||
#1 | 2222 | andrei_loskutov |
#13.10.2002 22:16 Revision 1.2.1 1) Documentation task is finished... Yes!!! 2) New feature: copy P4 depot path 3) Some small fixes to menu shortkeys belongs ctrl + c and ctrl + d 4) Clipboard fix because of JDK 1.2 - 1.3 bug 5) Smallest refactoring on some methods. 6) Html help updated |