package com.perforce.api; import java.io.*; import java.util.*; /* * Copyright (c) 2001, Perforce Software, All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * Representation of a source control environment. This information is * typically passed to a <code>P4Process</code> instance by the * <code>SourceControlObject</code> instances. It can also be set in the * {@link P4Process#getBase() base} P4Process instance. This will cause it * to be used as the default environment for all command execution. * <P> * Values for the environment can be easily loaded from a * {@link java.util.Properties Properties} file. This makes configuration of * the environment much simpler. * * @see java.util.Properties * @author <a href="mailto:david@markley.cc">David Markley</a> * @version $Date: 2002/01/23 $ $Revision: #3 $ */ public class Env { private String[] envp = { "P4USER=robot", "P4CLIENT=robot-client", "P4PORT=localhost:1666", "P4PASSWD=", "PATH=C:\\Program Files\\Perforce", "SystemDrive=C:", "SystemRoot=C:\\WINNT", "PATHEXT=.COM;.EXE;.BAT;.CMD" }; private Properties props; private String p4_exe; // Full path to the P4 executable. private String sep_path = null; private String sep_file = null; private long threshold = 10000; /** Default, no-argument constructor. */ public Env() { super(); setFromProperties(System.getProperties()); } /** * Constructs an environment from a properties file. * * @param propfile full path to a properties file. */ public Env(String propfile) throws PerforceException { super(); Properties props = new Properties(System.getProperties()); if (null != propfile) { try { props.load(new BufferedInputStream(new FileInputStream(propfile))); System.setProperties(props); } catch (Exception e) { System.err.println("Unable to load properties."); e.printStackTrace(System.err); throw new PerforceException("Unable to load properties from "+ propfile); } } setFromProperties(props); } /** * Constructor that uses another environment as its basis. This is useful * for cloning environments and then changing a few attributes. * * @param base Environment to be copied into the new environment. */ public Env(Env base) { this(); this.envp = (String[])((base.getEnvp()).clone()); this.props = (Properties)base.props.clone(); this.p4_exe = base.getExecutable(); } /** * Constructor that uses a set of <code>Properties</code> to set up the * environment. * * @see #setFromProperties(Properties) * @param props Used to construct the environment. */ public Env(Properties props) { super(); setFromProperties(props); } protected String[] getEnvp() { return envp; } /** * Checks the environment to see if it is valid. To check the validity of * the environment, the user information is accessed. This ensures that * the server can be contacted and that the password is set properly. * <p> * If the environment is valid, this method will return quietly. Otherwise, * it will throw a <code>PerforceException</code> with a message regarding * the failure. */ public void checkValidity() throws PerforceException { String[] msg = { "Connect to server failed; check $P4PORT", "Perforce password (P4PASSWD) invalid or unset.", "Can't create a new user - over license quota." }; int msgndx = -1, i; P4Process p = null; String l; String[] cmd = { "p4", "user", "-o"}; try { p = new P4Process(this); p.exec(cmd); while (null != (l = p.readLine())) { for (i = 0; i < msg.length; i++) { if (-1 != l.indexOf(msg[i])) msgndx = i; } } p.close(); } catch (IOException ex) { if (null != p) { try { p.close(); } catch (Exception ignex) { /* Ignored Exception */ } } } if (-1 != msgndx) throw new PerforceException(msg[msgndx]); } /** * Searches for the property with the specified key in this property list. * If the key is not found in this property list, the default property list, * and its defaults, recursively, are then checked. The method returns * the default value argument if the property is not found. * * @param key the property key * @param defaultValue a default value * @return the value in this property list with the specified key value * @see java.util.Properties */ public String getProperty(String key, String defaultValue) { if (null == props) return defaultValue; return props.getProperty(key, defaultValue); } /** * Searches for the property with the specified key in this property list. * If the key is not found in this property list, the default property list, * and its defaults, recursively, are then checked. The method returns null * if the property is not found. * * @param key the property key * @return the value in this property list with the specified key value * @see java.util.Properties */ public String getProperty(String key) { return getProperty(key, null); } /** * Calls the hashtable method put. Provided for parallelism with the * getProperty method. Enforces use of strings for property keys and values. * * @param key the key to be placed into this property list. * @param value the value corresponding to key. * @see java.util.Properties#setProperty(String,String) */ public String setProperty(String key, String value) { String val = (String)props.setProperty(key, value); if (! key.startsWith("p4.")) { return val; } if (key.equals("p4.user")) { setUser(value); } else if (key.equals("p4.client")) { setClient(value); } else if (key.equals("p4.port")) { setPort(value); } else if (key.equals("p4.password")) { setPassword(value); } else if (key.equals("p4.executable")) { setExecutable(value); } else if (key.equals("p4.sysdrive")) { setSystemDrive(value); } else if (key.equals("p4.sysroot")) { setSystemRoot(value); } else if (key.equals("p4.threshold")) { try { setServerTimeout(Integer.valueOf(value).intValue()); } catch (Exception ex) { /* Ignored Exception */ } } return val; } /** * Uses a set of <code>Properties</code> to set up the environment. The * properties that are used used by this method are: * * <table border="1"> * <thead><tr><th>Property</th><th>Value Set</th></tr></thead><tbody> * <tr><td>p4.user</td><td>P4USER</td></tr> * <tr><td>p4.client</td><td>P4CLIENT</td></tr> * <tr><td>p4.port</td><td>P4PORT</td></tr> * <tr><td>p4.password</td><td>P4PASSWORD</td></tr> * <tr><td>p4.executable</td><td>Executable</td></tr> * <tr><td>p4.sysdrive</td><td>SystemDrive</td></tr> * <tr><td>p4.sysroot</td><td>SystemRoot</td></tr> * <tr><td>p4.threshold</td><td>Server Timeout Threshold</td></tr> * </tbody></table> * * @see #setFromProperties(Properties) * @param props Used to construct the environment. */ public void setFromProperties(Properties props) { this.props = props; sep_path = getProperty("path.separator",";"); sep_file = getProperty("file.separator","/"); setUser(getProperty("p4.user","robot")); setClient(getProperty("p4.client","robot-client")); setPort(getProperty("p4.port","localhost:1666")); setPassword(getProperty("p4.password","")); setExecutable(getProperty("p4.executable", "P4")); setSystemDrive(getProperty("p4.sysdrive", "C:")); setSystemRoot(getProperty("p4.sysroot", "C:\\WINNT")); try { setServerTimeout(Integer.valueOf(getProperty("p4.threshold", "10000")).intValue()); } catch (Exception ex) { /* Ignored Exception */ } String os = props.getProperty("os.name"); if (null == os) { return; } if (os.startsWith("Windows")) { String windir = props.getProperty("com.ms.windir"); if (null != windir) { setPath(windir.substring(0,1)+"\\Program Files\\Perforce"); setSystemDrive(windir.substring(0,1)); setSystemRoot(windir); } } } /** * Sets the P4USER in the class information. * * @param user P4USER value. */ public void setUser(String user) { if (null != user) { envp[0] = "P4USER="+user; props.setProperty("p4.user", user); } } /** Returns the P4USER. */ public String getUser() { return envp[0].substring(7); } /** * Sets the P4CLIENT in the class information. * * @param user P4CLIENT value. */ public void setClient(String client) { if (null != client) { envp[1] = "P4CLIENT="+client; props.setProperty("p4.client", client); } } /** Returns the P4CLIENT. */ public String getClient() { return envp[1].substring(9); } /** * Sets the P4PORT in the class information. * * @param user P4PORT value. */ public void setPort(String port) { if (null != port) { envp[2] = "P4PORT="+port; props.setProperty("p4.port", port); } } /** Returns the P4PORT. */ public String getPort() { return envp[2].substring(7); } /** * Sets the P4PASSWD in the class information. * * @param user P4PASSWD value. */ public void setPassword(String password) { if (null != password) { envp[3] = "P4PASSWD="+password; props.setProperty("p4.password", password); } } /** Returns the P4PASSWORD. */ public String getPassword() { return envp[3].substring(9); } /** * Sets the PATH in the class information. * * @param user PATH value. */ public void setPath(String path) { if (null != path) { envp[4] = "PATH="+path; props.setProperty("p4.path", path); } } /** Returns the PATH. */ public String getPath() { return envp[4].substring(5); } /** * Sets the SystemDrive in the class information. This is only * meaningful under Windows. * * @param user SystemDrive value. */ public void setSystemDrive(String drive) { if (null != drive) { envp[5] = "SystemDrive="+drive; props.setProperty("p4.sysdrive", drive); } } /** * Sets the SystemRoot in the class information. This is only * meaningful under Windows. * * @param user SystemRoot value. */ public void setSystemRoot(String root) { if (null != root) { envp[6] = "SystemRoot="+root; props.setProperty("p4.sysroot", root); } } /** * Sets up the path to reach the p4 executable. The full path passed in must * contain the executable or at least end in the system's file separator * character. This gotten from the file.separator property. For example: * <pre> * p4.executable=/usr/bin/p4 # This will work * p4.executable=/usr/bin/ # This will work * <font color=Red>p4.executable=/usr/bin # This won't work</font> * </pre> * * @param exe Full path to the p4 executable. */ public void setExecutable(String exe) { int pos; if (null == exe) return; p4_exe = exe; if (null == sep_file) { sep_file = System.getProperties().getProperty("file.separator","\\"); } if (-1 == (pos = exe.lastIndexOf(sep_file))) return; if (null == sep_path) { sep_path = System.getProperties().getProperty("path.separator",";"); } envp[4] += sep_path + exe.substring(0,pos); props.setProperty("p4.executable", p4_exe); } /** Returns the path to the executable. */ public String getExecutable() { return p4_exe; } /** Set the server timeout threshold. */ public void setServerTimeout(long threshold) { this.threshold = threshold; props.setProperty("p4.threshold", String.valueOf(threshold)); } /** Return the server timeout threshold. */ public long getServerTimeout() { return threshold; } public String toString() { return envp[0]+"\n"+envp[1]+"\n"+envp[2]+"\n"+envp[4]+"\n"+ envp[5]+"\n"+envp[6]+"\n"+envp[7]; } /** * Returns an XML representation of the environment. */ public String toXML() { StringBuffer sb = new StringBuffer("<env"); sb.append(" user=\""); sb.append(getUser()); sb.append("\" client=\""); sb.append(getClient()); sb.append("\" port=\""); sb.append(getPort()); sb.append("\" password=\""); sb.append(getPassword()); sb.append("\" sysdrive=\""); sb.append(envp[5].substring(12)); sb.append("\" sysroot=\""); sb.append(envp[6].substring(10)); sb.append("\" threshold=\""); sb.append(threshold); sb.append("\"><executable>"); sb.append(getExecutable()); sb.append("</executable><path>"); sb.append(getPath()); sb.append("</path></env>"); return sb.toString(); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#9 | 8157 | michael | Archiving legacy java api projects which predate the supported P4Java api. | ||
#8 | 7580 | Sivananda Poreddy | Adding support for tickets, new jar file, small fix for DirEntry file | ||
#7 | 6433 | Sivananda Poreddy | Changing the communication preferences, and some documentation updates | ||
#6 | 6141 | Sivananda Poreddy | Updating the p4package with fixes, some new features, jar update, and doc updates | ||
#5 | 1826 | David Markley | Incorporating minor changes in support of upcoming p4jsp package. | ||
#4 | 1382 | David Markley | Publicly changed Env and FileEntry | ||
#3 | 1361 | David Markley | Added convenience methods to the Env class. | ||
#2 | 1149 | David Markley | Added more documentation, a few examples, a cleanUp method, and a server timeout threshold. | ||
#1 | 1043 | David Markley | Initial branch to public area. | ||
//guest/david_markley/p4package/com/perforce/api/Env.java | |||||
#2 | 1035 | David Markley | Changed the file type to provide for keyword expansion. | ||
#1 | 1034 | David Markley |
Added P4Package sources as subset of the P4WebPublisher project. Copyright (c) 2001, Perforce Software, All rights reserved. |