KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > backend > OptionalPkgDependency


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  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29   
30 package com.sun.enterprise.deployment.backend;
31
32 import java.io.*;
33 import java.util.jar.JarFile JavaDoc;
34 import java.util.jar.Manifest JavaDoc;
35 import java.util.jar.Attributes JavaDoc;
36 import java.net.URL JavaDoc;
37 import com.sun.logging.LogDomains;
38 import java.util.logging.*;
39 import java.util.*;
40 import java.util.zip.ZipException JavaDoc;
41 import java.rmi.RemoteException JavaDoc;
42
43
44 /**
45  * This class resolves the dependencies between optional packages and also between
46  * apps/stand-alone modules that depend on optional packages
47  * @author Sheetal Vartak
48  */

49
50 public class OptionalPkgDependency {
51
52     //optional packages are stored in hashtable that is keyed by the extension name
53
private static Hashtable optionalPackageStore = new Hashtable();
54
55     // Fully qualified names of all jar files in all ext dirs. Note that
56
// this will include even jars that do not explicitly declare the
57
// extension name and specification version in their manifest.
58
private static Set extDirJars = new LinkedHashSet();
59
60     private static Logger logger = LogDomains.getLogger(LogDomains.DPL_LOGGER);
61
62     public OptionalPkgDependency() {
63     }
64
65     public static boolean optionalPkgDependencyLogic(Manifest JavaDoc manifest, String JavaDoc archiveUri) {
66
67         boolean dependencySatisfied = true;
68
69         String JavaDoc extensionList = null;
70         try {
71             extensionList = manifest.getMainAttributes().
72                     getValue(Attributes.Name.EXTENSION_LIST);
73             logger.fine("extensionList..." + extensionList);
74         } catch (Exception JavaDoc npe) {
75             //ignore this exception
76
if (logger.isLoggable(Level.FINE)) {
77                 logger.log(Level.FINE,
78                         "OptionalPkgDependency : exception occurred ==> " + npe.toString());
79             }
80         }
81         
82         if (extensionList == null)
83             return dependencySatisfied;
84         
85         StringTokenizer st =
86                 new StringTokenizer(extensionList, " ");
87         while (st.hasMoreTokens()) {
88             
89             String JavaDoc token = st.nextToken().trim();
90             String JavaDoc extName = manifest.getMainAttributes().
91                     getValue(token + "-" + Attributes.Name.EXTENSION_NAME);
92             
93             String JavaDoc specVersion = manifest.getMainAttributes().
94                     getValue(token + "-" + Attributes.Name.SPECIFICATION_VERSION);
95             
96             if (specVersion == null) {
97                 specVersion = new String JavaDoc("");
98             }
99             if (!optionalPackageStore.containsKey(extName)) {
100                 logger.log(Level.WARNING,
101                         "enterprise.deployment.backend.optionalpkg.dependency.notexist",
102                         new Object JavaDoc[] {extName, archiveUri});
103                         dependencySatisfied = false;
104             } else if (!specVersion.equals("") &&
105                     (optionalPackageStore.get(extName).toString().equals("")) ||
106                     !specVersion.equals(optionalPackageStore.get(extName).toString())) {
107                 logger.log(Level.WARNING,
108                         "enterprise.deployment.backend.optionalpkg.dependency.notexist",
109                         new Object JavaDoc[] {extName, archiveUri});
110                         dependencySatisfied = false;
111             }
112         }
113         if (dependencySatisfied == true) {
114             logger.log(Level.INFO,
115                     "enterprise.deployment.backend.optionalpkg.dependency.satisfied",
116                     new Object JavaDoc[] {archiveUri});
117         }
118         return dependencySatisfied;
119     }
120
121     /**
122      * check whether the optional packages have all their
123      * internal dependencies resolved
124      */

125     public static void satisfyOptionalPackageDependencies() {
126
127     String JavaDoc ext_dirStr = new String JavaDoc(
128                        System.getProperty("java.ext.dirs"));
129     logger.fine("ext_dirStr..." + ext_dirStr);
130
131     Vector ext_dirs = new Vector();
132     StringTokenizer st = new StringTokenizer(ext_dirStr, File.pathSeparator);
133     while (st.hasMoreTokens()) {
134         String JavaDoc next = st.nextToken();
135         logger.log(Level.FINE,"string tokens..." + next);
136         ext_dirs.addElement(next);
137     }
138         
139         for (int v = 0; v < ext_dirs.size(); v++) {
140             
141             File JavaDoc ext_dir = new File JavaDoc((String JavaDoc)ext_dirs.elementAt(v));
142             
143             if (logger.isLoggable(Level.FINE)) {
144                 logger.log(Level.FINE,"extension dir..." + ext_dir);
145             }
146             
147             
148             File JavaDoc[] optionalPackages = ext_dir.listFiles();
149             if (optionalPackages != null) {
150                 try {
151                     for (int i = 0; i < optionalPackages.length; i++) {
152                         
153                         if (logger.isLoggable(Level.FINE)) {
154                             logger.log(Level.FINE,"optional package..." + optionalPackages[i]);
155                         }
156                         
157                         if (!optionalPackages[i].isDirectory()) {
158                             JarFile JavaDoc jarFile = new JarFile JavaDoc(optionalPackages[i]);
159                             Manifest JavaDoc manifest = jarFile.getManifest();
160                             jarFile.close();
161                             
162                             extDirJars.add(optionalPackages[i].toString());
163                             
164                             //Extension-Name of optional package
165
if (manifest!=null) {
166                                 String JavaDoc extNameOfOptionalPkg = manifest.getMainAttributes().
167                                         getValue(Attributes.Name.EXTENSION_NAME);
168                                 logger.fine("extNameOfOptionalPkg..." + extNameOfOptionalPkg);
169                                 String JavaDoc specVersion = manifest.getMainAttributes().
170                                         getValue(Attributes.Name.SPECIFICATION_VERSION);
171                                 logger.fine("specVersion..." + specVersion);
172                                 if (extNameOfOptionalPkg != null) {
173                                     if (specVersion == null) {
174                                         logger.log(Level.WARNING,
175                                                 "enterprise.tools.deployment.backend.optionalpkg.dependency.specversion.null",
176                                                 new Object JavaDoc[] {extNameOfOptionalPkg});
177                                                 specVersion = new String JavaDoc("");
178                                     }
179                                     optionalPackageStore.put(extNameOfOptionalPkg,
180                                             specVersion);
181                                     
182                                 }
183                             }
184                         }
185                     }
186                     for (int i = 0; i < optionalPackages.length; i++) {
187                         if (!optionalPackages[i].isDirectory()) {
188                             JarFile JavaDoc jarFile = null;
189                             try {
190                                 jarFile = new JarFile JavaDoc(optionalPackages[i]);
191                                 Manifest JavaDoc m = jarFile.getManifest();
192                                 if (m!=null) {
193                                     optionalPkgDependencyLogic(m, optionalPackages[i].getAbsolutePath());
194                                 }
195                             } finally {
196                                 if (jarFile!=null)
197                                     jarFile.close();
198                             }
199                             
200                         }
201                     }
202                 } catch (IOException e) {
203                     logger.log(Level.WARNING,
204                             "enterprise.deployment.backend.optionalpkg.dependency.exception", new Object JavaDoc[] {e.getMessage()});
205                 }
206             }
207         }
208     }
209
210     /**
211      * Adds all the jar files in all of the ext dirs into a single string
212      * in classpath format. Returns the empty string if there are no
213      * jar files in any ext dirs.
214      */

215     public static String JavaDoc getExtDirFilesAsClasspath() {
216        
217         StringBuffer JavaDoc classpath = new StringBuffer JavaDoc();
218
219         for(Iterator iter = extDirJars.iterator(); iter.hasNext();) {
220             String JavaDoc next = (String JavaDoc) iter.next();
221             if( classpath.length() > 0 ) {
222                 classpath.append(File.pathSeparator);
223             }
224             classpath.append(next);
225         }
226
227         return classpath.toString();
228     }
229     
230 }
231
Popular Tags