KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > nodes > Util


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.jboss4.nodes;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
27 import javax.management.AttributeNotFoundException JavaDoc;
28 import javax.management.InstanceNotFoundException JavaDoc;
29 import javax.management.MBeanException JavaDoc;
30 import javax.management.MBeanServerConnection JavaDoc;
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.QueryExp JavaDoc;
34 import javax.management.ReflectionException JavaDoc;
35 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
36 import javax.xml.parsers.ParserConfigurationException JavaDoc;
37 import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager;
38 import org.openide.ErrorManager;
39 import org.openide.nodes.AbstractNode;
40 import org.openide.nodes.Children;
41 import org.openide.nodes.Node;
42 import org.openide.util.Lookup;
43 import org.openide.util.NbBundle;
44 import org.w3c.dom.Document JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46
47 /**
48  *
49  * @author Michal Mocnak
50  */

51 public class Util {
52     public static final String JavaDoc WAIT_NODE = "wait_node"; //NOI18N
53
public static final String JavaDoc INFO_NODE = "info_node"; //NOI18N
54

55     /**
56      * Lookup a JBoss4 RMI Adaptor
57      */

58     public static MBeanServerConnection JavaDoc getRMIServer(Lookup lookup) {
59         return getRMIServer((JBDeploymentManager)lookup.lookup(JBDeploymentManager.class));
60     }
61     
62     /**
63      * Lookup a JBoss4 RMI Adaptor
64      */

65     public static MBeanServerConnection JavaDoc getRMIServer(JBDeploymentManager manager) {
66         return manager.getRMIServer();
67     }
68     
69     /* Creates and returns the instance of the node
70      * representing the status 'WAIT' of the node.
71      * It is used when it spent more time to create elements hierarchy.
72      * @return the wait node.
73      */

74     public static Node createWaitNode() {
75         AbstractNode n = new AbstractNode(Children.LEAF);
76         n.setName(NbBundle.getMessage(JBItemNode.class, "LBL_WaitNode_DisplayName")); //NOI18N
77
n.setIconBaseWithExtension("org/openide/src/resources/wait.gif"); // NOI18N
78
return n;
79     }
80     
81     /* Creates and returns the instance of the node
82      * representing the status 'INFO' of the node.
83      * It is used when it spent more time to create elements hierarchy.
84      * @return the wait node.
85      */

86     public static Node createInfoNode() {
87         AbstractNode n = new AbstractNode(Children.LEAF);
88         n.setName(NbBundle.getMessage(JBItemNode.class, "LBL_InfoNode_DisplayName")); //NOI18N
89
n.setShortDescription(NbBundle.getMessage(JBItemNode.class, "LBL_InfoNode_ToolTip")); //NOI18N
90
n.setIconBaseWithExtension("org/netbeans/core/resources/exception.gif"); // NOI18N
91
return n;
92     }
93     
94     /*
95      * Checks if the Jboss installation has installed remote management package
96      *
97      * @return is remote management supported
98      */

99     public static boolean isRemoteManagementSupported(Lookup lookup) {
100         
101         try {
102             Object JavaDoc server = Util.getRMIServer(lookup);
103             ObjectName JavaDoc searchPattern;
104             searchPattern = new ObjectName JavaDoc("jboss.management.local:*");
105             Set JavaDoc managedObj = (Set JavaDoc)server.getClass().getMethod("queryMBeans", new Class JavaDoc[] {ObjectName JavaDoc.class, QueryExp JavaDoc.class}).invoke(server, new Object JavaDoc[] {searchPattern, null});
106             
107             if(managedObj.size() == 0)
108                 return false;
109         } catch (SecurityException JavaDoc ex) {
110             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
111         } catch (IllegalArgumentException JavaDoc ex) {
112             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
113         } catch (IllegalAccessException JavaDoc ex) {
114             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
115         } catch (InvocationTargetException JavaDoc ex) {
116             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
117         } catch (NoSuchMethodException JavaDoc ex) {
118             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
119         } catch (MalformedObjectNameException JavaDoc ex) {
120             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
121         } catch (NullPointerException JavaDoc ex) {
122             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
123         }
124         
125         return true;
126     }
127     
128     /*
129      * Checks if the specified object is deployed in JBoss Application Server
130      *
131      * @return if specified object is deployed
132      */

133     public static boolean isObjectDeployed(Object JavaDoc server, ObjectName JavaDoc searchPattern) {
134         try {
135             Set JavaDoc managedObj = (Set JavaDoc)server.getClass().getMethod("queryMBeans", new Class JavaDoc[] {ObjectName JavaDoc.class, QueryExp JavaDoc.class}).invoke(server, new Object JavaDoc[] {searchPattern, null});
136             
137             if(managedObj.size() > 0)
138                 return true;
139         } catch (IllegalArgumentException JavaDoc ex) {
140             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
141         } catch (SecurityException JavaDoc ex) {
142             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
143         } catch (InvocationTargetException JavaDoc ex) {
144             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
145         } catch (IllegalAccessException JavaDoc ex) {
146             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
147         } catch (NoSuchMethodException JavaDoc ex) {
148             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
149         }
150         
151         return false;
152     }
153     
154     /*
155      * It only returns string representation of the ModuleType (accorded to the JBoss JMX requirements)
156      *
157      * @return string representation of the ModuleType
158      */

159     public static String JavaDoc getModuleTypeString(ModuleType JavaDoc mt) {
160         if(mt.equals(ModuleType.EAR))
161             return "J2EEApplication";
162         else if(mt.equals(ModuleType.WAR))
163             return "WebModule";
164         else if(mt.equals(ModuleType.EJB))
165             return "EJBModule";
166         
167         return "undefined";
168     }
169     
170     /*
171      * Returns MBean attribute which you can specify via method parameters
172      *
173      * @return MBean attribute
174      */

175     public static Object JavaDoc getMBeanParameter(JBDeploymentManager dm, String JavaDoc name, String JavaDoc targetObject) {
176         MBeanServerConnection JavaDoc server = dm.refreshRMIServer();
177         try {
178             return server.getAttribute(new ObjectName JavaDoc(targetObject), name);
179         } catch (InstanceNotFoundException JavaDoc ex) {
180             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
181         } catch (AttributeNotFoundException JavaDoc ex) {
182             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
183         } catch (MalformedObjectNameException JavaDoc ex) {
184             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
185         } catch (NullPointerException JavaDoc ex) {
186             // it's normal behaviour when the server is not running
187
} catch (IllegalArgumentException JavaDoc ex) {
188             // it's normal behaviour when the server is not running
189
} catch (ReflectionException JavaDoc ex) {
190             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
191         } catch (MBeanException JavaDoc ex) {
192             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
193         } catch (IOException JavaDoc ex) {
194             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
195         }
196         
197         return null;
198     }
199     
200     /*
201      * Parse web application's deployment descriptor and returns context root
202      *
203      * @return context-root of web application
204      */

205     public static String JavaDoc getWebContextRoot(String JavaDoc dd) {
206         
207         if (dd == null) {
208             return null;
209         }
210         
211         Document JavaDoc doc = null;
212         
213         try {
214             doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(dd.getBytes()));
215         } catch (SAXException JavaDoc ex) {
216             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
217             return null;
218         } catch (IOException JavaDoc ex) {
219             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
220             return null;
221         } catch (ParserConfigurationException JavaDoc ex) {
222             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
223             return null;
224         }
225         
226         org.w3c.dom.Node JavaDoc node = doc.getElementsByTagName("context-root").item(0);
227         
228         return node.getTextContent();
229     }
230 }
231
Popular Tags