001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.j2ee.jboss4;
042:
043: import java.util.Collections;
044: import java.util.Map;
045: import java.util.WeakHashMap;
046: import javax.naming.NameNotFoundException;
047: import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
048: import org.netbeans.modules.j2ee.jboss4.util.JBProperties;
049: import org.netbeans.modules.j2ee.jboss4.ide.JBJ2eePlatformFactory;
050: import java.io.File;
051: import java.io.IOException;
052: import java.io.InputStream;
053: import java.net.URLClassLoader;
054: import java.util.Hashtable;
055: import java.util.Locale;
056: import java.util.logging.Level;
057: import java.util.logging.Logger;
058: import javax.enterprise.deploy.model.DeployableObject;
059: import javax.enterprise.deploy.shared.DConfigBeanVersionType;
060: import javax.enterprise.deploy.shared.ModuleType;
061: import javax.enterprise.deploy.spi.DeploymentConfiguration;
062: import javax.enterprise.deploy.spi.DeploymentManager;
063: import javax.enterprise.deploy.spi.Target;
064: import javax.enterprise.deploy.spi.TargetModuleID;
065: import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException;
066: import javax.enterprise.deploy.spi.exceptions.InvalidModuleException;
067: import javax.enterprise.deploy.spi.exceptions.TargetException;
068: import javax.enterprise.deploy.spi.status.ProgressObject;
069: import javax.management.InstanceNotFoundException;
070: import javax.management.MBeanException;
071: import javax.management.MBeanServerConnection;
072: import javax.management.ObjectName;
073: import javax.management.ReflectionException;
074: import javax.naming.Context;
075: import javax.naming.InitialContext;
076: import javax.naming.NamingException;
077: import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties;
078: import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginUtils;
079:
080: /**
081: *
082: * @author Kirill Sorokin
083: */
084: public class JBDeploymentManager implements DeploymentManager {
085:
086: private static final Logger LOGGER = Logger
087: .getLogger(JBDeploymentManager.class.getName());
088:
089: private DeploymentManager dm;
090: private String realUri;
091: private MBeanServerConnection rmiServer;
092:
093: private int debuggingPort = 8787;
094:
095: private InstanceProperties instanceProperties;
096: private boolean needsRestart;
097:
098: /**
099: * Stores information about running instances. instance is represented by its InstanceProperties,
100: * running state by Boolean.TRUE, stopped state Boolean.FALSE.
101: * WeakHashMap should guarantee erasing of an unregistered server instance bcs instance properties are also removed along with instance.
102: */
103: private static Map/*<InstanceProperties, Boolean>*/propertiesToIsRunning = Collections
104: .synchronizedMap(new WeakHashMap());
105:
106: /** Creates a new instance of JBDeploymentManager */
107: public JBDeploymentManager(DeploymentManager dm, String uri,
108: String username, String password) {
109: realUri = uri;
110: this .dm = dm;
111: rmiServer = null;
112: }
113:
114: ////////////////////////////////////////////////////////////////////////////
115: // Connection data methods
116: ////////////////////////////////////////////////////////////////////////////
117: public String getHost() {
118: String host = InstanceProperties.getInstanceProperties(realUri)
119: .getProperty(JBPluginProperties.PROPERTY_HOST);
120: return host;
121: }
122:
123: public int getPort() {
124: String port = InstanceProperties.getInstanceProperties(realUri)
125: .getProperty(JBPluginProperties.PROPERTY_PORT);
126: return new Integer(port).intValue();
127: }
128:
129: public int getDebuggingPort() {
130: return debuggingPort;
131: }
132:
133: public String getUrl() {
134: return realUri;
135: }
136:
137: public InstanceProperties getInstanceProperties() {
138: if (instanceProperties == null)
139: instanceProperties = InstanceProperties
140: .getInstanceProperties(realUri);
141:
142: return instanceProperties;
143: }
144:
145: public synchronized MBeanServerConnection getRMIServer() {
146: if (rmiServer == null) {
147: ClassLoader oldLoader = null;
148:
149: try {
150: oldLoader = Thread.currentThread()
151: .getContextClassLoader();
152: InstanceProperties ip = this .getInstanceProperties();
153: URLClassLoader loader = JBDeploymentFactory
154: .getJBClassLoader(
155: ip
156: .getProperty(JBPluginProperties.PROPERTY_ROOT_DIR),
157: ip
158: .getProperty(JBPluginProperties.PROPERTY_SERVER_DIR));
159: Thread.currentThread().setContextClassLoader(loader);
160:
161: JBProperties props = getProperties();
162: Hashtable env = new Hashtable();
163:
164: // Sets the jboss naming environment
165: env.put(Context.INITIAL_CONTEXT_FACTORY,
166: "org.jboss.naming.NamingContextFactory");
167: env
168: .put(
169: Context.PROVIDER_URL,
170: "jnp://localhost:"
171: + JBPluginUtils
172: .getJnpPort(ip
173: .getProperty(JBPluginProperties.PROPERTY_SERVER_DIR)));
174: env.put(Context.OBJECT_FACTORIES, "org.jboss.naming");
175: env.put(Context.URL_PKG_PREFIXES,
176: "org.jboss.naming:org.jnp.interfaces");
177: env.put("jnp.disableDiscovery", Boolean.TRUE);
178:
179: final String JAVA_SEC_AUTH_LOGIN_CONF = "java.security.auth.login.config"; // NOI18N
180: String oldAuthConf = System
181: .getProperty(JAVA_SEC_AUTH_LOGIN_CONF);
182:
183: File securityConf = new File(props.getRootDir(),
184: "/client/auth.conf");
185: if (securityConf.exists()) {
186: env
187: .put(Context.INITIAL_CONTEXT_FACTORY,
188: "org.jboss.security.jndi.LoginInitialContextFactory");
189: env.put(Context.SECURITY_PRINCIPAL, props
190: .getUsername());
191: env.put(Context.SECURITY_CREDENTIALS, props
192: .getPassword());
193: System.setProperty(JAVA_SEC_AUTH_LOGIN_CONF,
194: securityConf.getAbsolutePath()); // NOI18N
195: }
196:
197: // Gets naming context
198: InitialContext ctx = new InitialContext(env);
199:
200: //restore java.security.auth.login.config system property
201: if (oldAuthConf != null) {
202: System.setProperty(JAVA_SEC_AUTH_LOGIN_CONF,
203: oldAuthConf);
204: } else {
205: System.clearProperty(JAVA_SEC_AUTH_LOGIN_CONF);
206: }
207:
208: // Lookup RMI Adaptor
209: rmiServer = (MBeanServerConnection) ctx
210: .lookup("/jmx/invoker/RMIAdaptor");
211: } catch (NameNotFoundException ex) {
212: LOGGER.log(Level.FINE, null, ex);
213: } catch (NamingException ex) {
214: LOGGER.log(Level.FINE, null, ex);
215: } finally {
216: if (oldLoader != null)
217: Thread.currentThread().setContextClassLoader(
218: oldLoader);
219: }
220: }
221:
222: return rmiServer;
223: }
224:
225: public Object invokeMBeanOperation(ObjectName name, String method,
226: Object[] params, String[] signature)
227: throws InstanceNotFoundException, MBeanException,
228: ReflectionException, IOException {
229:
230: MBeanServerConnection conn = null;
231: synchronized (this ) {
232: conn = getRMIServer();
233: }
234:
235: ClassLoader orig = Thread.currentThread()
236: .getContextClassLoader();
237: try {
238: Thread.currentThread().setContextClassLoader(
239: conn.getClass().getClassLoader());
240: return conn.invoke(name, method, params, signature);
241: } finally {
242: Thread.currentThread().setContextClassLoader(orig);
243: }
244:
245: }
246:
247: public synchronized MBeanServerConnection refreshRMIServer() {
248: rmiServer = null;
249: return getRMIServer();
250: }
251:
252: ////////////////////////////////////////////////////////////////////////////
253: // Methods for retrieving server instance state
254: ////////////////////////////////////////////////////////////////////////////
255: /**
256: * Returns true if the given instance properties are present in the map and value equals true.
257: * Otherwise return false.
258: */
259: public static boolean isRunningLastCheck(InstanceProperties ip) {
260: boolean isRunning = propertiesToIsRunning.containsKey(ip)
261: && propertiesToIsRunning.get(ip).equals(Boolean.TRUE);
262: return isRunning;
263: }
264:
265: /**
266: * Stores state of an instance represented by InstanceProperties.
267: */
268: public static void setRunningLastCheck(InstanceProperties ip,
269: Boolean isRunning) {
270: assert (ip != null);
271: propertiesToIsRunning.put(ip, isRunning);
272: }
273:
274: ////////////////////////////////////////////////////////////////////////////
275: // DeploymentManager Implementation
276: ////////////////////////////////////////////////////////////////////////////
277: public ProgressObject distribute(Target[] target, File file,
278: File file2) throws IllegalStateException {
279: return new JBDeployer(realUri, this ).deploy(target, file,
280: file2, getHost(), getPort());
281: }
282:
283: public DeploymentConfiguration createConfiguration(
284: DeployableObject deployableObject)
285: throws InvalidModuleException {
286: throw new RuntimeException(
287: "This method should never be called."); // NOI18N
288: }
289:
290: public ProgressObject redeploy(TargetModuleID[] targetModuleID,
291: InputStream inputStream, InputStream inputStream2)
292: throws UnsupportedOperationException, IllegalStateException {
293: return dm.redeploy(targetModuleID, inputStream, inputStream2);
294: }
295:
296: public ProgressObject distribute(Target[] target,
297: InputStream inputStream, InputStream inputStream2)
298: throws IllegalStateException {
299: return dm.distribute(target, inputStream, inputStream2);
300: }
301:
302: public ProgressObject distribute(Target[] target,
303: ModuleType moduleType, InputStream inputStream,
304: InputStream inputStream0) throws IllegalStateException {
305: return distribute(target, inputStream, inputStream0);
306: }
307:
308: public ProgressObject undeploy(TargetModuleID[] targetModuleID)
309: throws IllegalStateException {
310: return dm.undeploy(targetModuleID);
311: }
312:
313: public ProgressObject stop(TargetModuleID[] targetModuleID)
314: throws IllegalStateException {
315: return dm.stop(targetModuleID);
316: }
317:
318: public ProgressObject start(TargetModuleID[] targetModuleID)
319: throws IllegalStateException {
320: return dm.start(targetModuleID);
321: }
322:
323: public void setLocale(Locale locale)
324: throws UnsupportedOperationException {
325: dm.setLocale(locale);
326: }
327:
328: public boolean isLocaleSupported(Locale locale) {
329: return dm.isLocaleSupported(locale);
330: }
331:
332: public TargetModuleID[] getAvailableModules(ModuleType moduleType,
333: Target[] target) throws TargetException,
334: IllegalStateException {
335: //return dm.getAvailableModules(moduleType, target);
336: return new TargetModuleID[] {};
337: }
338:
339: public TargetModuleID[] getNonRunningModules(ModuleType moduleType,
340: Target[] target) throws TargetException,
341: IllegalStateException {
342: //return dm.getNonRunningModules(moduleType, target);
343: return new TargetModuleID[] {};
344: }
345:
346: public TargetModuleID[] getRunningModules(ModuleType moduleType,
347: Target[] target) throws TargetException,
348: IllegalStateException {
349: return dm.getRunningModules(moduleType, target);
350: }
351:
352: public ProgressObject redeploy(TargetModuleID[] targetModuleID,
353: File file, File file2)
354: throws UnsupportedOperationException, IllegalStateException {
355: return new JBDeployer(realUri, this ).redeploy(targetModuleID,
356: file, file2);
357: }
358:
359: public void setDConfigBeanVersion(
360: DConfigBeanVersionType dConfigBeanVersionType)
361: throws DConfigBeanVersionUnsupportedException {
362: dm.setDConfigBeanVersion(dConfigBeanVersionType);
363: }
364:
365: public boolean isDConfigBeanVersionSupported(
366: DConfigBeanVersionType dConfigBeanVersionType) {
367: return dm.isDConfigBeanVersionSupported(dConfigBeanVersionType);
368: }
369:
370: public void release() {
371: if (dm != null) {
372: dm.release();
373: }
374: }
375:
376: public boolean isRedeploySupported() {
377: return dm.isRedeploySupported();
378: }
379:
380: public Locale getCurrentLocale() {
381: return dm.getCurrentLocale();
382: }
383:
384: public DConfigBeanVersionType getDConfigBeanVersion() {
385: return dm.getDConfigBeanVersion();
386: }
387:
388: public Locale getDefaultLocale() {
389: return dm.getDefaultLocale();
390: }
391:
392: public Locale[] getSupportedLocales() {
393: return dm.getSupportedLocales();
394: }
395:
396: public Target[] getTargets() throws IllegalStateException {
397: return dm.getTargets();
398: }
399:
400: private JBJ2eePlatformFactory.J2eePlatformImplImpl jbPlatform;
401:
402: public JBJ2eePlatformFactory.J2eePlatformImplImpl getJBPlatform() {
403: if (jbPlatform == null) {
404: jbPlatform = (JBJ2eePlatformFactory.J2eePlatformImplImpl) new JBJ2eePlatformFactory()
405: .getJ2eePlatformImpl(this );
406: }
407: return jbPlatform;
408: }
409:
410: public JBProperties getProperties() {
411: return new JBProperties(this );
412: }
413:
414: /**
415: * Mark the server with a needs restart flag. This may be needed
416: * for instance when JDBC driver is deployed to a running server.
417: */
418: public synchronized void setNeedsRestart(boolean needsRestart) {
419: this .needsRestart = needsRestart;
420: }
421:
422: /**
423: * Returns true if the server needs to be restarted. This may occur
424: * for instance when JDBC driver was deployed to a running server
425: */
426: public synchronized boolean getNeedsRestart() {
427: return needsRestart;
428: }
429: }
|