KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > apiscan > packaging > ExtensionRef


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * OptPkgRef.java
26  *
27  * Created on August 13, 2004, 4:26 PM
28  */

29
30 package com.sun.enterprise.tools.verifier.apiscan.packaging;
31
32 import java.io.IOException JavaDoc;
33 import java.util.jar.Attributes JavaDoc;
34 import java.util.jar.Manifest JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 /**
39  * This class holds the information for each installed optional package
40  * reference. It is used to select the optional package actually referenced.
41  * Refer to http://java.sun.com/j2se/1.4.2/docs/guide/extensions/versioning.html#packages
42  * for more info.
43  *
44  * @author Sanjeeb.Sahoo@Sun.COM
45  * @see Archive
46  */

47 public class ExtensionRef {
48     private static String JavaDoc resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings";
49     public static Logger JavaDoc logger = Logger.getLogger("apiscan.packaging", resourceBundleName); // NOI18N
50
private static final String JavaDoc myClassName = "ExtensionRef"; // NOI18N
51
// a client can specify dependency using atleast the name or name and any other attr
52
private String JavaDoc name, implVendorId = "";
53     private String JavaDoc implVer;//See javadocs of java.lang.Package. implVer is a string and not DeweyDecimal
54
private DeweyDecimal specVer;
55
56     /**
57      * @param manifest Manifest file to be read.
58      * @param extName Name of the extension reference. It is the string that is
59      * mentioned in the Extension-List manifest attribute.
60      */

61     public ExtensionRef(Manifest JavaDoc manifest, String JavaDoc extName) {
62         Attributes JavaDoc attrs = manifest.getMainAttributes();
63         name = attrs.getValue(extName + "-" + Attributes.Name.EXTENSION_NAME); // NOI18N
64
String JavaDoc s = attrs.getValue(
65                 extName + "-" + Attributes.Name.SPECIFICATION_VERSION); // NOI18N
66
if (s != null) {
67             try {
68                 specVer = new DeweyDecimal(s);
69             } catch (NumberFormatException JavaDoc e) {
70                 logger.log(Level.SEVERE, getClass().getName() + ".exception1", new Object JavaDoc[]{e.getMessage()});
71                 logger.log(Level.SEVERE, "", e);
72                 throw e;
73             }
74         }
75         implVendorId =
76                 attrs.getValue(
77                         extName + "-" + Attributes.Name.IMPLEMENTATION_VENDOR_ID); // NOI18N
78
implVer =
79                 attrs.getValue(
80                         extName + "-" + Attributes.Name.IMPLEMENTATION_VERSION); // NOI18N
81
validate();
82     }
83
84     private void validate() {
85         if (name == null || name.length() <= 0) {
86             throw new IllegalArgumentException JavaDoc("Extension-Name can not be empty.");
87         }
88     }
89
90     /**
91      * @param another Archive whose specifications will be used for matching.
92      * @return true if the other archive meets the specifications of this
93      * extensionRef, else returns false.
94      */

95     public boolean isSatisfiedBy(Archive another) throws IOException JavaDoc {
96         logger.entering(myClassName, "isSatisfiedBy", another); // NOI18N
97
Attributes JavaDoc attrs = another.getManifest().getMainAttributes();
98         String JavaDoc name = attrs.getValue(Attributes.Name.EXTENSION_NAME);
99         String JavaDoc s = attrs.getValue(Attributes.Name.SPECIFICATION_VERSION);
100         DeweyDecimal specVer = null;
101         try {
102             specVer = s != null ? new DeweyDecimal(s) : null;
103         } catch (NumberFormatException JavaDoc e) {
104             logger.log(Level.WARNING, getClass().getName() + ".warning1", new Object JavaDoc[]{e.getMessage(), another.toString()});
105             return false;
106         }
107         String JavaDoc implVendorId = attrs.getValue(
108                 Attributes.Name.IMPLEMENTATION_VENDOR_ID);
109         String JavaDoc implVer = attrs.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
110         //implVendor is not used for comparision because it is not supposed to be used.
111
//See optional package versioning.
112
//The order of comparision is very well defined in
113
//http://java.sun.com/j2se/1.4.2/docs/guide/extensions/versioning.html
114
//Although that is specified for Java plugins for applets, it is equally
115
//applicable for J2EE.
116
//See J2EE 1.4 section#8.2
117
return this.name.equals(name) &&
118                 (this.specVer == null || this.specVer.isCompatible(specVer)) &&
119                 (this.implVendorId == null ||
120                 this.implVendorId.equals(implVendorId)) &&
121                 (this.implVer == null || this.implVer.equals(implVer));
122     }
123
124     /**
125      * Used for pretty printing.
126      */

127     public String JavaDoc toString() {
128         return "Extension-Name: " + name + "\n" + // NOI18N
129
(specVer != null ? "Specification-Version: " + specVer + "\n" : "") + // NOI18N
130
(implVendorId != null ?
131                 "Implementation-Vendor-Id: " + implVendorId + "\n" : "") + // NOI18N
132
(implVer != null ? "Implementation-Version: " + implVer + "\n" : ""); // NOI18N
133
}
134 }
135
Popular Tags