KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > layers > SessionManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup.layers;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import org.openide.filesystems.FileSystem;
27 import org.openide.util.Exceptions;
28
29 /** Session manager.
30  *
31  * @author Jan Pokorsky
32  */

33 public final class SessionManager {
34     /** session is opened */
35     public static final String JavaDoc PROP_OPEN = "session_open"; // NOI18N
36
/** session is closed */
37     public static final String JavaDoc PROP_CLOSE = "session_close"; // NOI18N
38
/** session layer */
39     public static final String JavaDoc LAYER_SESSION = "session"; // NOI18N
40
/** instalation layer */
41     public static final String JavaDoc LAYER_INSTALL = "install"; // NOI18N
42

43     private static SessionManager sm = null;
44     /** default system filesystem */
45     private SystemFileSystem systemFS;
46     private HashMap JavaDoc<String JavaDoc,FileSystem> layers = new HashMap JavaDoc<String JavaDoc,FileSystem>(); //<layer_key, fs>
47

48     /** Utility field holding list of PropertyChangeListeners. */
49     private transient java.util.ArrayList JavaDoc<PropertyChangeListener JavaDoc> propertyChangeListeners;
50     
51     /** Creates new SessionManager */
52     private SessionManager() {
53     }
54     
55     /** get default one */
56     public static SessionManager getDefault() {
57         if (sm == null) {
58             sm = new SessionManager();
59         }
60         return sm;
61     }
62     
63     /** Initializes and creates new repository. This repository's system fs is
64     * based on the content of ${HOME_DIR}/system and ${USER_DIR}/system directories
65     *
66     * @param userDir directory where user can write
67     * @param homeDir directory where netbeans has been installed, user need not have write access
68     * @param extradirs 0+ extra dirs to add; cf. #27151
69     * @return repository
70     * @exception PropertyVetoException if something fails
71     */

72     public FileSystem create(File JavaDoc userDir, File JavaDoc homeDir, File JavaDoc[] extradirs)
73     throws java.beans.PropertyVetoException JavaDoc, IOException JavaDoc {
74         systemFS = SystemFileSystem.create(userDir, homeDir, extradirs);
75         layers.put(LAYER_INSTALL, systemFS.getInstallationLayer());
76         layers.put(LAYER_SESSION, systemFS.getUserLayer());
77         return systemFS;
78     }
79     
80     /** Close session */
81     public void close() {
82         firePropertyChange(PROP_CLOSE);
83         waitForLocks ();
84     }
85     
86     /** get a layer associated with the name
87      * @param name layer name (LAYER_SESSION, ...)
88      * @return layer, can be <code>null</null>
89      */

90     public FileSystem getLayer(String JavaDoc name) {
91         return layers.get(name);
92     }
93
94     /** Registers PropertyChangeListener to receive events.
95      * @param listener The listener to register.
96      */

97     public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
98         if (propertyChangeListeners == null ) {
99             propertyChangeListeners = new java.util.ArrayList JavaDoc<PropertyChangeListener JavaDoc>();
100         }
101         propertyChangeListeners.add(listener);
102     }
103     
104     /** Removes PropertyChangeListener from the list of listeners.
105      * @param listener The listener to remove.
106      */

107     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc listener) {
108         if (propertyChangeListeners != null ) {
109             propertyChangeListeners.remove(listener);
110         }
111     }
112     
113     /** Notifies all registered listeners about the event.
114      * @param name the name to be fired
115      */

116     private void firePropertyChange(String JavaDoc name) {
117         java.util.ArrayList JavaDoc list;
118         synchronized (this) {
119             if (propertyChangeListeners == null || propertyChangeListeners.size() == 0) return;
120             list = (java.util.ArrayList JavaDoc)propertyChangeListeners.clone();
121         }
122         java.beans.PropertyChangeEvent JavaDoc event = new java.beans.PropertyChangeEvent JavaDoc(this, name, null, null);
123         for (int i = 0; i < list.size(); i++) {
124             try {
125                 ((PropertyChangeListener JavaDoc) list.get(i)).propertyChange(event);
126             }
127             catch (RuntimeException JavaDoc e) {
128                 Exceptions.printStackTrace(e);
129             }
130         }
131     }
132
133     private void waitForLocks () {
134         int count = 50; // 5 secs.
135

136         try {
137             while (LocalFileSystemEx.hasLocks () && 0 < count) {
138                 Thread.sleep(100);
139                 count--;
140             }
141         } catch (InterruptedException JavaDoc e) {
142             // ignore
143
}
144         
145         if (LocalFileSystemEx.hasLocks ()) {
146 // new Throwable ("SessionManager.waitForLocks callers thread.").printStackTrace ();
147

148             // timed out!
149
String JavaDoc locks [] = LocalFileSystemEx.getLocks ();
150             StringBuffer JavaDoc msg = new StringBuffer JavaDoc (256);
151             msg.append ("Settings saving "); //NOI18N
152
msg.append (count == 0 ? "timeout!" : "interrupted!"); //NOI18N
153
msg.append ("\nList of pending locks:\n"); //NOI18N
154
for (int i = 0; i < locks.length; i++) {
155                 msg.append (locks[i]);
156                 msg.append ("\n"); //NOI18N
157
/*
158                 Throwable source = LocalFileSystemEx.getLockSource (locks[i]);
159                 if (source != null) {
160                     StringWriter sw = new StringWriter (1024);
161                     PrintWriter w = new PrintWriter (sw);
162                     source.printStackTrace (w);
163                     w.close ();
164
165                     msg.append (sw.getBuffer ());
166                     msg.append ("\n"); //NOI18N
167                 }
168  */

169             }
170             System.err.println(msg.toString ());
171         }
172     }
173 }
174
Popular Tags