KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ws > mbean > WebServicesObjectName


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id$
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.ws.mbean;
26
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import javax.management.MalformedObjectNameException JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32
33 import org.objectweb.jonas.jmx.JmxService;
34 import org.objectweb.jonas.service.ServiceManager;
35
36 /**
37  * ToolKit for WebServices Mbeans. It Helps to create ObjectNames for the
38  * WebServices MBeans, simplify MBeans queries, ...
39  *
40  * @author Guillaume Sauthier
41  */

42 public class WebServicesObjectName {
43
44     /**
45      * WebService type property
46      */

47     public static final String JavaDoc WEBSERVICE_TYPE = "WebService";
48
49     /**
50      * WebServicePortComponent type property
51      */

52     public static final String JavaDoc PORTCOMPONENT_TYPE = "WebServicePortComponent";
53
54     /**
55      * WebServiceHandler type property
56      */

57     public static final String JavaDoc HANDLER_TYPE = "WebServiceHandler";
58
59     /**
60      * j2eeType for EJB modules
61      */

62     public static final String JavaDoc EJBMODULE = "EJBModule";
63
64     /**
65      * j2eeType for Web modules
66      */

67     public static final String JavaDoc WEBMODULE = "WebModule";
68
69     /**
70      * j2eeType for ear
71      */

72     public static final String JavaDoc J2EEAPPLICATION = "J2EEApplication";
73
74     /**
75      * j2eeType for server instance
76      */

77     public static final String JavaDoc J2EESERVER = "J2EEServer";
78
79     /**
80      * domain name
81      */

82     private static String JavaDoc domain = null;
83
84     /**
85      * Default private Constructor for tool class
86      */

87     private WebServicesObjectName() {
88     }
89
90     /**
91      * @return Returns the domain name of the JOnAS server.
92      *
93      * TODO refactor this out (duplicated code from J2eeObjectName)
94      */

95     private static String JavaDoc getDomain() {
96         if (domain == null) {
97             try {
98                 domain = ((JmxService) ServiceManager.getInstance()
99                         .getJmxService()).getJmxServer().getDefaultDomain();
100             } catch (Exception JavaDoc e) {
101                 // should never occurs
102
domain = null;
103             }
104         }
105         return domain;
106     }
107
108     /**
109      * @param name
110      * base ObjectName
111      * @return Returns a String that contains "inherited" properties from
112      * parent's ObjectName
113      */

114     private static String JavaDoc getInheritedPropertiesAsString(ObjectName JavaDoc name) {
115         Hashtable JavaDoc table = (Hashtable JavaDoc) name.getKeyPropertyList().clone();
116         // we remove some attributes from the ObjectName
117
table.remove("j2eeType");
118         table.remove("type");
119         table.remove("subtype");
120         table.remove("name");
121         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
122         for (Iterator JavaDoc i = table.keySet().iterator(); i.hasNext();) {
123             String JavaDoc key = (String JavaDoc) i.next();
124             sb.append(key + "=" + table.get(key) + ",");
125         }
126         if (sb.length() > 1) {
127             // remove the trailing comma
128
sb.setLength(sb.length() - 1);
129         }
130         return sb.toString();
131     }
132
133     /**
134      * @return ObjectName for Service
135      * @param name
136      * Service id
137      * @param parent
138      * parent name
139      * @exception MalformedObjectNameException
140      * Could not create ObjectName with the given String
141      */

142     public static ObjectName JavaDoc service(String JavaDoc name, ObjectName JavaDoc parent)
143             throws MalformedObjectNameException JavaDoc {
144         String JavaDoc parentModule = "";
145         if (parent.getKeyProperty("j2eeType").equals(EJBMODULE)) {
146             parentModule = EJBMODULE + "="
147                     + parent.getKeyProperty("name");
148         } else {
149             parentModule = WEBMODULE + "="
150                     + parent.getKeyProperty("name");
151         }
152         return ObjectName.getInstance(getDomain()
153                 + ":type=WebService,name=" + name + "," + parentModule + ","
154                 + getInheritedPropertiesAsString(parent));
155     }
156
157     /**
158      * @return ObjectName for PortComponent
159      * @param name
160      * PortComponent id
161      * @param parent
162      * parent name
163      * @exception MalformedObjectNameException
164      * Could not create ObjectName with the given String
165      */

166     public static ObjectName JavaDoc portComponent(String JavaDoc name, ObjectName JavaDoc parent)
167             throws MalformedObjectNameException JavaDoc {
168         String JavaDoc parentModule = "";
169         if (parent.getKeyProperty("type").equals(WEBSERVICE_TYPE)) {
170             parentModule = WEBSERVICE_TYPE + "="
171                     + parent.getKeyProperty("name");
172         }
173         return ObjectName.getInstance(getDomain()
174                 + ":type=" + PORTCOMPONENT_TYPE + ",name=" + name + ","
175                 + parentModule + "," + getInheritedPropertiesAsString(parent));
176     }
177
178     /**
179      * @return Returns the ObjectName for PortComponent
180      * @param name PortComponent id
181      * @param parent parent name
182      * @exception MalformedObjectNameException
183      * Could not create ObjectName with the given String
184      */

185     public static ObjectName JavaDoc handler(String JavaDoc name, ObjectName JavaDoc parent)
186             throws MalformedObjectNameException JavaDoc {
187         String JavaDoc parentModule = "";
188         if (parent.getKeyProperty("type").equals(PORTCOMPONENT_TYPE)) {
189             parentModule = PORTCOMPONENT_TYPE + "="
190                     + parent.getKeyProperty("name");
191         }
192         return ObjectName.getInstance(getDomain()
193                 + ":type=" + HANDLER_TYPE + ",name=" + name + ","
194                 + parentModule + "," + getInheritedPropertiesAsString(parent));
195     }
196
197     /**
198      * @param parent parent ObjectName (J2EEApplication or J2EEModule subtype)
199      * @param ejbName SSB name
200      * @return Returns a Query ObjectName used to find a StatelessSessionBean
201      * coming from a particular J2EEApplication/J2EEModule
202      */

203     public static ObjectName JavaDoc getStatelessSessionBeanQuery(ObjectName JavaDoc parent,
204             String JavaDoc ejbName) {
205         try {
206             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(parent.getDomain());
207             sb.append(":j2eeType=StatelessSessionBean");
208             sb.append(",name=");
209             sb.append(ejbName);
210             sb.append(",J2EEApplication=");
211             if (parent.getKeyProperty("J2EEApplication") != null) {
212                 // parent MBean maybe is not a J2EEApplication, but an EJBModule
213
sb.append(parent.getKeyProperty("J2EEApplication"));
214             } else {
215                 sb.append(parent.getKeyProperty("name"));
216             }
217             sb.append(",J2EEServer=");
218             sb.append(parent.getKeyProperty("J2EEServer"));
219             sb.append(",*");
220             return new ObjectName JavaDoc(sb.toString());
221         } catch (javax.management.MalformedObjectNameException JavaDoc e) {
222             // this should never occur
223
return null;
224         }
225     }
226
227     /**
228      * @param parent parent J2EEApplication's ObjectName
229      * @param servletName servlet name
230      * @return Returns a Query ObjectName used to find a Servlet
231      * coming from a particular J2EEApplication
232      */

233     public static ObjectName JavaDoc getServletQuery(ObjectName JavaDoc parent, String JavaDoc servletName) {
234         try {
235             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(parent.getDomain());
236             sb.append(":j2eeType=Servlet");
237             sb.append(",name=");
238             sb.append(servletName);
239             sb.append(",J2EEApplication=");
240             if (parent.getKeyProperty("J2EEApplication") != null) {
241                 // parent MBean maybe is not a J2EEApplication, but an WebModule
242
sb.append(parent.getKeyProperty("J2EEApplication"));
243             } else {
244                 sb.append(parent.getKeyProperty("name"));
245             }
246             sb.append(",J2EEServer=");
247             sb.append(parent.getKeyProperty("J2EEServer"));
248             sb.append(",*");
249             return new ObjectName JavaDoc(sb.toString());
250         } catch (javax.management.MalformedObjectNameException JavaDoc e) {
251             // this should never occur
252
return null;
253         }
254     }
255
256     /**
257      * @param ear parent J2EEApplication's ObjectName
258      * @param key EJBModule name
259      * @return Returns a Query ObjectName used to find an EJBModule
260      * coming from a particular J2EEApplication
261      */

262     public static ObjectName JavaDoc getEJBModule(ObjectName JavaDoc ear, String JavaDoc key) {
263         try {
264             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(ear.getDomain());
265             sb.append(":j2eeType=EJBModule");
266             sb.append(",name=");
267             sb.append(key);
268             sb.append(",J2EEApplication=");
269             sb.append(ear.getKeyProperty("name"));
270             sb.append(",J2EEServer=");
271             sb.append(ear.getKeyProperty("J2EEServer"));
272             sb.append(",*");
273             return new ObjectName JavaDoc(sb.toString());
274         } catch (javax.management.MalformedObjectNameException JavaDoc e) {
275             // this should never occur
276
return null;
277         }
278     }
279
280     /**
281      * @param ear parent J2EEApplication's ObjectName
282      * @param key WebModule name
283      * @return Returns a Query ObjectName used to find an WebModule
284      * coming from a particular J2EEApplication
285      */

286     public static ObjectName JavaDoc getWebModule(ObjectName JavaDoc ear, String JavaDoc key) {
287         try {
288             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(ear.getDomain());
289             sb.append(":j2eeType=WebModule");
290             sb.append(",name=");
291             sb.append(key);
292             sb.append(",J2EEApplication=");
293             sb.append(ear.getKeyProperty("name"));
294             sb.append(",J2EEServer=");
295             sb.append(ear.getKeyProperty("J2EEServer"));
296             sb.append(",*");
297             return new ObjectName JavaDoc(sb.toString());
298         } catch (javax.management.MalformedObjectNameException JavaDoc e) {
299             // this should never occur
300
return null;
301         }
302     }
303 }
304
Popular Tags