1 19 20 package org.netbeans.core.startup.layers; 21 22 import java.beans.PropertyChangeListener ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.HashMap ; 26 import org.openide.filesystems.FileSystem; 27 import org.openide.util.Exceptions; 28 29 33 public final class SessionManager { 34 35 public static final String PROP_OPEN = "session_open"; 37 public static final String PROP_CLOSE = "session_close"; 39 public static final String LAYER_SESSION = "session"; 41 public static final String LAYER_INSTALL = "install"; 43 private static SessionManager sm = null; 44 45 private SystemFileSystem systemFS; 46 private HashMap <String ,FileSystem> layers = new HashMap <String ,FileSystem>(); 48 49 private transient java.util.ArrayList <PropertyChangeListener > propertyChangeListeners; 50 51 52 private SessionManager() { 53 } 54 55 56 public static SessionManager getDefault() { 57 if (sm == null) { 58 sm = new SessionManager(); 59 } 60 return sm; 61 } 62 63 72 public FileSystem create(File userDir, File homeDir, File [] extradirs) 73 throws java.beans.PropertyVetoException , IOException { 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 81 public void close() { 82 firePropertyChange(PROP_CLOSE); 83 waitForLocks (); 84 } 85 86 90 public FileSystem getLayer(String name) { 91 return layers.get(name); 92 } 93 94 97 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { 98 if (propertyChangeListeners == null ) { 99 propertyChangeListeners = new java.util.ArrayList <PropertyChangeListener >(); 100 } 101 propertyChangeListeners.add(listener); 102 } 103 104 107 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener listener) { 108 if (propertyChangeListeners != null ) { 109 propertyChangeListeners.remove(listener); 110 } 111 } 112 113 116 private void firePropertyChange(String name) { 117 java.util.ArrayList list; 118 synchronized (this) { 119 if (propertyChangeListeners == null || propertyChangeListeners.size() == 0) return; 120 list = (java.util.ArrayList )propertyChangeListeners.clone(); 121 } 122 java.beans.PropertyChangeEvent event = new java.beans.PropertyChangeEvent (this, name, null, null); 123 for (int i = 0; i < list.size(); i++) { 124 try { 125 ((PropertyChangeListener ) list.get(i)).propertyChange(event); 126 } 127 catch (RuntimeException e) { 128 Exceptions.printStackTrace(e); 129 } 130 } 131 } 132 133 private void waitForLocks () { 134 int count = 50; 136 try { 137 while (LocalFileSystemEx.hasLocks () && 0 < count) { 138 Thread.sleep(100); 139 count--; 140 } 141 } catch (InterruptedException e) { 142 } 144 145 if (LocalFileSystemEx.hasLocks ()) { 146 148 String locks [] = LocalFileSystemEx.getLocks (); 150 StringBuffer msg = new StringBuffer (256); 151 msg.append ("Settings saving "); msg.append (count == 0 ? "timeout!" : "interrupted!"); msg.append ("\nList of pending locks:\n"); for (int i = 0; i < locks.length; i++) { 155 msg.append (locks[i]); 156 msg.append ("\n"); 169 } 170 System.err.println(msg.toString ()); 171 } 172 } 173 } 174 | Popular Tags |