KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collections JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.WeakHashMap JavaDoc;
23 import javax.naming.NameNotFoundException JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
26 import org.netbeans.modules.j2ee.jboss4.config.WarDeploymentConfiguration;
27 import org.netbeans.modules.j2ee.jboss4.util.JBProperties;
28 import org.netbeans.modules.j2ee.jboss4.ide.JBJ2eePlatformFactory;
29 import java.io.File JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.net.URLClassLoader JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Locale JavaDoc;
34 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
35 import javax.enterprise.deploy.shared.DConfigBeanVersionType JavaDoc;
36 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
37 import javax.enterprise.deploy.spi.DeploymentConfiguration JavaDoc;
38 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
39 import javax.enterprise.deploy.spi.Target JavaDoc;
40 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
41 import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException JavaDoc;
42 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException JavaDoc;
43 import javax.enterprise.deploy.spi.exceptions.TargetException JavaDoc;
44 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
45 import javax.management.MBeanServerConnection JavaDoc;
46 import javax.naming.Context JavaDoc;
47 import javax.naming.InitialContext JavaDoc;
48 import org.netbeans.modules.j2ee.jboss4.config.CarDeploymentConfiguration;
49 import org.netbeans.modules.j2ee.jboss4.config.EarDeploymentConfiguration;
50 import org.netbeans.modules.j2ee.jboss4.config.EjbDeploymentConfiguration;
51 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties;
52 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginUtils;
53
54 /**
55  *
56  * @author Kirill Sorokin
57  */

58 public class JBDeploymentManager implements DeploymentManager JavaDoc {
59     
60     private DeploymentManager JavaDoc dm;
61     private String JavaDoc realUri;
62     private MBeanServerConnection JavaDoc rmiServer;
63     
64     private int debuggingPort = 8787;
65     
66     private InstanceProperties instanceProperties;
67     
68     /**
69      * Stores information about running instances. instance is represented by its InstanceProperties,
70      * running state by Boolean.TRUE, stopped state Boolean.FALSE.
71      * WeakHashMap should guarantee erasing of an unregistered server instance bcs instance properties are also removed along with instance.
72      */

73     private static Map JavaDoc/*<InstanceProperties, Boolean>*/ propertiesToIsRunning = Collections.synchronizedMap(new WeakHashMap JavaDoc());
74     
75     /** Creates a new instance of JBDeploymentManager */
76     public JBDeploymentManager(DeploymentManager JavaDoc dm, String JavaDoc uri, String JavaDoc username, String JavaDoc password) {
77         realUri = uri;
78         this.dm = dm;
79         rmiServer = null;
80     }
81     
82     ////////////////////////////////////////////////////////////////////////////
83
// Connection data methods
84
////////////////////////////////////////////////////////////////////////////
85
public String JavaDoc getHost() {
86         String JavaDoc host = InstanceProperties.getInstanceProperties(realUri).
87                 getProperty(JBPluginProperties.PROPERTY_HOST);
88         return host;
89     }
90     
91     public int getPort() {
92         String JavaDoc port = InstanceProperties.getInstanceProperties(realUri).
93                 getProperty(JBPluginProperties.PROPERTY_PORT);
94         return new Integer JavaDoc(port).intValue();
95     }
96     
97     public int getDebuggingPort() {
98         return debuggingPort;
99     }
100     
101     public String JavaDoc getUrl() {
102         return realUri;
103     }
104     
105     
106     public InstanceProperties getInstanceProperties() {
107         if (instanceProperties == null)
108             instanceProperties = InstanceProperties.getInstanceProperties(realUri);
109         
110         return instanceProperties;
111     }
112     
113     public synchronized MBeanServerConnection JavaDoc getRMIServer() {
114         if(rmiServer == null) {
115             ClassLoader JavaDoc oldLoader = null;
116             
117             try {
118                 oldLoader = Thread.currentThread().getContextClassLoader();
119                 InstanceProperties ip = this.getInstanceProperties();
120                 URLClassLoader JavaDoc loader = JBDeploymentFactory.getJBClassLoader(ip.getProperty(JBPluginProperties.PROPERTY_ROOT_DIR),
121                         ip.getProperty(JBPluginProperties.PROPERTY_SERVER_DIR));
122                 Thread.currentThread().setContextClassLoader(loader);
123                 
124                 JBProperties props = getProperties();
125                 Hashtable JavaDoc env = new Hashtable JavaDoc();
126                 
127                 // Sets the jboss naming environment
128
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
129                 env.put(Context.PROVIDER_URL, "jnp://localhost:"+JBPluginUtils.getJnpPort(ip.getProperty(JBPluginProperties.PROPERTY_SERVER_DIR)));
130                 env.put(Context.OBJECT_FACTORIES, "org.jboss.naming");
131                 env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" );
132                 env.put("jnp.disableDiscovery", Boolean.TRUE);
133                 
134                 final String JavaDoc JAVA_SEC_AUTH_LOGIN_CONF = "java.security.auth.login.config"; // NOI18N
135
String JavaDoc oldAuthConf = System.getProperty(JAVA_SEC_AUTH_LOGIN_CONF);
136                 
137                 File JavaDoc securityConf = new File JavaDoc(props.getRootDir(), "/client/auth.conf");
138                 if (securityConf.exists()) {
139                     env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.LoginInitialContextFactory");
140                     env.put(Context.SECURITY_PRINCIPAL, props.getUsername());
141                     env.put(Context.SECURITY_CREDENTIALS, props.getPassword());
142                     System.setProperty(JAVA_SEC_AUTH_LOGIN_CONF, securityConf.getAbsolutePath()); // NOI18N
143
}
144                 
145                 // Gets naming context
146
InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
147
148                 //restore java.security.auth.login.config system property
149
if (oldAuthConf != null) {
150                     System.setProperty(JAVA_SEC_AUTH_LOGIN_CONF, oldAuthConf);
151                 } else {
152                     System.clearProperty(JAVA_SEC_AUTH_LOGIN_CONF);
153                 }
154                 
155                 // Lookup RMI Adaptor
156
rmiServer = (MBeanServerConnection JavaDoc)ctx.lookup("/jmx/invoker/RMIAdaptor");
157             } catch (NameNotFoundException JavaDoc ex) {
158             } catch (NamingException JavaDoc ex) {
159                 // Nothing to do
160
} finally {
161                 if (oldLoader != null)
162                     Thread.currentThread().setContextClassLoader(oldLoader);
163             }
164         }
165         
166         return rmiServer;
167     }
168     
169     public synchronized MBeanServerConnection JavaDoc refreshRMIServer() {
170         rmiServer = null;
171         return getRMIServer();
172     }
173     
174     ////////////////////////////////////////////////////////////////////////////
175
// Methods for retrieving server instance state
176
////////////////////////////////////////////////////////////////////////////
177
/**
178      * Returns true if the given instance properties are present in the map and value equals true.
179      * Otherwise return false.
180      */

181     public static boolean isRunningLastCheck(InstanceProperties ip) {
182         boolean isRunning = propertiesToIsRunning.containsKey(ip) && propertiesToIsRunning.get(ip).equals(Boolean.TRUE);
183         return isRunning;
184     }
185     
186     /**
187      * Stores state of an instance represented by InstanceProperties.
188      */

189     public static void setRunningLastCheck(InstanceProperties ip, Boolean JavaDoc isRunning) {
190         assert(ip != null);
191         propertiesToIsRunning.put(ip, isRunning);
192     }
193     
194     ////////////////////////////////////////////////////////////////////////////
195
// DeploymentManager Implementation
196
////////////////////////////////////////////////////////////////////////////
197
public ProgressObject JavaDoc distribute(Target JavaDoc[] target, File JavaDoc file, File JavaDoc file2) throws IllegalStateException JavaDoc {
198         return new JBDeployer(realUri, this).deploy(target, file, file2, getHost(), getPort());
199     }
200     
201     public DeploymentConfiguration createConfiguration(DeployableObject JavaDoc deployableObject) throws InvalidModuleException JavaDoc {
202         ModuleType JavaDoc type = deployableObject.getType();
203         if (type == ModuleType.WAR) {
204             return new WarDeploymentConfiguration(deployableObject);
205         } else if (type == ModuleType.EAR) {
206             return new EarDeploymentConfiguration(deployableObject);
207         } else if (type == ModuleType.EJB) {
208             return new EjbDeploymentConfiguration(deployableObject);
209         } else if (type == ModuleType.CAR) {
210             return new CarDeploymentConfiguration(deployableObject);
211         } else {
212             throw new InvalidModuleException JavaDoc("Unsupported module type: " + type.toString()); // NOI18N
213
}
214     }
215     
216     public ProgressObject JavaDoc redeploy(TargetModuleID JavaDoc[] targetModuleID, InputStream JavaDoc inputStream, InputStream JavaDoc inputStream2) throws UnsupportedOperationException JavaDoc, IllegalStateException JavaDoc {
217         return dm.redeploy(targetModuleID, inputStream, inputStream2);
218     }
219     
220     public ProgressObject JavaDoc distribute(Target JavaDoc[] target, InputStream JavaDoc inputStream, InputStream JavaDoc inputStream2) throws IllegalStateException JavaDoc {
221         return dm.distribute(target, inputStream, inputStream2);
222     }
223     
224     public ProgressObject JavaDoc distribute(Target JavaDoc[] target, ModuleType JavaDoc moduleType, InputStream JavaDoc inputStream, InputStream JavaDoc inputStream0) throws IllegalStateException JavaDoc {
225         return distribute(target, inputStream, inputStream0);
226     }
227     
228     public ProgressObject JavaDoc undeploy(TargetModuleID JavaDoc[] targetModuleID) throws IllegalStateException JavaDoc {
229         return dm.undeploy(targetModuleID);
230     }
231     
232     public ProgressObject JavaDoc stop(TargetModuleID JavaDoc[] targetModuleID) throws IllegalStateException JavaDoc {
233         return dm.stop(targetModuleID);
234     }
235     
236     public ProgressObject JavaDoc start(TargetModuleID JavaDoc[] targetModuleID) throws IllegalStateException JavaDoc {
237         return dm.start(targetModuleID);
238     }
239     
240     public void setLocale(Locale JavaDoc locale) throws UnsupportedOperationException JavaDoc {
241         dm.setLocale(locale);
242     }
243     
244     public boolean isLocaleSupported(Locale JavaDoc locale) {
245         return dm.isLocaleSupported(locale);
246     }
247     
248     public TargetModuleID JavaDoc[] getAvailableModules(ModuleType JavaDoc moduleType, Target JavaDoc[] target) throws TargetException JavaDoc, IllegalStateException JavaDoc {
249         //return dm.getAvailableModules(moduleType, target);
250
return new TargetModuleID JavaDoc[]{};
251     }
252     
253     public TargetModuleID JavaDoc[] getNonRunningModules(ModuleType JavaDoc moduleType, Target JavaDoc[] target) throws TargetException JavaDoc, IllegalStateException JavaDoc {
254         //return dm.getNonRunningModules(moduleType, target);
255
return new TargetModuleID JavaDoc[]{};
256     }
257     
258     public TargetModuleID JavaDoc[] getRunningModules(ModuleType JavaDoc moduleType, Target JavaDoc[] target) throws TargetException JavaDoc, IllegalStateException JavaDoc {
259         return dm.getRunningModules(moduleType, target);
260     }
261     
262     public ProgressObject JavaDoc redeploy(TargetModuleID JavaDoc[] targetModuleID, File JavaDoc file, File JavaDoc file2) throws UnsupportedOperationException JavaDoc, IllegalStateException JavaDoc {
263         return new JBDeployer(realUri, this).redeploy(targetModuleID, file, file2);
264     }
265     
266     public void setDConfigBeanVersion(DConfigBeanVersionType JavaDoc dConfigBeanVersionType) throws DConfigBeanVersionUnsupportedException JavaDoc {
267         dm.setDConfigBeanVersion(dConfigBeanVersionType);
268     }
269     
270     public boolean isDConfigBeanVersionSupported(DConfigBeanVersionType JavaDoc dConfigBeanVersionType) {
271         return dm.isDConfigBeanVersionSupported(dConfigBeanVersionType);
272     }
273     
274     public void release() {
275         if (dm != null) {
276             dm.release();
277         }
278     }
279     
280     public boolean isRedeploySupported() {
281         return dm.isRedeploySupported();
282     }
283     
284     public Locale JavaDoc getCurrentLocale() {
285         return dm.getCurrentLocale();
286     }
287     
288     public DConfigBeanVersionType JavaDoc getDConfigBeanVersion() {
289         return dm.getDConfigBeanVersion();
290     }
291     
292     public Locale JavaDoc getDefaultLocale() {
293         return dm.getDefaultLocale();
294     }
295     
296     public Locale JavaDoc[] getSupportedLocales() {
297         return dm.getSupportedLocales();
298     }
299     
300     public Target JavaDoc[] getTargets() throws IllegalStateException JavaDoc {
301         return dm.getTargets();
302     }
303     
304     private JBJ2eePlatformFactory.J2eePlatformImplImpl jbPlatform;
305     
306     public JBJ2eePlatformFactory.J2eePlatformImplImpl getJBPlatform() {
307         if (jbPlatform == null) {
308             jbPlatform = (JBJ2eePlatformFactory.J2eePlatformImplImpl) new JBJ2eePlatformFactory().getJ2eePlatformImpl(this);
309         }
310         return jbPlatform;
311     }
312     
313     public JBProperties getProperties() {
314         return new JBProperties(this);
315     }
316     
317 }
318
Popular Tags