KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > controllers > ControllerUtil


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.sun.ide.controllers;
20
21 import java.util.HashSet JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.Vector JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32
33 import javax.management.Attribute JavaDoc;
34 import javax.management.MBeanAttributeInfo JavaDoc;
35 import javax.management.MBeanInfo JavaDoc;
36 import javax.management.MBeanServerConnection JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.RuntimeMBeanException JavaDoc;
39 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
40
41 import com.sun.appserv.management.base.AMX;
42 import com.sun.appserv.management.base.Util;
43 import com.sun.appserv.management.base.QueryMgr;
44 import com.sun.appserv.management.base.XTypes;
45 import com.sun.appserv.management.j2ee.J2EEManagedObject;
46 import com.sun.appserv.management.j2ee.J2EETypes;
47 import com.sun.appserv.management.client.AppserverConnectionSource;
48 import com.sun.appserv.management.config.DeployedItemRefConfig;
49 import com.sun.appserv.management.config.ModuleConfig;
50 import com.sun.appserv.management.config.ResourceConfig;
51 import com.sun.appserv.management.config.ResourceRefConfig;
52 import com.sun.appserv.management.config.ServerConfig;
53 import java.io.File JavaDoc;
54
55 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
56 import org.netbeans.modules.j2ee.sun.bridge.apis.AppserverMgmtController;
57 import org.netbeans.modules.j2ee.sun.util.AppserverConnectionFactory;
58 import org.netbeans.modules.j2ee.sun.util.GUIUtils;
59 import org.netbeans.modules.j2ee.sun.util.NodeTypes;
60 import org.netbeans.modules.j2ee.sun.util.PluginRequestInterceptor;
61 import org.openide.util.NbBundle;
62
63 /**
64  *
65  */

66 public class ControllerUtil {
67     
68     private static final String JavaDoc DELIMINATOR = " ";
69     private static String JavaDoc PROPERTIES_INTERFACE = "PropertiesAccess";
70     private static String JavaDoc DESCRIPTION_INTERFACE = "Description";
71     private static String JavaDoc ENABLED_INTERFACE = "Enabled";
72     private static String JavaDoc RESOURCE_CONFIG_INTERFACE = "ResourceConfig";
73     private static String JavaDoc MODULE_CONFIG_INTERFACE = "ModuleConfig";
74     private static String JavaDoc J2EE_DEPLOYED_OBJ_INTERFACE = "J2EEDeployedObject";
75     private static String JavaDoc OBJECT_TYPE_INTERFACE = "ObjectType";
76     private static String JavaDoc NAME_ATTRIBUTE = "Name";
77     private static String JavaDoc JNDI_NAME_ATTRIBUTE = "JNDIName";
78     
79     private static final String JavaDoc ASADMIN_KEY = "asadmin";
80     private static final String JavaDoc ADMIN_GUI_KEY = "admingui";
81     private static final String JavaDoc ADMIN_GUI_KEY2 = "admin-console";
82     private static final String JavaDoc ADMIN_APP_KEY = "adminapp";
83     private static final String JavaDoc WEB_UI_KEY = "com_sun_web_ui";
84     private static final String JavaDoc MEJB_SYS_APP = "MEjbApp";
85     private static final String JavaDoc EJB_TIMER_SYS_APP = "__ejb_container_timer_app";
86     private static final String JavaDoc JWS_APPCLIENTS_KEY = "__JWSappclients";
87     private static final String JavaDoc WSIT_KEY = "WSTXServices";
88     private static final String JavaDoc SERVER_KEY = "//server/";
89     
90     static final String JavaDoc[] RES_ADAPTOR_KEYS = {"__xa", "jmsra", "__ds"};
91     
92     private static final String JavaDoc CONFIG_OBJ_NAME = "com.sun.appserv:type=applications,category=config";
93     private static Logger JavaDoc logger;
94     private static Map JavaDoc j2eeTypeToConfigMap;
95     
96     private static final String JavaDoc DAS_SERVER_NAME = "server";
97     
98     static {
99         logger = Logger.getLogger("org.netbeans.modules.j2ee.sun");
100     }
101     
102     static {
103         j2eeTypeToConfigMap = new HashMap JavaDoc();
104         j2eeTypeToConfigMap.put(J2EETypes.J2EE_DOMAIN, XTypes.DOMAIN_CONFIG);
105         j2eeTypeToConfigMap.put(J2EETypes.J2EE_CLUSTER, XTypes.CLUSTER_CONFIG);
106         j2eeTypeToConfigMap.put(J2EETypes.J2EE_SERVER,
107                 XTypes.STANDALONE_SERVER_CONFIG);
108         j2eeTypeToConfigMap.put(J2EETypes.JVM, XTypes.JAVA_CONFIG);
109         j2eeTypeToConfigMap.put(J2EETypes.J2EE_APPLICATION,
110                 XTypes.J2EE_APPLICATION_CONFIG);
111         j2eeTypeToConfigMap.put(J2EETypes.EJB_MODULE, XTypes.EJB_MODULE_CONFIG);
112         j2eeTypeToConfigMap.put(J2EETypes.WEB_MODULE, XTypes.WEB_MODULE_CONFIG);
113         j2eeTypeToConfigMap.put(J2EETypes.APP_CLIENT_MODULE,
114                 XTypes.APP_CLIENT_MODULE_CONFIG);
115         j2eeTypeToConfigMap.put(J2EETypes.RESOURCE_ADAPTER_MODULE,
116                 XTypes.RAR_MODULE_CONFIG);
117         j2eeTypeToConfigMap.put(J2EETypes.RESOURCE_ADAPTER,
118                 XTypes.RESOURCE_ADAPTER_CONFIG);
119      }
120
121     
122     /**
123      * Private constructor to avoid instantiation.
124      */

125     private ControllerUtil() {
126     }
127     
128     
129     /**
130      *
131      *
132      */

133     public static AppserverMgmtController getAppserverMgmtControllerFromDeployMgr(final DeploymentManager JavaDoc deployMgr) {
134         AppserverConnectionSource con = createAppserverConnFromDeploymentMgr(deployMgr);
135         if (con==null){
136             return null; //no way to get a valid controller sinc the connection is bad: no password, or bad one
137
}
138         else {
139             return new AppserverMgmtController(deployMgr, con);
140         }
141     }
142     
143     
144     /**
145      *
146      *
147      */

148     public static AppserverConnectionSource createAppserverConnFromDeploymentMgr(final DeploymentManager JavaDoc deployMgr) {
149         AppserverConnectionSource connection = null;
150         SunDeploymentManagerInterface sunDpmtMgr =
151                 (SunDeploymentManagerInterface) deployMgr;
152
153         try {
154            // connection = AppserverConnectionFactory.getAppserverConnection(
155
connection = AppserverConnectionFactory.getHTTPAppserverConnection(
156                 sunDpmtMgr.getHost(), sunDpmtMgr.getPort(),
157                 sunDpmtMgr.getUserName(), sunDpmtMgr.getPassword(),
158                 sunDpmtMgr.isSecure());
159             
160             //next line is important to test if the connection is correct. If it is not, the expcetion is raised
161
//so that we can return a null one.
162
connection.getDomainRoot();
163
164         } catch (Exception JavaDoc ioex) {
165             connection=null;
166             //GUIUtils.showError("Bad AppserverConnection: "+ioex.getMessage());
167

168
169         
170         }
171         return connection;
172     }
173     
174     
175     /**
176      *
177      *
178      */

179     public static MBeanServerConnection JavaDoc getMBeanServerConnWithInterceptor(
180             final SunDeploymentManagerInterface sunDplymtIntrface,
181             final AppserverConnectionSource connSource) {
182         MBeanServerConnection JavaDoc conn = null;
183         try {
184             conn = connSource.getMBeanServerConnection(false);
185         } catch (IOException JavaDoc io) {
186             logger.log(Level.FINE, io.getMessage(), io);
187         }
188         MBeanServerConnection JavaDoc connWithInterceptor =
189             new PluginRequestInterceptor(sunDplymtIntrface, conn);
190         return connWithInterceptor;
191     }
192     
193     
194     /**
195      *
196      *
197      */

198     static public AMX getAMXComponentFromMap(Map JavaDoc map, String JavaDoc keyName) {
199         for(Iterator JavaDoc itr = map.values().iterator(); itr.hasNext(); ) {
200             AMX component = (AMX)itr.next();
201             if(component.getName().equals(keyName)) {
202                 return component;
203             }
204         }
205         return null;
206     }
207     
208     
209     /**
210      *
211      */

212     static public String JavaDoc[] getComponentNamesFromMap(Map JavaDoc map) {
213        String JavaDoc [] names = new String JavaDoc[map.size()];
214        int pos = 0;
215        for(Iterator JavaDoc itr = map.values().iterator(); itr.hasNext(); ) {
216            AMX component = (AMX)itr.next();
217            names[pos] = component.getName();
218            pos++;
219        }
220        return names;
221     }
222     
223     /**
224      * Return a string representation of an array.
225      */

226     static public String JavaDoc arrayToString(Object JavaDoc [] array) {
227         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
228         for(int i = 0; i < array.length; i++) {
229             buffer.append(array[i]);
230             buffer.append(DELIMINATOR);
231         }
232         return buffer.toString();
233     }
234     
235     
236     /**
237      *
238      *
239      */

240     public static Map JavaDoc getAllAttributes(final Class JavaDoc clazz,
241             final AMX amx, final List JavaDoc propsToIgnore,
242             final MBeanServerConnection JavaDoc conn, final String JavaDoc nodeType) {
243         Map JavaDoc attrs = null;
244         Set JavaDoc declaredAttrs =
245             extractAttributeNamesFromMethods(
246                 getGetterMethods(clazz.getDeclaredMethods()));
247         Set JavaDoc inheritedAttrs = getExtraAttributes(clazz);
248         declaredAttrs.addAll(inheritedAttrs);
249         if(isJNDINameAbsent(declaredAttrs)) {
250             declaredAttrs.add(NAME_ATTRIBUTE);
251         }
252         if(nodeType.equals(NodeTypes.WEB_MODULE)){
253             //In this filter, the props in the list are displayed and the rest ignored.
254
attrs = getAttributeProperties(
255                 applyFilterToAttributeNamesForWeb(propsToIgnore,declaredAttrs), amx, conn);
256         }else{
257             attrs = getAttributeProperties(
258                 applyFilterToAttributeNames(propsToIgnore,declaredAttrs), amx, conn);
259         }
260         attrs = modifyEnabledProperty(attrs, amx);
261         return attrs;
262     }
263     
264     /**
265      *
266      *
267      */

268     private static Set JavaDoc getExtraAttributes(Class JavaDoc clazz) {
269         Set JavaDoc attrSet = new HashSet JavaDoc();
270         Class JavaDoc [] classes = clazz.getInterfaces();
271         for(int i = 0; i < classes.length; i++) {
272             if(isExtraInterface(getSimpleClassName(classes[i]))) {
273                 attrSet.addAll(extractAttributeNamesFromMethods(
274                         getGetterMethods(classes[i].getDeclaredMethods())));
275                 
276             }
277             attrSet.addAll(getExtraAttributes(classes[i]));
278         }
279         return attrSet;
280     }
281     
282     /**
283      *
284      */

285     private static String JavaDoc getSimpleClassName(Class JavaDoc clazz) {
286         String JavaDoc fullClassName = clazz.getName();
287         int lastIndex = fullClassName.lastIndexOf(".");
288         return fullClassName.substring(lastIndex + 1,
289             fullClassName.length()).trim();
290     }
291     
292     /**
293      *
294      *
295      */

296     private static boolean isExtraInterface(String JavaDoc intrfaceName) {
297         return (DESCRIPTION_INTERFACE.equals(intrfaceName)
298                 || ENABLED_INTERFACE.equals(intrfaceName)
299                 || PROPERTIES_INTERFACE.equals(intrfaceName)
300                 || RESOURCE_CONFIG_INTERFACE.equals(intrfaceName)
301                 || J2EE_DEPLOYED_OBJ_INTERFACE.equals(intrfaceName)
302                 || MODULE_CONFIG_INTERFACE.equals(intrfaceName)
303                 || OBJECT_TYPE_INTERFACE.equals(intrfaceName));
304     }
305     
306     
307     /**
308      *
309      */

310     private static boolean isJNDINameAbsent(Set JavaDoc attrs) {
311         for(Iterator JavaDoc itr = attrs.iterator(); itr.hasNext(); ) {
312             if(JNDI_NAME_ATTRIBUTE.equals(itr.next())) {
313                 return false;
314             }
315         }
316         return true;
317     }
318     
319     
320     /**
321      *
322      */

323     private static Set JavaDoc applyFilterToAttributeNames(
324             final List JavaDoc propsToIgnore, final Set JavaDoc names) {
325        if(propsToIgnore != null && propsInListAreStrings(propsToIgnore)) {
326            for(Iterator JavaDoc itr = propsToIgnore.iterator(); itr.hasNext(); ) {
327                String JavaDoc name = (String JavaDoc) itr.next();
328                if(names.contains(name)) {
329                    names.remove(name);
330                }
331            }
332        }
333        return names;
334     }
335     
336     private static Set JavaDoc applyFilterToAttributeNamesForWeb(
337     final List JavaDoc propsToAdd, final Set JavaDoc names) {
338         Set JavaDoc attrNames = new HashSet JavaDoc();
339         if(propsToAdd == null){
340             return names;
341         }
342         if(propsToAdd != null && propsInListAreStrings(propsToAdd)) {
343             for(Iterator JavaDoc itr = propsToAdd.iterator(); itr.hasNext(); ) {
344                 String JavaDoc name = (String JavaDoc) itr.next();
345                 if(names.contains(name)) {
346                     attrNames.add(name);
347                 }
348             }
349         }
350         return attrNames;
351     }
352     
353     
354     /**
355      *
356      */

357     public static boolean propsInListAreStrings(List JavaDoc propsToIgnore) {
358        for(Iterator JavaDoc itr = propsToIgnore.iterator(); itr.hasNext(); ) {
359            if(!(itr.next() instanceof String JavaDoc)) {
360                    return false;
361            }
362        }
363        return true;
364     }
365     
366     
367     /**
368      *
369      *
370      */

371     private static Map JavaDoc getAttributeProperties(Set JavaDoc names, AMX amx,
372             MBeanServerConnection JavaDoc conn) {
373         //get attribute info
374
Map JavaDoc returnMap = new HashMap JavaDoc();
375         final ObjectName JavaDoc on = Util.getObjectName(amx);
376         //get the attribute info
377
try {
378             final MBeanInfo JavaDoc mi = conn.getMBeanInfo(on);
379             final MBeanAttributeInfo JavaDoc[] mai = mi.getAttributes();
380             for (int i = 0 ; i < mai.length ; i++) {
381                 if (mai[i] != null && names.contains(mai[i].getName())) {
382                     //get the attribute from the mbean
383
try {
384                         Object JavaDoc attrValue = conn.getAttribute(on, mai[i].getName());
385                         //put it in the return map indexed by attribute
386
returnMap.put(new Attribute JavaDoc(mai[i].getName(),
387                                 attrValue), mai[i]);
388                     } catch(javax.management.AttributeNotFoundException JavaDoc ex) {
389                         continue;
390                     }
391                 }
392             }
393         } catch(Exception JavaDoc e) {
394             logger.log(Level.FINE, e.getMessage(), e);
395         }
396         if(returnMap.size() == 0 || returnMap == null) {
397             logger.log(Level.FINE, "The return Map in getAttrProperties is " +
398                     "size 0!");
399         }
400         return returnMap;
401     }
402     
403
404     /**
405      *
406      *
407      */

408     public static Set JavaDoc extractAttributeNamesFromMethods(final Method JavaDoc[] method) {
409         final Set JavaDoc attrNames = new HashSet JavaDoc();
410         for (int i = 0 ; i < method.length ; i++) {
411             final String JavaDoc methodName = method[i].getName();
412             final String JavaDoc name =
413                 methodName.substring(methodName.indexOf("get")+3);
414             attrNames.add(name);
415         }
416         return attrNames;
417     }
418     
419     
420     /**
421      *
422      *
423      */

424     public static Method JavaDoc[] getGetterMethods(Method JavaDoc[] methods) {
425         final Vector JavaDoc v = new Vector JavaDoc();
426         for (int i = 0 ; i < methods.length ; i++) {
427             if (methods[i].getName().startsWith("get")
428                     && !methods[i].getName().endsWith("Stats")) {
429                 v.add(methods[i]);
430             }
431         }
432         final Method JavaDoc[] mm = new Method JavaDoc[v.size()];
433         return (Method JavaDoc[]) v.toArray(mm);
434     }
435     
436     
437     /**
438      *
439      *
440      */

441     public static AMX getConfigPeer(final AMX amx) {
442         J2EEManagedObject managedObj = (J2EEManagedObject) amx;
443         AMX configPeer = managedObj.getConfigPeer();
444         if(configPeer != null) {
445             return configPeer;
446         } else {
447             String JavaDoc configXType = getConfigPeerAMXType(amx.getJ2EEType());
448             QueryMgr queryMgr = amx.getDomainRoot().getQueryMgr();
449             java.util.Set JavaDoc set = queryMgr.queryJ2EETypeSet(configXType);
450             for(Iterator JavaDoc itr = set.iterator(); itr.hasNext(); ) {
451                 AMX config = (AMX) itr.next();
452                 if(config.getName().equals(amx.getName())) {
453                     return config;
454                 }
455             }
456         }
457        return null;
458     }
459     
460     
461     /**
462      *
463      *
464      */

465     public static Attribute JavaDoc setAttributeValue(AMX j2eeMod, AMX configPeer, String JavaDoc attrName, Object JavaDoc value,
466             MBeanServerConnection JavaDoc conn) {
467         Attribute JavaDoc modAttr = new Attribute JavaDoc(attrName, value);
468         try {
469             if(attrName.equals(NodeTypes.ENABLED) && (configPeer != null)){
470                 updateEnabled(configPeer, value);
471             }else{
472                 ObjectName JavaDoc configOn = Util.getObjectName(configPeer);
473                 conn.setAttribute(configOn, modAttr);
474             }
475         } catch (RuntimeMBeanException JavaDoc e) {
476             if(j2eeMod != null){
477                 ObjectName JavaDoc j2eeOn = Util.getObjectName(j2eeMod);
478                 modAttr = setAttrValue(j2eeOn, attrName, modAttr, conn);
479             }else
480                 modAttr = null;
481         } catch (Exception JavaDoc ex) {
482             Object JavaDoc [] params = new Object JavaDoc[] {attrName, ex.getMessage()};
483             GUIUtils.showError(
484                 getLocalizedString("unexpected_setAttr_except", params));
485             modAttr = null;
486         }
487         return modAttr;
488     }
489     
490     public static Attribute JavaDoc setAttributeValue(AMX amx, String JavaDoc attrName, Object JavaDoc value,
491             MBeanServerConnection JavaDoc conn) {
492         final ObjectName JavaDoc on = Util.getObjectName(amx);
493         Attribute JavaDoc modAttr = new Attribute JavaDoc(attrName, value);
494         try {
495             if(attrName.equals(NodeTypes.ENABLED) && (amx != null))
496                 updateEnabled(amx, value);
497             else
498                 conn.setAttribute(on, modAttr);
499         } catch (RuntimeMBeanException JavaDoc e) {
500             modAttr = setAttributeOnConfigPeer(amx, attrName, value, conn);
501         } catch (Exception JavaDoc ex) {
502             Object JavaDoc [] params = new Object JavaDoc[] {attrName, ex.getMessage()};
503             GUIUtils.showError(
504                 getLocalizedString("unexpected_setAttr_except", params));
505             modAttr = null;
506         }
507         return modAttr;
508     }
509     
510     private static void updateEnabled(AMX amx, Object JavaDoc value){
511         if(amx instanceof ResourceConfig){
512             setResourceEnabled((ResourceConfig)amx, value);
513         } else {
514             if(amx instanceof ModuleConfig) {
515                 setModuleEnabled((ModuleConfig)amx, value);
516             }
517         }
518     }
519     
520     private static Attribute JavaDoc setAttrValue(ObjectName JavaDoc on, String JavaDoc attrName, Attribute JavaDoc modAttr,
521             MBeanServerConnection JavaDoc conn) {
522         try {
523             conn.setAttribute(on, modAttr);
524         } catch (Exception JavaDoc ex) {
525             Object JavaDoc [] params = new Object JavaDoc[] {attrName, ex.getMessage()};
526             GUIUtils.showError(
527                 getLocalizedString("unexpected_setAttr_except", params));
528             modAttr = null;
529         }
530         return modAttr;
531     }
532     
533     private static final String JavaDoc CONST_SET_PROP_EXCEPT = "unexpected_setProp_except"; // NOI18N
534

535     /**
536      *
537      *
538      */

539      public static void setPropertyValue(com.sun.appserv.management.config.ResourceConfig resConfig, Object JavaDoc[] props) {
540         try {
541             for(int i=0; i<props.length; i++){
542                 Attribute JavaDoc attr = (Attribute JavaDoc)props[i];
543                 String JavaDoc propValue = null;
544                 if(attr.getValue() != null){
545                     propValue = attr.getValue().toString();
546                 }
547                 resConfig.setPropertyValue(attr.getName(), propValue);
548             }
549         } catch (RuntimeMBeanException JavaDoc e) {
550             Object JavaDoc [] params = new Object JavaDoc[] {resConfig.getJNDIName(), e.getMessage()};
551             GUIUtils.showError(
552                 getLocalizedString(CONST_SET_PROP_EXCEPT, params));
553         } catch (Exception JavaDoc ex) {
554             Object JavaDoc [] params = new Object JavaDoc[] {resConfig.getJNDIName(), ex.getMessage()};
555             GUIUtils.showError(
556                 getLocalizedString(CONST_SET_PROP_EXCEPT, params));
557         }
558     }
559     
560      /**
561       *
562       *
563       */

564      public static void setPropertyValue(com.sun.appserv.management.config.JDBCConnectionPoolConfig resConfig, Object JavaDoc[] props) {
565          try {
566              for(int i=0; i<props.length; i++){
567                  Attribute JavaDoc attr = (Attribute JavaDoc)props[i];
568                  String JavaDoc propValue = null;
569                  if(attr.getValue() != null){
570                      propValue = attr.getValue().toString();
571                  }
572                  resConfig.setPropertyValue(attr.getName(), propValue);
573              }
574          } catch (RuntimeMBeanException JavaDoc e) {
575              Object JavaDoc [] params = new Object JavaDoc[] {resConfig.getName(), e.getMessage()};
576              GUIUtils.showError(
577                      getLocalizedString(CONST_SET_PROP_EXCEPT, params));
578          } catch (Exception JavaDoc ex) {
579              Object JavaDoc [] params = new Object JavaDoc[] {resConfig.getName(), ex.getMessage()};
580              GUIUtils.showError(
581                      getLocalizedString(CONST_SET_PROP_EXCEPT, params));
582          }
583      }
584      
585      /**
586       *
587       *
588       */

589      public static void setPropertyValue(com.sun.appserv.management.config.ConnectorConnectionPoolConfig resConfig, Object JavaDoc[] props) {
590          try {
591              for(int i=0; i<props.length; i++){
592                  Attribute JavaDoc attr = (Attribute JavaDoc)props[i];
593                  String JavaDoc propValue = null;
594                  if(attr.getValue() != null){
595                      propValue = attr.getValue().toString();
596                  }
597                  resConfig.setPropertyValue(attr.getName(), propValue);
598              }
599          } catch (RuntimeMBeanException JavaDoc e) {
600              Object JavaDoc [] params = new Object JavaDoc[] {resConfig.getName(), e.getMessage()};
601              GUIUtils.showError(
602                      getLocalizedString(CONST_SET_PROP_EXCEPT, params));
603          } catch (Exception JavaDoc ex) {
604              Object JavaDoc [] params = new Object JavaDoc[] {resConfig.getName(), ex.getMessage()};
605              GUIUtils.showError(
606                      getLocalizedString(CONST_SET_PROP_EXCEPT, params));
607          }
608      }
609      
610     /**
611      *
612      *
613      */

614     private static Attribute JavaDoc setAttributeOnConfigPeer(final AMX amx,
615             final String JavaDoc attrName, final Object JavaDoc value,
616             final MBeanServerConnection JavaDoc conn) {
617         AMX configPeer = getConfigPeer(amx);
618         Attribute JavaDoc modAttr = new Attribute JavaDoc(attrName, value);
619         try {
620             final ObjectName JavaDoc on = Util.getObjectName(configPeer);
621             conn.setAttribute(on, modAttr);
622         } catch (RuntimeMBeanException JavaDoc e) {
623             Object JavaDoc [] params = new Object JavaDoc[] {attrName, e.getMessage()};
624             GUIUtils.showError(
625                 getLocalizedString("unexpected_setAttrConfigPeer_except", params));
626             modAttr = null;
627         } catch (Exception JavaDoc ex) {
628             Object JavaDoc [] params = new Object JavaDoc[] {attrName, ex.getMessage()};
629             GUIUtils.showError(
630                 getLocalizedString("unexpected_setAttrConfigPeer_except", params));
631             modAttr = null;
632         }
633         return modAttr;
634     }
635     
636     
637     /**
638      * Removes all system level modules on the Sun Java System
639      * Application Server.
640      *
641      * @param allModuleNames All the names of the app and/or web modules.
642      * @return All the non-system related modules.
643      */

644     protected static String JavaDoc[] stripOutSystemApps(String JavaDoc [] allModuleNames) {
645         final java.util.Vector JavaDoc nonSystemApps = new java.util.Vector JavaDoc();
646         for(int i = 0; i < allModuleNames.length; i++) {
647             if(allModuleNames[i].indexOf(ASADMIN_KEY) == -1
648                     && allModuleNames[i].indexOf(MEJB_SYS_APP) == -1
649                     && allModuleNames[i].indexOf(EJB_TIMER_SYS_APP) == -1
650                     && allModuleNames[i].indexOf(JWS_APPCLIENTS_KEY) == -1
651                     && ! allModuleNames[i].matches(SERVER_KEY) ) {
652                 nonSystemApps.add(allModuleNames[i]);
653             }
654         }
655         final String JavaDoc[] str = new String JavaDoc[nonSystemApps.size()];
656         return (String JavaDoc[]) nonSystemApps.toArray(str);
657     }
658     
659     
660     /**
661      * Removes all system level modules on the Sun Java System
662      * Application Server.
663      *
664      * @param allModules All the app and/or web modules.
665      * @return All the non-system related modules.
666      */

667     protected static Map JavaDoc stripOutSystemApps(final Map JavaDoc allModules) {
668         Map JavaDoc nonSystemApps = new HashMap JavaDoc();
669         for(Iterator JavaDoc itr = allModules.keySet().iterator(); itr.hasNext();) {
670             String JavaDoc moduleName = (String JavaDoc) itr.next();
671             if(moduleName.indexOf(ASADMIN_KEY) == -1
672                     && moduleName.indexOf(ADMIN_GUI_KEY) == -1
673                     && moduleName.indexOf(ADMIN_APP_KEY) == -1
674                     && moduleName.indexOf(WEB_UI_KEY) == -1
675                     && moduleName.indexOf(MEJB_SYS_APP) == -1
676                     && moduleName.indexOf(EJB_TIMER_SYS_APP) == -1
677                     && moduleName.indexOf(JWS_APPCLIENTS_KEY) == -1
678                     && moduleName.indexOf(ADMIN_GUI_KEY2) == -1
679                     && moduleName.indexOf(WSIT_KEY) == -1
680                     && ! moduleName.matches(SERVER_KEY)) {
681                 nonSystemApps.put(moduleName, allModules.get(moduleName));
682             }
683         }
684         return nonSystemApps;
685     }
686     
687     
688     /**
689      *
690      */

691     public static String JavaDoc getConfigPeerAMXType(final String JavaDoc j2eeType) {
692         return (String JavaDoc) j2eeTypeToConfigMap.get(j2eeType);
693     }
694     
695     
696     
697     /**
698       * Checks to see if the appserver is suspended in debug mode. If the
699       * server is suspended, then a popup error window is displayed and a
700       * java.lang.RuntimeException is thrown to avoid IDE hanging.
701       *
702       * @param dplymtMgr The DeploymentManager used to determining the status
703       * of the appserver.
704       */

705      public static void checkIfServerInDebugMode(final DeploymentManager JavaDoc dplymtMgr) {
706         SunDeploymentManagerInterface sunDpmtMgr =
707                 (SunDeploymentManagerInterface) dplymtMgr;
708         if(sunDpmtMgr != null) {
709             if(sunDpmtMgr.isSuspended()) {
710                 GUIUtils.showInformation(
711                     getLocalizedString("server_dbg_mode_notify"));
712                 throw new RuntimeException JavaDoc(
713                     getLocalizedString("server_dbg_mode_notify"));
714             }
715         }
716     }
717
718      
719     /**
720      *
721      *
722      */

723      public static Map JavaDoc getLogAttributes(final AMX config, final Map JavaDoc propNames, final MBeanServerConnection JavaDoc conn){
724             Map JavaDoc returnMap = new HashMap JavaDoc();
725             final ObjectName JavaDoc on = Util.getObjectName(config);
726             //get the attribute info
727
try {
728                 final MBeanInfo JavaDoc mi = conn.getMBeanInfo(on);
729                 final MBeanAttributeInfo JavaDoc[] mai = mi.getAttributes();
730                 for (int i = 0 ; i < mai.length ; i++) {
731                     if (mai[i] != null && propNames.containsKey(mai[i].getName())) {
732                         //get the attribute from the mbean
733
try {
734                             Object JavaDoc attrValue = conn.getAttribute(on, mai[i].getName());
735                             //put it in the return map indexed by attribute
736
returnMap.put(new Attribute JavaDoc(mai[i].getName(),
737                                     attrValue), mai[i]);
738                         } catch(javax.management.AttributeNotFoundException JavaDoc ex) {
739                             continue;
740                         }
741                     }
742                 }
743             } catch(Exception JavaDoc e) {
744                 logger.log(Level.FINE, e.getMessage(), e);
745             }
746             if(returnMap.size() == 0 || returnMap == null) {
747                 logger.log(Level.FINE, "The return Map in getAttrProperties is " +
748                         "size 0!");
749             }
750             return returnMap;
751      }
752      
753     /**
754      *
755      *
756      */

757     private static String JavaDoc getLocalizedString(final String JavaDoc bundleStrProp) {
758         return NbBundle.getMessage(ControllerUtil.class,
759                 bundleStrProp);
760     }
761     
762     
763     /**
764      *
765      *
766      */

767     private static String JavaDoc getLocalizedString(final String JavaDoc bundleStrProp,
768             final Object JavaDoc[] params) {
769         return NbBundle.getMessage(ControllerUtil.class,
770                 bundleStrProp, params);
771     }
772     
773     public static boolean isGlassFish(DeploymentManager JavaDoc dm){
774     //now test for AS 9 (J2EE 5.0) which should work for this plugin
775
File JavaDoc candidate = ((SunDeploymentManagerInterface)dm).getPlatformRoot();
776         File JavaDoc as9 = new File JavaDoc(candidate.getAbsolutePath()+
777                 "/lib/dtds/sun-web-app_2_5-0.dtd"); //NOI18N
778
return as9.exists();
779     }
780     
781     public static ObjectName JavaDoc[] getSubComponentsFromConfig(String JavaDoc modName, MBeanServerConnection JavaDoc conn){
782         String JavaDoc[] subComponents = new String JavaDoc[] {};
783         try{
784             ObjectName JavaDoc oName = new ObjectName JavaDoc(CONFIG_OBJ_NAME);
785             Object JavaDoc[] params = {modName};
786             String JavaDoc[] signature = {"java.lang.String"};
787             subComponents = (String JavaDoc[])conn.invoke(oName, "getModuleComponents", params, signature);
788         }catch(Exception JavaDoc ex){}
789         return convertToObjNames(subComponents);
790     }
791     
792     private static ObjectName JavaDoc[] convertToObjNames(String JavaDoc[] objNames){
793         try{
794             ObjectName JavaDoc[] compNames = new ObjectName JavaDoc[objNames.length];
795             for(int i=0; i<objNames.length; i++){
796                 ObjectName JavaDoc name = new ObjectName JavaDoc(objNames[i]);
797                 compNames[i] = name;
798             }
799             return compNames;
800         }catch(Exception JavaDoc ex){
801             return new ObjectName JavaDoc[0];
802         }
803     }
804         
805     private static void setResourceEnabled(ResourceConfig resConfig, Object JavaDoc value){
806         boolean val = Boolean.valueOf(value.toString()).booleanValue();
807         ResourceRefConfig config = getResourceRefConfig(resConfig);
808         if(config != null) {
809             config.setEnabled(val);
810         }
811     }
812     
813     private static void setModuleEnabled(ModuleConfig modConfig, Object JavaDoc value){
814         boolean val = Boolean.valueOf(value.toString()).booleanValue();
815         DeployedItemRefConfig config = getDeployedItemRefConfig(modConfig);
816         if(config != null) {
817             config.setEnabled(val);
818         }
819     }
820     
821     private static ResourceRefConfig getResourceRefConfig(AMX appConfig){
822         String JavaDoc appName = appConfig.getName();
823         ResourceRefConfig itemName = (ResourceRefConfig)getDASConfig(appConfig).getResourceRefConfigMap().get(appName);
824         return itemName;
825     }
826     
827     private static ServerConfig getDASConfig(AMX appConfig){
828         Map JavaDoc serverConfigs = getServerInstancesMap(appConfig);
829         ServerConfig serverConfig = (ServerConfig)serverConfigs.get(DAS_SERVER_NAME);
830         return serverConfig;
831     }
832     
833     public static Map JavaDoc modifyEnabledProperty(Map JavaDoc j2eeProps, AMX configPeer){
834         Map JavaDoc modProps = j2eeProps;
835         for(Iterator JavaDoc itr = j2eeProps.keySet().iterator(); itr.hasNext(); ) {
836             Attribute JavaDoc attr = (Attribute JavaDoc) itr.next();
837             if(attr.getName().equals(NodeTypes.ENABLED)){
838                 MBeanAttributeInfo JavaDoc info = (MBeanAttributeInfo JavaDoc) j2eeProps.get(attr);
839                 Boolean JavaDoc value = (Boolean JavaDoc)attr.getValue();
840                 boolean attrVal = calculateIsEnabled(configPeer, value.booleanValue());
841                 Attribute JavaDoc enabled = new Attribute JavaDoc(NodeTypes.ENABLED, Boolean.valueOf(attrVal));
842                 modProps.remove(attr);
843                 modProps.put(enabled, info);
844                 break;
845             }
846         }
847         return modProps;
848     }
849     
850     protected static boolean calculateIsEnabled(AMX appConfig, boolean configEnabled){
851         boolean isEnabled = configEnabled;
852         boolean refEnabled = configEnabled;
853         if(appConfig instanceof ResourceConfig) {
854             ResourceRefConfig itemName = getResourceRefConfig(appConfig);
855             if(itemName != null) {
856                 refEnabled = itemName.getEnabled();
857             }
858         } else {
859             DeployedItemRefConfig itemName = getDeployedItemRefConfig(appConfig);
860             if(itemName != null) {
861                 refEnabled = itemName.getEnabled();
862             }
863         }
864         if(!configEnabled || !refEnabled) {
865             isEnabled = false;
866         }
867         return isEnabled;
868     }
869     
870     protected static DeployedItemRefConfig getDeployedItemRefConfig(AMX appConfig){
871         String JavaDoc appName = appConfig.getName();
872         DeployedItemRefConfig itemName = (DeployedItemRefConfig)getDASConfig(appConfig).getDeployedItemRefConfigMap().get(appName);
873         return itemName;
874     }
875   
876     public static String JavaDoc[] getServerTargets(AMX amx){
877         return getComponentNamesFromMap(getServerInstancesMap(amx));
878     }
879     
880     public static Map JavaDoc getServerInstancesMap(AMX amx) {
881        Map JavaDoc serverConfigs = amx.getDomainRoot().getDomainConfig().getServerConfigMap();
882        return serverConfigs;
883     }
884     
885     public static Map JavaDoc getStandaloneServerInstancesMap(AMX amx) {
886        Map JavaDoc serverConfigs = amx.getDomainRoot().getDomainConfig().getStandaloneServerConfigMap();
887        return serverConfigs;
888     }
889 }
890
Popular Tags