Example #1
0
  /**
   * Dale Anson: One of the main reasons I borrowed this class from Ant was to be able to read
   * environment variables. It makes sense to add a method to easily fetch the value of an
   * environment variable here.
   *
   * @param name the name of an environment variable. Much of this code was copied from
   *     org.apache.tools.ant.taskdefs.Execute.
   * @return the value of the environment variable, or null if there is no value for the given name
   */
  public static String getEnvironmentValue(String name) {
    if (environment != null) {
      return (String) environment.get(name);
    }
    environment = new Hashtable<String, String>();

    try {
      String[] env_cmd = getProcEnvCommand();
      Process process = Runtime.getRuntime().exec(env_cmd);
      InputStream is = new BufferedInputStream(process.getInputStream());
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      CopyUtils.copy(is, baos);

      BufferedReader in = new BufferedReader(new StringReader(baos.toString()));

      // this portion copied from org.apache.tools.ant.taskdefs.Execute //
      Vector<String> procEnvironment = new Vector<String>();
      String var = null;
      String line, lineSep = System.getProperty("line.separator");
      while ((line = in.readLine()) != null) {
        if (line.indexOf('=') == -1) {
          // Chunk part of previous env var (UNIX env vars can
          // contain embedded new lines).
          if (var == null) {
            var = lineSep + line;
          } else {
            var += lineSep + line;
          }
        } else {
          // New env var...append the previous one if we have it.
          if (var != null) {
            procEnvironment.addElement(var);
          }
          var = line;
        }
      }
      // Since we "look ahead" before adding, there's one last env var.
      if (var != null) {
        procEnvironment.addElement(var);
      }
      // end copy from Execute //

      // now split out the names from the values and populate a Hashtable
      if (procEnvironment.size() > 0) {
        java.util.Iterator it = procEnvironment.iterator();
        while (it.hasNext()) {
          var = (String) it.next();
          int index = var.indexOf("=");
          String key = var.substring(0, index);
          String value = var.substring(index + 1);
          environment.put(key, value);
        }
      }
    } catch (Exception ignored) {
    }
    return getEnvironmentValue(name);
  }
Example #2
0
 /**
  * A map that creates new collections by cloning collectionSpecimen. Allow one to customize an
  * instance, contrary to the constructor which only takes a class.
  *
  * @param collectionSpecimen the collection from which to all others will be cloned.
  * @param initialCapacity the initial capacity
  * @throws IllegalArgumentException is not a Cloneable.
  */
 public CollectionMap(Collection<V> collectionSpecimen, final int initialCapacity) {
   super(initialCapacity);
   this.collectionClass = null;
   if (!(collectionSpecimen instanceof Cloneable))
     throw new IllegalArgumentException(collectionSpecimen + " not a cloneable.");
   // allow to pass an existing collection w/o us modifying it
   // plus test if copy() succeeds
   this.collectionSpecimen = CopyUtils.copy(collectionSpecimen);
   this.collectionSpecimen.clear();
 }
Example #3
0
 /*
  * (non-Javadoc)
  *
  * @see org.apache.commons.collections.MultiHashMap#createCollection(java.util.Collection)
  */
 public Collection<V> createCollection(Collection coll) {
   if (this.collectionClass != null)
     try {
       if (coll == null) {
         return this.collectionClass.newInstance();
       } else {
         return this.collectionClass
             .getConstructor(new Class[] {Collection.class})
             .newInstance(new Object[] {coll});
       }
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
   else if (this.collectionSpecimen != null) {
     try {
       final Collection<V> res = CopyUtils.copy(this.collectionSpecimen);
       if (coll != null) res.addAll(coll);
       return res;
     } catch (Exception e) {
       throw ExceptionUtils.createExn(IllegalStateException.class, "clone() failed", e);
     }
   } else return super.createCollection(coll);
 }