KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > Server


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
20
21 package org.netbeans.modules.j2ee.deployment.impl;
22
23 import java.io.OutputStream JavaDoc;
24 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager JavaDoc;
25 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
26 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException JavaDoc;
27 import javax.enterprise.deploy.spi.*;
28 import org.netbeans.modules.j2ee.deployment.common.api.ValidationException;
29 import org.netbeans.modules.j2ee.deployment.impl.gen.nbd.*;
30 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
31 import org.netbeans.modules.j2ee.deployment.impl.ui.RegistryNodeProvider;
32 import org.openide.filesystems.*;
33 import org.openide.loaders.*;
34 import org.openide.util.Lookup;
35 import org.openide.cookies.InstanceCookie;
36 import org.openide.nodes.Node;
37 import org.openide.ErrorManager;
38 import org.openide.util.NbBundle;
39 import java.util.*;
40 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
41 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
42
43
44 public class Server implements Node.Cookie {
45     
46     static public final String JavaDoc ATTR_needsFindServerUI = "needsFindServerUI";
47     
48     final NetbeansDeployment dep;
49     final Class JavaDoc factoryCls;
50     DeploymentFactory JavaDoc factory = null;
51     DeploymentManager manager = null;
52     RegistryNodeProvider nodeProvider = null;
53     final String JavaDoc name;
54     Map configMap;
55     Map customMap;
56     Lookup lkp;
57     boolean needsFindServerUI = false;
58     
59     public Server(FileObject fo) throws Exception JavaDoc {
60         //long t0 = System.currentTimeMillis();
61
initDeploymentConfigurationFileList(fo);
62         name = fo.getName();
63         FileObject descriptor = fo.getFileObject("Descriptor");
64         if(descriptor == null) {
65             String JavaDoc msg = NbBundle.getMessage(Server.class, "MSG_InvalidServerPlugin", name);
66             throw new IllegalStateException JavaDoc(msg);
67         }
68         needsFindServerUI = getBooleanValue(descriptor.getAttribute(ATTR_needsFindServerUI), false);
69         
70         dep = NetbeansDeployment.createGraph(descriptor.getInputStream());
71         
72         lkp = new FolderLookup (DataFolder.findContainer (fo)).getLookup ();
73         factory = (DeploymentFactory JavaDoc) lkp.lookup (DeploymentFactory JavaDoc.class);
74         if (factory != null) {
75             factoryCls = factory.getClass ();
76         } else {
77             FileObject factoryinstance = fo.getFileObject("Factory.instance");
78             if(factoryinstance == null) {
79                 String JavaDoc msg = NbBundle.getMessage(Server.class, "MSG_NoFactoryInstanceClass", name);
80                 ErrorManager.getDefault().log(ErrorManager.ERROR, msg);
81                 factoryCls = null;
82                 return;
83             }
84             DataObject dobj = DataObject.find(factoryinstance);
85             InstanceCookie cookie = (InstanceCookie) dobj.getCookie(InstanceCookie.class);
86             if(cookie == null) {
87                 String JavaDoc msg = NbBundle.getMessage(Server.class, "MSG_FactoryFailed", name, cookie);
88                 ErrorManager.getDefault().log(ErrorManager.ERROR, msg);
89                 factoryCls = null;
90                 return;
91             }
92             factoryCls = cookie.instanceClass();
93
94             // speculative code depending on the DF implementation and if it registers
95
// itself with DFM or not
96

97             try {
98                 factory = (DeploymentFactory JavaDoc) cookie.instanceCreate();
99             } catch (Exception JavaDoc e) {
100                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
101             }
102         }
103         //System.out.println("Create plugin "+name+" in "+(System.currentTimeMillis() - t0));
104
}
105     
106     
107     private DeploymentFactory JavaDoc getFactory() {
108         if (factory == null) {
109             DeploymentFactoryManager JavaDoc dfm = DeploymentFactoryManager.getInstance();
110 // System.err.println(dfm);
111
// System.err.println(dfm.getDeploymentFactories().length);
112
// System.err.println(factoryCls);
113
// System.err.println(factoryCls.getName());
114
try {
115 // System.err.println(Class.forName(factoryCls.getName()));
116
Thread.sleep(5000);
117             } catch (Exception JavaDoc e) {}
118 // System.err.println(dfm.getDeploymentFactories().length);
119
// System.err.println(DeploymentFactoryManager.getInstance());
120
DeploymentFactory JavaDoc[] factories = DeploymentFactoryManager.getInstance().getDeploymentFactories();
121             for(int i = 0; i < factories.length; i++) {
122 // System.err.println("Checking factory " + factories[i]);
123
if(factoryCls.isInstance(factories[i])) {
124                     factory = factories[i];
125                     break;
126                 }
127             }
128         }
129         if(factory == null) {
130             // ServerRegistry.getInstance().removePlugin(sfo);
131
throw new IllegalStateException JavaDoc();
132         }
133         return factory;
134     }
135     
136     public DeploymentManager getDisconnectedDeploymentManager() throws DeploymentManagerCreationException JavaDoc {
137         if(manager == null) {
138             manager = getDisconnectedDeploymentManager(dep.getDisconnectedString());
139         }
140         return manager;
141     }
142     
143     public DeploymentManager getDisconnectedDeploymentManager(String JavaDoc uri) throws DeploymentManagerCreationException JavaDoc {
144         return getFactory().getDisconnectedDeploymentManager(uri);
145     }
146     
147     public boolean handlesUri(String JavaDoc uri) {
148         try {
149             return getFactory().handlesURI(uri);
150         } catch (Exception JavaDoc e) {
151             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
152             return false;
153         }
154     }
155     
156     public DeploymentManager getDeploymentManager(String JavaDoc uri, String JavaDoc username, String JavaDoc password) throws DeploymentManagerCreationException JavaDoc {
157         return getFactory().getDeploymentManager(uri,username, password);
158     }
159     
160     public String JavaDoc getDisplayName() {
161         return getFactory().getDisplayName();
162     }
163     
164     public String JavaDoc getShortName() {
165         return name;
166     }
167     
168     public String JavaDoc getIconBase() {
169         return dep.getIcon();
170     }
171     
172     public boolean canDeployEars() {
173         return dep.getContainerLimitation() == null || dep.getContainerLimitation().isEarDeploy();
174     }
175     
176     public boolean canDeployWars() {
177         return dep.getContainerLimitation() == null || dep.getContainerLimitation().isWarDeploy();
178     }
179     
180     public boolean canDeployEjbJars() {
181         return dep.getContainerLimitation() == null || dep.getContainerLimitation().isEjbjarDeploy();
182     }
183     
184     public ConfigBeanDescriptor getConfigBeanDescriptor(String JavaDoc className) {
185         if(configMap == null) {
186             ConfigBean[] beans = dep.getConfigBean();
187             configMap = new HashMap();
188             for(int i = 0; i < beans.length; i++)
189                 configMap.put(beans[i].getClassName(),new ConfigBeanDescriptor(beans[i]));
190         }
191         return (ConfigBeanDescriptor) configMap.get(className);
192     }
193     
194     // PENDING should be cached?
195
public String JavaDoc getHelpId(String JavaDoc beanClass) {
196         ConfigBean[] beans = dep.getConfigBean();
197         for(int i = 0; i < beans.length; i++) {
198             if(beans[i].getClassName().equals(beanClass))
199                 return beans[i].getHelpid();
200         }
201         return null;
202     }
203     
204     public RegistryNodeProvider getNodeProvider() {
205         if (nodeProvider != null)
206             return nodeProvider;
207         
208         RegistryNodeFactory nodeFact = (RegistryNodeFactory) lkp.lookup(RegistryNodeFactory.class);
209         if (nodeFact == null) {
210             String JavaDoc msg = NbBundle.getMessage(Server.class, "MSG_NoInstance", name, RegistryNodeFactory.class);
211             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, msg);
212         }
213         nodeProvider = new RegistryNodeProvider(nodeFact); //null is acceptable
214
return nodeProvider;
215     }
216     
217     public RegistryNodeFactory getRegistryNodeFactory() {
218         return (RegistryNodeFactory) lkp.lookup(RegistryNodeFactory.class);
219     }
220     
221     /** returns OptionalDeploymentManagerFactory or null it is not provided by the plugin */
222     public OptionalDeploymentManagerFactory getOptionalFactory () {
223         OptionalDeploymentManagerFactory o = (OptionalDeploymentManagerFactory) lkp.lookup (OptionalDeploymentManagerFactory.class);
224         return o;
225     }
226     
227     /** returns J2eePlatformFactory or null if it is not provided by the plugin */
228     public J2eePlatformFactory getJ2eePlatformFactory () {
229         J2eePlatformFactory o = (J2eePlatformFactory) lkp.lookup (J2eePlatformFactory.class);
230         return o;
231     }
232     
233 // /** returns DConfigBeanUIFactory or null it is not provided by the plugin */
234
// public DConfigBeanUIFactory getDConfigBeanUIFactory () {
235
// DConfigBeanUIFactory o = (DConfigBeanUIFactory) lkp.lookup (DConfigBeanUIFactory.class);
236
// return o;
237
// }
238

239 // public DConfigBeanProperties getDConfigBeanProperties(DConfigBean bean) {
240
// DConfigBeanUIFactory beanUIFactory = getDConfigBeanUIFactory();
241
// if (beanUIFactory == null) return null;
242
// return beanUIFactory.getUICustomization(bean);
243
// }
244

245     public ConfigurationSupport getConfigurationSupport() {
246         ConfigurationSupport cs = (ConfigurationSupport) lkp.lookup (ConfigurationSupport.class);
247         return cs;
248     }
249
250     public VerifierSupport getVerifierSupport() {
251         VerifierSupport vs = (VerifierSupport) lkp.lookup (VerifierSupport.class);
252         return vs;
253     }
254     
255     public boolean canVerify(Object JavaDoc moduleType) {
256         VerifierSupport vs = getVerifierSupport();
257         return vs != null && vs.supportsModuleType(moduleType);
258     }
259     
260     public void verify(FileObject target, OutputStream JavaDoc logger) throws ValidationException {
261         getVerifierSupport().verify(target, logger);
262     }
263     
264     public ServerInstance[] getInstances() {
265         Collection ret = new ArrayList();
266         for (Iterator i=ServerRegistry.getInstance().getInstances().iterator(); i.hasNext();) {
267             ServerInstance inst = (ServerInstance) i.next();
268             if (name.equals(inst.getServer().getShortName()))
269                 ret.add(inst);
270         }
271         return (ServerInstance[]) ret.toArray(new ServerInstance[ret.size()]);
272     }
273     
274     public WebContextRoot getWebContextRoot() {
275         return dep.getWebContextRoot();
276     }
277     
278     public DeploymentFactory JavaDoc getDeploymentFactory() {
279         return factory;
280     }
281
282     static public boolean getBooleanValue(Object JavaDoc v, boolean dvalue) {
283         if (v instanceof Boolean JavaDoc)
284             return ((Boolean JavaDoc)v).booleanValue();
285         if (v instanceof String JavaDoc)
286             return Boolean.valueOf((String JavaDoc) v).booleanValue();
287         return dvalue;
288     }
289     
290     public boolean needsFindServerUI() {
291         return needsFindServerUI;
292     }
293     
294     public String JavaDoc toString () {
295         return getShortName ();
296     }
297     
298     public boolean supportsModuleType(ModuleType JavaDoc type) {
299         if (J2eeModule.WAR.equals(type)) {
300             return this.canDeployWars();
301         } else if (J2eeModule.EJB.equals(type)) {
302             return this.canDeployEjbJars();
303         } else if (J2eeModule.EAR.equals(type)) {
304             return this.canDeployEars();
305         } else {
306             // PENDING, precise answer for other module types, for now assume true
307
return true;
308         }
309     }
310
311     public static final String JavaDoc LAYER_DEPLOYMENT_FILE_NAMES = "DeploymentFileNames"; //NOI18N
312
private Map deployConfigDescriptorMap;
313     private void initDeploymentConfigurationFileList(FileObject fo) {
314         deployConfigDescriptorMap = new HashMap();
315         FileObject deplFNames = fo.getFileObject(LAYER_DEPLOYMENT_FILE_NAMES);
316         if (deplFNames != null) {
317             FileObject mTypes [] = deplFNames.getChildren();
318             for (int j=0; j < mTypes.length; j++) {
319                 String JavaDoc mTypeName = mTypes [j].getName().toUpperCase();
320                 FileObject allNames [] = mTypes [j].getChildren();
321                 if (allNames == null || allNames.length == 0)
322                     continue;
323                 ArrayList filepaths = new ArrayList();
324                 for (int i = 0; i < allNames.length; i++) {
325                     if (allNames[i] == null)
326                         continue;
327                     String JavaDoc fname = allNames [i].getNameExt();
328                     filepaths.add(fname.replace('\\', '/')); //just in case..
329
}
330                 deployConfigDescriptorMap.put(mTypeName, filepaths.toArray(new String JavaDoc[filepaths.size()]));
331             }
332         }
333     }
334     
335     public String JavaDoc[] getDeploymentPlanFiles(Object JavaDoc type) {
336         return (String JavaDoc[]) deployConfigDescriptorMap.get(type.toString().toUpperCase());
337     }
338 }
339
Popular Tags