1 23 package com.sun.enterprise.tools.deployment.main; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.util.Vector ; 30 import java.util.Enumeration ; 31 import java.util.Hashtable ; 32 import java.util.Properties ; 33 import com.sun.enterprise.deployment.*; 34 import com.sun.enterprise.util.NotificationListener; 35 import com.sun.enterprise.util.NotificationEvent; 36 38 41 42 public class ApplicationManager 43 implements NotificationListener 44 { 45 46 47 public static String ACTIVE_CHANGED = "active changed"; 49 50 public static String APPLICATION_CHANGED = "application changed"; 52 53 public static String APPLICATION_LIST_CHANGED = "application list changed"; 55 56 public static String LISTENER_ADDED = "listener added"; 58 59 public static String APPLICATION_ADDED = "application added"; 61 62 public static String APPLICATION_REMOVED = "application removed"; 64 65 public static String APPLICATION_PROPERTY = "application property"; 67 69 private Application activeApplication; 70 private Vector applications = new Vector (); 71 private Vector applicationListeners = new Vector (); 72 73 private File preferencesDirectory; 74 private File temp; 75 76 79 public ApplicationManager(File preferencesDirectory, File temp) { 80 this.preferencesDirectory = preferencesDirectory; 81 this.temp = temp; 82 } 83 84 85 public File getTemp() { 86 return temp; 87 } 88 89 93 public Hashtable restoreFromUserHome() throws IOException { 94 Hashtable badApplicationJarFilenamesToExceptions = new Hashtable (); 95 File appsFile = new File (preferencesDirectory, getConfigAppFileName()); 96 if (appsFile.exists()) { 97 FileInputStream fis = new FileInputStream (appsFile); 98 Properties applications = new Properties (); 99 applications.load(fis); 100 for (Enumeration e = applications.propertyNames(); 101 e.hasMoreElements();) { 102 String appName = (String ) e.nextElement(); 103 String appJarFilename = (String ) 104 applications.getProperty(appName); 105 Application openedApp=null; 106 try { 107 this.openApplication(new File (appJarFilename)); 108 } catch (Throwable t) { 109 badApplicationJarFilenamesToExceptions.put( 110 appJarFilename, t); 111 } 112 } 113 if (fis != null) { 114 fis.close(); 115 } 116 } 117 return badApplicationJarFilenamesToExceptions; 118 } 119 120 122 public void saveToUserHome() throws IOException { 123 File appsFile = new File (preferencesDirectory, getConfigAppFileName()); 124 FileOutputStream fos = new FileOutputStream (appsFile); 125 Properties applications = new Properties (); 126 for (Enumeration 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"); if (fos != null) { 135 fos.close(); 136 } 137 } 138 139 140 public void addNotificationListener(NotificationListener nl) { 141 applicationListeners.addElement(nl); 142 this.notify(LISTENER_ADDED, null, null); 143 } 144 145 146 public void removeNotificationListener(NotificationListener nl) { 147 applicationListeners.removeElement(nl); 148 } 149 150 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 163 public Application getActiveApplication() { 164 return activeApplication; 165 } 166 167 168 public Vector getApplications() { 169 Vector clone = (Vector ) applications.clone(); 170 return clone; 171 } 172 173 public Application getApplicationWithJar(File jarFilename) { 174 for (Enumeration 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 187 public Vector getApplicationNames() { 188 Vector names = new Vector (); 189 for (Enumeration e = applications.elements();e.hasMoreElements();) { 190 names.addElement(((Application) e.nextElement()).getName()); 191 } 192 return names; 193 } 194 195 protected String getUniqueApplicationName(String trialName) { 196 return Application.createUniqueNameAmongst(trialName, 197 this.getApplicationNames()); 198 } 199 200 202 public Application newApplication(String name, String jarFile) { 203 Application newApplication = new Application( 204 this.getUniqueApplicationName(name), new File (jarFile)); 205 206 209 try { 210 this.saveApplication(newApplication); 211 } catch (IOException ioe) { 212 Log.print(this, ioe); 213 } 214 this.addApplication(newApplication); 215 return newApplication; 216 } 217 218 219 public void saveApplication(Application application) 220 throws IOException { 221 ApplicationArchivist archivist = application.getApplicationArchivist(); 222 archivist.save(archivist.getApplicationFile(), true); 223 224 } 229 230 232 public void saveAsApplication(Application application, File newJar) 233 throws IOException { 234 ApplicationArchivist archivist = application.getApplicationArchivist(); 235 archivist.save(newJar, true); 236 } 241 242 243 public Application openApplication(File name) throws Exception { 244 Application openedApplication = ApplicationArchivist.openAT(name); for (java.util.Iterator itr = openedApplication.getWebBundleDescriptors().iterator(); itr.hasNext();) { 247 WebBundleDescriptor webBundleDescriptor = (WebBundleDescriptor) itr.next(); 248 for(Enumeration 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 com.sun.enterprise.tools.deployment.ui.sunone.SunOneUtils.createSunOneXml(openedApplication); 259 com.sun.enterprise.tools.deployment.Util.addMappingsSkeleton(openedApplication); 260 this.addApplication(openedApplication); 262 openedApplication.doneOpening(); 266 return openedApplication; 267 } 268 269 270 public void closeApplication(Application application) { 271 applications.removeElement(application); 272 273 280 this.setActiveApplication(null); 281 this.notify(APPLICATION_REMOVED, APPLICATION_PROPERTY, application); 282 } 283 284 285 public void addApplication(Application application) { 286 String 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 296 297 private static String CFG_APP_FILE = "applications"; private String configFileName = CFG_APP_FILE; 299 protected String getConfigAppFileName() { 300 return this.configFileName; 301 } 302 public void setConfigAppFileName(String cfgAppFile) { 303 this.configFileName = (cfgAppFile != null)? cfgAppFile : CFG_APP_FILE; 304 } 305 306 309 310 311 public void notification(NotificationEvent ne) { 312 this.notify(ne.getType(), NotificationEvent.OBJECT_THAT_CHANGED, 313 ne.getObjectThatChanged()); 314 } 315 316 317 public void notify(String type, String name, Object 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 listenersClone = null; 325 synchronized (applicationListeners) { 326 listenersClone = (Vector ) applicationListeners.clone(); 327 } 328 for (Enumeration e = listenersClone.elements(); e.hasMoreElements();) { 329 NotificationListener nl = (NotificationListener) e.nextElement(); 330 nl.notification(ne); 332 } 334 } 336 337 339 340 341 public String toString() { 342 return "Application Manager"; } 344 345 } 346 347 | Popular Tags |