KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > bridge > apis > AppserverMgmtControllerBase


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 package org.netbeans.modules.j2ee.sun.bridge.apis;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import javax.management.remote.JMXConnector JavaDoc;
31 import javax.management.Notification JavaDoc;
32 import javax.management.MBeanServerConnection JavaDoc;
33
34 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
35 import javax.enterprise.deploy.spi.Target JavaDoc;
36
37 import com.sun.appserv.management.config.StandaloneServerConfig;
38 import com.sun.appserv.management.config.DomainConfig;
39 import com.sun.appserv.management.DomainRoot;
40 import com.sun.appserv.management.base.AMX;
41 import com.sun.appserv.management.base.QueryMgr;
42 import com.sun.appserv.management.client.AppserverConnectionSource;
43 import com.sun.appserv.management.client.ProxyFactory;
44 import com.sun.appserv.management.config.RARModuleConfig;
45 import com.sun.appserv.management.config.ResourceConfig;
46 import com.sun.appserv.management.config.ServerConfig;
47 import com.sun.appserv.management.j2ee.J2EEManagedObject;
48 import com.sun.appserv.management.j2ee.J2EEDomain;
49 import com.sun.appserv.management.j2ee.ResourceAdapterModule;
50
51 import org.netbeans.modules.j2ee.sun.util.NodeTypes;
52 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
53 import org.netbeans.modules.j2ee.sun.appsrvapi.PortDetector;
54
55 import com.sun.enterprise.deployment.backend.DeploymentStatus;
56 import com.sun.enterprise.deployment.client.DeploymentFacility;
57 import com.sun.enterprise.deployment.client.ServerConnectionIdentifier;
58 import com.sun.enterprise.deployment.client.DeploymentFacilityFactory;
59 import com.sun.enterprise.deployment.client.JESProgressObject;
60 import java.util.Properties JavaDoc;
61 import java.util.Vector JavaDoc;
62 import org.netbeans.modules.j2ee.sun.ide.controllers.ControllerUtil;
63
64 /**
65  *
66  */

67 public abstract class AppserverMgmtControllerBase
68         implements javax.management.NotificationListener JavaDoc {
69     
70     private AMX amxObj;
71     private ProxyFactory amxProxyFactory;
72     private MBeanServerConnection JavaDoc mbeanServerConn;
73     private DeploymentManager JavaDoc deployMgr;
74     private static Logger JavaDoc logger;
75     
76     protected AppserverConnectionSource appMgmtConnection;
77     protected static final String JavaDoc DAS_SERVER_NAME = "server";
78     protected static final String JavaDoc BACKEND_COM_SUN_APPSERV_MBEAN_DOMAIN_NAME =
79             "com.sun.appserv";
80         
81     static {
82         logger = Logger.getLogger("org.netbeans.modules.j2ee.sun");
83     }
84     
85     
86     /**
87      * Create an instance of AppserverMgmtController used in the interaction
88      * with AMX API for Sun Java System Application Server.
89      *
90      * @param connection An AppserverConnectionSource object used to connect
91      * to the appserver.
92      */

93     public AppserverMgmtControllerBase(final DeploymentManager JavaDoc deploymentMgr,
94                 final AppserverConnectionSource connection) {
95         this.deployMgr = deploymentMgr;
96         this.appMgmtConnection = connection;
97         initializeAMXConnectorWithInterceptor();
98     }
99     
100     
101     /**
102      * Create an instance of AppserverMgmtController used in the interaction
103      * with AMX API for Sun Java System Application Server.
104      *
105      * @param amxObject The amx object that this controller wraps.
106      * @param connection An AppserverConnectionSource object used to connect
107      * to the appserver.
108      */

109     public AppserverMgmtControllerBase(final AMX amxObject,
110             final DeploymentManager JavaDoc deploymentMgr,
111             final AppserverConnectionSource connection) {
112         this.deployMgr = deploymentMgr;
113         this.appMgmtConnection = connection;
114         initializeAMXConnectorWithInterceptor();
115         this.amxObj = amxObject;
116     }
117
118     
119     
120     /**
121      * Create an instance of AppserverMgmtController used in the interaction
122      * with AMX API for Sun Java System Application Server.
123      *
124      * @param amxObject The amx object that this controller wraps.
125      * @param connection An AppserverConnectionSource object used to connect
126      * to the appserver.
127      */

128     public AppserverMgmtControllerBase(final AMX amxObject,
129             final AppserverConnectionSource connection) {
130         appMgmtConnection = connection;
131         setupProxyFactory();
132         this.amxObj = amxObject;
133     }
134     
135     
136     /* getter for AppserverConnectionSource
137      * can be used to see if it is null (possible case when a password or
138      * user name is incorrect
139      **/

140     
141     public AppserverConnectionSource getAppserverConnectionSource(){
142         return appMgmtConnection;
143     }
144     /**
145      *
146      *
147      */

148     private void initializeAMXConnectorWithInterceptor() {
149         try {
150             ControllerUtil.checkIfServerInDebugMode(getDeploymentManager());
151             JMXConnector JavaDoc jmxConn =
152                 appMgmtConnection.getJMXConnector(false);
153             jmxConn.addConnectionNotificationListener(this,null,null);
154             //setup the proxy factory
155
setupProxyFactory();
156         } catch (RuntimeException JavaDoc rex) {
157             getLogger().log(Level.FINE, rex.getMessage(), rex);
158         } catch (Exception JavaDoc ex) {
159             getLogger().log(Level.FINE, ex.getMessage(), ex);
160         }
161     }
162     
163     
164     /**
165      * Sets up the local proxyFactory connection.
166      */

167     private void setupProxyFactory() {
168         try {
169             ControllerUtil.checkIfServerInDebugMode(getDeploymentManager());
170             this.amxProxyFactory =
171                 ProxyFactory.getInstance(getMBeanServerConnection());
172         } catch (RuntimeException JavaDoc rex) {
173             getLogger().log(Level.FINE, rex.getMessage(), rex);
174         } catch (Exception JavaDoc ex) {
175             getLogger().log(Level.FINE, ex.getMessage(), ex);
176         }
177     }
178     
179     
180     /**
181      *
182      *
183      */

184     public void handleNotification(final Notification JavaDoc notification,
185             final Object JavaDoc handback) {
186         try {
187             JMXConnector JavaDoc jmxConn = appMgmtConnection.getJMXConnector(false);
188             if(jmxConn != null) {
189                 jmxConn.close();
190             }
191         } catch(IOException JavaDoc io) {
192             getLogger().log(Level.FINE, io.getMessage(), io);
193         }
194         appMgmtConnection = null;
195     }
196     
197     
198     
199     /**
200      * Returns the MBeanServerConnection associated with this connection.
201      *
202      * @return The MBeanServerConnection.
203      */

204     public MBeanServerConnection JavaDoc getMBeanServerConnection() {
205         try {
206             if(mbeanServerConn == null) {
207                 testIfServerInDebugAndLogException();
208                 mbeanServerConn =
209                     ControllerUtil.getMBeanServerConnWithInterceptor(
210                         (SunDeploymentManagerInterface)deployMgr,
211                             appMgmtConnection);
212             }
213             testIfServerInDebugAndLogException();
214         } catch (RuntimeException JavaDoc rex) {
215             getLogger().log(Level.FINE, rex.getMessage(), rex);
216         } catch(Exception JavaDoc e) {
217             getLogger().log(Level.FINE, e.getMessage(), e);
218         }
219         return mbeanServerConn;
220     }
221     
222     
223     /**
224      * Returns the DeploymentManager object from the controller.
225      *
226      * @return The DeploymentManager.
227      */

228     public DeploymentManager JavaDoc getDeploymentManager() {
229         return deployMgr;
230     }
231     
232     
233     /**
234      * Returns the name of the component that this controller wraps.
235      *
236      * @return The name of the component that this controller wraps.
237      */

238     public String JavaDoc getName() {
239         return getAMXObject().getName();
240     }
241     
242     
243     /**
244      * Returns the AMX mbean that this controller wraps.
245      *
246      * @return The AMX mbean that this controller wraps.
247      */

248     public AMX getAMXObject() {
249         return amxObj;
250     }
251     
252
253     /**
254      * Returns the AMX API DomainRoot for the connected appserver.
255      *
256      * @return the DomainRoot of the application server.
257      */

258     final DomainRoot getDomainRoot() {
259         testIfServerInDebug();
260         return amxProxyFactory.getDomainRoot();
261     }
262     
263     
264     /**
265      *
266      */

267     final DomainConfig getDomainConfig() {
268         // stop anything from crossing into AMX if the server is in debug
269
testIfServerInDebug();
270         return getDomainRoot().getDomainConfig();
271     }
272     
273     
274     /**
275      * Returns the AMX API DomainRoot for the connected appserver.
276      *
277      * @return the DomainRoot of the application server.
278      */

279     protected final J2EEDomain getJ2EEDomain() {
280         return getDomainRoot().getJ2EEDomain();
281     }
282     
283         
284     /**
285      * Returns the QueryMgr object for this controller.
286      *
287      * @return The QueryMgr AMX mbean.
288      */

289     final protected QueryMgr getQueryMgr() {
290         return getDomainRoot().getQueryMgr();
291     }
292     
293     
294     /**
295      * Returns the proxy factory object in AMX. This is used for looking up
296      * AMX mbean instances given and objectName or setting a custom
297      * MBeanServerConnection such as the one used for intercepting requests to
298      * the server (org.netbeans.modules.j2ee.sun.util.PluginRequestInterceptor).
299      *
300      * @return The ProxyFactory instance for the current JMX connection.
301      */

302     final ProxyFactory getAMXProxyFactory() {
303         return this.amxProxyFactory;
304     }
305     
306     
307     /**
308      * Returns the properties of the application given the nodeType and name.
309      *
310      * @param nodeType The name of the node.
311      * @param amx The amx mbean from which properties will be extracted.
312      * @param propsToIgnore Properties to be ignored.
313      * @return All the component properties.
314      */

315     public Map JavaDoc getJ2EEAndConfigProperties(String JavaDoc nodeType,
316             AMX j2eeMod, AMX configPeer, List JavaDoc propsToIgnore) {
317         try {
318             Map JavaDoc propsMap = new HashMap JavaDoc();
319             if(configPeer == null && j2eeMod != null){
320                 configPeer = getConfigPeerByNodeTypeAndName(j2eeMod, nodeType);
321             }
322             if(configPeer != null) {
323                 propsMap = getConfigPropertiesFromBackend(nodeType,
324                         configPeer, propsToIgnore);
325                 
326             }
327             if(j2eeMod != null){
328                 Map JavaDoc j2eeProps =
329                         getPropertiesFromBackend(nodeType, j2eeMod, propsToIgnore);
330                 propsMap.putAll(j2eeProps);
331             }
332             if(propsMap == null || propsMap.size() == 0) {
333                 getLogger().log(Level.FINE, "The props in getJ2EE is 0!");
334             }
335             propsMap = ControllerUtil.modifyEnabledProperty(propsMap, configPeer);
336             return propsMap;
337         } catch (RuntimeException JavaDoc rex) {
338             getLogger().log(Level.FINE, rex.getMessage(), rex);
339             return new HashMap JavaDoc();
340         } catch (Exception JavaDoc e) {
341             getLogger().log(Level.FINE, e.getMessage());
342             return new HashMap JavaDoc();
343         }
344     }
345
346     
347     /**
348      * Return all the properties for this particular AMX resource.
349      *
350      * @param nodeType The type of node defined in NodeTypes.
351      * @param amx The AMX mbean interface.
352      * @param propsToIgnore Properties to be ignored.
353      * @return A java.util.Map of Attributes and their respective
354      * MBeanAttributeInfos.
355      */

356     protected Map JavaDoc getPropertiesFromBackend(final String JavaDoc nodeType,
357             final AMX res, final List JavaDoc propsToIgnore) {
358         testIfServerInDebug();
359         Class JavaDoc intrface = NodeTypes.getAMXInterface(nodeType);
360         return ControllerUtil.getAllAttributes(intrface, res, propsToIgnore,
361                 getMBeanServerConnection(), nodeType);
362     }
363     
364     
365     /**
366      * Return all the properties for this particular AMX resource.
367      *
368      * @param nodeType The type of node defined in NodeTypes.
369      * @param amx The AMX mbean interface.
370      * @param propsToIgnore Properties to be ignored.
371      * @return A java.util.Map of Attributes and their respective
372      * MBeanAttributeInfos.
373      */

374     protected Map JavaDoc getConfigPropertiesFromBackend(final String JavaDoc nodeType,
375             final AMX res, final List JavaDoc propsToIgnore) {
376         testIfServerInDebug();
377         Class JavaDoc intrface = NodeTypes.getAMXConfigPeerInterface(nodeType);
378         return ControllerUtil.getAllAttributes(intrface, res, propsToIgnore,
379                 getMBeanServerConnection(), nodeType);
380     }
381     
382     
383     /**
384      * Returns the config peer for the J2EE_TYPE and module name given. This
385      * is required since some of the getConfigPeer methods for JSR77 related
386      * AMX mbeans are not working as of SJSAS 8.1.
387      *
388      * @param nodeType The nodeType.
389      * @param moduleName The name of the module.
390      * @return The AMX module interface for the config peer.
391      */

392     protected AMX getConfigPeerByNodeTypeAndName(final AMX amx,
393             final String JavaDoc nodeType) {
394         testIfServerInDebug();
395         J2EEManagedObject managedObj = (J2EEManagedObject) amx;
396         AMX configPeer = managedObj.getConfigPeer();
397         if(configPeer != null) {
398             return configPeer;
399         } else {
400             final String JavaDoc j2eeType =
401                 NodeTypes.getAMXConfigPeerJ2EETypeByNodeType(nodeType);
402             java.util.Set JavaDoc set = getQueryMgr().queryJ2EETypeSet(j2eeType);
403             for(Iterator JavaDoc itr = set.iterator(); itr.hasNext(); ) {
404                 AMX config = (AMX) itr.next();
405                 if(config.getName().equals(amx.getName())) {
406                     return config;
407                 }
408             }
409         }
410        return null;
411     }
412     
413     /**
414      * Return all the properties for this particular AMX resource.
415      *
416      * @param amx The AMX mbean interface.
417      * @param propNames Properties to be added.
418      * @return A java.util.Map of Attributes and their respective
419      * MBeanAttributeInfos.
420      */

421     protected Map JavaDoc getLogPropertiesFromBackend(final AMX res, final Map JavaDoc propNames) {
422         testIfServerInDebug();
423         return ControllerUtil.getLogAttributes(res, propNames, getMBeanServerConnection());
424     }
425     
426     /**
427      * Undeploys an application, web module, app client, connector, or ejb
428      * module. Currently, this method first removes the reference to the
429      * component using the ServerConfig AMX mbean. Then it uses the
430      * AMX DeploymentMgr mbean to undeploy the component. Finally, since there
431      * is a bug in the underlying old JMX config mbeans, the corresponding
432      * JSR77 mbeans have to be cleaned up (removed) because the backend doesn't
433      * currently handle this without passing a target server name to the
434      * undeploy method on com.sun.appserv:type=applications,category=config
435      * mbean.
436      *
437      */

438     public void undeploy() {
439         testIfServerInDebug();
440         DeploymentFacility df = createDeploymentFacility();
441         JESProgressObject progressObject = null;
442         Properties JavaDoc props = null;
443         
444         if((getAMXObject() instanceof ResourceAdapterModule) ||
445                 (getAMXObject() instanceof RARModuleConfig)){
446             props = new Properties JavaDoc();
447             props.put("name", getName()); //NOI18N
448
props.put("cascade", "true"); //NOI18N
449
}
450         
451         if (df.isConnected()) {
452             String JavaDoc[] instances = getRelevantTargets();
453             Target JavaDoc[] targets = df.createTargets(instances);
454             progressObject = df.undeploy(targets, getName(), props);
455             df.waitFor(progressObject);
456         }
457         // TODO : this looks suspect. resolve
458
if (null != progressObject) {
459             DeploymentStatus ds = progressObject.getCompletedStatus();
460             ds.toString();
461         }
462     }
463     
464     private String JavaDoc[] getRelevantTargets(){
465         Map JavaDoc serverConfigs = ControllerUtil.getServerInstancesMap(getAMXObject());
466         Vector JavaDoc instances = new Vector JavaDoc();
467         for(Iterator JavaDoc itr = serverConfigs.values().iterator(); itr.hasNext(); ) {
468             ServerConfig config = (ServerConfig)itr.next();
469             boolean contains = config.getDeployedItemRefConfigMap().containsKey(getName());
470             if(contains){
471                 instances.add(config.getName());
472             }
473         }
474         String JavaDoc[] targets = new String JavaDoc[instances.size()];
475         for(int i=0; i<instances.size(); i++){
476             targets[i] = instances.get(i).toString();
477         }
478         return targets;
479     }
480     
481     private DeploymentFacility createDeploymentFacility(){
482         DeploymentFacility df = DeploymentFacilityFactory.getDeploymentFacility();
483         SunDeploymentManagerInterface sunDplmtMgr = (SunDeploymentManagerInterface)getDeploymentManager();
484         ServerConnectionIdentifier conn = createServerConnectionIdentifier(
485                 sunDplmtMgr.getHost(), sunDplmtMgr.getPort(), sunDplmtMgr.getUserName(), sunDplmtMgr.getPassword());
486         df.connect(conn);
487         return df;
488     }
489     
490     private ServerConnectionIdentifier createServerConnectionIdentifier(
491             String JavaDoc host, int port, String JavaDoc user, String JavaDoc password) {
492         ServerConnectionIdentifier conn =
493             new ServerConnectionIdentifier();
494         conn.setHostName(host);
495         conn.setHostPort(port);
496         conn.setUserName(user);
497         conn.setPassword(password);
498         try{
499             if (PortDetector.isSecurePort(host, port)) {
500                 conn.setSecure(true);
501             } else {
502                 conn.setSecure(false);
503             }
504         }catch(Exception JavaDoc ex){
505             conn.setSecure(false);
506         }
507         return conn;
508     }
509     
510     
511    
512     
513     /**
514      * Removes the server reference to a resource
515      *
516      * @param resName The name of the resource to be removed as a reference.
517      */

518     protected void removeResourceRef(ResourceConfig resConfig, String JavaDoc resName) {
519         testIfServerInDebug();
520         
521         Map JavaDoc serverConfigs = ControllerUtil.getStandaloneServerInstancesMap(resConfig);
522         for(Iterator JavaDoc itr = serverConfigs.values().iterator(); itr.hasNext(); ) {
523             StandaloneServerConfig config = (StandaloneServerConfig)itr.next();
524             boolean contains = config.getResourceRefConfigMap().containsKey(resName);
525             if(contains) {
526                 config.removeResourceRefConfig(resName);
527             }
528         }
529     }
530         
531     /**
532      * Returns the logger for the controller.
533      *
534      * @returns The java.util.logging.Logger impl. for this controller.
535      */

536      protected final Logger JavaDoc getLogger() {
537           if ( logger == null ) {
538                logger = Logger.getLogger("org.netbeans.modules.j2ee.sun");
539           }
540           return logger;
541      }
542      
543      
544     /**
545      *
546      *
547      */

548     protected void testIfServerInDebug() {
549         try {
550             ControllerUtil.checkIfServerInDebugMode(getDeploymentManager());
551         } catch (RuntimeException JavaDoc rex) {
552             getLogger().log(Level.FINE, rex.getMessage(), rex);
553             throw rex;
554         } catch(Exception JavaDoc e) {
555             getLogger().log(Level.FINE, e.getMessage(), e);
556         }
557     }
558     
559     
560     /**
561      *
562      *
563      */

564     protected void testIfServerInDebugAndLogException() {
565         try {
566             ControllerUtil.checkIfServerInDebugMode(getDeploymentManager());
567         } catch (RuntimeException JavaDoc rex) {
568             getLogger().log(Level.FINE, rex.getMessage(), rex);
569         } catch(Exception JavaDoc e) {
570             getLogger().log(Level.FINE, e.getMessage(), e);
571         }
572     }
573
574     public boolean isDeployMgrLocal(){
575         return ((SunDeploymentManagerInterface)deployMgr).isLocal();
576     }
577     
578         
579 }
580
Popular Tags