Primarily for Conor, but I am attaching an EJB compile task that works with BEA WLS 6.x. It is a bit simpler than the older version but nevertheless based on the old version of optional EJB task.

Derek
package org.apache.tools.ant.taskdefs.optional.bea;

import java.io.File;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Project;

import org.apache.tools.ant.taskdefs.Java;

import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;

public class EJBC extends Task
{
  private File src, dest;
  private Path cmdClasspath;
  private String compiler;
  private boolean keepgenerated;
  private String debug;

  public EJBC()
  {
    keepgenerated = false;
  }

  /**
   * required.  There is no default.
   * 
   * @param in     src JAR filename
   */
  public void setSrc(File in) 
  {
    src = in;
  }

  /**
   * required.  There is no default.
   * 
   * @param out    dest JAR filename
   */
  public void setDest(File out) 
  {
    dest = out;
  }

  /**
   * optional.  The JAVA compiler to use for the ejb compile.
   * 
   * @param compiler    the name of the compiler, e.g. javac
   */
  public void setCompiler(String compiler) 
  {
    this.compiler = compiler;
  }

  /**
   * optional.  The default is false.
   * 
   * @param keepgenerated    on or off (true or false)
   */
  public void setKeepgenerated(boolean keepgenerated) 
  {
    this.keepgenerated = keepgenerated;
  }

  /**
   * optional.
   * 
   * @param debug    value = 1
   */
  public void setDebug(String debug) 
  {
    this.debug = debug;
  }

  /**
   * Set the classpath to be used for this compilation.
   */
  public void setClasspath(Path classpath) 
  {
    if ( cmdClasspath == null )
    {
      log("set classpath: "+classpath, Project.MSG_VERBOSE);
      cmdClasspath = classpath;
    }
    else
    {
      log("appending classpath: "+classpath, Project.MSG_VERBOSE);
      cmdClasspath.append(classpath);
    }
  }

  /** Gets the classpath to be used for this compilation. */
  public Path getClasspath() 
  {
    return cmdClasspath;
  }

  /**
   * Maybe creates a nested classpath element.
   */
  public Path createClasspath() 
  {
    if ( cmdClasspath == null )
    {
      cmdClasspath = new Path(project);
    }
    return cmdClasspath.createPath();
  }

  /**
   * Adds a reference to a CLASSPATH defined elsewhere.
   */
  public void setClasspathRef(Reference r) 
  {
    createClasspath().setRefid(r);
  }


  private String[] getArgs() 
  {
    java.util.Vector argV = new java.util.Vector(5);

    String[] rtn;
    int idx = 0;

    if (keepgenerated)
      argV.add("-keepgenerated");

    if (compiler != null && compiler.length() > 0)
    {
      argV.add("-compiler");
      argV.add(compiler);
    }

    argV.add(src.getAbsolutePath());
    argV.add(dest.getAbsolutePath());

    rtn = new String[argV.size()];
    argV.copyInto(rtn);

    return rtn;
  }

  // The method executing the task
  public void execute() throws BuildException 
  {
    long srcModified  = src.lastModified();
    long destModified = dest.lastModified();

    if ( !(src.exists() && src.isFile()) )
      throw new BuildException("Error src filename not found");

    if ( destModified == 0 || srcModified > destModified )
    {
      log("EJBC Compiling "+src.getPath()+" ...", Project.MSG_WARN);
      String[] args = getArgs();
      try
      {
        Java helperTask = (Java)project.createTask("java");
        helperTask.setTaskName(getTaskName());
        helperTask.setFork(true);
        helperTask.setClassname("weblogic.ejbc");
        if (debug!=null && debug.length() > 0)
        {
          Environment.Variable debugVar = new Environment.Variable();
          debugVar.setKey("weblogic.ejb20.debug");
          debugVar.setValue(debug);
          helperTask.addSysproperty(debugVar);
        }
                                    
        String[] line = getArgs();
        for (int i=0; i < line.length; i++) {
            helperTask.createArg().setValue(line[i]);
        }
        
        String cp = project.translatePath(System.getProperty("java.class.path")
                                          + ":" + cmdClasspath);
        helperTask.setClasspath(new Path(project, cp));
        if (helperTask.executeJava() != 0) {                         
            throw new BuildException("Execution of ejbc failed");
        }
      }
      catch ( Exception e )
      {
        throw new BuildException(e, location);
      }
    }
    else
    {
      log(dest.getPath()+" is up to date.", Project.MSG_WARN);
    }
  }

}

Reply via email to