1 28 29 30 31 package org.objectweb.jonas_ejb.genidl; 32 33 import java.io.File ; 34 import java.net.URLClassLoader ; 35 import java.net.URL ; 36 import java.net.MalformedURLException ; 37 import java.util.Enumeration ; 38 import java.util.StringTokenizer ; 39 import java.util.Vector ; 40 41 import org.objectweb.common.Cmd; 42 import org.objectweb.common.Env; 43 import org.objectweb.jonas_ejb.deployment.api.BeanDesc; 44 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc; 45 import org.objectweb.jonas_ejb.deployment.api.MessageDrivenDesc; 46 import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager; 47 import org.objectweb.jonas_ejb.lib.BeanNaming; 48 import org.objectweb.jonas_lib.version.Version; 49 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 50 51 52 60 61 public class GenIDL { 62 63 private static final int EXIT_SUCCESS = 0; 64 private static final int EXIT_FAILURE = 2; 65 66 private boolean verbose = false; 68 69 private String directoryOutputName = null; 71 72 private Vector vBeanDD = null; 74 75 private Vector remoteClasses = null; 77 78 79 111 112 public static void main(String args[]) { 113 114 if (Env.getJavaVersion() < Env.JAVA_1_3) { 117 GenIDL.fatalError("'rmic -idl' not available on a JDK version less than 1.3."); 118 } 119 120 boolean error = false; 121 122 boolean isHelp = false; 123 boolean isVerbose = false; 124 boolean parseWithValidation = true; 125 126 String fileInputName = null; 127 String dirOutputName = null; 128 Vector optionsRmiC = new Vector (); 129 130 for (int argn=0; argn<args.length; argn++) { 132 String arg = args[argn]; 133 if (arg.equals("-help") || arg.equals("-?")) { 134 isHelp = true; 135 continue; 136 } 137 if (arg.equals("-novalidation")) { 138 parseWithValidation = false; 139 continue; 140 } 141 if (arg.equals("-verbose")) { 142 isVerbose = true; 143 continue; 144 } 145 if (arg.equals("-rmicopts")) { 146 argn++; 147 if (argn<args.length) { 148 StringTokenizer st = new StringTokenizer (args[argn]); 149 while (st.hasMoreTokens()) { 150 optionsRmiC.addElement(st.nextToken()); 151 } 152 } else { 153 error = true; 154 } 155 continue; 156 } 157 if (arg.equals("-d")) { 158 argn++; 159 if (argn<args.length) { 160 dirOutputName = args[argn]; 161 } else { 162 error = true; 163 } 164 continue; 165 } 166 fileInputName = args[argn]; 167 } 168 169 if (isHelp) { 171 usage(); 172 System.exit(EXIT_SUCCESS); 173 } 174 175 if (error || (fileInputName == null)) { 177 usage(); 178 System.exit(EXIT_FAILURE); 179 } 180 if (dirOutputName == null) { 181 dirOutputName = new String (""); 182 } 183 184 String classpath; 186 if (fileInputName.endsWith(".jar")) { 187 classpath = fileInputName + 188 File.pathSeparator + 189 System.getProperty("java.class.path", ""); 190 } else { 191 classpath = System.getProperty("java.class.path", ""); 192 } 193 optionsRmiC.addElement("-classpath"); 194 optionsRmiC.addElement(classpath); 195 196 if (!parseWithValidation) { 198 EjbDeploymentDescManager.setParsingWithValidation(false); 199 } 200 201 try { 203 DeploymentDesc ejbJarDD = null; 204 if (fileInputName.endsWith(".jar")) { 205 URL url[] = new URL [1]; 207 url[0] = (new File (fileInputName)).toURL(); 208 URLClassLoader cl = new URLClassLoader (url); 209 ejbJarDD = EjbDeploymentDescManager.getDeploymentDesc(fileInputName, cl); 210 } else { 211 ejbJarDD = EjbDeploymentDescManager.getDeploymentDesc(fileInputName, 213 BeanNaming.getJonasXmlName(fileInputName), 214 BeanNaming.getParentName(fileInputName)); 215 } 216 217 GenIDL gwc = new GenIDL(ejbJarDD, dirOutputName, isVerbose); 218 gwc.generate(optionsRmiC); 219 220 } catch (MalformedURLException e) { 221 GenIDL.fatalError("Invalid ejb-jar file name (" + e.getMessage() + ")"); 222 } catch (GenIDLException e) { 223 GenIDL.fatalError(e.getMessage()); 224 } catch (DeploymentDescException e) { 225 GenIDL.fatalError("Cannot read the Deployment Descriptors from " 226 +fileInputName+": "+e.getMessage()); 227 } 228 } 230 231 240 public GenIDL(DeploymentDesc ejbJarDesc, String dirOutputName, boolean isVerbose) 241 throws GenIDLException { 242 243 verbose = isVerbose; 244 directoryOutputName = dirOutputName; 245 remoteClasses = new Vector (); 246 247 vBeanDD = new Vector (); 248 BeanDesc[] beansDD = ejbJarDesc.getBeanDesc(); 249 for (int i=0; i<beansDD.length; i++) { 250 if (!(beansDD[i] instanceof MessageDrivenDesc)) { 251 vBeanDD.addElement(beansDD[i]); 253 } 254 } 255 256 StringBuffer message = new StringBuffer (); 258 message.append("GenIDL for JOnAS "+Version.NUMBER+": "); 259 if (vBeanDD.size()!=0) { 260 message.append("IDL generation for beans "); 261 for (int i=0; i < vBeanDD.size(); i++) { 262 BeanDesc dd = (BeanDesc)vBeanDD.elementAt(i); 263 message.append((i==0?"":", ") + "'" + dd.getEjbName() + "'"); 264 } 265 message.append(" ..."); 266 } else { 267 message.append("No generation to do (only message driven beans)"); 268 } 269 GenIDL.info(message.toString()); 270 271 for (Enumeration e = vBeanDD.elements() ; e.hasMoreElements() ;) { 273 BeanDesc dd = (BeanDesc)e.nextElement(); 274 if (dd.getHomeClass() != null) { 275 remoteClasses.addElement(dd.getHomeClass().getName()); 276 } 277 if (dd.getRemoteClass() != null) { 278 remoteClasses.addElement(dd.getRemoteClass().getName()); 279 } 280 } 281 } 282 283 289 public void generate(Vector optionsRmiC) 290 throws GenIDLException { 291 292 String optDirOutput; 293 Cmd cmd; 294 295 if (vBeanDD.size()==0) { 296 return; 297 } 298 299 cmd = new Cmd("rmic"); 301 cmd.addArgument("-idl"); 302 cmd.addArgument("-always"); 303 if (directoryOutputName.length() != 0) { 304 cmd.addArgument("-d"); 305 cmd.addArgument(directoryOutputName); 306 } 307 cmd.addArguments(optionsRmiC); 308 for (Enumeration e = remoteClasses.elements() ; e.hasMoreElements() ;) { 309 String className = (String )e.nextElement(); 310 cmd.addArgument(className); 311 } 312 313 trace("Running '" + cmd.toString() + "'"); 314 if (cmd.run()) { 315 trace("The IDL of the Home and Remote interfaces of the beans" 316 +" are successfully generated via rmic -idl."); 317 } else { 318 throw new GenIDLException("Failed when generating the IDL of the Home and Remote interfaces of the beans via rmic -idl."); 319 } 320 321 } 322 323 326 private static void usage() { 327 StringBuffer msg = new StringBuffer (); 328 msg.append("Usage: java org.objectweb.jonas_ejb.genidl.GenIDL -help \n"); 329 msg.append(" to print this help message \n"); 330 msg.append(" or java org.objectweb.jonas_ejb.genidl.GenIDL <Options> <Input_File> \n"); 331 msg.append(" to generate the IDLs for given EJB(s). \n"); 332 msg.append(" \n"); 333 msg.append("Options include: \n"); 334 msg.append(" -d <output_dir> specify where to place the generated files \n"); 335 msg.append(" -novalidation parse the XML deployment descriptors without \n"); 336 msg.append(" validation \n"); 337 msg.append(" -rmicopts <opt> specify the options to pass to the rmi compiler \n"); 338 msg.append(" -verbose \n"); 339 msg.append(" \n"); 340 msg.append("Input_File standard deployment descriptor file's name or \n"); 341 msg.append(" ejb-jar file's name \n"); 342 GenIDL.info(msg.toString()); 343 } 344 345 349 void trace(String msg) { 350 if (verbose) { 351 info("-- "+msg); 352 } 353 } 354 355 359 static void info(String msg) { 360 System.out.println(msg); 361 } 362 363 367 static void error(String errMsg) { 368 System.err.println("GenIDL error: " + errMsg); 369 } 370 371 372 377 static void fatalError(String errMsg) { 378 System.err.println("GenIDL fatal error: " + errMsg); 379 System.exit(EXIT_FAILURE); 380 } 381 } 382 383 | Popular Tags |