001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id$
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.ws.mbean;
025:
026: import java.util.Hashtable;
027: import java.util.Iterator;
028:
029: import javax.management.MalformedObjectNameException;
030: import javax.management.ObjectName;
031:
032: import org.objectweb.jonas.jmx.JmxService;
033: import org.objectweb.jonas.service.ServiceManager;
034:
035: /**
036: * ToolKit for WebServices Mbeans. It Helps to create ObjectNames for the
037: * WebServices MBeans, simplify MBeans queries, ...
038: *
039: * @author Guillaume Sauthier
040: */
041: public class WebServicesObjectName {
042:
043: /**
044: * WebService type property
045: */
046: public static final String WEBSERVICE_TYPE = "WebService";
047:
048: /**
049: * WebServicePortComponent type property
050: */
051: public static final String PORTCOMPONENT_TYPE = "WebServicePortComponent";
052:
053: /**
054: * WebServiceHandler type property
055: */
056: public static final String HANDLER_TYPE = "WebServiceHandler";
057:
058: /**
059: * j2eeType for EJB modules
060: */
061: public static final String EJBMODULE = "EJBModule";
062:
063: /**
064: * j2eeType for Web modules
065: */
066: public static final String WEBMODULE = "WebModule";
067:
068: /**
069: * j2eeType for ear
070: */
071: public static final String J2EEAPPLICATION = "J2EEApplication";
072:
073: /**
074: * j2eeType for server instance
075: */
076: public static final String J2EESERVER = "J2EEServer";
077:
078: /**
079: * domain name
080: */
081: private static String domain = null;
082:
083: /**
084: * Default private Constructor for tool class
085: */
086: private WebServicesObjectName() {
087: }
088:
089: /**
090: * @return Returns the domain name of the JOnAS server.
091: *
092: * TODO refactor this out (duplicated code from J2eeObjectName)
093: */
094: private static String getDomain() {
095: if (domain == null) {
096: try {
097: domain = ((JmxService) ServiceManager.getInstance()
098: .getJmxService()).getDomainName();
099: } catch (Exception e) {
100: // should never occurs
101: domain = null;
102: }
103: }
104: return domain;
105: }
106:
107: /**
108: * @param name
109: * base ObjectName
110: * @return Returns a String that contains "inherited" properties from
111: * parent's ObjectName
112: */
113: private static String getInheritedPropertiesAsString(ObjectName name) {
114: Hashtable table = (Hashtable) name.getKeyPropertyList().clone();
115: // we remove some attributes from the ObjectName
116: table.remove("j2eeType");
117: table.remove("type");
118: table.remove("subtype");
119: table.remove("name");
120: StringBuffer sb = new StringBuffer();
121: for (Iterator i = table.keySet().iterator(); i.hasNext();) {
122: String key = (String) i.next();
123: sb.append(key + "=" + table.get(key) + ",");
124: }
125: if (sb.length() > 1) {
126: // remove the trailing comma
127: sb.setLength(sb.length() - 1);
128: }
129: return sb.toString();
130: }
131:
132: /**
133: * @return ObjectName for Service
134: * @param name
135: * Service id
136: * @param parent
137: * parent name
138: * @exception MalformedObjectNameException
139: * Could not create ObjectName with the given String
140: */
141: public static ObjectName service(String name, ObjectName parent)
142: throws MalformedObjectNameException {
143: String parentModule = "";
144: if (parent.getKeyProperty("j2eeType").equals(EJBMODULE)) {
145: parentModule = EJBMODULE + "="
146: + parent.getKeyProperty("name");
147: } else {
148: parentModule = WEBMODULE + "="
149: + parent.getKeyProperty("name");
150: }
151: return ObjectName.getInstance(getDomain()
152: + ":type=WebService,name=" + name + "," + parentModule
153: + "," + getInheritedPropertiesAsString(parent));
154: }
155:
156: /**
157: * @return ObjectName for PortComponent
158: * @param name
159: * PortComponent id
160: * @param parent
161: * parent name
162: * @exception MalformedObjectNameException
163: * Could not create ObjectName with the given String
164: */
165: public static ObjectName portComponent(String name,
166: ObjectName parent) throws MalformedObjectNameException {
167: String parentModule = "";
168: if (parent.getKeyProperty("type").equals(WEBSERVICE_TYPE)) {
169: parentModule = WEBSERVICE_TYPE + "="
170: + parent.getKeyProperty("name");
171: }
172: return ObjectName.getInstance(getDomain() + ":type="
173: + PORTCOMPONENT_TYPE + ",name=" + name + ","
174: + parentModule + ","
175: + 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 handler(String name, ObjectName parent)
186: throws MalformedObjectNameException {
187: String parentModule = "";
188: if (parent.getKeyProperty("type").equals(PORTCOMPONENT_TYPE)) {
189: parentModule = PORTCOMPONENT_TYPE + "="
190: + parent.getKeyProperty("name");
191: }
192: return ObjectName.getInstance(getDomain() + ":type="
193: + HANDLER_TYPE + ",name=" + name + "," + parentModule
194: + "," + 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 getStatelessSessionBeanQuery(
204: ObjectName parent, String ejbName) {
205: try {
206: StringBuffer sb = new StringBuffer(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(sb.toString());
221: } catch (javax.management.MalformedObjectNameException 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 getServletQuery(ObjectName parent,
234: String servletName) {
235: try {
236: StringBuffer sb = new StringBuffer(parent.getDomain());
237: sb.append(":j2eeType=Servlet");
238: sb.append(",name=");
239: sb.append(servletName);
240: sb.append(",J2EEApplication=");
241: if (parent.getKeyProperty("J2EEApplication") != null) {
242: // parent MBean maybe is not a J2EEApplication, but an WebModule
243: sb.append(parent.getKeyProperty("J2EEApplication"));
244: } else {
245: sb.append(parent.getKeyProperty("name"));
246: }
247: sb.append(",J2EEServer=");
248: sb.append(parent.getKeyProperty("J2EEServer"));
249: sb.append(",*");
250: return new ObjectName(sb.toString());
251: } catch (javax.management.MalformedObjectNameException e) {
252: // this should never occur
253: return null;
254: }
255: }
256:
257: /**
258: * @param ear parent J2EEApplication's ObjectName
259: * @param key EJBModule name
260: * @return Returns a Query ObjectName used to find an EJBModule
261: * coming from a particular J2EEApplication
262: */
263: public static ObjectName getEJBModule(ObjectName ear, String key) {
264: try {
265: StringBuffer sb = new StringBuffer(ear.getDomain());
266: sb.append(":j2eeType=EJBModule");
267: sb.append(",name=");
268: sb.append(key);
269: sb.append(",J2EEApplication=");
270: sb.append(ear.getKeyProperty("name"));
271: sb.append(",J2EEServer=");
272: sb.append(ear.getKeyProperty("J2EEServer"));
273: sb.append(",*");
274: return new ObjectName(sb.toString());
275: } catch (javax.management.MalformedObjectNameException e) {
276: // this should never occur
277: return null;
278: }
279: }
280:
281: /**
282: * @param ear parent J2EEApplication's ObjectName
283: * @param key WebModule name
284: * @return Returns a Query ObjectName used to find an WebModule
285: * coming from a particular J2EEApplication
286: */
287: public static ObjectName getWebModule(ObjectName ear, String key) {
288: try {
289: StringBuffer sb = new StringBuffer(ear.getDomain());
290: sb.append(":j2eeType=WebModule");
291: sb.append(",name=");
292: sb.append(key);
293: sb.append(",J2EEApplication=");
294: sb.append(ear.getKeyProperty("name"));
295: sb.append(",J2EEServer=");
296: sb.append(ear.getKeyProperty("J2EEServer"));
297: sb.append(",*");
298: return new ObjectName(sb.toString());
299: } catch (javax.management.MalformedObjectNameException e) {
300: // this should never occur
301: return null;
302: }
303: }
304: }
|