WsGenTask.java

00001 
00026 package org.objectweb.jonas.ant;
00027 
00028 import java.io.File;
00029 
00030 import org.apache.tools.ant.AntClassLoader;
00031 import org.apache.tools.ant.BuildException;
00032 import org.apache.tools.ant.DirectoryScanner;
00033 import org.apache.tools.ant.Project;
00034 import org.apache.tools.ant.taskdefs.Java;
00035 import org.apache.tools.ant.taskdefs.MatchingTask;
00036 import org.apache.tools.ant.types.Path;
00037 
00038 public class WsGenTask extends MatchingTask {
00039 
00041     private static final String BOOTSTRAP_CLASS = "org.objectweb.jonas.server.Bootstrap";
00042 
00044     private static final String WSGEN_CLASS = "org.objectweb.jonas_ws.wsgen.WsGen";
00045 
00047     private File destination   = null;
00048 
00050     private File source        = null;
00051 
00053     private boolean validation = true;
00054 
00056     private String javac       = null;
00057 
00059     private String javac_opts  = null;
00060 
00062     private boolean keep_gen   = false;
00063 
00065     private boolean no_config  = false;
00066 
00068     private boolean verbose    = false;
00069 
00071     private boolean debug      = false;
00072 
00074     private File jonas_root    = null;
00075 
00077     private File jonas_base    = null;
00078 
00079     public void setDestdir(File d) {
00080         destination = d;
00081     }
00082 
00083     public void setSrcdir(File s) {
00084         source = s;
00085     }
00086 
00087     public void setValidation(boolean v) {
00088         validation = v;
00089     }
00090 
00091     public void setJavac(String j) {
00092         javac = j;
00093     }
00094 
00095     public void setJavacopts(String opts) {
00096         javac_opts = opts;
00097     }
00098 
00099     public void setKeepgen(boolean k) {
00100         keep_gen = k;
00101     }
00102 
00103     public void setNoconfig(boolean c) {
00104         no_config = c;
00105     }
00106 
00107     public void setVerbose(boolean v) {
00108         verbose = v;
00109     }
00110 
00111     public void setDebug(boolean b) {
00112         debug = b;
00113     }
00114 
00115     public void setJonasroot(File jr) {
00116         jonas_root = jr;
00117     }
00118 
00119     public void setJonasbase(File jb) {
00120         jonas_base = jb;
00121     }
00122 
00126     public void execute() throws BuildException {
00127 
00128         // init the wsgen task
00129         initWsGenTask();
00130 
00131         // execute
00132         DirectoryScanner ds = getDirectoryScanner(source);
00133         ds.scan();
00134         String[] files = ds.getIncludedFiles();
00135 
00136         for (int i = 0; i < files.length; i++) {
00137             
00138             Java wsgen = createWsGen(source + File.separator + files[i]);
00139             
00140             // calling WsGen task
00141             log("Calling  WsGen task for '" + source + File.separator + files[i] + "'.", Project.MSG_VERBOSE);
00142             
00143             if (wsgen.executeJava() != 0) {
00144                 throw new BuildException("WsGen reported an error.");
00145             }
00146         }
00147 
00148     }
00149 
00150     private Java createWsGen(String filename) throws BuildException {
00151 
00152         Java wsgenTask = null;  // WsGen task 
00153         boolean error = false;  // true if an error occurs during the GenIC call
00154 
00155         wsgenTask = (Java) getProject().createTask("java");
00156         wsgenTask.setTaskName("wsgen");
00157         wsgenTask.setFork(true);
00158 
00159         // jonas root
00160         wsgenTask.createJvmarg().setValue("-Dinstall.root=" + jonas_root);
00161 
00162         // jonas base
00163         wsgenTask.createJvmarg().setValue("-Djonas.base=" + jonas_base);
00164 
00165         // Endorsed directory
00166         File endorsedDir = new File(new File(jonas_root, "lib"), "endorsed");
00167         wsgenTask.createJvmarg().setValue("-Djava.endorsed.dirs=" + endorsedDir);
00168 
00169         // java policy file
00170         String jonasConfigDir = jonas_root + File.separator + "conf";
00171         File javaPolicyFile = new File(jonasConfigDir, "java.policy");
00172         if (javaPolicyFile.exists()) {
00173             wsgenTask.createJvmarg().setValue("-Djava.security.policy="
00174                                               + javaPolicyFile.toString());
00175         }
00176 
00177         // The bootstrap class must launch the GenIC class
00178         wsgenTask.createArg().setValue(WSGEN_CLASS);
00179 
00180         wsgenTask.createArg().setValue("-d");
00181         wsgenTask.createArg().setFile(destination);
00182 
00183         // add ow_jonas_bootstrap.jar
00184         String bootJar = jonas_root + File.separator + "lib" + File.separator + "common"
00185             + File.separator + "ow_jonas_bootstrap.jar";
00186         Path classpath = new Path(getProject(), bootJar);
00187       
00188         log("Using classpath: " + classpath.toString(), Project.MSG_VERBOSE);
00189         wsgenTask.setClasspath(classpath);
00190 
00191         if (!checkBootstrapClassName(classpath)) {
00192             log("Cannot find bootstrap class in classpath.", Project.MSG_ERR);
00193             throw new BuildException("Bootstrap class not found, please check the classpath.");
00194         } else {
00195             wsgenTask.setClassname(BOOTSTRAP_CLASS);
00196         }
00197 
00198         // keepgenerated
00199         if (keep_gen) {
00200             wsgenTask.createArg().setValue("-keepgenerated");
00201         }
00202 
00203         // noconfig
00204         if (no_config) {
00205             wsgenTask.createArg().setValue("-noconfig");
00206         }
00207 
00208         // novalidation
00209         if (!validation) {
00210             wsgenTask.createArg().setValue("-novalidation");
00211         }
00212 
00213         // javac
00214         if (javac != null) {
00215             wsgenTask.createArg().setValue("-javac");
00216             wsgenTask.createArg().setLine(javac);
00217         }
00218 
00219         // javacopts
00220         if (javac_opts != null && !javac_opts.equals("")) {
00221             wsgenTask.createArg().setValue("-javacopts");
00222             wsgenTask.createArg().setLine(javac_opts);
00223         }
00224 
00225         // verbose
00226         if (verbose) {
00227             wsgenTask.createArg().setValue("-verbose");
00228         }
00229 
00230         // debug
00231         if (debug) {
00232             wsgenTask.createArg().setValue("-debug");
00233         }
00234 
00235         // input file to process by GenIC
00236         wsgenTask.createArg().setValue(filename);
00237 
00238         return wsgenTask;
00239         
00240     }
00241 
00242     private void initWsGenTask() throws BuildException {
00243         // try to define jonas_root if not set
00244         if (jonas_root == null ) {
00245             // get ant property
00246             String jr = getProject().getProperty("jonas.root");
00247             jonas_root = new File(jr);
00248             
00249             if (jr == null) {
00250                 throw new BuildException("attribute jonasroot is necessary.");
00251             }
00252         }
00253 
00254         // use jonas_root as jonas_base if not set
00255         if (jonas_base == null) {
00256             String jb = getProject().getProperty("jonas.base");
00257             if (jb == null) {
00258                 jonas_base = jonas_root;
00259             } else {
00260                 jonas_base = new File(jb);
00261             }
00262             
00263         }
00264 
00265         // default destination : project directory
00266         if (destination == null) {
00267             destination = getProject().getBaseDir();
00268         }
00269 
00270         // javac javac_opts
00271         // by default WsGen already handle them
00272 
00273     }
00280     private boolean checkBootstrapClassName(Path classpath) {
00281         log("Looking for bootstrap class in classpath: " + classpath.toString(), Project.MSG_VERBOSE);
00282         AntClassLoader cl = new AntClassLoader(classpath.getProject(), classpath);
00283         try {
00284             cl.loadClass(BOOTSTRAP_CLASS);
00285             log("Found Bootstrap class '" + BOOTSTRAP_CLASS
00286                 + "' in classpath.", Project.MSG_VERBOSE);
00287         } catch (ClassNotFoundException cnf1) {
00288             log("Bootstrap class '" + BOOTSTRAP_CLASS
00289                 + "' not found in classpath.", Project.MSG_VERBOSE);
00290             return false;
00291         }
00292         return true;
00293     }
00294 
00295 }

Generated on Tue Feb 15 15:05:14 2005 for JOnAS by  doxygen 1.3.9.1