KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > loading > MLetTag


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.loading;
10
11 import java.net.MalformedURLException JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16
17 /**
18  * Represents an MLET tag, as documented in the JMX specification.
19  *
20  * @version $Revision: 1.6 $
21  */

22 public class MLetTag
23 {
24    private String JavaDoc code;
25    private String JavaDoc object;
26    private String JavaDoc archive;
27    private String JavaDoc codebase;
28    private ObjectName JavaDoc objectName;
29    private String JavaDoc version;
30    private ArrayList JavaDoc signature = new ArrayList JavaDoc();
31    private ArrayList JavaDoc arguments = new ArrayList JavaDoc();
32
33    /**
34     * Normalizes the codebase held by this MLetTag (specified in the MLet file) using the
35     * URL of the MLet file as default.
36     * This means that if the codebase in the MLet file is not provided or it is relative, then
37     * the URL of the MLet file will be taken as base for computing the normalized codebase;
38     * otherwise, if a full URL has been specified as codebase in the MLet file, that URL is taken
39     * and the URL of the MLet file is discarded.
40     *
41     * @param mletFileURL The URL of the MLet file
42     * @return The normalized codebase
43     */

44    public URL JavaDoc normalizeCodeBase(URL JavaDoc mletFileURL)
45    {
46       // If the codebase specified in the MLet file is relative, or null,
47
// then the codebase is the one of the mletFileURL, otherwise
48
// the given codebase must be used.
49

50       URL JavaDoc codebaseURL = null;
51       String JavaDoc codebase = getCodeBase();
52       if (codebase != null)
53       {
54          // Try to see if it's a URL
55
try
56          {
57             codebaseURL = new URL JavaDoc(codebase);
58          }
59          catch (MalformedURLException JavaDoc ignored)
60          {
61             // Not a complete URL, use the mletFileURL as a base
62
try
63             {
64                codebaseURL = new URL JavaDoc(mletFileURL, codebase);
65             }
66             catch (MalformedURLException JavaDoc alsoIgnored)
67             {
68             }
69          }
70       }
71
72       // Either codebase not provided or failed to be created, use the mletFileURL
73
if (codebaseURL == null)
74       {
75          String JavaDoc path = mletFileURL.getPath();
76          int index = path.lastIndexOf('/');
77
78          try
79          {
80             codebaseURL = new URL JavaDoc(mletFileURL, path.substring(0, index + 1));
81          }
82          catch (MalformedURLException JavaDoc ignored)
83          {
84             // Cannot fail, we just remove the mlet file name from the path
85
// leaving the directory where it resides as a codebase
86
}
87       }
88       return codebaseURL;
89    }
90
91    /**
92     * Returns the jars file names specified in the ARCHIVE attribute of the MLet tag.
93     */

94    public String JavaDoc[] parseArchive()
95    {
96       String JavaDoc archive = getArchive();
97       ArrayList JavaDoc archives = new ArrayList JavaDoc();
98       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(archive, ",");
99       while (tokenizer.hasMoreTokens())
100       {
101          String JavaDoc token = tokenizer.nextToken().trim();
102          if (token.length() > 0)
103          {
104             token = token.replace('\\', '/');
105             archives.add(token);
106          }
107       }
108       return (String JavaDoc[])archives.toArray(new String JavaDoc[0]);
109    }
110
111    /**
112     * Returns the URL for the given archive file name using the provided URL as a codebase,
113     * or null if the URL cannot be created.
114     */

115    public URL JavaDoc createArchiveURL(URL JavaDoc codebase, String JavaDoc archive)
116    {
117       try
118       {
119          return new URL JavaDoc(codebase, archive);
120       }
121       catch (MalformedURLException JavaDoc ignored)
122       {
123       }
124       return null;
125    }
126
127    public String JavaDoc getVersion()
128    {
129       return version;
130    }
131
132    public String JavaDoc getCodeBase()
133    {
134       return codebase;
135    }
136
137    public String JavaDoc getArchive()
138    {
139       return archive;
140    }
141
142    public String JavaDoc getCode()
143    {
144       return code;
145    }
146
147    public ObjectName JavaDoc getObjectName()
148    {
149       return objectName;
150    }
151
152    public String JavaDoc getObject()
153    {
154       return object;
155    }
156
157    public String JavaDoc[] getSignature()
158    {
159       return signature == null ? new String JavaDoc[0] : (String JavaDoc[])signature.toArray(new String JavaDoc[signature.size()]);
160    }
161
162    public Object JavaDoc[] getArguments()
163    {
164       return arguments == null ? new Object JavaDoc[0] : (Object JavaDoc[])arguments.toArray(new Object JavaDoc[arguments.size()]);
165    }
166
167    //
168
// Setters, called by MLetParser
169
//
170

171    void setArchive(String JavaDoc archive)
172    {
173       this.archive = archive;
174    }
175
176    void setCode(String JavaDoc code)
177    {
178       this.code = code;
179    }
180
181    void setCodeBase(String JavaDoc codebase)
182    {
183       // Important that the codebase ends with a slash, see usages of getCodeBase()
184

185       codebase = codebase.replace('\\', '/');
186       if (!codebase.endsWith("/")) codebase += "/";
187       this.codebase = codebase;
188    }
189
190    void setName(ObjectName JavaDoc name)
191    {
192       objectName = name;
193    }
194
195    void setObject(String JavaDoc object)
196    {
197       this.object = object;
198    }
199
200    void setVersion(String JavaDoc version)
201    {
202       this.version = version;
203    }
204
205    void addArg(String JavaDoc type, Object JavaDoc value)
206    {
207       signature.add(type);
208       arguments.add(value);
209    }
210 }
211
Popular Tags