DistCopyCommandsHandler.java :  » Build » invicta » net » sf » invicta » handler » Java Open Source

Java Open Source » Build » invicta 
invicta » net » sf » invicta » handler » DistCopyCommandsHandler.java
package net.sf.invicta.handler;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import net.sf.invicta.InvictaConstants;
import net.sf.invicta.InvictaException;
import net.sf.invicta.api.DefinedProperty;
import net.sf.invicta.api.Product;

/**
 * Map:
 *     distDirProperty - (String, Required) Property name of the distribution destination directory.
 *      addComponentName - (Boolean, optional, false) Whether to add the name of the component to the destination directory.
 *    srcListProperty - (String, optiona) A comma-separated list of source files.
 * or 
 *      srcDirProperty - (String, Optional) Property name of the source file.  
 *      flatten - (Boolean, optional, false) - Whether to flatten the source files when copying, or create the directory structure.
 */
public class DistCopyCommandsHandler extends InvictaBasicHandler {
  private final static boolean DEFAULT_ADD_COMPONENT_NAME = false;
  private final static boolean DEFAULT_IS_SRC_PROPERTY = true;  
  private final static boolean DEFAULT_FLATTEN = false;
  private final static String DIST_LIST_SEPARATOR = ",";
  
  protected boolean flatten;
  protected boolean addComponentName;
  protected String projectDistDir;
  protected String srcDir;
  protected List srcList;
  
  /**
   * 
   */
  public String getName() {
    return "distCopyCommands";
  }
  
  /** 
   * 
   */
  public String handle(Map paramsMap) throws InvictaException {
    
    // Get the destination directory property and make sure it exists.
    String projectDistDirProperty = getRequiredParameter("distDirProperty");
    this.projectDistDir =
      getPropertyReference(projectDistDirProperty);
    this.addComponentName = getBooleanParameter("addComponentName", DEFAULT_ADD_COMPONENT_NAME);      
    this.flatten = getBooleanParameter("flatten", DEFAULT_FLATTEN);
    String srcDirProperty = getParameter("srcDirProperty");
    String srcListProperty = getParameter("srcListProperty");
    
    // If no source was specified, then the products of
    // the component should be copied.
    if ((srcListProperty == null) && (srcDirProperty == null)) {
      return getProductsCopyCommands();
    } 
    
    if (srcDirProperty != null) {
      this.srcDir = getPropertyReference(srcDirProperty);
    } else {      
      this.srcDir = getComponent().getDir();
      getPropertyReference(srcListProperty);
      String srcDirListStr = getComponent().getPropertyValue(srcListProperty);        
      this.srcList = new ArrayList();
      StringTokenizer st = new StringTokenizer(srcDirListStr, DIST_LIST_SEPARATOR);
      while (st.hasMoreTokens())
        this.srcList.add(st.nextToken().trim());      
        
    }
          
    return getFilesetCopyCommand(); 
  }

  
  /**
   * Return the commands of the files of the product.
   *
   * @return String
   */
  private String getProductsCopyCommands() {
    String result = "";
    
    // Get the component's dist dir.
    String localDistDir = getComponent().getDir();
    String distDir = this.projectDistDir + InvictaConstants.FILE_SEPARATOR + localDistDir;
    
    // Add copy commands for all products
    for (Iterator prodIter = getComponent().getSelfProducts().iterator(); prodIter.hasNext();) {
      Product product = (Product) prodIter.next();
    
      if (product.getFile() != null) {
        
        String productDir = product.getRelativeDir();
        if (product.getFile().charAt(0) != InvictaConstants.FILE_SEPARATOR_CHAR)
          productDir = InvictaConstants.FILE_SEPARATOR + productDir;          
        result += getCopyCommand(product.getPath(),  distDir + productDir);
      }
    }
    return result;
  }
  
  /**
   * Return ANT commands for creating dstDir directory and copying
   * (with flatten) the srcFile to dstDir
   * 
   * @param srcFile
   * @param distDir
   * @return String
   */
  private String getCopyCommand(String srcFile, String distDir) {
    return "<mkdir dir=\"" + this.projectDistDir  + "\"/>\n"
      + "<copy file=\"" + srcFile + "\" " 
      + "flatten=\"" + String.valueOf(flatten) + "\" "
      + "todir=\"" + distDir
      + "\" preservelastmodified=\"true\""
      + " includeemptydirs=\"false\""  + "/>\n";
  }
  
  /**
   * Return ANT command for creating dstDir directory and copying
   * a fileset of all recursive content of srcDir to dstDir
   * @param srcDir
   * @param dstDir
   * @return String
   */
  private String getFilesetCopyCommand() {
    String fullDistDir;
    if (addComponentName)
      fullDistDir = this.projectDistDir + InvictaConstants.FILE_SEPARATOR + getComponent().getDir();
    else
      fullDistDir = this.projectDistDir;
    
    String command = "<mkdir dir=\""
      + fullDistDir
      + "\"/>\n"
      + "<copy todir=\""
      + fullDistDir
      + "\" preservelastmodified=\"true\""
      + " includeemptydirs=\"false\""
      + " flatten=\"" + String.valueOf(flatten) + "\""                         
      + ">\n"
      + "<fileset dir=\"" + srcDir + "\">\n";
      
    if (this.srcList != null) {
      for (Iterator iter = this.srcList.iterator(); iter.hasNext();) {
        String include = (String) iter.next();
        command += "<include name=\"" + include + "/**/*\"/>\n";
        command += "<include name=\"" + include + "\"/>\n";
      }
    }
          
    command += "</fileset>\n";
    command += "</copy>\n";    
    return command;      
  }
    
  
  /** 
   * 
   */
  protected String getPropertyReference(String propertyName) throws InvictaHandlerException {
    DefinedProperty property = getComponent().getDefinedProperty(propertyName);
    if (property == null) {
      throw new InvictaHandlerException(this,
        "Property not defined: " + propertyName
        + " for component '" + getComponent().getName()
        + "' (type '" + getComponent().getTypeName() + "')"); 
    }
    return property.getReferenceValue();      
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.