KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > deployment > main > ApplicationManager


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 package com.sun.enterprise.tools.deployment.main;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.Vector JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Properties JavaDoc;
33 import com.sun.enterprise.deployment.*;
34 import com.sun.enterprise.util.NotificationListener;
35 import com.sun.enterprise.util.NotificationEvent;
36 //import com.sun.enterprise.tools.deployment.ui.utils.*;
37

38 /** Objects of this type manage applications within a tool.
39 * @author Danny Coward
40 */

41
42 public class ApplicationManager
43     implements NotificationListener
44 {
45
46     /** Notification type for change of active application.*/
47     public static String JavaDoc ACTIVE_CHANGED = "active changed"; // NOI18N
48

49     /** Notification type for an application change.*/
50     public static String JavaDoc APPLICATION_CHANGED = "application changed"; // NOI18N
51

52     /** Notification type that the list of apps changed in this manager.*/
53     public static String JavaDoc APPLICATION_LIST_CHANGED = "application list changed"; // NOI18N
54

55     /** Notification type that a listener was added.*/
56     public static String JavaDoc LISTENER_ADDED = "listener added"; // NOI18N
57

58     /** Notification type for addition of an application.*/
59     public static String JavaDoc APPLICATION_ADDED = "application added"; // NOI18N
60

61     /** Notification type that an application was removed.*/
62     public static String JavaDoc APPLICATION_REMOVED = "application removed"; // NOI18N
63

64     /** Notification property to store the application that changed.*/
65     public static String JavaDoc APPLICATION_PROPERTY = "application property"; // NOI18N
66

67 // private static Hashtable projects = new Hashtable();
68

69     private Application activeApplication;
70     private Vector JavaDoc applications = new Vector JavaDoc();
71     private Vector JavaDoc applicationListeners = new Vector JavaDoc();
72
73     private File JavaDoc preferencesDirectory;
74     private File JavaDoc temp;
75     
76     /** Construct a new application manager storing its preferences in the
77     *** given directory.
78     **/

79     public ApplicationManager(File JavaDoc preferencesDirectory, File JavaDoc temp) {
80     this.preferencesDirectory = preferencesDirectory;
81     this.temp = temp;
82     }
83     
84     /** Gets the temporary directory. */
85     public File JavaDoc getTemp() {
86     return temp;
87     }
88     
89     /** retore state from the last session, returning a Hashtable of
90     ** filenames to exceptions for each application that could
91     * not be reopened.
92     */

93     public Hashtable JavaDoc restoreFromUserHome() throws IOException JavaDoc {
94     Hashtable JavaDoc badApplicationJarFilenamesToExceptions = new Hashtable JavaDoc();
95     File JavaDoc appsFile = new File JavaDoc(preferencesDirectory, getConfigAppFileName());
96     if (appsFile.exists()) {
97         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(appsFile);
98         Properties JavaDoc applications = new Properties JavaDoc();
99         applications.load(fis);
100         for (Enumeration JavaDoc e = applications.propertyNames();
101         e.hasMoreElements();) {
102         String JavaDoc appName = (String JavaDoc) e.nextElement();
103         String JavaDoc appJarFilename = (String JavaDoc)
104             applications.getProperty(appName);
105         Application openedApp=null;
106         try {
107             this.openApplication(new File JavaDoc(appJarFilename));
108         } catch (Throwable JavaDoc t) {
109             badApplicationJarFilenamesToExceptions.put(
110             appJarFilename, t);
111         }
112         }
113             if (fis != null) {
114                 fis.close();
115             }
116     }
117     return badApplicationJarFilenamesToExceptions;
118     }
119     
120     /** Saves the list of open applications to the working dir under
121     *** '.jpedeploytool/.' */

122     public void saveToUserHome() throws IOException JavaDoc {
123     File JavaDoc appsFile = new File JavaDoc(preferencesDirectory, getConfigAppFileName());
124     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(appsFile);
125     Properties JavaDoc applications = new Properties JavaDoc();
126     for (Enumeration JavaDoc e = this.getApplications().elements();
127         e.hasMoreElements();) {
128         Application nextApplication = (Application) e.nextElement();
129         applications.put(nextApplication.getName(),
130         nextApplication.getApplicationArchivist().
131             getApplicationFile().toString());
132     }
133     applications.store(fos, "J2EE Applications"); // NOI18N
134
if (fos != null) {
135             fos.close();
136         }
137     }
138     
139     /** Register for state changes. */
140     public void addNotificationListener(NotificationListener nl) {
141     applicationListeners.addElement(nl);
142     this.notify(LISTENER_ADDED, null, null);
143     }
144     
145     /** Deregister for state changes. */
146     public void removeNotificationListener(NotificationListener nl) {
147     applicationListeners.removeElement(nl);
148     }
149     
150     /** Sets the active application. */
151     public void setActiveApplication(Application application) {
152     if (application != activeApplication) {
153         this.activeApplication = application;
154         if (application != null) {
155         this.notify(ACTIVE_CHANGED, APPLICATION_PROPERTY, application);
156         } else {
157         this.notify(ACTIVE_CHANGED, null, null);
158         }
159     }
160     }
161     
162     /** Return the active application. */
163     public Application getActiveApplication() {
164     return activeApplication;
165     }
166     
167     /** The Vector open application objects. */
168     public Vector JavaDoc getApplications() {
169     Vector JavaDoc clone = (Vector JavaDoc) applications.clone();
170     return clone;
171     }
172     
173     public Application getApplicationWithJar(File JavaDoc jarFilename) {
174     for (Enumeration JavaDoc e = this.getApplications().elements();
175         e.hasMoreElements();) {
176         Application application = (Application) e.nextElement();
177         if (application.getApplicationArchivist().
178         getApplicationFile().getAbsolutePath().equals(
179             jarFilename.getAbsolutePath())) {
180         return application;
181         }
182     }
183     return null;
184     }
185     
186     /* The names of the open applications. */
187     public Vector JavaDoc getApplicationNames() {
188     Vector JavaDoc names = new Vector JavaDoc();
189     for (Enumeration JavaDoc e = applications.elements();e.hasMoreElements();) {
190         names.addElement(((Application) e.nextElement()).getName());
191     }
192     return names;
193     }
194     
195     protected String JavaDoc getUniqueApplicationName(String JavaDoc trialName) {
196     return Application.createUniqueNameAmongst(trialName,
197         this.getApplicationNames());
198     }
199     
200     /** Create a new application with the given name and jar file and
201     ** makes it the active one.. */

202     public Application newApplication(String JavaDoc name, String JavaDoc jarFile) {
203     Application newApplication = new Application(
204         this.getUniqueApplicationName(name), new File JavaDoc(jarFile));
205
206 // Project newProj = new ProjectImpl(newApplication);
207
// Project.addProject(newApplication, newProj);
208

209     try {
210         this.saveApplication(newApplication);
211     } catch (IOException JavaDoc ioe) {
212         Log.print(this, ioe);
213     }
214     this.addApplication(newApplication);
215     return newApplication;
216     }
217     
218     /** Save the given applicastion to its JAR file. */
219     public void saveApplication(Application application)
220     throws IOException JavaDoc {
221     ApplicationArchivist archivist = application.getApplicationArchivist();
222     archivist.save(archivist.getApplicationFile(), true);
223
224 // ProjectImpl proj = (ProjectImpl)Project.getProject(application);
225
// if (proj != null) {
226
// proj.save();
227
// }
228
}
229
230     /** Save the given application to the supllied file.
231     */

232     public void saveAsApplication(Application application, File JavaDoc newJar)
233     throws IOException JavaDoc {
234     ApplicationArchivist archivist = application.getApplicationArchivist();
235     archivist.save(newJar, true);
236 // ProjectImpl proj = (ProjectImpl)Project.getProject(application);
237
// if (proj != null) {
238
// proj.save();
239
// }
240
}
241
242     /** Open the JAR file application. */
243     public Application openApplication(File JavaDoc name) throws Exception JavaDoc {
244     Application openedApplication = ApplicationArchivist.openAT(name); //4691307; 4774785
245
// fix bug# 4766725
246
for (java.util.Iterator JavaDoc itr = openedApplication.getWebBundleDescriptors().iterator(); itr.hasNext();) {
247                 WebBundleDescriptor webBundleDescriptor = (WebBundleDescriptor) itr.next();
248                 for(Enumeration JavaDoc e = webBundleDescriptor.getWebComponentDescriptors(); e.hasMoreElements();){
249                         WebComponentDescriptorImpl comp = (WebComponentDescriptorImpl) e.nextElement();
250                         if (comp.getName().equals(""))
251                             comp.setName(comp.getCanonicalName());
252                 }
253         }
254         
255         // start IASRI4691307
256
// Initialize Sun Specific Descriptors HERE
257
//
258
com.sun.enterprise.tools.deployment.ui.sunone.SunOneUtils.createSunOneXml(openedApplication);
259         com.sun.enterprise.tools.deployment.Util.addMappingsSkeleton(openedApplication);
260         // end IASRI4691307
261
this.addApplication(openedApplication);
262 // ProjectImpl newProj = new ProjectImpl(openedApplication);
263
// newProj.load();
264
// Project.addProject(openedApplication, newProj);
265
openedApplication.doneOpening();
266     return openedApplication;
267     }
268     
269     /** Close the given application. */
270     public void closeApplication(Application application) {
271     applications.removeElement(application);
272
273 // UIProject.removeProject(application);
274
// ProjectImpl proj = (ProjectImpl)Project.getProject(application);
275
// if ( proj != null ) {
276
// proj.remove();
277
// Project.removeProject(application);
278
// }
279

280     this.setActiveApplication(null);
281     this.notify(APPLICATION_REMOVED, APPLICATION_PROPERTY, application);
282     }
283     
284     /** Adds an application to the manager.*/
285     public void addApplication(Application application) {
286     String JavaDoc newName = this.getUniqueApplicationName(application.getName());
287     application.setName(newName);
288     applications.addElement(application);
289     application.addNotificationListener(this);
290     this.notify(APPLICATION_ADDED, APPLICATION_PROPERTY, application);
291     this.setActiveApplication(application);
292     }
293
294     /* -------------------------------------------------------------------------
295     */

296
297     private static String JavaDoc CFG_APP_FILE = "applications"; // NOI18N
298
private String JavaDoc configFileName = CFG_APP_FILE;
299     protected String JavaDoc getConfigAppFileName() {
300     return this.configFileName;
301     }
302     public void setConfigAppFileName(String JavaDoc cfgAppFile) {
303     this.configFileName = (cfgAppFile != null)? cfgAppFile : CFG_APP_FILE;
304     }
305
306     /* -------------------------------------------------------------------------
307     ** NotificationListener interface
308     */

309
310     /** I am recieving a notification event.*/
311     public void notification(NotificationEvent ne) {
312     this.notify(ne.getType(), NotificationEvent.OBJECT_THAT_CHANGED,
313         ne.getObjectThatChanged());
314     }
315
316     /** Convenience method for notifying listeners.*/
317     public void notify(String JavaDoc type, String JavaDoc name, Object JavaDoc value) {
318     NotificationEvent ne = null;
319     if (name == null) {
320         ne = new NotificationEvent(this, type);
321     } else {
322         ne = new NotificationEvent(this, type, name, value);
323     }
324     Vector JavaDoc listenersClone = null;
325     synchronized (applicationListeners) {
326         listenersClone = (Vector JavaDoc) applicationListeners.clone();
327     }
328     for (Enumeration JavaDoc e = listenersClone.elements(); e.hasMoreElements();) {
329         NotificationListener nl = (NotificationListener) e.nextElement();
330         //System.out.println("notifying " + nl);
331
nl.notification(ne);
332         //System.out.println("Done");
333
}
334     //System.out.println("Notify: " + (System.currentTimeMillis() - time));
335
}
336
337     /* -------------------------------------------------------------------------
338     */

339
340     /** Formatted String. */
341     public String JavaDoc toString() {
342     return "Application Manager"; // NOI18N
343
}
344
345 }
346
347
Popular Tags