KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > packaging > InstallManager


1 /*
2  
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5  
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8  
9  */

10
11 package org.mmbase.applications.packaging;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.mmbase.applications.packaging.bundlehandlers.BundleInterface;
18 import org.mmbase.applications.packaging.installhandlers.installThread;
19 import org.mmbase.applications.packaging.packagehandlers.PackageInterface;
20 import org.mmbase.util.logging.Logger;
21 import org.mmbase.util.logging.Logging;
22
23 /**
24  * install manager, keeps track of what is being installed, provides background
25  * threads and logs errors & message to feedback to users.
26  *
27  * @author Daniel Ockeloen (MMBased)
28  */

29 public class InstallManager {
30     private static Logger log = Logging.getLoggerInstance(InstallManager.class);
31
32     // is the install manager active (for dependencies reasons)
33
private static boolean active = false;
34
35     // signal if we are installing a bundle or a package
36
private static boolean bundle = false;
37  
38     // package we are installing
39
private static PackageInterface pkg;
40
41     private static BundleInterface bnd;
42
43     private static installThread runner;
44
45     private static String JavaDoc state;
46
47     private static boolean running = false;
48
49     private static ArrayList JavaDoc autoresetfiles = null;
50
51     public static synchronized void init() {
52         if (!PackageManager.isRunning()) PackageManager.init();
53         if (!BundleManager.isRunning()) BundleManager.init();
54         if (!ProviderManager.isRunning()) ProviderManager.init();
55         if (!ShareManager.isRunning()) ShareManager.init();
56         if (!ProjectManager.isRunning()) ProjectManager.init();
57         running = true;
58     }
59
60     /**
61     * install a package
62     */

63     public static synchronized boolean installPackage(PackageInterface p) {
64         if (!active) {
65             // turn the installManager to active
66
active = true;
67         
68             // signal we are a package only install
69
bundle = false;
70
71             // reset the autoresetfiles
72
autoresetfiles = null;
73
74             // set the package
75
pkg = p;
76
77             state = "installing";
78
79             p.clearInstallSteps();
80
81             runner = new installThread();
82         
83             return true;
84         } else {
85             // returning false _allways_ means we where busy
86
// error feedback is provided by the processsteps
87
return false;
88         }
89     }
90
91
92     /**
93     * install a bundle
94     */

95     public static synchronized boolean installBundle(BundleInterface b) {
96         if (!active) {
97             // turn the installManager to active
98
active = true;
99         
100             // signal we are a bundle install
101
bundle = true;
102
103             // reset the autoresetfiles
104
autoresetfiles = null;
105
106             // set the bundle
107
bnd = b;
108             state = "installing";
109             b.clearInstallSteps();
110             runner = new installThread();
111         
112             return true;
113         } else {
114             // returning false _allways_ means we where busy
115
// error feedback is provided by the processsteps
116
return false;
117         }
118     }
119
120     /**
121     * called by the install thread class, performs the real install in
122     * the background and keeps providing feedback using the steps interfaces
123     */

124     public static void performInstall() {
125         try {
126             if (bnd != null) {
127                 bnd.install();
128                 state = "waiting";
129                 active = false;
130
131             } else if (pkg != null) {
132                 pkg.install();
133                 state = "waiting";
134                 active = false;
135             }
136            // rename all the files that result in a server reset in a loop
137
renameAutoResetFiles();
138         } catch(Exception JavaDoc e) {
139             log.error("perform Install problem",e);
140         }
141     }
142
143     public void setState(String JavaDoc state) {
144         this.state=state;
145     }
146     
147     public String JavaDoc getState() {
148         return state;
149     }
150
151     public static boolean isActive() {
152         return active;
153     }
154
155     public static boolean isRunning() {
156         return running;
157     }
158
159     public static PackageInterface getInstallingPackage() {
160         return pkg;
161     }
162
163     public static BundleInterface getInstallingBundle() {
164         return bnd;
165     }
166
167     public static Iterator JavaDoc getInstallSteps() {
168         return pkg.getInstallSteps();
169     }
170
171   
172     public static void addAutoResetFile(String JavaDoc path) {
173         if (autoresetfiles == null) autoresetfiles = new ArrayList JavaDoc();
174         autoresetfiles.add(path);
175     }
176
177     private static void renameAutoResetFiles() {
178         if (autoresetfiles != null) {
179         Iterator JavaDoc e=autoresetfiles.iterator();
180             while (e.hasNext()) {
181                 String JavaDoc name=(String JavaDoc)e.next();
182                 File JavaDoc oldfile = new File JavaDoc(name);
183                 File JavaDoc newfile = new File JavaDoc(name.substring(0,name.length()-4));
184                 if (oldfile.exists()) {
185                     oldfile.renameTo(newfile);
186         }
187             }
188         }
189     }
190
191 }
192
Popular Tags