1 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 32 35 36 public class StandAloneManager 37 implements NotificationListener 38 { 39 40 43 44 public static final String STANDALONE_PROPERTY = "StandAloneProperty"; public static final String STANDALONE_ADDED = "StandAloneAdded"; public static final String STANDALONE_REMOVED = "StandAloneRemoved"; public static final String LISTENER_ADDED = "ListenerAdded"; public static final String ACTIVE_CHANGED = "ActiveChanged"; 50 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 63 64 public StandAloneManager(File preferencesDirectory, File temp) 65 { 66 this.preferencesDirectory = preferencesDirectory; 67 this.temp = temp; 68 } 69 70 72 73 74 public File getTemp() { 75 return temp; 76 } 77 78 79 public Vector getStandAlones() 80 { 81 return (Vector)this.standalones.clone(); 82 } 83 84 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 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 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 } 116 } 117 return null; 118 } 119 120 121 public Descriptor getActiveStandAlone() 122 { 123 return this.activeStandAlone; 124 } 125 126 127 protected String getUniqueStandAloneName(String trialName) 128 { 129 return Descriptor.createUniqueNameAmongst(trialName, 130 this.getStandAloneNames()); 131 } 132 133 135 136 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 name = (String )e.nextElement(); 149 String jarFilename = (String )props.getProperty(name); 150 try { 151 this.openStandAlone(new File(jarFilename)); 152 } catch (Throwable t) { 153 badFiles.put(jarFilename, t); 154 } 155 } 156 fis.close(); 157 } 158 return badFiles; 159 } 160 161 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 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"); fos.close(); 189 } 190 191 193 194 public Descriptor openStandAlone(File name) 195 throws Exception 196 { 197 198 199 if (ConnectorArchivist.isConnector(name)) { 200 ConnectorDescriptor d = ConnectorArchivist.open(name, false, true); 203 205 this.addStandAlone(d); 206 207 return d; 211 } 212 213 214 if (ApplicationArchivist.isApplication(name)) { 215 Application d = ApplicationArchivist.openAT(name); this.addStandAlone(d); 217 d.doneOpening(); 221 return d; 222 } 223 224 225 return null; 226 227 } 228 229 230 public void closeStandAlone(Descriptor desc) 231 { 232 this.standalones.removeElement(desc); 233 this.setActiveStandAlone(null); 240 this.notify(STANDALONE_REMOVED, STANDALONE_PROPERTY, desc); 241 } 242 243 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 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 267 268 269 public void addStandAlone(Descriptor desc) 270 { 271 String oldName = desc.getName(); 272 String 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 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 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 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 321 322 323 public void addNotificationListener(NotificationListener nl) 324 { 325 standaloneListeners.addElement(nl); 326 this.notify(LISTENER_ADDED, null, null); 327 } 328 329 330 public void removeNotificationListener(NotificationListener nl) 331 { 332 standaloneListeners.removeElement(nl); 333 } 334 335 336 public void notification(NotificationEvent ne) 337 { 338 this.notify(ne.getType(), NotificationEvent.OBJECT_THAT_CHANGED, 339 ne.getObjectThatChanged()); 340 } 341 342 343 public void notify(String type, String name, Object value) 344 { 345 NotificationEvent ne = (name == null)? 346 new NotificationEvent(this, type) : 347 new NotificationEvent(this, type, name, value); 348 349 350 Vector listenersClone = null; 351 synchronized (standaloneListeners) { 352 listenersClone = (Vector)standaloneListeners.clone(); 353 } 354 355 356 for (Enumeration e = listenersClone.elements(); e.hasMoreElements();) { 357 NotificationListener nl = (NotificationListener)e.nextElement(); 358 nl.notification(ne); 359 } 360 361 } 362 363 365 366 public static final String CFG_CONNECTOR_FILE = "standalone"; private String configFileName = CFG_CONNECTOR_FILE; 368 protected String getConfigFileName() 369 { 370 return this.configFileName; 371 } 372 373 375 376 377 public String toString() { 378 return "Stand-Alone Manager"; } 380 381 } 382 383 384 | Popular Tags |