1 19 package org.netbeans.modules.j2ee.jboss4.ide; 20 21 import java.io.BufferedReader ; 22 import java.io.InputStreamReader ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.WeakHashMap ; 26 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 27 import java.io.File ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.util.HashSet ; 31 import java.util.Set ; 32 import org.netbeans.api.java.platform.JavaPlatform; 33 import org.netbeans.modules.j2ee.deployment.common.api.J2eeLibraryTypeProvider; 34 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 35 import org.netbeans.modules.j2ee.deployment.plugins.api.J2eePlatformFactory; 36 import org.netbeans.modules.j2ee.deployment.plugins.api.J2eePlatformImpl; 37 import org.netbeans.modules.j2ee.jboss4.util.JBProperties; 38 import org.netbeans.spi.project.libraries.LibraryImplementation; 39 import org.openide.ErrorManager; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileStateInvalidException; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.filesystems.URLMapper; 44 import org.openide.util.NbBundle; 45 import javax.enterprise.deploy.spi.DeploymentManager ; 46 import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager; 47 import java.io.FilenameFilter ; 48 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 49 import org.openide.modules.InstalledFileLocator; 50 51 52 56 public class JBJ2eePlatformFactory extends J2eePlatformFactory { 57 58 private static final WeakHashMap <InstanceProperties,J2eePlatformImplImpl> instanceCache = new WeakHashMap <InstanceProperties,J2eePlatformImplImpl>(); 59 60 public synchronized J2eePlatformImpl getJ2eePlatformImpl(DeploymentManager dm) { 61 assert JBDeploymentManager.class.isAssignableFrom(dm.getClass()) : this + " cannot create platform for unknown deployment manager:" + dm; 62 JBDeploymentManager manager = (JBDeploymentManager) dm; 64 InstanceProperties ip = manager.getInstanceProperties(); 65 if (ip == null) { 66 throw new RuntimeException ("Cannot create J2eePlatformImpl instance for " + manager.getUrl()); } 68 J2eePlatformImplImpl platform = instanceCache.get(ip); 69 if (platform == null) { 70 platform = new J2eePlatformImplImpl(manager.getProperties()); 71 instanceCache.put(ip, platform); 72 } 73 return platform; 74 } 75 76 public static class J2eePlatformImplImpl extends J2eePlatformImpl { 77 private static final String J2EE_API_DOC = "docs/javaee5-doc-api.zip"; private static final Set MODULE_TYPES = new HashSet (); 79 static { 80 MODULE_TYPES.add(J2eeModule.EAR); 81 MODULE_TYPES.add(J2eeModule.WAR); 82 MODULE_TYPES.add(J2eeModule.EJB); 83 MODULE_TYPES.add(J2eeModule.CONN); 84 MODULE_TYPES.add(J2eeModule.CLIENT); 85 } 86 87 private static final Set SPEC_VERSIONS = new HashSet (); 88 private static final Set SPEC_VERSIONS_5 = new HashSet (); 89 static { 90 SPEC_VERSIONS.add(J2eeModule.J2EE_14); 91 SPEC_VERSIONS_5.add(J2eeModule.J2EE_14); 92 SPEC_VERSIONS_5.add(J2eeModule.JAVA_EE_5); 93 } 94 95 private LibraryImplementation[] libraries; 96 97 private final JBProperties properties; 98 99 public J2eePlatformImplImpl(JBProperties properties) { 100 this.properties = properties; 101 } 102 103 public Set getSupportedSpecVersions() { 104 if (properties.supportsJavaEE5ejb3() && properties.supportsJavaEE5web()) { 105 return SPEC_VERSIONS_5; 106 } else { 107 return SPEC_VERSIONS; 108 } 109 } 110 111 public Set <String > getSupportedSpecVersions(Object moduleType) { 112 if (properties.supportsJavaEE5ejb3() && properties.supportsJavaEE5web() && !J2eeModule.CLIENT.equals(moduleType)) { 114 return SPEC_VERSIONS_5; 115 } 116 if (properties.supportsJavaEE5ejb3() && !(J2eeModule.WAR.equals(moduleType) || J2eeModule.CLIENT.equals(moduleType))) { 118 return SPEC_VERSIONS_5; 119 } else { 120 return SPEC_VERSIONS; 121 } 122 } 123 124 public Set getSupportedModuleTypes() { 125 return MODULE_TYPES; 126 } 127 128 public Set getSupportedJavaPlatformVersions() { 129 Set versions = new HashSet (); 130 versions.add("1.4"); versions.add("1.5"); return versions; 133 } 134 135 public JavaPlatform getJavaPlatform() { 136 return null; 137 } 138 139 public File [] getPlatformRoots() { 140 return new File [] { 141 properties.getRootDir() 142 }; 143 } 144 145 private static class FF implements FilenameFilter { 146 public boolean accept(File dir, String name) { 147 return name.endsWith(".jar") || new File (dir, name).isDirectory(); } 149 } 150 151 public LibraryImplementation[] getLibraries() { 152 if (libraries == null) { 153 initLibraries(); 154 } 155 return libraries; 156 } 157 158 public void notifyLibrariesChanged() { 159 initLibraries(); 160 firePropertyChange(PROP_LIBRARIES, null, libraries.clone()); 161 } 162 163 public java.awt.Image getIcon() { 164 return null; 165 } 166 167 public String getDisplayName() { 168 return NbBundle.getMessage(JBJ2eePlatformFactory.class, "TITLE_JBOSS_FACTORY"); 169 170 } 171 172 public boolean isToolSupported(String toolName) { 173 if (J2eePlatform.TOOL_WSCOMPILE.equals(toolName) 174 || J2eePlatform.TOOL_APP_CLIENT_RUNTIME.equals(toolName) ) { 175 return true; 176 } 177 if ("org.hibernate.ejb.HibernatePersistence".equals(toolName) || 178 "oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider".equals(toolName) || 179 "kodo.persistence.PersistenceProviderImpl".equals(toolName)) 180 { 181 return containsPersistenceProvider(toolName); 182 } 183 if ("hibernatePersistenceProviderIsDefault".equals(toolName)) { 184 return true; 185 } 186 187 return false; 188 } 189 190 private boolean containsPersistenceProvider(String providerName) { 191 return containsService(libraries, "javax.persistence.spi.PersistenceProvider", providerName); 192 } 193 194 private static boolean containsService(LibraryImplementation[] libraries, String serviceName, String serviceImplName) { 195 for (LibraryImplementation libImpl : libraries) { 196 if (containsService(libImpl, serviceName, serviceImplName)) { return true; 198 } 199 } 200 return false; 201 } 202 203 private static boolean containsService(LibraryImplementation library, String serviceName, String serviceImplName) { 204 List roots = library.getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH); 205 for (Iterator it = roots.iterator(); it.hasNext();) { 206 URL rootUrl = (URL ) it.next(); 207 FileObject root = URLMapper.findFileObject(rootUrl); 208 if (root != null && "jar".equals(rootUrl.getProtocol())) { FileObject archiveRoot = FileUtil.getArchiveRoot(FileUtil.getArchiveFile(root)); 210 String serviceRelativePath = "META-INF/services/" + serviceName; FileObject serviceFO = archiveRoot.getFileObject(serviceRelativePath); 212 if (serviceFO != null && containsService(serviceFO, serviceName, serviceImplName)) { 213 return true; 214 } 215 } 216 } 217 return false; 218 } 219 220 private static boolean containsService(FileObject serviceFO, String serviceName, String serviceImplName) { 221 try { 222 BufferedReader br = new BufferedReader (new InputStreamReader (serviceFO.getInputStream())); 223 String line; 224 while ((line = br.readLine()) != null) { 225 int ci = line.indexOf('#'); 226 if (ci >= 0) line = line.substring(0, ci); 227 if (line.trim().equals(serviceImplName)) { 228 return true; 229 } 230 } 231 } 232 catch (Exception ex) { 233 try { 234 ErrorManager.getDefault().annotate(ex, serviceFO.getURL().toString()); 235 } catch (FileStateInvalidException fsie) { 236 } 238 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 239 } 240 return false; 241 } 242 243 public File [] getToolClasspathEntries(String toolName) { 244 if (J2eePlatform.TOOL_WSCOMPILE.equals(toolName)) { 245 File root = InstalledFileLocator.getDefault().locate("modules/ext/jaxrpc16", null, false); return new File [] { 247 new File (root, "saaj-api.jar"), new File (root, "saaj-impl.jar"), new File (root, "jaxrpc-api.jar"), new File (root, "jaxrpc-impl.jar"), }; 252 } 253 if (J2eePlatform.TOOL_APP_CLIENT_RUNTIME.equals(toolName)) { 254 return new File (properties.getRootDir(), "client").listFiles(new FF()); } 256 return null; 257 } 258 259 260 private URL fileToUrl(File file) throws MalformedURLException { 262 URL url = file.toURI().toURL(); 263 if (FileUtil.isArchiveFile(url)) { 264 url = FileUtil.getArchiveRoot(url); 265 } 266 return url; 267 } 268 269 public String getToolProperty(String toolName, String propertyName) { 270 if (J2eePlatform.TOOL_APP_CLIENT_RUNTIME.equals(toolName)) { 271 if (J2eePlatform.TOOL_PROP_MAIN_CLASS.equals(propertyName)) { 272 return ""; } 274 if (J2eePlatform.TOOL_PROP_MAIN_CLASS_ARGS.equals(propertyName)) { 275 return ""; } 277 if ("j2ee.clientName".equals(propertyName)) { return "${jar.name}"; } 280 if (J2eePlatform.TOOL_PROP_JVM_OPTS.equals(propertyName)) { 281 return "-Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory" + " -Djava.naming.provider.url=jnp://localhost:1099" + " -Djava.naming.factory.url.pkgs=org.jboss.naming.client"; } 285 } 286 return null; 287 } 288 289 290 292 private void initLibraries() { 293 LibraryImplementation lib = new J2eeLibraryTypeProvider().createLibrary(); 295 lib.setName(NbBundle.getMessage(JBJ2eePlatformFactory.class, "TITLE_JBOSS_LIBRARY")); 296 lib.setContent(J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH, properties.getClasses()); 297 lib.setContent(J2eeLibraryTypeProvider.VOLUME_TYPE_JAVADOC, properties.getJavadocs()); 298 lib.setContent(J2eeLibraryTypeProvider.VOLUME_TYPE_SRC, properties.getSources()); 299 libraries = new LibraryImplementation[] {lib}; 300 } 301 } 302 } 303 | Popular Tags |