KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.catalina.util;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.jar.Manifest JavaDoc;
21 import java.util.jar.Attributes JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 /**
25  * Representation of a Manifest file and its available extensions and
26  * required extensions
27  *
28  * @author Greg Murray
29  * @author Justyna Horwat
30  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
31  *
32  */

33 public class ManifestResource {
34     
35     // ------------------------------------------------------------- Properties
36

37     // These are the resource types for determining effect error messages
38
public static final int SYSTEM = 1;
39     public static final int WAR = 2;
40     public static final int APPLICATION = 3;
41     
42     private ArrayList JavaDoc availableExtensions = null;
43     private ArrayList JavaDoc requiredExtensions = null;
44     
45     private String JavaDoc resourceName = null;
46     private int resourceType = -1;
47         
48     public ManifestResource(String JavaDoc resourceName, Manifest JavaDoc manifest,
49                             int resourceType) {
50         this.resourceName = resourceName;
51         this.resourceType = resourceType;
52         processManifest(manifest);
53     }
54     
55     /**
56      * Gets the name of the resource
57      *
58      * @return The name of the resource
59      */

60     public String JavaDoc getResourceName() {
61         return resourceName;
62     }
63
64     /**
65      * Gets the list of available extensions
66      *
67      * @return List of available extensions
68      */

69     public ArrayList JavaDoc getAvailableExtensions() {
70         return availableExtensions;
71     }
72     
73     /**
74      * Gets the list of required extensions
75      *
76      * @return List of required extensions
77      */

78     public ArrayList JavaDoc getRequiredExtensions() {
79         return requiredExtensions;
80     }
81     
82     // --------------------------------------------------------- Public Methods
83

84     /**
85      * Gets the number of available extensions
86      *
87      * @return The number of available extensions
88      */

89     public int getAvailableExtensionCount() {
90         return (availableExtensions != null) ? availableExtensions.size() : 0;
91     }
92     
93     /**
94      * Gets the number of required extensions
95      *
96      * @return The number of required extensions
97      */

98     public int getRequiredExtensionCount() {
99         return (requiredExtensions != null) ? requiredExtensions.size() : 0;
100     }
101     
102     /**
103      * Convienience method to check if this <code>ManifestResource</code>
104      * has an requires extensions.
105      *
106      * @return true if required extensions are present
107      */

108     public boolean requiresExtensions() {
109         return (requiredExtensions != null) ? true : false;
110     }
111     
112     /**
113      * Returns <code>true</code> if all required extension dependencies
114      * have been meet for this <code>ManifestResource</code> object.
115      *
116      * @return boolean true if all extension dependencies have been satisfied
117      */

118     public boolean isFulfilled() {
119         if (requiredExtensions == null) {
120             return false;
121         }
122         Iterator JavaDoc it = requiredExtensions.iterator();
123         while (it.hasNext()) {
124             Extension ext = (Extension)it.next();
125             if (!ext.isFulfilled()) return false;
126         }
127         return true;
128     }
129     
130     public String JavaDoc toString() {
131
132         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("ManifestResource[");
133         sb.append(resourceName);
134
135         sb.append(", isFulfilled=");
136         sb.append(isFulfilled() +"");
137         sb.append(", requiredExtensionCount =");
138         sb.append(getRequiredExtensionCount());
139         sb.append(", availableExtensionCount=");
140         sb.append(getAvailableExtensionCount());
141         switch (resourceType) {
142             case SYSTEM : sb.append(", resourceType=SYSTEM"); break;
143             case WAR : sb.append(", resourceType=WAR"); break;
144             case APPLICATION : sb.append(", resourceType=APPLICATION"); break;
145         }
146         sb.append("]");
147         return (sb.toString());
148     }
149
150
151     // -------------------------------------------------------- Private Methods
152

153     private void processManifest(Manifest JavaDoc manifest) {
154         availableExtensions = getAvailableExtensions(manifest);
155         requiredExtensions = getRequiredExtensions(manifest);
156     }
157     
158     /**
159      * Return the set of <code>Extension</code> objects representing optional
160      * packages that are required by the application associated with the
161      * specified <code>Manifest</code>.
162      *
163      * @param manifest Manifest to be parsed
164      *
165      * @return List of required extensions, or null if the application
166      * does not require any extensions
167      */

168     private ArrayList JavaDoc getRequiredExtensions(Manifest JavaDoc manifest) {
169
170         Attributes JavaDoc attributes = manifest.getMainAttributes();
171         String JavaDoc names = attributes.getValue("Extension-List");
172         if (names == null)
173             return null;
174
175         ArrayList JavaDoc extensionList = new ArrayList JavaDoc();
176         names += " ";
177
178         while (true) {
179
180             int space = names.indexOf(' ');
181             if (space < 0)
182                 break;
183             String JavaDoc name = names.substring(0, space).trim();
184             names = names.substring(space + 1);
185
186             String JavaDoc value =
187                 attributes.getValue(name + "-Extension-Name");
188             if (value == null)
189                 continue;
190             Extension extension = new Extension();
191             extension.setExtensionName(value);
192             extension.setImplementationURL
193                 (attributes.getValue(name + "-Implementation-URL"));
194             extension.setImplementationVendorId
195                 (attributes.getValue(name + "-Implementation-Vendor-Id"));
196             String JavaDoc version = attributes.getValue(name + "-Implementation-Version");
197             extension.setImplementationVersion(version);
198             extension.setSpecificationVersion
199                 (attributes.getValue(name + "-Specification-Version"));
200             extensionList.add(extension);
201         }
202         return extensionList;
203     }
204     
205     /**
206      * Return the set of <code>Extension</code> objects representing optional
207      * packages that are bundled with the application associated with the
208      * specified <code>Manifest</code>.
209      *
210      * @param manifest Manifest to be parsed
211      *
212      * @return List of available extensions, or null if the web application
213      * does not bundle any extensions
214      */

215     private ArrayList JavaDoc getAvailableExtensions(Manifest JavaDoc manifest) {
216
217         Attributes JavaDoc attributes = manifest.getMainAttributes();
218         String JavaDoc name = attributes.getValue("Extension-Name");
219         if (name == null)
220             return null;
221
222         ArrayList JavaDoc extensionList = new ArrayList JavaDoc();
223
224         Extension extension = new Extension();
225         extension.setExtensionName(name);
226         extension.setImplementationURL(
227             attributes.getValue("Implementation-URL"));
228         extension.setImplementationVendor(
229             attributes.getValue("Implementation-Vendor"));
230         extension.setImplementationVendorId(
231             attributes.getValue("Implementation-Vendor-Id"));
232         extension.setImplementationVersion(
233             attributes.getValue("Implementation-Version"));
234         extension.setSpecificationVersion(
235             attributes.getValue("Specification-Version"));
236
237         extensionList.add(extension);
238
239         return extensionList;
240     }
241     
242 }
243
Popular Tags