1 18 package org.apache.tools.ant.taskdefs.optional.ejb; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileWriter ; 23 import java.io.IOException ; 24 import java.io.ObjectInputStream ; 25 import java.io.PrintWriter ; 26 import java.util.Vector ; 27 import javax.ejb.deployment.DeploymentDescriptor; 28 import javax.ejb.deployment.EntityDescriptor; 29 30 31 38 public final class EjbcHelper { 39 42 private File descriptorDirectory; 43 44 47 private File generatedFilesDirectory; 48 49 52 private File manifestFile; 53 54 58 private File sourceDirectory; 59 60 64 String [] descriptors; 65 67 private boolean keepGenerated; 68 69 74 public static void main(String [] args) throws Exception { 75 EjbcHelper helper = new EjbcHelper(args); 76 helper.process(); 77 } 78 79 82 private EjbcHelper(String [] args) { 83 int index = 0; 84 descriptorDirectory = new File (args[index++]); 85 generatedFilesDirectory = new File (args[index++]); 86 sourceDirectory = new File (args[index++]); 87 manifestFile = new File (args[index++]); 88 keepGenerated = Boolean.valueOf(args[index++]).booleanValue(); 89 90 descriptors = new String [args.length - index]; 91 for (int i = 0; index < args.length; ++i) { 92 descriptors[i] = args[index++]; 93 } 94 } 95 96 private String [] getCommandLine(boolean debug, File descriptorFile) { 97 Vector v = new Vector (); 98 if (!debug) { 99 v.addElement("-noexit"); 100 } 101 if (keepGenerated) { 102 v.addElement("-keepgenerated"); 103 } 104 v.addElement("-d"); 105 v.addElement(generatedFilesDirectory.getPath()); 106 v.addElement(descriptorFile.getPath()); 107 108 String [] args = new String [v.size()]; 109 v.copyInto(args); 110 return args; 111 } 112 113 129 private boolean isRegenRequired(File descriptorFile) throws IOException { 130 FileInputStream fis = null; 135 try { 136 fis = new FileInputStream (descriptorFile); 137 ObjectInputStream ois = new ObjectInputStream (fis); 138 DeploymentDescriptor dd = (DeploymentDescriptor) ois.readObject(); 139 fis.close(); 140 141 String homeInterfacePath 142 = dd.getHomeInterfaceClassName().replace('.', '/') + ".java"; 143 String remoteInterfacePath 144 = dd.getRemoteInterfaceClassName().replace('.', '/') + ".java"; 145 String primaryKeyClassPath = null; 146 if (dd instanceof EntityDescriptor) { 147 primaryKeyClassPath 148 = ((EntityDescriptor) dd).getPrimaryKeyClassName(); 149 primaryKeyClassPath 150 = primaryKeyClassPath.replace('.', '/') + ".java"; 151 } 152 153 File homeInterfaceSource = new File (sourceDirectory, homeInterfacePath); 154 File remoteInterfaceSource = new File (sourceDirectory, remoteInterfacePath); 155 File primaryKeyClassSource = null; 156 if (primaryKeyClassPath != null) { 157 primaryKeyClassSource = new File (sourceDirectory, remoteInterfacePath); 158 } 159 160 String beanClassBase = dd.getEnterpriseBeanClassName().replace('.', '/'); 164 File ejbImplentationClass 165 = new File (generatedFilesDirectory, beanClassBase + "EOImpl.class"); 166 File homeImplementationClass 167 = new File (generatedFilesDirectory, beanClassBase + "HomeImpl.class"); 168 File beanStubClass 169 = new File (generatedFilesDirectory, beanClassBase + "EOImpl_WLStub.class"); 170 171 if (!ejbImplentationClass.exists() 173 || !homeImplementationClass.exists() 174 || !beanStubClass.exists()) { 175 return true; 176 } 177 178 long classModificationTime = ejbImplentationClass.lastModified(); 181 if (homeImplementationClass.lastModified() < classModificationTime) { 182 classModificationTime = homeImplementationClass.lastModified(); 183 } 184 if (beanStubClass.lastModified() < classModificationTime) { 185 classModificationTime = beanStubClass.lastModified(); 186 } 187 188 if (descriptorFile.lastModified() > classModificationTime 189 || homeInterfaceSource.lastModified() > classModificationTime 190 || remoteInterfaceSource.lastModified() > classModificationTime) { 191 return true; 192 } 193 194 if (primaryKeyClassSource != null 195 && primaryKeyClassSource.lastModified() > classModificationTime) { 196 return true; 197 } 198 } catch (Throwable descriptorLoadException) { 199 System.out.println("Exception occurred reading " 200 + descriptorFile.getName() + " - continuing"); 201 return true; 203 } finally { 204 if (fis != null) { 205 fis.close(); 206 } 207 } 208 209 return false; 210 } 211 212 216 private void process() throws Exception { 217 String manifest = "Manifest-Version: 1.0\n\n"; 218 for (int i = 0; i < descriptors.length; ++i) { 219 String descriptorName = descriptors[i]; 220 File descriptorFile = new File (descriptorDirectory, descriptorName); 221 222 if (isRegenRequired(descriptorFile)) { 223 System.out.println("Running ejbc for " + descriptorFile.getName()); 224 regenerateSupportClasses(descriptorFile); 225 } else { 226 System.out.println(descriptorFile.getName() + " is up to date"); 227 } 228 manifest += "Name: " + descriptorName.replace('\\', '/') 229 + "\nEnterprise-Bean: True\n\n"; 230 } 231 232 FileWriter fw = new FileWriter (manifestFile); 233 PrintWriter pw = new PrintWriter (fw); 234 pw.print(manifest); 235 fw.flush(); 236 fw.close(); 237 } 238 239 245 private void regenerateSupportClasses(File descriptorFile) throws Exception { 246 248 249 String [] args = getCommandLine(false, descriptorFile); 250 251 try { 252 weblogic.ejbc.main(args); 253 } catch (Exception e) { 254 String [] newArgs = getCommandLine(true, descriptorFile); 256 weblogic.ejbc.main(newArgs); 257 } 258 } 259 } 260 | Popular Tags |