KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > weblogic9 > WLDeploymentFactory


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.weblogic9;
20
21 import java.io.*;
22 import java.lang.reflect.Method JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLClassLoader JavaDoc;
26 import java.security.AllPermission JavaDoc;
27 import java.security.CodeSource JavaDoc;
28 import java.security.PermissionCollection JavaDoc;
29 import java.security.Permissions JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.WeakHashMap JavaDoc;
33 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager JavaDoc;
34 import org.netbeans.modules.j2ee.weblogic9.util.WLDebug;
35 import org.openide.ErrorManager;
36
37 import javax.enterprise.deploy.spi.*;
38 import javax.enterprise.deploy.spi.factories.*;
39 import javax.enterprise.deploy.spi.exceptions.*;
40 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
41
42 import org.openide.util.NbBundle;
43
44 /**
45  * The main entry point to the plugin. Keeps the required static data for the
46  * plugin and returns the DeploymentManagers required for deployment and
47  * configuration. Does not directly perform any interaction with the server.
48  *
49  * @author Kirill Sorokin
50  */

51 public class WLDeploymentFactory implements DeploymentFactory {
52     
53     public static final String JavaDoc URI_PREFIX = "deployer:WebLogic:http://"; // NOI18N
54

55     /**
56      * The singleton instance of the factory
57      */

58     private static WLDeploymentFactory instance;
59
60     private DeploymentFactory wlFactory = null;
61
62     private static final WeakHashMap JavaDoc<InstanceProperties, WLDeploymentManager> managerCache =
63             new WeakHashMap JavaDoc<InstanceProperties, WLDeploymentManager>();
64     
65     /**
66      * The singleton factory method
67      *
68      * @return the singleton instance of the factory
69      */

70     public static synchronized DeploymentFactory getInstance() {
71         if (instance == null) {
72             instance = new WLDeploymentFactory();
73             DeploymentFactoryManager.getInstance().registerDeploymentFactory(instance);
74         }
75         return instance;
76     }
77     
78     public static class WLClassLoader extends URLClassLoader JavaDoc {
79
80         public WLClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc parent) throws MalformedURLException JavaDoc, RuntimeException JavaDoc {
81             super(urls, parent);
82         }
83         
84         public void addURL(File f) throws MalformedURLException JavaDoc, RuntimeException JavaDoc {
85             if (f.isFile()) {
86                 addURL(f.toURL());
87             }
88         }
89
90         protected PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc codeSource) {
91             Permissions JavaDoc p = new Permissions JavaDoc();
92             p.add(new AllPermission JavaDoc());
93             return p;
94         }
95         
96        public Enumeration JavaDoc<URL JavaDoc> getResources(String JavaDoc name) throws IOException {
97            // get rid of annoying warnings
98
if (name.indexOf("jndi.properties") != -1 || name.indexOf("i18n_user.properties") != -1) { // NOI18N
99
return Collections.enumeration(Collections.<URL JavaDoc>emptyList());
100            }
101
102            return super.getResources(name);
103        }
104     }
105     
106     private static WLClassLoader loader;
107     
108     public static ClassLoader JavaDoc getWLClassLoader (String JavaDoc serverRoot) {
109         if (loader == null) {
110             resetWLClassLoader(serverRoot);
111         }
112         return loader;
113     }
114     
115     private static void resetWLClassLoader (String JavaDoc serverRoot) {
116         loader = null;
117         try {
118             URL JavaDoc[] urls = new URL JavaDoc[] { new File(serverRoot + "/server/lib/weblogic.jar").toURI().toURL()}; // NOI18N
119
loader = new WLClassLoader(urls, WLDeploymentFactory.class.getClassLoader());
120         } catch (Exception JavaDoc e) {
121             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
122         }
123     }
124     
125     private DeploymentManager getDM(String JavaDoc uri, String JavaDoc username, String JavaDoc password, String JavaDoc host, String JavaDoc port) throws DeploymentManagerCreationException {
126         if (WLDebug.isEnabled()) {
127             WLDebug.notify(WLDeploymentFactory.class, "getDM, uri:" + uri+" username:" + username+" password:"+password+" host:"+host+" port:"+port);
128         }
129         DeploymentManagerCreationException dmce = null;
130         ClassLoader JavaDoc orig = Thread.currentThread().getContextClassLoader();
131         try {
132             String JavaDoc serverRoot = InstanceProperties.getInstanceProperties(uri).
133                                     getProperty(WLPluginProperties.SERVER_ROOT_ATTR);
134             // if serverRoot is null, then we are in a server instance registration process, thus this call
135
// is made from InstanceProperties creation -> WLPluginProperties singleton contains
136
// install location of the instance being registered
137
if (serverRoot == null)
138                 serverRoot = WLPluginProperties.getInstance().getInstallLocation();
139             
140             ClassLoader JavaDoc loader = getWLClassLoader(serverRoot);
141             Thread.currentThread().setContextClassLoader(loader);
142             Class JavaDoc helperClazz = loader.loadClass("weblogic.deploy.api.tools.SessionHelper"); //NOI18N
143
Method JavaDoc m = helperClazz.getDeclaredMethod("getDeploymentManager", new Class JavaDoc [] {String JavaDoc.class,String JavaDoc.class,String JavaDoc.class,String JavaDoc.class});
144             Object JavaDoc o = m.invoke(null, new Object JavaDoc [] {host, port, username, password});
145             if (DeploymentManager.class.isAssignableFrom(o.getClass())) {
146                 return (DeploymentManager) o;
147             } else {
148                 dmce = new DeploymentManagerCreationException ("Instance created by weblogic is not DeploymentManager instance.");
149             }
150         } catch (Exception JavaDoc e) {
151             dmce = new DeploymentManagerCreationException ("Cannot create weblogic DeploymentManager instance.");
152             ErrorManager.getDefault().annotate(dmce, e);
153         } finally {
154             Thread.currentThread().setContextClassLoader(orig);
155         }
156         throw dmce;
157     }
158     
159     private DeploymentManager getDiscoDM(String JavaDoc uri) throws DeploymentManagerCreationException {
160         DeploymentManagerCreationException dmce = null;
161         ClassLoader JavaDoc orig = Thread.currentThread().getContextClassLoader();
162         try {
163             String JavaDoc serverRoot = InstanceProperties.getInstanceProperties(uri).
164                                     getProperty(WLPluginProperties.SERVER_ROOT_ATTR);
165             // if serverRoot is null, then we are in a server instance registration process, thus this call
166
// is made from InstanceProperties creation -> WLPluginProperties singleton contains
167
// install location of the instance being registered
168
if (serverRoot == null)
169                 serverRoot = WLPluginProperties.getInstance().getInstallLocation();
170             
171             ClassLoader JavaDoc loader = getWLClassLoader(serverRoot);
172             Thread.currentThread().setContextClassLoader(loader);
173             Class JavaDoc helperClazz = loader.loadClass("weblogic.deploy.api.tools.SessionHelper"); //NOI18N
174
Method JavaDoc m = helperClazz.getDeclaredMethod("getDisconnectedDeploymentManager", new Class JavaDoc [] {});
175             Object JavaDoc o = m.invoke(null, new Object JavaDoc [] {});
176             if (DeploymentManager.class.isAssignableFrom(o.getClass())) {
177                 return (DeploymentManager) o;
178             } else {
179                 dmce = new DeploymentManagerCreationException ("Instance created by weblogic is not disconnected DeploymentManager instance.");
180             }
181         } catch (Exception JavaDoc e) {
182             dmce = new DeploymentManagerCreationException ("Cannot create weblogic disconnected DeploymentManager instance.");
183             ErrorManager.getDefault().annotate(dmce, e);
184         } finally {
185             Thread.currentThread().setContextClassLoader(orig);
186         }
187         throw dmce;
188     }
189     
190     public boolean handlesURI(String JavaDoc uri) {
191         if (uri != null && uri.startsWith(URI_PREFIX)) {
192             return true;
193         }
194         
195         return false;
196     }
197     
198     public DeploymentManager getDeploymentManager(String JavaDoc uri, String JavaDoc username,
199             String JavaDoc password) throws DeploymentManagerCreationException {
200         if (WLDebug.isEnabled()) {
201             WLDebug.notify(WLDeploymentFactory.class, "getDeploymentManager, uri:" + uri+" username:" + username+" password:"+password);
202         }
203         
204         String JavaDoc[] parts = uri.split(":"); // NOI18N
205
String JavaDoc host = parts[3].substring(2);
206         String JavaDoc port = parts[4];
207         WLDeploymentManager dm = new WLDeploymentManager(getDM (uri, username, password, host, port), uri, username, password, host, port);
208         updateManagerCache(dm, uri);
209         return dm;
210     }
211     
212     public DeploymentManager getDisconnectedDeploymentManager(String JavaDoc uri)
213             throws DeploymentManagerCreationException {
214         if (WLDebug.isEnabled()) {
215             WLDebug.notify(WLDeploymentFactory.class, "getDisconnectedDeploymentManager, uri:" + uri);
216         }
217         String JavaDoc[] parts = uri.split(":"); // NOI18N
218
String JavaDoc host = parts[3].substring(2);
219         String JavaDoc port = parts[4];
220         WLDeploymentManager dm = new WLDeploymentManager(getDiscoDM(uri), uri, host, port);
221         updateManagerCache(dm, uri);
222         return dm;
223     }
224     
225     private void updateManagerCache(WLDeploymentManager dm, String JavaDoc uri) {
226         InstanceProperties ip = InstanceProperties.getInstanceProperties(uri);
227         if (managerCache.get(ip) != null) {
228             dm.setServerProcess(managerCache.get(ip).getServerProcess());
229         }
230         managerCache.put(ip, dm);
231     }
232     
233     public String JavaDoc getProductVersion() {
234         return NbBundle.getMessage(WLDeploymentFactory.class,
235                 "TXT_productVersion"); // NOI18N
236
}
237     
238     public String JavaDoc getDisplayName() {
239         return NbBundle.getMessage(WLDeploymentFactory.class,
240                 "TXT_displayName"); // NOI18N
241
}
242 }
243
Popular Tags