001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package mx4j.loading;
010:
011: import java.net.MalformedURLException;
012: import java.net.URL;
013: import java.util.ArrayList;
014: import java.util.StringTokenizer;
015: import javax.management.ObjectName;
016:
017: /**
018: * Represents an MLET tag, as documented in the JMX specification.
019: *
020: * @version $Revision: 1.6 $
021: */
022: public class MLetTag {
023: private String code;
024: private String object;
025: private String archive;
026: private String codebase;
027: private ObjectName objectName;
028: private String version;
029: private ArrayList signature = new ArrayList();
030: private ArrayList arguments = new ArrayList();
031:
032: /**
033: * Normalizes the codebase held by this MLetTag (specified in the MLet file) using the
034: * URL of the MLet file as default.
035: * This means that if the codebase in the MLet file is not provided or it is relative, then
036: * the URL of the MLet file will be taken as base for computing the normalized codebase;
037: * otherwise, if a full URL has been specified as codebase in the MLet file, that URL is taken
038: * and the URL of the MLet file is discarded.
039: *
040: * @param mletFileURL The URL of the MLet file
041: * @return The normalized codebase
042: */
043: public URL normalizeCodeBase(URL mletFileURL) {
044: // If the codebase specified in the MLet file is relative, or null,
045: // then the codebase is the one of the mletFileURL, otherwise
046: // the given codebase must be used.
047:
048: URL codebaseURL = null;
049: String codebase = getCodeBase();
050: if (codebase != null) {
051: // Try to see if it's a URL
052: try {
053: codebaseURL = new URL(codebase);
054: } catch (MalformedURLException ignored) {
055: // Not a complete URL, use the mletFileURL as a base
056: try {
057: codebaseURL = new URL(mletFileURL, codebase);
058: } catch (MalformedURLException alsoIgnored) {
059: }
060: }
061: }
062:
063: // Either codebase not provided or failed to be created, use the mletFileURL
064: if (codebaseURL == null) {
065: String path = mletFileURL.getPath();
066: int index = path.lastIndexOf('/');
067:
068: try {
069: codebaseURL = new URL(mletFileURL, path.substring(0,
070: index + 1));
071: } catch (MalformedURLException ignored) {
072: // Cannot fail, we just remove the mlet file name from the path
073: // leaving the directory where it resides as a codebase
074: }
075: }
076: return codebaseURL;
077: }
078:
079: /**
080: * Returns the jars file names specified in the ARCHIVE attribute of the MLet tag.
081: */
082: public String[] parseArchive() {
083: String archive = getArchive();
084: ArrayList archives = new ArrayList();
085: StringTokenizer tokenizer = new StringTokenizer(archive, ",");
086: while (tokenizer.hasMoreTokens()) {
087: String token = tokenizer.nextToken().trim();
088: if (token.length() > 0) {
089: token = token.replace('\\', '/');
090: archives.add(token);
091: }
092: }
093: return (String[]) archives.toArray(new String[0]);
094: }
095:
096: /**
097: * Returns the URL for the given archive file name using the provided URL as a codebase,
098: * or null if the URL cannot be created.
099: */
100: public URL createArchiveURL(URL codebase, String archive) {
101: try {
102: return new URL(codebase, archive);
103: } catch (MalformedURLException ignored) {
104: }
105: return null;
106: }
107:
108: public String getVersion() {
109: return version;
110: }
111:
112: public String getCodeBase() {
113: return codebase;
114: }
115:
116: public String getArchive() {
117: return archive;
118: }
119:
120: public String getCode() {
121: return code;
122: }
123:
124: public ObjectName getObjectName() {
125: return objectName;
126: }
127:
128: public String getObject() {
129: return object;
130: }
131:
132: public String[] getSignature() {
133: return signature == null ? new String[0] : (String[]) signature
134: .toArray(new String[signature.size()]);
135: }
136:
137: public Object[] getArguments() {
138: return arguments == null ? new Object[0] : (Object[]) arguments
139: .toArray(new Object[arguments.size()]);
140: }
141:
142: //
143: // Setters, called by MLetParser
144: //
145:
146: void setArchive(String archive) {
147: this .archive = archive;
148: }
149:
150: void setCode(String code) {
151: this .code = code;
152: }
153:
154: void setCodeBase(String codebase) {
155: // Important that the codebase ends with a slash, see usages of getCodeBase()
156:
157: codebase = codebase.replace('\\', '/');
158: if (!codebase.endsWith("/"))
159: codebase += "/";
160: this .codebase = codebase;
161: }
162:
163: void setName(ObjectName name) {
164: objectName = name;
165: }
166:
167: void setObject(String object) {
168: this .object = object;
169: }
170:
171: void setVersion(String version) {
172: this .version = version;
173: }
174:
175: void addArg(String type, Object value) {
176: signature.add(type);
177: arguments.add(value);
178: }
179: }
|