1 22 package org.jboss.deployment; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.jar.Attributes ; 29 import java.util.jar.Manifest ; 30 31 import org.jboss.deployers.plugins.structure.ContextInfoImpl; 32 import org.jboss.deployers.plugins.structure.vfs.AbstractStructureDeployer; 33 import org.jboss.deployers.spi.structure.vfs.StructureMetaData; 34 import org.jboss.deployers.spi.structure.vfs.StructuredDeployers; 35 import org.jboss.metadata.XmlFileLoader; 36 import org.jboss.virtual.VirtualFile; 37 import org.jboss.virtual.VirtualFileFilter; 38 import org.jboss.virtual.plugins.vfs.helpers.SuffixMatchFilter; 39 import org.w3c.dom.Element ; 40 41 48 public class EARStructure extends AbstractStructureDeployer 49 { 50 53 public static final VirtualFileFilter DEFAULT_EAR_LIB_FILTER = new SuffixMatchFilter(".jar"); 54 55 58 private VirtualFileFilter earLibFilter = DEFAULT_EAR_LIB_FILTER; 59 60 64 public EARStructure() 65 { 66 setRelativeOrder(1000); 67 } 68 69 74 public VirtualFileFilter getEarLibFilter() 75 { 76 return earLibFilter; 77 } 78 79 85 public void setEarLibFilter(VirtualFileFilter earLibFilter) 86 { 87 if (earLibFilter == null) 88 throw new IllegalArgumentException ("Null filter"); 89 this.earLibFilter = earLibFilter; 90 } 91 92 98 public boolean determineStructure(VirtualFile root, 99 StructureMetaData metaData, StructuredDeployers deployers) 100 { 101 boolean valid = false; 102 try 103 { 104 if( root.isLeaf() == true || root.getName().endsWith(".ear") == false) 105 return false; 106 107 ContextInfoImpl context = new ContextInfoImpl(root.getPathName()); 108 context.setMetaDataPath("META-INF"); 109 110 VirtualFile applicationXml = getMetaDataFile(root, "META-INF/application.xml"); 111 VirtualFile jbossAppXml = getMetaDataFile(root, "META-INF/jboss-app.xml"); 112 VirtualFile lib = null; 113 J2eeApplicationMetaData j2eeMetaData = new J2eeApplicationMetaData(); 114 boolean scan = true; 115 116 if (applicationXml != null) 117 { 118 InputStream in = applicationXml.openStream(); 119 XmlFileLoader xfl = new XmlFileLoader(false); 120 Element application = xfl.getDocument(in, "META-INF/application.xml").getDocumentElement(); 121 j2eeMetaData.importXml(application); 122 in.close(); 123 scan = false; 124 } 125 if (jbossAppXml != null) 126 { 127 InputStream in = jbossAppXml.openStream(); 128 XmlFileLoader xfl = new XmlFileLoader(false); 129 Element jbossApp = xfl.getDocument(in, "META-INF/jboss-app.xml").getDocumentElement(); 130 j2eeMetaData.importXml(jbossApp); 131 in.close(); 132 } 133 134 metaData.addContext(context); 137 138 String libDir = j2eeMetaData.getLibraryDirectory() == null ? "lib" : j2eeMetaData.getLibraryDirectory(); 140 if (j2eeMetaData.getLibraryDirectory() != null) 141 { 142 try 143 { 144 lib = root.findChild(libDir); 145 if (lib != null) 146 { 147 List <VirtualFile> archives = lib.getChildren(earLibFilter); 148 for (VirtualFile archive : archives) 149 { 150 super.addClassPath(root, archive, true, true, context); 151 if (archive.findChild("META-INF/persistence.xml") != null) 153 { 154 log.trace(archive.getName() + " in ear lib directory has persistence units"); 155 if (deployers.determineStructure(archive, metaData) == false) 156 { 157 throw new RuntimeException (archive.getName() 158 + " in lib directory has persistence.xml but is not a recognized deployment, .ear: " 159 + root.getName()); 160 } 161 } 162 } 163 } 164 } 165 catch (IOException ignored) 166 { 167 } 169 } 170 171 super.addClassPath(root, root, false, true, context); 173 174 if( scan ) 176 { 177 scanEar(root, j2eeMetaData); 178 } 179 180 for (Iterator iter = j2eeMetaData.getModules(); iter.hasNext();) 182 { 183 J2eeModuleMetaData mod = (J2eeModuleMetaData) iter.next(); 184 String fileName = mod.getFileName(); 185 if (fileName != null && (fileName = fileName.trim()).length() > 0) 186 { 187 try 188 { 189 VirtualFile module = root.findChild(fileName); 190 if (module == null) 191 { 192 throw new RuntimeException (fileName + " module listed in application.xml does not exist within .ear " + root.getName()); 193 } 194 if( deployers.determineStructure(module, metaData) == false ) 196 { 197 throw new RuntimeException (fileName 198 + " module listed in application.xml is not a recognized deployment, .ear: " 199 + root.getName()); 200 } 201 } 202 catch (IOException ignored) 203 { 204 throw new RuntimeException (fileName + " module listed in application.xml does not exist within .ear " + root.getName(), ignored); 205 } 206 } 207 } 208 209 valid = true; 210 } 211 catch(Exception e) 212 { 213 throw new RuntimeException ("Error determining structure: " + root.getName(), e); 214 } 215 216 return valid; 217 } 218 219 240 private void scanEar(VirtualFile root, J2eeApplicationMetaData j2eeMetaData) 241 throws IOException 242 { 243 List <VirtualFile> archives = root.getChildren(); 244 if( archives != null ) 245 { 246 String earPath = root.getPathName(); 247 for(VirtualFile archive : archives) 248 { 249 String module = earRelativePath(earPath, archive.getPathName()); 250 int type = typeFromSuffix(module, archive); 251 if( type >= 0 ) 252 { 253 J2eeModuleMetaData mod = new J2eeModuleMetaData(type, module); 254 j2eeMetaData.addModule(mod); 255 } 256 } 257 } 258 } 259 260 private int typeFromSuffix(String path, VirtualFile archive) 261 throws IOException 262 { 263 int type = -1; 264 if( path.endsWith(".war") ) 265 type = J2eeModuleMetaData.WEB; 266 else if( path.endsWith(".rar") ) 267 type = J2eeModuleMetaData.CONNECTOR; 268 else if( path.endsWith(".har") ) 269 type = J2eeModuleMetaData.HAR; 270 else if( path.endsWith(".sar") ) 271 type = J2eeModuleMetaData.SERVICE; 272 else if( path.endsWith(".jar") ) 273 { 274 VirtualFile mfFile = getMetaDataFile(archive, "META-INF/MANIFEST.MF"); 276 VirtualFile clientXml = getMetaDataFile(archive, "META-INF/application-client.xml"); 277 VirtualFile ejbXml = getMetaDataFile(archive, "META-INF/ejb-jar.xml"); 278 VirtualFile jbossXml = getMetaDataFile(archive, "META-INF/jboss.xml"); 279 280 if( clientXml != null ) 281 { 282 type = J2eeModuleMetaData.CLIENT; 283 } 284 else if( mfFile != null ) 285 { 286 InputStream is = mfFile.openStream(); 287 Manifest mf = new Manifest (is); 288 is.close(); 289 Attributes attrs = mf.getMainAttributes(); 290 if( attrs.containsKey(Attributes.Name.MAIN_CLASS) ) 291 { 292 type = J2eeModuleMetaData.CLIENT; 293 } 294 else 295 { 296 type = J2eeModuleMetaData.EJB; 298 } 299 } 300 else if( ejbXml != null || jbossXml != null ) 301 { 302 type = J2eeModuleMetaData.EJB; 303 } 304 else 305 { 306 type = J2eeModuleMetaData.EJB; 308 } 309 } 310 311 return type; 312 } 313 314 private String earRelativePath(String earPath, String pathName) 315 { 316 StringBuilder tmp = new StringBuilder (pathName); 317 tmp.delete(0, earPath.length()); 318 return tmp.toString(); 319 } 320 321 private VirtualFile getMetaDataFile(VirtualFile file, String path) 322 { 323 VirtualFile metaFile = null; 324 try 325 { 326 metaFile = file.findChild(path); 327 } 328 catch(IOException e) 329 { 330 } 331 return metaFile; 332 } 333 } 334 | Popular Tags |