KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > Extension


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.util;
20
21
22 import java.util.StringTokenizer JavaDoc;
23
24
25 /**
26  * Utility class that represents either an available "Optional Package"
27  * (formerly known as "Standard Extension") as described in the manifest
28  * of a JAR file, or the requirement for such an optional package. It is
29  * used to support the requirements of the Servlet Specification, version
30  * 2.3, related to providing shared extensions to all webapps.
31  * <p>
32  * In addition, static utility methods are available to scan a manifest
33  * and return an array of either available or required optional modules
34  * documented in that manifest.
35  * <p>
36  * For more information about optional packages, see the document
37  * <em>Optional Package Versioning</em> in the documentation bundle for your
38  * Java2 Standard Edition package, in file
39  * <code>guide/extensions/versioning.html</code>.
40  *
41  * @author Craig McClanahan
42  * @author Justyna Horwat
43  * @author Greg Murray
44  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
45  */

46
47 public final class Extension {
48
49
50     // ------------------------------------------------------------- Properties
51

52
53     /**
54      * The name of the optional package being made available, or required.
55      */

56     private String JavaDoc extensionName = null;
57     
58
59     public String JavaDoc getExtensionName() {
60         return (this.extensionName);
61     }
62
63     public void setExtensionName(String JavaDoc extensionName) {
64         this.extensionName = extensionName;
65     }
66
67     /**
68      * The URL from which the most recent version of this optional package
69      * can be obtained if it is not already installed.
70      */

71     private String JavaDoc implementationURL = null;
72
73     public String JavaDoc getImplementationURL() {
74         return (this.implementationURL);
75     }
76
77     public void setImplementationURL(String JavaDoc implementationURL) {
78         this.implementationURL = implementationURL;
79     }
80
81
82     /**
83      * The name of the company or organization that produced this
84      * implementation of this optional package.
85      */

86     private String JavaDoc implementationVendor = null;
87
88     public String JavaDoc getImplementationVendor() {
89         return (this.implementationVendor);
90     }
91
92     public void setImplementationVendor(String JavaDoc implementationVendor) {
93         this.implementationVendor = implementationVendor;
94     }
95
96
97     /**
98      * The unique identifier of the company that produced the optional
99      * package contained in this JAR file.
100      */

101     private String JavaDoc implementationVendorId = null;
102
103     public String JavaDoc getImplementationVendorId() {
104         return (this.implementationVendorId);
105     }
106
107     public void setImplementationVendorId(String JavaDoc implementationVendorId) {
108         this.implementationVendorId = implementationVendorId;
109     }
110
111
112     /**
113      * The version number (dotted decimal notation) for this implementation
114      * of the optional package.
115      */

116     private String JavaDoc implementationVersion = null;
117
118     public String JavaDoc getImplementationVersion() {
119         return (this.implementationVersion);
120     }
121
122     public void setImplementationVersion(String JavaDoc implementationVersion) {
123         this.implementationVersion = implementationVersion;
124     }
125
126
127     /**
128      * The name of the company or organization that originated the
129      * specification to which this optional package conforms.
130      */

131     private String JavaDoc specificationVendor = null;
132
133     public String JavaDoc getSpecificationVendor() {
134         return (this.specificationVendor);
135     }
136
137     public void setSpecificationVendor(String JavaDoc specificationVendor) {
138         this.specificationVendor = specificationVendor;
139     }
140
141
142     /**
143      * The version number (dotted decimal notation) of the specification
144      * to which this optional package conforms.
145      */

146     private String JavaDoc specificationVersion = null;
147
148     public String JavaDoc getSpecificationVersion() {
149         return (this.specificationVersion);
150     }
151
152     public void setSpecificationVersion(String JavaDoc specificationVersion) {
153         this.specificationVersion = specificationVersion;
154     }
155
156
157     /**
158      * fulfilled is true if all the required extension dependencies have been
159      * satisfied
160      */

161     private boolean fulfilled = false;
162
163     public void setFulfilled(boolean fulfilled) {
164         this.fulfilled = fulfilled;
165     }
166     
167     public boolean isFulfilled() {
168         return fulfilled;
169     }
170
171     // --------------------------------------------------------- Public Methods
172

173     /**
174      * Return <code>true</code> if the specified <code>Extension</code>
175      * (which represents an optional package required by this application)
176      * is satisfied by this <code>Extension</code> (which represents an
177      * optional package that is already installed. Otherwise, return
178      * <code>false</code>.
179      *
180      * @param required Extension of the required optional package
181      */

182     public boolean isCompatibleWith(Extension required) {
183
184         // Extension Name must match
185
if (extensionName == null)
186             return (false);
187         if (!extensionName.equals(required.getExtensionName()))
188             return (false);
189
190         // If specified, available specification version must be >= required
191
if (required.getSpecificationVersion() != null) {
192             if (!isNewer(specificationVersion,
193                          required.getSpecificationVersion()))
194                 return (false);
195         }
196
197         // If specified, Implementation Vendor ID must match
198
if (required.getImplementationVendorId() != null) {
199             if (implementationVendorId == null)
200                 return (false);
201             if (!implementationVendorId.equals(required
202                     .getImplementationVendorId()))
203                 return (false);
204         }
205
206         // If specified, Implementation version must be >= required
207
if (required.getImplementationVersion() != null) {
208             if (!isNewer(implementationVersion,
209                          required.getImplementationVersion()))
210                 return (false);
211         }
212
213         // This available optional package satisfies the requirements
214
return (true);
215
216     }
217
218     /**
219      * Return a String representation of this object.
220      */

221     public String JavaDoc toString() {
222
223         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Extension[");
224         sb.append(extensionName);
225         if (implementationURL != null) {
226             sb.append(", implementationURL=");
227             sb.append(implementationURL);
228         }
229         if (implementationVendor != null) {
230             sb.append(", implementationVendor=");
231             sb.append(implementationVendor);
232         }
233         if (implementationVendorId != null) {
234             sb.append(", implementationVendorId=");
235             sb.append(implementationVendorId);
236         }
237         if (implementationVersion != null) {
238             sb.append(", implementationVersion=");
239             sb.append(implementationVersion);
240         }
241         if (specificationVendor != null) {
242             sb.append(", specificationVendor=");
243             sb.append(specificationVendor);
244         }
245         if (specificationVersion != null) {
246             sb.append(", specificationVersion=");
247             sb.append(specificationVersion);
248         }
249         sb.append("]");
250         return (sb.toString());
251
252     }
253
254
255     // -------------------------------------------------------- Private Methods
256

257
258
259     /**
260      * Return <code>true</code> if the first version number is greater than
261      * or equal to the second; otherwise return <code>false</code>.
262      *
263      * @param first First version number (dotted decimal)
264      * @param second Second version number (dotted decimal)
265      *
266      * @exception NumberFormatException on a malformed version number
267      */

268     private boolean isNewer(String JavaDoc first, String JavaDoc second)
269         throws NumberFormatException JavaDoc {
270
271         if ((first == null) || (second == null))
272             return (false);
273         if (first.equals(second))
274             return (true);
275
276         StringTokenizer JavaDoc fTok = new StringTokenizer JavaDoc(first, ".", true);
277         StringTokenizer JavaDoc sTok = new StringTokenizer JavaDoc(second, ".", true);
278         int fVersion = 0;
279         int sVersion = 0;
280         while (fTok.hasMoreTokens() || sTok.hasMoreTokens()) {
281             if (fTok.hasMoreTokens())
282                 fVersion = Integer.parseInt(fTok.nextToken());
283             else
284                 fVersion = 0;
285             if (sTok.hasMoreTokens())
286                 sVersion = Integer.parseInt(sTok.nextToken());
287             else
288                 sVersion = 0;
289             if (fVersion < sVersion)
290                 return (false);
291             else if (fVersion > sVersion)
292                 return (true);
293             if (fTok.hasMoreTokens()) // Swallow the periods
294
fTok.nextToken();
295             if (sTok.hasMoreTokens())
296                 sTok.nextToken();
297         }
298
299         return (true); // Exact match
300

301     }
302
303
304 }
305
Popular Tags