001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.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: JSR77ManagementIdentifier.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.jsr77;
025:
026: import java.net.URL;
027: import java.util.Hashtable;
028: import java.util.Iterator;
029:
030: import javax.management.MalformedObjectNameException;
031: import javax.management.ObjectName;
032:
033: import org.ow2.easybeans.api.jmx.EZBManagementIdentifier;
034: import org.ow2.util.log.Log;
035: import org.ow2.util.log.LogFactory;
036:
037: /**
038: * Specialized {@link EZBManagementIdentifier} for JSR77 MBeans.
039: * @author Guillaume Sauthier
040: * @author Florent BENOIT
041: * @param <T> Managed Type
042: */
043: public abstract class JSR77ManagementIdentifier<T> implements
044: EZBManagementIdentifier<T> {
045:
046: /**
047: * Default domain name.
048: */
049: private static final String DEFAULT_DOMAIN_NAME = "";
050:
051: /**
052: * Logger.
053: */
054: private static Log logger = LogFactory
055: .getLog(JSR77ManagementIdentifier.class);
056:
057: /**
058: * Domain name.
059: */
060: private String domainName = null;
061:
062: /**
063: * Server name.
064: */
065: private String serverName = null;
066:
067: /**
068: * Empty default constructor.
069: */
070: protected JSR77ManagementIdentifier() {
071: }
072:
073: /**
074: * @param name base ObjectName
075: * @return Returns a String that contains "inherited" properties from
076: * parent's ObjectName
077: */
078: @SuppressWarnings("unchecked")
079: protected static String getInheritedPropertiesAsString(
080: final ObjectName name) {
081: Hashtable<String, ?> table = (Hashtable<String, ?>) name
082: .getKeyPropertyList().clone();
083: // we remove some attributes from the ObjectName
084: table.remove("j2eeType");
085: table.remove("type");
086: table.remove("subtype");
087: table.remove("name");
088: StringBuffer sb = new StringBuffer();
089: Iterator<String> it = table.keySet().iterator();
090: while (it.hasNext()) {
091: String key = it.next();
092: sb.append(key + "=" + table.get(key) + ",");
093: }
094: if (sb.length() > 1) {
095: // remove the trailing comma
096: sb.setLength(sb.length() - 1);
097: }
098: return sb.toString();
099: }
100:
101: /**
102: * @param parentObjectName Parent ObjectName.
103: * @return Returns the couple j2eetype=name of the parent ObjectName.
104: */
105: protected static String getParentNameProperty(
106: final String parentObjectName) {
107: ObjectName on = null;
108: try {
109: on = ObjectName.getInstance(parentObjectName);
110: } catch (MalformedObjectNameException e) {
111: logger.error("Cannot get objectname on {0}",
112: parentObjectName, e);
113: return "";
114: } catch (NullPointerException e) {
115: logger.error("Cannot get objectname on {0}",
116: parentObjectName, e);
117: return "";
118: }
119:
120: String type = on.getKeyProperty("j2eeType");
121: String name = on.getKeyProperty("name");
122:
123: return type + "=" + name;
124: }
125:
126: /**
127: * @param objectName given ObjectName.
128: * @return the coupe property=value with value which is the value stored in the objectName
129: */
130: protected static String getPropertyNameValue(
131: final String objectName, final String property) {
132: ObjectName on = null;
133: try {
134: on = ObjectName.getInstance(objectName);
135: } catch (MalformedObjectNameException e) {
136: logger.error("Cannot get objectname on {0}", objectName, e);
137: return "";
138: } catch (NullPointerException e) {
139: logger.error("Cannot get objectname on {0}", objectName, e);
140: return "";
141: }
142:
143: String value = on.getKeyProperty(property);
144: return property + "=" + value;
145: }
146:
147: /**
148: * @return Returns the JMX Domain name of the MBean.
149: */
150: public String getDomain() {
151: if (domainName == null) {
152: return DEFAULT_DOMAIN_NAME;
153: }
154: return domainName;
155: }
156:
157: /**
158: * Sets the domain for this identifier.
159: * @param domainName the JMX Domain name of the MBean.
160: */
161: public void setDomain(final String domainName) {
162: this .domainName = domainName;
163: }
164:
165: /**
166: * @return the JMX Server name of the MBean.
167: */
168: public String getServerName() {
169: return serverName;
170: }
171:
172: /**
173: * Sets the Server name for this identifier.
174: * @param serverName the JMX Server name of this MBean.
175: */
176: public void setServerName(final String serverName) {
177: this .serverName = serverName;
178: }
179:
180: /**
181: * {@inheritDoc}
182: */
183: public String getTypeName() {
184: return "j2eeType";
185: }
186:
187: /**
188: * {@inheritDoc}
189: */
190: public String getTypeProperty() {
191: return getTypeName() + "=" + getTypeValue();
192: }
193:
194: /**
195: * @return the logger
196: */
197: public static final Log getLogger() {
198: return logger;
199: }
200:
201: /**
202: * @return the String for J2EEServer.
203: */
204: protected String getJ2EEServerString() {
205: return "J2EEServer=" + serverName;
206: }
207:
208: /**
209: * For a given URL, make a shorter name.
210: * @param url the given URL
211: * @return a short name for the given URL
212: */
213: public String shorterName(final URL url) {
214:
215: String urlExternalForm = url.toExternalForm();
216:
217: // if it ends by a /, remove this last one
218: if (urlExternalForm.charAt(urlExternalForm.length() - 1) == '/') {
219: urlExternalForm = urlExternalForm.substring(0,
220: urlExternalForm.length() - 1);
221: }
222:
223: // get string after the last / (all URL have this, no need to test if
224: // there is a / character)
225: int slashPos = urlExternalForm.lastIndexOf('/');
226: String shortName = urlExternalForm.substring(slashPos + 1,
227: urlExternalForm.length());
228: int dotPos = shortName.lastIndexOf('.');
229: if (dotPos != -1) {
230: shortName = shortName.substring(0, dotPos);
231: }
232: return shortName;
233: }
234: }
|