KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
26 import java.util.*;
27 import com.sun.enterprise.util.*;
28 import com.sun.enterprise.deployment.*;
29 //import com.sun.enterprise.tools.deployment.ui.UIUtils;
30
//import com.sun.enterprise.tools.deployment.ui.utils.*;
31

32 /* Manage stand-alone objects within the deploytool.
33 ** @author Martin D. Flynn
34 */

35
36 public class StandAloneManager
37     implements NotificationListener
38 {
39
40     /* ------------------------------------------------------------------------
41     ** Constants
42     */

43
44     public static final String JavaDoc STANDALONE_PROPERTY = "StandAloneProperty"; // NOI18N
45
public static final String JavaDoc STANDALONE_ADDED = "StandAloneAdded"; // NOI18N
46
public static final String JavaDoc STANDALONE_REMOVED = "StandAloneRemoved"; // NOI18N
47
public static final String JavaDoc LISTENER_ADDED = "ListenerAdded"; // NOI18N
48
public static final String JavaDoc ACTIVE_CHANGED = "ActiveChanged"; // NOI18N
49

50     /* ------------------------------------------------------------------------
51     */

52
53     private Vector standalones = new Vector();
54     private Vector standaloneListeners = new Vector();
55     private Descriptor activeStandAlone = null;
56
57     private File preferencesDirectory = null;
58     private File temp = null;
59
60     /* ------------------------------------------------------------------------
61     ** constructors
62     */

63
64     public StandAloneManager(File preferencesDirectory, File temp)
65     {
66     this.preferencesDirectory = preferencesDirectory;
67     this.temp = temp;
68     }
69
70     /* ------------------------------------------------------------------------
71     */

72     
73     /** Gets the temporary directory. */
74     public File getTemp() {
75     return temp;
76     }
77             
78     /* return Vector containing open stand-alone objects */
79     public Vector getStandAlones()
80     {
81     return (Vector)this.standalones.clone();
82     }
83
84     /* return names of the open stand-alone objects */
85     public Vector getStandAloneNames()
86     {
87     Vector names = new Vector();
88     Enumeration e = this.standalones.elements();
89     for (;e.hasMoreElements();) {
90         names.addElement(((Descriptor)e.nextElement()).getName());
91     }
92     return names;
93     }
94         
95     public Descriptor getStandAloneWithJar(File jarFilename)
96     {
97     String JavaDoc absPath = jarFilename.getAbsolutePath();
98     Enumeration e = this.standalones.elements();
99     for (;e.hasMoreElements();) {
100         Descriptor d = (Descriptor)e.nextElement();
101         if (d instanceof ConnectorDescriptor) {
102         String JavaDoc uri = ((ConnectorDescriptor)d).getArchivist().
103             getArchiveUri();
104             if ((new File(uri)).getAbsolutePath().equals(absPath)) {
105             return d;
106             }
107         } else
108         if (d instanceof Application) {
109             if (((Application)d).getApplicationArchivist().
110             getApplicationFile().getAbsolutePath().equals(absPath)) {
111             return d;
112             }
113         } else {
114         // XXX - unknown descriptor
115
}
116     }
117     return null;
118     }
119     
120     /** Return the active stand-alone object */
121     public Descriptor getActiveStandAlone()
122     {
123     return this.activeStandAlone;
124     }
125
126     /* uniquify stand-alone name */
127     protected String JavaDoc getUniqueStandAloneName(String JavaDoc trialName)
128     {
129     return Descriptor.createUniqueNameAmongst(trialName,
130         this.getStandAloneNames());
131     }
132
133     /* ------------------------------------------------------------------------
134     */

135
136     /* retore state from the last session,
137     ** return Hashtable of filenames that could not be reopened */

138     public Hashtable restoreFromUserHome()
139     throws IOException
140     {
141     Hashtable badFiles = new Hashtable();
142     File propsFile = new File(preferencesDirectory, getConfigFileName());
143     if (propsFile.exists()) {
144         FileInputStream fis = new FileInputStream(propsFile);
145         Properties props = new Properties();
146         props.load(fis);
147         for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
148         String JavaDoc name = (String JavaDoc)e.nextElement();
149         String JavaDoc jarFilename = (String JavaDoc)props.getProperty(name);
150         try {
151             this.openStandAlone(new File(jarFilename));
152         } catch (Throwable JavaDoc t) {
153             badFiles.put(jarFilename, t);
154         }
155         }
156             fis.close();
157     }
158     return badFiles;
159     }
160     
161     /** Saves the list of open stand-alone objects to the working dir */
162     public void saveToUserHome()
163     throws IOException
164     {
165     File propsFile = new File(preferencesDirectory, getConfigFileName());
166     FileOutputStream fos = new FileOutputStream(propsFile);
167     Properties props = new Properties();
168     Enumeration e = this.standalones.elements();
169     for (;e.hasMoreElements();) {
170         Descriptor next = (Descriptor)e.nextElement();
171         String JavaDoc loc = null;
172         if (next instanceof ConnectorDescriptor) {
173         loc = ((ConnectorDescriptor)next).getArchivist().
174             getArchiveUri();
175         } else
176         if (next instanceof Application) {
177         loc = ((Application)next).getApplicationArchivist().
178             getApplicationFile().toString();
179         }
180         if (loc != null) {
181             props.put(next.getName(), loc);
182         } else {
183         System.err.println("Unsupported Stand-Alone descriptor type:");
184         System.err.println(" " + next.getClass().getName());
185         }
186     }
187     props.store(fos, "J2EE Stand-Alone Descriptors"); // NOI18N
188
fos.close();
189     }
190
191     /* ------------------------------------------------------------------------
192     */

193
194     public Descriptor openStandAlone(File name)
195     throws Exception JavaDoc
196     {
197
198     /* ConnectorDescriptor */
199     if (ConnectorArchivist.isConnector(name)) {
200             //IASRI 4691307 Anissa
201
//Open the rar file with ValidateXML set to false, and include S1AS descriptor.
202
ConnectorDescriptor d = ConnectorArchivist.open(name, false, true);
203             // IASRI 4691307 end.
204

205         this.addStandAlone(d);
206             
207 // ProjectImpl newProj = new ProjectImpl(d);
208
// newProj.load();
209
// Project.addProject(d, newProj);
210
return d;
211     }
212
213     /* Application */
214     if (ApplicationArchivist.isApplication(name)) {
215         Application d = ApplicationArchivist.openAT(name); //bug# 4774785; 4691307
216
this.addStandAlone(d);
217 // ProjectImpl newProj = new ProjectImpl(d);
218
// newProj.load();
219
// Project.addProject(d, newProj);
220
d.doneOpening();
221         return d;
222     }
223
224     /* not found */
225     return null;
226
227     }
228     
229     /** Close the given stand-alone object */
230     public void closeStandAlone(Descriptor desc)
231     {
232     this.standalones.removeElement(desc);
233 // UIProject.removeProject(desc);
234
// ProjectImpl proj = (ProjectImpl)Project.getProject(desc);
235
// if (proj != null) {
236
// proj.remove();
237
// Project.removeProject(desc);
238
// }
239
this.setActiveStandAlone(null);
240     this.notify(STANDALONE_REMOVED, STANDALONE_PROPERTY, desc);
241     }
242
243     /* save specified stand-alone object */
244     public void saveStandAlone(Descriptor desc)
245     throws IOException
246     {
247     if (desc instanceof ConnectorDescriptor) {
248         ConnectorDescriptor cd = (ConnectorDescriptor)desc;
249         ConnectorArchivist ca = (ConnectorArchivist)cd.getArchivist();
250         ca.save(new File(ca.getArchiveUri()), true);
251     }
252     }
253
254     /* save specified stand-alone object */
255     public void saveStandAloneAs(Descriptor desc, File newFile)
256     throws IOException
257     {
258     if (desc instanceof ConnectorDescriptor) {
259         ConnectorDescriptor cd = (ConnectorDescriptor)desc;
260         ConnectorArchivist ca = (ConnectorArchivist)cd.getArchivist();
261         ca.save(newFile, true);
262     }
263     }
264
265     /* -------------------------------------------------------------------------
266     */

267     
268     /* Adds a stand-alone object to the manager */
269     public void addStandAlone(Descriptor desc)
270     {
271     String JavaDoc oldName = desc.getName();
272     String JavaDoc newName = this.getUniqueStandAloneName(oldName);
273     if (!oldName.equals(newName)) {
274         desc.setName(newName);
275     }
276     this.standalones.addElement(desc);
277     desc.addNotificationListener(this);
278     this.notify(STANDALONE_ADDED, STANDALONE_PROPERTY, desc);
279     this.setActiveStandAlone(desc);
280     }
281
282     /* Sets the active stand-alone object */
283     public void setActiveStandAlone(Descriptor desc)
284     {
285     if (desc != this.activeStandAlone) {
286         this.activeStandAlone = desc;
287         if (desc != null) {
288         this.notify(ACTIVE_CHANGED, STANDALONE_PROPERTY, desc);
289         } else {
290         this.notify(ACTIVE_CHANGED, null, null);
291         }
292     }
293     }
294
295     /* return true if specified descriptor belongs to the stand-alone set */
296     public boolean isStandAloneDescriptor(Descriptor desc)
297     {
298     Enumeration e = this.standalones.elements();
299     for (;e.hasMoreElements();) {
300         if (desc == e.nextElement()) {
301         return true;
302         }
303     }
304     return false;
305     }
306
307     /* return true if stand-alone object descriptor is dirty (been changed) */
308     public boolean isDirty(Descriptor desc)
309     {
310     if (desc instanceof ConnectorDescriptor) {
311         return ((ConnectorDescriptor)desc).isDirty();
312     } else
313     if (desc instanceof Application) {
314         return ((Application)desc).isDirty();
315     }
316     return false;
317     }
318
319     /* ------------------------------------------------------------------------
320     */

321     
322     /*rRegister for state changes */
323     public void addNotificationListener(NotificationListener nl)
324     {
325     standaloneListeners.addElement(nl);
326     this.notify(LISTENER_ADDED, null, null);
327     }
328     
329     /* deregister for state changes */
330     public void removeNotificationListener(NotificationListener nl)
331     {
332     standaloneListeners.removeElement(nl);
333     }
334
335     /* forward notification events */
336     public void notification(NotificationEvent ne)
337     {
338     this.notify(ne.getType(), NotificationEvent.OBJECT_THAT_CHANGED,
339         ne.getObjectThatChanged());
340     }
341
342     /** Convenience method for notifying listeners.*/
343     public void notify(String JavaDoc type, String JavaDoc name, Object JavaDoc value)
344     {
345     NotificationEvent ne = (name == null)?
346         new NotificationEvent(this, type) :
347         new NotificationEvent(this, type, name, value);
348
349     /* make a copy of the listener list */
350     Vector listenersClone = null;
351     synchronized (standaloneListeners) {
352         listenersClone = (Vector)standaloneListeners.clone();
353     }
354
355     /* notify listeners */
356     for (Enumeration e = listenersClone.elements(); e.hasMoreElements();) {
357         NotificationListener nl = (NotificationListener)e.nextElement();
358         nl.notification(ne);
359     }
360
361     }
362
363     /* -------------------------------------------------------------------------
364     */

365
366     public static final String JavaDoc CFG_CONNECTOR_FILE = "standalone"; // NOI18N
367
private String JavaDoc configFileName = CFG_CONNECTOR_FILE;
368     protected String JavaDoc getConfigFileName()
369     {
370     return this.configFileName;
371     }
372
373     /* -------------------------------------------------------------------------
374     */

375
376     /** Formatted String. */
377     public String JavaDoc toString() {
378     return "Stand-Alone Manager"; // NOI18N
379
}
380
381 }
382
383
384
Popular Tags