KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.addon;
26
27 import java.io.*;
28 import java.lang.reflect.*;
29 import java.net.URL JavaDoc;
30 import java.net.URLClassLoader JavaDoc;
31 import java.util.jar.*;
32 import java.util.*;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 /**
37  * This class
38  * iterates over all the jars under install/lib/addons
39  * read the manifest file of each jar, if supplied
40  * read main-class, instantiated it through reflection
41  * call *main* method passing user/password as string array arguments
42  *
43  */

44 public class AddonInstaller {
45     
46     /** Creates a new instance of AddonInstaller */
47     
48     public AddonInstaller() {
49         
50     }
51     
52     /**
53      * This will install all the addons under installDir/lib/addons
54      * @param installDir Installation directory
55      * @param instanceRoot Domain instance directory
56      */

57     public void installAllAddons(String JavaDoc installDir, String JavaDoc instanceRoot){
58         
59         String JavaDoc addonJar = "";
60         //Properties registry = new Properties();
61
Registry registry = null;
62         FileInputStream in = null;
63         FileOutputStream out = null;
64         try {
65             String JavaDoc addonDir = installDir + File.separator + AddonConstants.LIB + File.separator + AddonConstants.ADDONS;
66             String JavaDoc domainConfigRoot = instanceRoot + File.separator + AddonConstants.CONFIG;
67             //Logger.getAnonymousLogger().log(Level.FINE, "domainConfigRoot==="+domainConfigRoot);
68
String JavaDoc domainRegistry = domainConfigRoot + File.separator + AddonConstants.DOMAIN_REGISTRY;
69             File registryFile = new File(domainRegistry);
70             registry = new Registry();
71             registry.load(registryFile);
72             File libAddonDirectory = new File(addonDir);
73             File[] fileArray = libAddonDirectory.listFiles();
74         
75             for(int i = 0;i<fileArray.length;i++) {
76                 addonJar = fileArray[i].getName();
77                 String JavaDoc jarExtension = "";
78                 int dotLastIndex = addonJar.lastIndexOf(".");
79                 String JavaDoc jarNameWithoutExtension = addonJar;
80                 if(dotLastIndex != -1) {
81                     jarExtension = addonJar.substring(dotLastIndex + 1);
82                     jarNameWithoutExtension = addonJar.substring(0, dotLastIndex);
83                 }
84                 if(jarExtension.equalsIgnoreCase("jar")) {
85                     //Logger.getAnonymousLogger().log(Level.INFO, "fileArray[i].getName()="+fileArray[i].getName());
86
//String key = domainName + "." + fileArray[i].getName() + "." + "installed";
87
String JavaDoc key = jarNameWithoutExtension + "." + "enabled";
88                     String JavaDoc installed = registry.getProperty(key);
89                     if(installed != null && installed.equals("true")) {
90                         Logger.getAnonymousLogger().log(Level.FINE, "Addon "+addonJar+" is already installed");
91                             continue;
92                     }
93                     Addon addon = new Addon(fileArray[i]);
94                     boolean install = addon.install(installDir,instanceRoot);
95                     if(install)
96                     registry.setProperty(key, "true");
97                     
98                 }
99             }
100             registry.store();
101            
102         }catch(Exception JavaDoc ex) {
103             Logger.getAnonymousLogger().log(Level.WARNING, "Error while installing the addon "+addonJar, ex);
104         }finally {
105             try {
106                 if(registry != null)
107                 registry.close();
108             }catch(Exception JavaDoc e) {
109                 
110             }
111         }
112     
113     }
114     
115     
116     
117     public static void main(String JavaDoc[] args) {
118         AddonInstaller installer = new AddonInstaller();
119         installer.installAllAddons(args[0], args[1]);
120     }
121     
122 }
123
Popular Tags