KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > addon > Addon


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 package com.sun.enterprise.addon;
25
26 import java.io.*;
27 import java.lang.reflect.*;
28 import java.net.URL JavaDoc;
29 import java.net.URLClassLoader JavaDoc;
30 import java.util.jar.*;
31 import java.util.*;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import com.sun.enterprise.util.OS;
35
36 /**
37  * This class represents a particular addon
38  * @author servesh.singh@sun.com
39  */

40 public class Addon {
41     
42     private File addonJar;
43    
44     /** Creates a new instance of Addon */
45     public Addon(File addonJar) {
46         this.addonJar = addonJar;
47     }
48     
49     public boolean install(String JavaDoc installDir, String JavaDoc instanceRoot) throws Exception JavaDoc
50     {
51         JarFile file = new JarFile(addonJar);
52         Manifest mf = file.getManifest();
53         Attributes attributes = null;
54         if(mf != null) {
55             attributes = mf.getMainAttributes();
56             if(attributes != null) {
57                 String JavaDoc mainClass = attributes.getValue(Attributes.Name.MAIN_CLASS);
58                 Logger.getAnonymousLogger().log(Level.FINE, "Addon getting installed: "+addonJar);
59                 Logger.getAnonymousLogger().log(Level.FINE, "Main Class "+mainClass);
60                 if(mainClass != null) {
61                     URL JavaDoc url = addonJar.toURI().toURL();
62                     List<URL JavaDoc> classPath = new ArrayList<URL JavaDoc>();
63                     classPath.add(url);
64                     addClasspath(installDir, classPath);
65                     //URL[] urls = new URL[] {url};
66
URL JavaDoc[] urls = classPath.toArray(new URL JavaDoc[] {});
67                     URLClassLoader JavaDoc loader = new URLClassLoader JavaDoc(urls);
68                     Class JavaDoc main = loader.loadClass(mainClass);
69                     //for(int j =0; j < urls.length; j++) {
70
// Logger.getAnonymousLogger().log(Level.INFO, "urls "+urls[j].toString());
71
//}
72
Object JavaDoc obj = main.newInstance();
73                     String JavaDoc[] argu = new String JavaDoc[] {installDir, instanceRoot};
74                     Class JavaDoc[] types = new Class JavaDoc[] {argu.getClass()};
75                     Method method = main.getMethod(AddonConstants.MAIN, types);
76                     Object JavaDoc[] args = new Object JavaDoc[] { argu };
77                     method.invoke(obj,args);
78                     return true;
79                     
80                 } else
81                     return false;
82             } else
83                 return false;
84         } else
85             return false;
86     
87     }
88     
89     /**
90      * This will add the ant jars and lib jars into classpath
91      * @param installDir Installation Directory
92      * @param classPath
93      */

94     private void addClasspath(String JavaDoc installDir, List classPath) throws Exception JavaDoc{
95         BufferedReader bf = null;
96         InputStream in = null;
97         try {
98             String JavaDoc asenv="";
99             if(OS.isUNIX())
100                 asenv = installDir + File.separator + AddonConstants.CONFIG + File.separator + AddonConstants.ASENVCONF;
101             else
102              asenv = installDir + File.separator + AddonConstants.CONFIG + File.separator + AddonConstants.ASENVBAT;
103         
104             in = new FileInputStream(asenv);
105             String JavaDoc antLib = "";
106             bf = new BufferedReader(new InputStreamReader(in));
107             String JavaDoc line = bf.readLine();
108             while(line != null) {
109                 if(line.indexOf(AddonConstants.ASANTLIB) != -1) {
110                     int pos = line.indexOf("=");
111                     if (pos > 0) {
112                         String JavaDoc lhs = (line.substring(0, pos)).trim();
113                         String JavaDoc rhs = (line.substring(pos + 1)).trim();
114
115                         if (OS.isWindows()) { //trim off the "set "
116
lhs = (lhs.substring(3)).trim();
117                         }
118
119                         if (OS.isUNIX()) { // take the quotes out
120
pos = rhs.indexOf("\"");
121                             if(pos != -1) {
122                                 rhs = (rhs.substring(pos+1)).trim();
123                                 pos = rhs.indexOf("\"");
124                                 if(pos != -1)
125                                 rhs = (rhs.substring(0, pos)).trim();
126                             }
127                 
128                         }
129                         antLib = rhs;
130                         break;
131                     }
132                 }
133                 line = bf.readLine();
134              }
135                 
136             Logger.getAnonymousLogger().log(Level.FINE,"antLib "+antLib);
137             File antLibDir = new File(antLib);
138             File[] fileArray = antLibDir.listFiles();
139         
140             for(int i = 0;i<fileArray.length;i++) {
141                 if(fileArray[i].getName().endsWith(".jar")) {
142                     URL JavaDoc url = fileArray[i].toURI().toURL();
143                     classPath.add(url);
144                 }
145             }
146             File installLib = new File(installDir + File.separator + AddonConstants.LIB );
147             File[] installLibArray = installLib.listFiles();
148         
149             Logger.getAnonymousLogger().log(Level.FINE,"installLib "+installLib.getAbsolutePath());
150             for(int i = 0;i<installLibArray.length;i++) {
151                 if(installLibArray[i].getName().endsWith(".jar")) {
152                     URL JavaDoc url = installLibArray[i].toURI().toURL();
153                     classPath.add(url);
154                 }
155             }
156         }catch(Exception JavaDoc e) {
157             throw e;
158         } finally {
159           if(bf != null)
160             bf.close();
161           if(in != null)
162             in.close();
163         }
164         
165     }
166     
167 }
168
Popular Tags