001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.loading;
023:
024: import java.util.*;
025:
026: /**
027: * Dataholder class used with MBean file parsers. Contains the information
028: * that at minimum should allow the MBean loaded and registered to the MBean
029: * server.
030: *
031: * @see org.jboss.mx.loading.MBeanFileParser
032: * @see org.jboss.mx.loading.MLetParser
033: * @see org.jboss.mx.loading.XMLMBeanParser
034: *
035: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
036: * @version $Revision: 57200 $
037: *
038: */
039: public class MBeanElement {
040: // Attributes ----------------------------------------------------
041:
042: // Constants -----------------------------------------------------
043:
044: /**
045: * Property key for setting the boolean value 'delegateToCLR' for MLet
046: * MBeans (JMX 1.2)
047: */
048: public final static String MLET_DELEGATE_TO_CLR = "delegateToCLR";
049:
050: /**
051: * Additional properties to be provided to the MBean installer/registration
052: * process. This map should contain properties that are not necessarily
053: * shared between all MBeans (for example, the delegateToCLR property for
054: * MLets).
055: */
056: private Map properties = new HashMap(2);
057:
058: /**
059: * Fully qualified class name.
060: */
061: private String code = null;
062:
063: /**
064: * Name of serialized MBean representation in the archive.
065: */
066: private String object = null;
067:
068: /**
069: * Object name
070: */
071: private String name = null;
072:
073: /**
074: * Overrides default codebase.
075: */
076: private String codebase = null;
077:
078: /**
079: * MBean jars.
080: */
081: private ArrayList archives = new ArrayList();
082:
083: /**
084: * spec only allows one version tag -- doesn't work very well with an archivelist.
085: */
086: private ArrayList versions = new ArrayList();
087:
088: /**
089: * MBean constructor argument types.
090: */
091: private ArrayList argTypes = new ArrayList();
092:
093: /**
094: * MBean constructor argument values.
095: */
096: private ArrayList argValues = new ArrayList();
097:
098: // Public --------------------------------------------------------
099:
100: /**
101: * Returns fully qualified class name of the MBean.
102: *
103: * @return class name or <tt>null</tt> if name not set
104: */
105: public String getCode() {
106: return code;
107: }
108:
109: /**
110: * Returns the name of a serialized MBean representation in the archive.
111: * Note that if the archive contains a file structure then the path to the
112: * serialized file is included in this string.
113: *
114: * @return serial file name or <tt>null</tt> if not set
115: */
116: public String getObject() {
117: return object;
118: }
119:
120: /**
121: * Returns the object name of the MBean.
122: *
123: * @return string representation of object name or <tt>null</tt> if not set
124: */
125: public String getName() {
126: return name;
127: }
128:
129: /**
130: * Returns MBean archives.
131: *
132: * @return a list of MBean Java archives. An empty list if archives is not set.
133: */
134: public List getArchives() {
135: return archives;
136: }
137:
138: /**
139: * Returns MBean versions.
140: *
141: * @return a list of MBean versions. An empty list if versions is not set.
142: */
143: public List getVersions() {
144: return versions;
145: }
146:
147: /**
148: * Returns MBean codebase URL.
149: *
150: * @return codebase or <tt>null</tt> if not set
151: */
152: public String getCodebase() {
153: return codebase;
154: }
155:
156: /**
157: * Sets the fully qualified class name of the MBean entry. The name is trimmed
158: * of quotes (") and additional equals (=) sign.
159: *
160: * @param code fully qualified class name of the MBean
161: */
162: public void setCode(String code) {
163: this .code = trim(code);
164: if (this .code.endsWith(".class"))
165: this .code = this .code.substring(0, this .code.length() - 6);
166: }
167:
168: /**
169: * Sets the name of the serialized MBean instance. Notice that if the archive
170: * contains a file structure its path must be included in the name. Tje name is
171: * trimmed of quotes (") and additional equals (=) sign.
172: *
173: * @param object file name and path in the archive
174: */
175: public void setObject(String object) {
176: this .object = trim(object);
177: }
178:
179: /**
180: * Sets the object name of the MBean. The name is trimmed of quotes (") and additional
181: * equals (=) sign.
182: *
183: * @param name string representation of an MBean object name
184: */
185: public void setName(String name) {
186: this .name = trim(name);
187: }
188:
189: /**
190: * Sets the code base for an MLET entry. The codebase is trimmed of quotes (") and
191: * additional equals (=) sign.
192: *
193: * @param url url string pointing to the codebase
194: */
195: public void setCodebase(String url) {
196: this .codebase = trim(url);
197: }
198:
199: public void setArchive(String archive) {
200: archive = trim(archive);
201: StringTokenizer tokenizer = new StringTokenizer(archive, " ,");
202:
203: while (tokenizer.hasMoreTokens())
204: archives.add(tokenizer.nextToken());
205: }
206:
207: public void setVersion(String version) {
208: version = trim(version);
209: StringTokenizer tokenizer = new StringTokenizer(version, " ,");
210:
211: while (tokenizer.hasMoreTokens())
212: versions.add(tokenizer.nextToken());
213: }
214:
215: public void addArg(String type, String value) {
216: argTypes.add(trim(type));
217: argValues.add(trim(value));
218: }
219:
220: public String[] getConstructorTypes() {
221: return (String[]) argTypes.toArray(new String[0]);
222: }
223:
224: public String[] getConstructorValues() {
225: return (String[]) argValues.toArray(new String[0]);
226: }
227:
228: // Private -------------------------------------------------------
229: private String trim(String str) {
230: if (str == null)
231: return str;
232:
233: // trim values that start with '="someValue"'
234: if (str.startsWith("="))
235: str = str.substring(1, str.length());
236:
237: if (str.startsWith("\"") && str.endsWith("\""))
238: return str.substring(1, str.length() - 1);
239: else
240: return str;
241: }
242:
243: public void setProperty(String key, Object value) {
244: if (key == null || key.equals(""))
245: throw new IllegalArgumentException(
246: "null or empty string keys not allowed");
247: if (value == null)
248: throw new IllegalArgumentException(
249: "null values not allowed");
250:
251: properties.put(key, value);
252: }
253:
254: public Object getProperty(String key) {
255: return properties.get(key);
256: }
257:
258: }
|