001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.util;
019:
020: import java.util.StringTokenizer;
021:
022: /**
023: * Utility class that represents either an available "Optional Package"
024: * (formerly known as "Standard Extension") as described in the manifest
025: * of a JAR file, or the requirement for such an optional package. It is
026: * used to support the requirements of the Servlet Specification, version
027: * 2.3, related to providing shared extensions to all webapps.
028: * <p>
029: * In addition, static utility methods are available to scan a manifest
030: * and return an array of either available or required optional modules
031: * documented in that manifest.
032: * <p>
033: * For more information about optional packages, see the document
034: * <em>Optional Package Versioning</em> in the documentation bundle for your
035: * Java2 Standard Edition package, in file
036: * <code>guide/extensions/versioning.html</code>.
037: *
038: * @author Craig McClanahan
039: * @author Justyna Horwat
040: * @author Greg Murray
041: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
042: */
043:
044: public final class Extension {
045:
046: // ------------------------------------------------------------- Properties
047:
048: /**
049: * The name of the optional package being made available, or required.
050: */
051: private String extensionName = null;
052:
053: public String getExtensionName() {
054: return (this .extensionName);
055: }
056:
057: public void setExtensionName(String extensionName) {
058: this .extensionName = extensionName;
059: }
060:
061: /**
062: * The URL from which the most recent version of this optional package
063: * can be obtained if it is not already installed.
064: */
065: private String implementationURL = null;
066:
067: public String getImplementationURL() {
068: return (this .implementationURL);
069: }
070:
071: public void setImplementationURL(String implementationURL) {
072: this .implementationURL = implementationURL;
073: }
074:
075: /**
076: * The name of the company or organization that produced this
077: * implementation of this optional package.
078: */
079: private String implementationVendor = null;
080:
081: public String getImplementationVendor() {
082: return (this .implementationVendor);
083: }
084:
085: public void setImplementationVendor(String implementationVendor) {
086: this .implementationVendor = implementationVendor;
087: }
088:
089: /**
090: * The unique identifier of the company that produced the optional
091: * package contained in this JAR file.
092: */
093: private String implementationVendorId = null;
094:
095: public String getImplementationVendorId() {
096: return (this .implementationVendorId);
097: }
098:
099: public void setImplementationVendorId(String implementationVendorId) {
100: this .implementationVendorId = implementationVendorId;
101: }
102:
103: /**
104: * The version number (dotted decimal notation) for this implementation
105: * of the optional package.
106: */
107: private String implementationVersion = null;
108:
109: public String getImplementationVersion() {
110: return (this .implementationVersion);
111: }
112:
113: public void setImplementationVersion(String implementationVersion) {
114: this .implementationVersion = implementationVersion;
115: }
116:
117: /**
118: * The name of the company or organization that originated the
119: * specification to which this optional package conforms.
120: */
121: private String specificationVendor = null;
122:
123: public String getSpecificationVendor() {
124: return (this .specificationVendor);
125: }
126:
127: public void setSpecificationVendor(String specificationVendor) {
128: this .specificationVendor = specificationVendor;
129: }
130:
131: /**
132: * The version number (dotted decimal notation) of the specification
133: * to which this optional package conforms.
134: */
135: private String specificationVersion = null;
136:
137: public String getSpecificationVersion() {
138: return (this .specificationVersion);
139: }
140:
141: public void setSpecificationVersion(String specificationVersion) {
142: this .specificationVersion = specificationVersion;
143: }
144:
145: /**
146: * fulfilled is true if all the required extension dependencies have been
147: * satisfied
148: */
149: private boolean fulfilled = false;
150:
151: public void setFulfilled(boolean fulfilled) {
152: this .fulfilled = fulfilled;
153: }
154:
155: public boolean isFulfilled() {
156: return fulfilled;
157: }
158:
159: // --------------------------------------------------------- Public Methods
160:
161: /**
162: * Return <code>true</code> if the specified <code>Extension</code>
163: * (which represents an optional package required by this application)
164: * is satisfied by this <code>Extension</code> (which represents an
165: * optional package that is already installed. Otherwise, return
166: * <code>false</code>.
167: *
168: * @param required Extension of the required optional package
169: */
170: public boolean isCompatibleWith(Extension required) {
171:
172: // Extension Name must match
173: if (extensionName == null)
174: return (false);
175: if (!extensionName.equals(required.getExtensionName()))
176: return (false);
177:
178: // If specified, available specification version must be >= required
179: if (required.getSpecificationVersion() != null) {
180: if (!isNewer(specificationVersion, required
181: .getSpecificationVersion()))
182: return (false);
183: }
184:
185: // If specified, Implementation Vendor ID must match
186: if (required.getImplementationVendorId() != null) {
187: if (implementationVendorId == null)
188: return (false);
189: if (!implementationVendorId.equals(required
190: .getImplementationVendorId()))
191: return (false);
192: }
193:
194: // If specified, Implementation version must be >= required
195: if (required.getImplementationVersion() != null) {
196: if (!isNewer(implementationVersion, required
197: .getImplementationVersion()))
198: return (false);
199: }
200:
201: // This available optional package satisfies the requirements
202: return (true);
203:
204: }
205:
206: /**
207: * Return a String representation of this object.
208: */
209: public String toString() {
210:
211: StringBuffer sb = new StringBuffer("Extension[");
212: sb.append(extensionName);
213: if (implementationURL != null) {
214: sb.append(", implementationURL=");
215: sb.append(implementationURL);
216: }
217: if (implementationVendor != null) {
218: sb.append(", implementationVendor=");
219: sb.append(implementationVendor);
220: }
221: if (implementationVendorId != null) {
222: sb.append(", implementationVendorId=");
223: sb.append(implementationVendorId);
224: }
225: if (implementationVersion != null) {
226: sb.append(", implementationVersion=");
227: sb.append(implementationVersion);
228: }
229: if (specificationVendor != null) {
230: sb.append(", specificationVendor=");
231: sb.append(specificationVendor);
232: }
233: if (specificationVersion != null) {
234: sb.append(", specificationVersion=");
235: sb.append(specificationVersion);
236: }
237: sb.append("]");
238: return (sb.toString());
239:
240: }
241:
242: // -------------------------------------------------------- Private Methods
243:
244: /**
245: * Return <code>true</code> if the first version number is greater than
246: * or equal to the second; otherwise return <code>false</code>.
247: *
248: * @param first First version number (dotted decimal)
249: * @param second Second version number (dotted decimal)
250: *
251: * @exception NumberFormatException on a malformed version number
252: */
253: private boolean isNewer(String first, String second)
254: throws NumberFormatException {
255:
256: if ((first == null) || (second == null))
257: return (false);
258: if (first.equals(second))
259: return (true);
260:
261: StringTokenizer fTok = new StringTokenizer(first, ".", true);
262: StringTokenizer sTok = new StringTokenizer(second, ".", true);
263: int fVersion = 0;
264: int sVersion = 0;
265: while (fTok.hasMoreTokens() || sTok.hasMoreTokens()) {
266: if (fTok.hasMoreTokens())
267: fVersion = Integer.parseInt(fTok.nextToken());
268: else
269: fVersion = 0;
270: if (sTok.hasMoreTokens())
271: sVersion = Integer.parseInt(sTok.nextToken());
272: else
273: sVersion = 0;
274: if (fVersion < sVersion)
275: return (false);
276: else if (fVersion > sVersion)
277: return (true);
278: if (fTok.hasMoreTokens()) // Swallow the periods
279: fTok.nextToken();
280: if (sTok.hasMoreTokens())
281: sTok.nextToken();
282: }
283:
284: return (true); // Exact match
285:
286: }
287:
288: }
|