KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > ide > JBJ2eePlatformFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.jboss4.ide;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
26 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
27 import java.io.File JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Set JavaDoc;
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 JavaDoc;
46 import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager;
47 import java.io.FilenameFilter JavaDoc;
48 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
49 import org.openide.modules.InstalledFileLocator;
50
51  
52 /**
53  *
54  * @author Kirill Sorokin <Kirill.Sorokin@Sun.COM>
55  */

56 public class JBJ2eePlatformFactory extends J2eePlatformFactory {
57     
58     private static final WeakHashMap JavaDoc<InstanceProperties,J2eePlatformImplImpl> instanceCache = new WeakHashMap JavaDoc<InstanceProperties,J2eePlatformImplImpl>();
59     
60     public synchronized J2eePlatformImpl getJ2eePlatformImpl(DeploymentManager JavaDoc dm) {
61         assert JBDeploymentManager.class.isAssignableFrom(dm.getClass()) : this + " cannot create platform for unknown deployment manager:" + dm;
62         // Ensure that for each server instance will be always used the same instance of the J2eePlatformImpl
63
JBDeploymentManager manager = (JBDeploymentManager) dm;
64         InstanceProperties ip = manager.getInstanceProperties();
65         if (ip == null) {
66             throw new RuntimeException JavaDoc("Cannot create J2eePlatformImpl instance for " + manager.getUrl()); // NOI18N
67
}
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 JavaDoc J2EE_API_DOC = "docs/javaee5-doc-api.zip"; // NOI18N
78
private static final Set JavaDoc MODULE_TYPES = new HashSet JavaDoc();
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 JavaDoc SPEC_VERSIONS = new HashSet JavaDoc();
88         private static final Set JavaDoc SPEC_VERSIONS_5 = new HashSet JavaDoc();
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 JavaDoc getSupportedSpecVersions() {
104             if (properties.supportsJavaEE5ejb3() && properties.supportsJavaEE5web()) {
105                 return SPEC_VERSIONS_5;
106             } else {
107                 return SPEC_VERSIONS;
108             }
109         }
110
111         public Set JavaDoc<String JavaDoc> getSupportedSpecVersions(Object JavaDoc moduleType) {
112             // JavaEE5 app client is not supported for JBoss 5.x
113
if (properties.supportsJavaEE5ejb3() && properties.supportsJavaEE5web() && !J2eeModule.CLIENT.equals(moduleType)) {
114                 return SPEC_VERSIONS_5;
115             }
116             // JavaEE5 web and app client modules are not supported for JBoss 4.x
117
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 JavaDoc getSupportedModuleTypes() {
125             return MODULE_TYPES;
126         }
127         
128         public Set JavaDoc/*<String>*/ getSupportedJavaPlatformVersions() {
129             Set JavaDoc versions = new HashSet JavaDoc();
130             versions.add("1.4"); // NOI18N
131
versions.add("1.5"); // NOI18N
132
return versions;
133         }
134         
135         public JavaPlatform getJavaPlatform() {
136             return null;
137         }
138
139         public File JavaDoc[] getPlatformRoots() {
140             return new File JavaDoc[] {
141                 properties.getRootDir()
142             };
143         }
144         
145         private static class FF implements FilenameFilter JavaDoc {
146             public boolean accept(File JavaDoc dir, String JavaDoc name) {
147                 return name.endsWith(".jar") || new File JavaDoc(dir, name).isDirectory(); //NOI18N
148
}
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 JavaDoc getIcon() {
164             return null;
165         }
166
167         public String JavaDoc getDisplayName() {
168             return NbBundle.getMessage(JBJ2eePlatformFactory.class, "TITLE_JBOSS_FACTORY");
169
170         }
171
172         public boolean isToolSupported(String JavaDoc 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 JavaDoc providerName) {
191             return containsService(libraries, "javax.persistence.spi.PersistenceProvider", providerName);
192         }
193         
194         private static boolean containsService(LibraryImplementation[] libraries, String JavaDoc serviceName, String JavaDoc serviceImplName) {
195             for (LibraryImplementation libImpl : libraries) {
196                 if (containsService(libImpl, serviceName, serviceImplName)) { //NOI18N
197
return true;
198                 }
199             }
200             return false;
201         }
202                 
203         private static boolean containsService(LibraryImplementation library, String JavaDoc serviceName, String JavaDoc serviceImplName) {
204             List JavaDoc roots = library.getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH);
205             for (Iterator JavaDoc it = roots.iterator(); it.hasNext();) {
206                 URL JavaDoc rootUrl = (URL JavaDoc) it.next();
207                 FileObject root = URLMapper.findFileObject(rootUrl);
208                 if (root != null && "jar".equals(rootUrl.getProtocol())) { //NOI18N
209
FileObject archiveRoot = FileUtil.getArchiveRoot(FileUtil.getArchiveFile(root));
210                     String JavaDoc serviceRelativePath = "META-INF/services/" + serviceName; //NOI18N
211
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 JavaDoc serviceName, String JavaDoc serviceImplName) {
221             try {
222                 BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(serviceFO.getInputStream()));
223                 String JavaDoc 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 JavaDoc ex) {
233                 try {
234                     ErrorManager.getDefault().annotate(ex, serviceFO.getURL().toString());
235                 } catch (FileStateInvalidException fsie) {
236                     //noop
237
}
238                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
239             }
240             return false;
241         }
242         
243         public File JavaDoc[] getToolClasspathEntries(String JavaDoc toolName) {
244             if (J2eePlatform.TOOL_WSCOMPILE.equals(toolName)) {
245                 File JavaDoc root = InstalledFileLocator.getDefault().locate("modules/ext/jaxrpc16", null, false); // NOI18N
246
return new File JavaDoc[] {
247                     new File JavaDoc(root, "saaj-api.jar"), // NOI18N
248
new File JavaDoc(root, "saaj-impl.jar"), // NOI18N
249
new File JavaDoc(root, "jaxrpc-api.jar"), // NOI18N
250
new File JavaDoc(root, "jaxrpc-impl.jar"), // NOI18N
251
};
252             }
253             if (J2eePlatform.TOOL_APP_CLIENT_RUNTIME.equals(toolName)) {
254                 return new File JavaDoc(properties.getRootDir(), "client").listFiles(new FF()); // NOI18N
255
}
256             return null;
257         }
258
259
260         // copied from appserv plugin
261
private URL JavaDoc fileToUrl(File JavaDoc file) throws MalformedURLException JavaDoc {
262             URL JavaDoc url = file.toURI().toURL();
263             if (FileUtil.isArchiveFile(url)) {
264                 url = FileUtil.getArchiveRoot(url);
265             }
266             return url;
267         }
268         
269         public String JavaDoc getToolProperty(String JavaDoc toolName, String JavaDoc propertyName) {
270             if (J2eePlatform.TOOL_APP_CLIENT_RUNTIME.equals(toolName)) {
271                 if (J2eePlatform.TOOL_PROP_MAIN_CLASS.equals(propertyName)) {
272                     return ""; // NOI18N
273
}
274                 if (J2eePlatform.TOOL_PROP_MAIN_CLASS_ARGS.equals(propertyName)) {
275                     return ""; // NOI18N
276
}
277                 if ("j2ee.clientName".equals(propertyName)) { // NOI18N
278
return "${jar.name}"; // NOI18N
279
}
280                 if (J2eePlatform.TOOL_PROP_JVM_OPTS.equals(propertyName)) {
281                     return "-Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory" // NOI18N
282
+ " -Djava.naming.provider.url=jnp://localhost:1099" // NOI18N
283
+ " -Djava.naming.factory.url.pkgs=org.jboss.naming.client"; // NOI18N
284
}
285             }
286             return null;
287         }
288         
289             
290         // private helper methods -------------------------------------------------
291

292         private void initLibraries() {
293             // create library
294
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