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: package org.apache.catalina.util;
018:
019: import java.util.Iterator;
020: import java.util.jar.Manifest;
021: import java.util.jar.Attributes;
022: import java.util.ArrayList;
023:
024: /**
025: * Representation of a Manifest file and its available extensions and
026: * required extensions
027: *
028: * @author Greg Murray
029: * @author Justyna Horwat
030: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
031: *
032: */
033: public class ManifestResource {
034:
035: // ------------------------------------------------------------- Properties
036:
037: // These are the resource types for determining effect error messages
038: public static final int SYSTEM = 1;
039: public static final int WAR = 2;
040: public static final int APPLICATION = 3;
041:
042: private ArrayList availableExtensions = null;
043: private ArrayList requiredExtensions = null;
044:
045: private String resourceName = null;
046: private int resourceType = -1;
047:
048: public ManifestResource(String resourceName, Manifest manifest,
049: int resourceType) {
050: this .resourceName = resourceName;
051: this .resourceType = resourceType;
052: processManifest(manifest);
053: }
054:
055: /**
056: * Gets the name of the resource
057: *
058: * @return The name of the resource
059: */
060: public String getResourceName() {
061: return resourceName;
062: }
063:
064: /**
065: * Gets the list of available extensions
066: *
067: * @return List of available extensions
068: */
069: public ArrayList getAvailableExtensions() {
070: return availableExtensions;
071: }
072:
073: /**
074: * Gets the list of required extensions
075: *
076: * @return List of required extensions
077: */
078: public ArrayList getRequiredExtensions() {
079: return requiredExtensions;
080: }
081:
082: // --------------------------------------------------------- Public Methods
083:
084: /**
085: * Gets the number of available extensions
086: *
087: * @return The number of available extensions
088: */
089: public int getAvailableExtensionCount() {
090: return (availableExtensions != null) ? availableExtensions
091: .size() : 0;
092: }
093:
094: /**
095: * Gets the number of required extensions
096: *
097: * @return The number of required extensions
098: */
099: public int getRequiredExtensionCount() {
100: return (requiredExtensions != null) ? requiredExtensions.size()
101: : 0;
102: }
103:
104: /**
105: * Convienience method to check if this <code>ManifestResource</code>
106: * has an requires extensions.
107: *
108: * @return true if required extensions are present
109: */
110: public boolean requiresExtensions() {
111: return (requiredExtensions != null) ? true : false;
112: }
113:
114: /**
115: * Returns <code>true</code> if all required extension dependencies
116: * have been meet for this <code>ManifestResource</code> object.
117: *
118: * @return boolean true if all extension dependencies have been satisfied
119: */
120: public boolean isFulfilled() {
121: if (requiredExtensions == null) {
122: return false;
123: }
124: Iterator it = requiredExtensions.iterator();
125: while (it.hasNext()) {
126: Extension ext = (Extension) it.next();
127: if (!ext.isFulfilled())
128: return false;
129: }
130: return true;
131: }
132:
133: public String toString() {
134:
135: StringBuffer sb = new StringBuffer("ManifestResource[");
136: sb.append(resourceName);
137:
138: sb.append(", isFulfilled=");
139: sb.append(isFulfilled() + "");
140: sb.append(", requiredExtensionCount =");
141: sb.append(getRequiredExtensionCount());
142: sb.append(", availableExtensionCount=");
143: sb.append(getAvailableExtensionCount());
144: switch (resourceType) {
145: case SYSTEM:
146: sb.append(", resourceType=SYSTEM");
147: break;
148: case WAR:
149: sb.append(", resourceType=WAR");
150: break;
151: case APPLICATION:
152: sb.append(", resourceType=APPLICATION");
153: break;
154: }
155: sb.append("]");
156: return (sb.toString());
157: }
158:
159: // -------------------------------------------------------- Private Methods
160:
161: private void processManifest(Manifest manifest) {
162: availableExtensions = getAvailableExtensions(manifest);
163: requiredExtensions = getRequiredExtensions(manifest);
164: }
165:
166: /**
167: * Return the set of <code>Extension</code> objects representing optional
168: * packages that are required by the application associated with the
169: * specified <code>Manifest</code>.
170: *
171: * @param manifest Manifest to be parsed
172: *
173: * @return List of required extensions, or null if the application
174: * does not require any extensions
175: */
176: private ArrayList getRequiredExtensions(Manifest manifest) {
177:
178: Attributes attributes = manifest.getMainAttributes();
179: String names = attributes.getValue("Extension-List");
180: if (names == null)
181: return null;
182:
183: ArrayList extensionList = new ArrayList();
184: names += " ";
185:
186: while (true) {
187:
188: int space = names.indexOf(' ');
189: if (space < 0)
190: break;
191: String name = names.substring(0, space).trim();
192: names = names.substring(space + 1);
193:
194: String value = attributes
195: .getValue(name + "-Extension-Name");
196: if (value == null)
197: continue;
198: Extension extension = new Extension();
199: extension.setExtensionName(value);
200: extension.setImplementationURL(attributes.getValue(name
201: + "-Implementation-URL"));
202: extension.setImplementationVendorId(attributes
203: .getValue(name + "-Implementation-Vendor-Id"));
204: String version = attributes.getValue(name
205: + "-Implementation-Version");
206: extension.setImplementationVersion(version);
207: extension.setSpecificationVersion(attributes.getValue(name
208: + "-Specification-Version"));
209: extensionList.add(extension);
210: }
211: return extensionList;
212: }
213:
214: /**
215: * Return the set of <code>Extension</code> objects representing optional
216: * packages that are bundled with the application associated with the
217: * specified <code>Manifest</code>.
218: *
219: * @param manifest Manifest to be parsed
220: *
221: * @return List of available extensions, or null if the web application
222: * does not bundle any extensions
223: */
224: private ArrayList getAvailableExtensions(Manifest manifest) {
225:
226: Attributes attributes = manifest.getMainAttributes();
227: String name = attributes.getValue("Extension-Name");
228: if (name == null)
229: return null;
230:
231: ArrayList extensionList = new ArrayList();
232:
233: Extension extension = new Extension();
234: extension.setExtensionName(name);
235: extension.setImplementationURL(attributes
236: .getValue("Implementation-URL"));
237: extension.setImplementationVendor(attributes
238: .getValue("Implementation-Vendor"));
239: extension.setImplementationVendorId(attributes
240: .getValue("Implementation-Vendor-Id"));
241: extension.setImplementationVersion(attributes
242: .getValue("Implementation-Version"));
243: extension.setSpecificationVersion(attributes
244: .getValue("Specification-Version"));
245:
246: extensionList.add(extension);
247:
248: return extensionList;
249: }
250:
251: }
|