KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > JBDeploymentFactory


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;
20
21 import java.util.HashMap JavaDoc;
22 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
23 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLClassLoader JavaDoc;
29 import java.security.AllPermission JavaDoc;
30 import java.security.CodeSource JavaDoc;
31 import java.security.PermissionCollection JavaDoc;
32 import java.security.Permissions JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.HashSet JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Set JavaDoc;
39 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginUtils;
40 import org.openide.ErrorManager;
41 import org.openide.util.NbBundle;
42 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
43 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException JavaDoc;
44 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
45 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager JavaDoc;
46 import org.openide.filesystems.FileObject;
47 import org.openide.filesystems.FileUtil;
48 import org.openide.filesystems.Repository;
49 import org.openide.util.Lookup;
50
51 /**
52  *
53  * @author Kirill Sorokin
54  */

55 public class JBDeploymentFactory implements DeploymentFactory JavaDoc {
56     
57     public static final String JavaDoc URI_PREFIX = "jboss-deployer:"; // NOI18N
58

59     private static final String JavaDoc DISCONNECTED_URI = "jboss-deployer:http://localhost:8080&"; // NOI18N
60

61     private static JBDeploymentFactory instance;
62     
63     public static synchronized DeploymentFactory JavaDoc create() {
64         if (instance == null) {
65             instance = new JBDeploymentFactory();
66             DeploymentFactoryManager.getInstance().registerDeploymentFactory(instance);
67
68             registerDefaultServerInstance();
69         }
70         
71         return instance;
72     }
73     
74 // private DeploymentFactory jbossFactory = null;
75
/**
76      * Mapping of a server installation directory to a deployment factory
77      */

78     private HashMap JavaDoc/*<String, DeploymentFactory*/ jbossFactories = new HashMap JavaDoc();
79     
80     public static class JBClassLoader extends URLClassLoader JavaDoc {
81
82         public JBClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc parent) throws MalformedURLException JavaDoc, RuntimeException JavaDoc {
83             super(urls, parent);
84         }
85         
86         protected PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc codeSource) {
87             Permissions JavaDoc p = new Permissions JavaDoc();
88             p.add(new AllPermission JavaDoc());
89             return p;
90         }
91         
92        public Enumeration JavaDoc<URL JavaDoc> getResources(String JavaDoc name) throws IOException JavaDoc {
93            // get rid of annoying warnings
94
if (name.indexOf("jndi.properties") != -1) {// || name.indexOf("i18n_user.properties") != -1) { // NOI18N
95
return Collections.enumeration(Collections.<URL JavaDoc>emptyList());
96            }
97
98            return super.getResources(name);
99        }
100     }
101
102     public static URLClassLoader JavaDoc getJBClassLoader(String JavaDoc serverRoot, String JavaDoc domainRoot){
103         try {
104             // dom4j.jar library for JBoss Application Server 4.0.4 and lower and JBoss Application Server 5.0
105
File JavaDoc dom404 = new File JavaDoc(serverRoot + "/lib/dom4j.jar"); // NOI18N
106

107             // dom4j.jar library for JBoss Application Server 4.0.5
108
File JavaDoc dom405 = new File JavaDoc(domainRoot + "/lib/dom4j.jar"); // NOI18N
109

110             // jboss-common-client.jar JBoss Application Server 4.x
111
File JavaDoc client40 = new File JavaDoc(serverRoot + "/client/jboss-common-client.jar"); // NOI18N
112

113             // jboss-client.jar JBoss Application Server 5.0
114
File JavaDoc client50 = new File JavaDoc(serverRoot + "/client/jboss-client.jar"); // NOI18N
115

116             // jboss-common-core.jar for JBoss Application Server 5.0
117
File JavaDoc core50 = new File JavaDoc(serverRoot + "/client/jboss-common-core.jar"); // NOI18N
118

119             // jboss-logging-spi.jar for JBoss Application Server 5.0
120
File JavaDoc logging50 = new File JavaDoc(serverRoot + "/client/jboss-logging-spi.jar"); // NOI18N
121

122             List JavaDoc<URL JavaDoc> urlList = new ArrayList JavaDoc<URL JavaDoc>();
123             
124             urlList.add(new File JavaDoc(serverRoot + "/client/jbossall-client.jar").toURI().toURL()); //NOI18N
125
urlList.add(new File JavaDoc(serverRoot + "/client/jboss-deployment.jar").toURI().toURL()); //NOI18N
126
urlList.add(new File JavaDoc(serverRoot + "/client/jnp-client.jar").toURI().toURL()); //NOI18N
127
urlList.add((dom404.exists()) ? (dom404.toURI().toURL()) : (dom405.toURI().toURL())); //NOI18N
128

129             if(client40.exists())
130                 urlList.add(client40.toURI().toURL());
131
132             if(client50.exists())
133                 urlList.add(client50.toURI().toURL());
134
135             if(core50.exists())
136                 urlList.add(core50.toURI().toURL());
137
138             if(logging50.exists())
139                 urlList.add(logging50.toURI().toURL());
140             
141             URLClassLoader JavaDoc loader = new JBClassLoader(urlList.toArray(new URL JavaDoc[] {}), JBDeploymentFactory.class.getClassLoader());
142             return loader;
143         } catch (Exception JavaDoc e) {
144             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
145         }
146         return null;
147     }
148     
149     public DeploymentFactory JavaDoc getFactory(String JavaDoc instanceURL) {
150         DeploymentFactory JavaDoc jbossFactory = null;
151         try {
152             String JavaDoc jbossRoot = InstanceProperties.getInstanceProperties(instanceURL).
153                                     getProperty(JBPluginProperties.PROPERTY_ROOT_DIR);
154             
155             String JavaDoc domainRoot = InstanceProperties.getInstanceProperties(instanceURL).
156                                     getProperty(JBPluginProperties.PROPERTY_SERVER_DIR);
157             
158             // if jbossRoot is null, then we are in a server instance registration process, thus this call
159
// is made from InstanceProperties creation -> JBPluginProperties singleton contains
160
// install location of the instance being registered
161
if (jbossRoot == null)
162                 jbossRoot = JBPluginProperties.getInstance().getInstallLocation();
163             
164             // if domainRoot is null, then we are in a server instance registration process, thus this call
165
// is made from InstanceProperties creation -> JBPluginProperties singleton contains
166
// install location of the instance being registered
167
if (domainRoot == null)
168                 domainRoot = JBPluginProperties.getInstance().getDomainLocation();
169             
170             jbossFactory = (DeploymentFactory JavaDoc) jbossFactories.get(jbossRoot);
171             if ( jbossFactory == null ) {
172                 URLClassLoader JavaDoc loader = getJBClassLoader(jbossRoot, domainRoot);
173                 jbossFactory = (DeploymentFactory JavaDoc) loader.loadClass("org.jboss.deployment.spi.factories.DeploymentFactoryImpl").newInstance();//NOI18N
174

175                 jbossFactories.put(jbossRoot, jbossFactory);
176             }
177         } catch (Exception JavaDoc e) {
178             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
179         }
180
181         return jbossFactory;
182     }
183     
184     public boolean handlesURI(String JavaDoc uri) {
185         if (uri != null && uri.startsWith(URI_PREFIX)) {
186             return true;
187         }
188         
189         return false;
190     }
191     
192     public DeploymentManager JavaDoc getDeploymentManager(String JavaDoc uri, String JavaDoc uname, String JavaDoc passwd) throws DeploymentManagerCreationException JavaDoc {
193         if (!handlesURI(uri)) {
194             throw new DeploymentManagerCreationException JavaDoc(NbBundle.getMessage(JBDeploymentFactory.class, "MSG_INVALID_URI", uri)); // NOI18N
195
}
196         
197         DeploymentFactory JavaDoc df = getFactory(uri);
198         if (df == null)
199             throw new DeploymentManagerCreationException JavaDoc(NbBundle.getMessage(JBDeploymentFactory.class, "MSG_ERROR_CREATING_DM", uri)); // NOI18N
200

201         String JavaDoc jbURI = uri;
202         try {
203             jbURI = uri.substring(0, uri.indexOf("&")); // NOI18N
204
}
205         catch (Exception JavaDoc e) {
206             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
207         }
208         
209         return new JBDeploymentManager(df.getDeploymentManager(jbURI, uname, passwd), uri, uname, passwd);
210     }
211      
212     public DeploymentManager JavaDoc getDisconnectedDeploymentManager(String JavaDoc uri) throws DeploymentManagerCreationException JavaDoc {
213         if (!handlesURI(uri)) {
214             throw new DeploymentManagerCreationException JavaDoc(NbBundle.getMessage(JBDeploymentFactory.class, "MSG_INVALID_URI", uri)); // NOI18N
215
}
216         
217         DeploymentFactory JavaDoc df = null;
218         InstanceProperties ip = InstanceProperties.getInstanceProperties(uri);
219         if (ip == null) {
220             // null ip either means that the instance is not registered, or that this is the disconnected URL
221
if (!DISCONNECTED_URI.equals(uri)) {
222                 throw new DeploymentManagerCreationException JavaDoc("JBoss instance " + uri + " is not registered in the IDE."); // NOI18N
223
}
224         }
225         else {
226             df = getFactory(uri);
227             if (df == null) {
228                 throw new DeploymentManagerCreationException JavaDoc(NbBundle.getMessage(JBDeploymentFactory.class, "MSG_ERROR_CREATING_DM", uri)); // NOI18N
229
}
230         }
231
232         String JavaDoc jbURI = uri;
233         try {
234             jbURI = uri.substring(0, uri.indexOf("&")); // NOI18N
235
}
236         catch (Exception JavaDoc e) {
237             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
238         }
239
240         return new JBDeploymentManager((df != null ? df.getDisconnectedDeploymentManager(jbURI) : null), uri, null, null);
241     }
242     
243     public String JavaDoc getProductVersion() {
244        
245         return NbBundle.getMessage (JBDeploymentFactory.class, "LBL_JBossFactoryVersion");
246     }
247     
248     public String JavaDoc getDisplayName() {
249         return NbBundle.getMessage(JBDeploymentFactory.class, "SERVER_NAME"); // NOI18N
250
}
251     
252     private static final String JavaDoc INSTALL_ROOT_PROP_NAME = "org.netbeans.modules.j2ee.jboss4.installRoot"; // NOI18N
253

254     private static void registerDefaultServerInstance() {
255         try {
256             FileObject serverInstanceDir = getServerInstanceDir();
257             String JavaDoc serverLocation = getDefaultInstallLocation();
258             String JavaDoc domainLocation = serverLocation + File.separator + "server" + File.separator + "default"; // NOI18N
259
setRemovability(serverInstanceDir, domainLocation);
260             if (JBPluginUtils.isGoodJBServerLocation4x(new File JavaDoc(serverLocation)) &&
261                 JBPluginUtils.isGoodJBInstanceLocation4x(new File JavaDoc(domainLocation)) ||
262                 JBPluginUtils.isGoodJBServerLocation5x(new File JavaDoc(serverLocation)) &&
263                 JBPluginUtils.isGoodJBInstanceLocation5x(new File JavaDoc(domainLocation)))
264             {
265                 if (!isAlreadyRegistered(serverInstanceDir, domainLocation)) {
266                     String JavaDoc host = "localhost"; // NOI18N
267
String JavaDoc port = JBPluginUtils.getHTTPConnectorPort(domainLocation); // NOI18N
268
register(serverInstanceDir, serverLocation, domainLocation, host, port);
269                 }
270             }
271         }
272         catch (IOException JavaDoc ioe) {
273             ErrorManager.getDefault().log(ErrorManager.EXCEPTION, ioe.getMessage());
274         }
275     }
276     
277     private static String JavaDoc getDefaultInstallLocation() {
278         String JavaDoc installRoot = System.getProperty(INSTALL_ROOT_PROP_NAME);
279         if (installRoot != null && new File JavaDoc(installRoot).exists()) {
280             return installRoot;
281         }
282         
283         return "";
284     }
285     
286     private static boolean isAlreadyRegistered(FileObject serverInstanceDir, String JavaDoc domainLocation) throws IOException JavaDoc {
287         String JavaDoc domainLocationCan = new File JavaDoc(domainLocation).getCanonicalPath();
288         for (FileObject instanceFO : serverInstanceDir.getChildren()) {
289             String JavaDoc installedLocation = (String JavaDoc)instanceFO.getAttribute(JBPluginProperties.PROPERTY_SERVER_DIR);
290             if (installedLocation != null) {
291                 String JavaDoc installedLocationCan = new File JavaDoc(installedLocation).getCanonicalPath();
292                 if (domainLocationCan.equals(installedLocationCan)) {
293                     return true; // do not overwrite registered instance
294
}
295             }
296         }
297         
298         return false;
299     }
300
301     private static void setRemovability(FileObject serverInstanceDir, String JavaDoc domainLocation) throws IOException JavaDoc {
302         String JavaDoc domainLocationCan = new File JavaDoc(domainLocation).getCanonicalPath();
303         for (FileObject instanceFO : serverInstanceDir.getChildren()) {
304             String JavaDoc url = (String JavaDoc)instanceFO.getAttribute(InstanceProperties.URL_ATTR);
305             if (url.startsWith(URI_PREFIX)) { // it's JBoss instance
306
String JavaDoc installedLocation = (String JavaDoc)instanceFO.getAttribute(JBPluginProperties.PROPERTY_SERVER_DIR);
307                 String JavaDoc installedLocationCan = new File JavaDoc(installedLocation).getCanonicalPath();
308                 if (domainLocationCan.equals(installedLocationCan)) {
309                     instanceFO.setAttribute(InstanceProperties.REMOVE_FORBIDDEN, Boolean.TRUE);
310                 }
311                 else {
312                     if (instanceFO.getAttribute(InstanceProperties.REMOVE_FORBIDDEN) != null) {
313                         instanceFO.setAttribute(InstanceProperties.REMOVE_FORBIDDEN, Boolean.FALSE);
314                     }
315                 }
316             }
317         }
318     }
319
320     private static void register(FileObject serverInstanceDir, String JavaDoc serverLocation, String JavaDoc domainLocation, String JavaDoc host, String JavaDoc port) throws IOException JavaDoc {
321         String JavaDoc displayName = generateDisplayName(serverInstanceDir);
322         
323         String JavaDoc url = URI_PREFIX + host + ":" + port + "#default&" + serverLocation; // NOI18N
324

325         String JavaDoc name = FileUtil.findFreeFileName(serverInstanceDir, "instance", null); // NOI18N
326
FileObject instanceFO = serverInstanceDir.createData(name);
327
328         instanceFO.setAttribute(InstanceProperties.URL_ATTR, url);
329         instanceFO.setAttribute(InstanceProperties.USERNAME_ATTR, "");
330         instanceFO.setAttribute(InstanceProperties.PASSWORD_ATTR, "");
331         instanceFO.setAttribute(InstanceProperties.DISPLAY_NAME_ATTR, displayName);
332         instanceFO.setAttribute(InstanceProperties.REMOVE_FORBIDDEN, "true");
333
334         instanceFO.setAttribute(JBPluginProperties.PROPERTY_SERVER, "default"); // NOI18N
335
String JavaDoc deployDir = JBPluginUtils.getDeployDir(domainLocation);
336         instanceFO.setAttribute(JBPluginProperties.PROPERTY_DEPLOY_DIR, deployDir);
337         instanceFO.setAttribute(JBPluginProperties.PROPERTY_SERVER_DIR, domainLocation);
338         instanceFO.setAttribute(JBPluginProperties.PROPERTY_ROOT_DIR, serverLocation);
339         instanceFO.setAttribute(JBPluginProperties.PROPERTY_HOST, host);
340         instanceFO.setAttribute(JBPluginProperties.PROPERTY_PORT, port);
341     }
342
343     private static FileObject getServerInstanceDir() {
344         Repository rep = (Repository)Lookup.getDefault().lookup(Repository.class);
345         FileObject dir = rep.getDefaultFileSystem().findResource("/J2EE/InstalledServers"); // NOI18N
346
return dir;
347     }
348     
349     private static String JavaDoc generateDisplayName(FileObject serverInstanceDir) {
350         final String JavaDoc serverName = NbBundle.getMessage(JBDeploymentFactory.class, "SERVER_NAME"); // NOI18N
351

352         String JavaDoc instanceName = serverName;
353         int counter = 1;
354         Set JavaDoc<String JavaDoc> registeredInstances = getServerInstancesNames(serverInstanceDir);
355         
356         while (registeredInstances.contains(instanceName.toUpperCase())) {
357             instanceName = serverName + " (" + String.valueOf(counter++) + ")";
358         }
359         
360         return instanceName;
361     }
362
363     private static Set JavaDoc<String JavaDoc> getServerInstancesNames(FileObject serverInstanceDir) {
364         Set JavaDoc<String JavaDoc> names = new HashSet JavaDoc<String JavaDoc>();
365         for (FileObject instanceFO : serverInstanceDir.getChildren()) {
366             String JavaDoc instanceName = (String JavaDoc)instanceFO.getAttribute(InstanceProperties.DISPLAY_NAME_ATTR);
367             names.add(instanceName.toUpperCase());
368         }
369         
370         return names;
371     }
372     
373 }
374
375
Popular Tags