001    package org.hackystat.sensor.ant.perforce;
002    
003    import com.perforce.api.Debug;
004    import com.perforce.api.Env;
005    
006    /**
007     * Provides a simple mechanism for configuring the Perforce Java API connection to the p4 tool. 
008     * Provides a wrapper around the Perforce Java API Env object, and supplies defaults for certain
009     * properties. 
010     * Typical usage:
011     * <pre>
012     * P4Environment p4Env = new P4Environment();
013     * p4Env.setPort("myperforceserver.foo.com:1666");
014     * p4Env.setUser("philip_johnson");
015     * p4Env.setPassword("foo");
016     * p4Env.setVerbose(true);
017     * Env env = p4Env.getEnv();
018     * </pre>
019     * @author Philip Johnson
020     */
021    public class P4Environment {
022      /** The path to the p4 executable. */
023      private String p4Executable = "C:\\Program Files\\Perforce\\P4.EXE";
024      /** The hostname and port for the perforce server. */
025      private String p4Port = "public.perforce.com:1666";
026      /** The authorized Perforce user for this server. */
027      private String p4User = null;
028      /** The password for this user on this server. */
029      private String p4Password = null;
030      /** If running on windows, need this for p4. */
031      private String p4SysRoot = "C:\\WINDOWS";
032      /** If running on windows, need this for p4. */
033      private String p4SysDrive = "C:";
034      /** Determines whether the p4 output is verbose or not. */
035      private boolean isVerbose = false;
036    
037      /** Constructs a new P4Environment with default property values. */
038      public P4Environment () {
039        // Does nothing.
040      }
041      
042      /**
043       * Sets the path to the p4 executable. Default: "C:\\Program Files\\Perforce\\P4.EXE"
044       * @param path The path. 
045       */
046      public void setP4Executable(String path) {
047        this.p4Executable = path;
048      }
049      
050      /**
051       * Sets the port for the perforce server. Default: "public.perforce.com:1666".
052       * @param port The port.
053       */
054      public void setP4Port(String port) {
055        this.p4Port = port;
056      }
057      
058      /**
059       * Sets the perforce user.  Example: philip_johnson.
060       * @param user The user. 
061       */
062      public void setP4User(String user) {
063        this.p4User = user;
064      }
065      
066      /**
067       * Sets the password for the perforce user. 
068       * @param password The password.
069       */
070      public void setP4Password(String password) {
071        this.p4Password = password;
072      }
073      
074      /**
075       * Sets the system root. Only needed on Windows systems. Default: "C:\\WINDOWS". 
076       * @param sysroot The system root.
077       */
078      public void setP4SystemRoot (String sysroot) {
079        this.p4SysRoot = sysroot;
080      }
081      
082      /**
083       * Sets the system drive.  Only needed on Windows systems. Default: "C:". 
084       * @param sysdrive The system drive.
085       */
086      public void setP4SystemDrive (String sysdrive) {
087        this.p4SysDrive = sysdrive;
088      }
089      
090      /**
091       * Sets verbose mode. If true, lots of output regarding p4 tool execution is printed. 
092       * @param isVerbose True to set verbose mode. 
093       */
094      public void setVerbose(boolean isVerbose) {
095        this.isVerbose = isVerbose;
096      }
097    
098      /**
099       * Returns a Perforce Java API Env instance constructed from the data supplied to this instance.
100       * @return The Env instance. 
101       */
102      public Env getEnv() {
103        Env env = new Env();
104        env.setExecutable(p4Executable);
105        env.setPort(p4Port);
106        env.setUser(p4User);
107        env.setPassword(p4Password);
108        env.setSystemRoot(p4SysRoot);
109        env.setSystemDrive(p4SysDrive);
110        if (isVerbose) {
111          Debug.setDebugLevel(Debug.VERBOSE); 
112          Debug.setLogLevel(Debug.LOG_SPLIT);
113        }
114        return env;
115      }
116    }