1 19 20 package org.netbeans.modules.j2ee.deployment.impl; 21 22 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 23 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 24 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceCreationException; 25 import javax.enterprise.deploy.spi.DeploymentManager ; 26 import org.openide.filesystems.*; 27 import org.openide.*; 28 import org.openide.util.Lookup; 29 import org.openide.util.NbBundle; 30 31 import java.util.*; 32 import java.io.*; 33 import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener; 34 import org.netbeans.modules.j2ee.deployment.profiler.spi.Profiler; 35 import org.openide.modules.InstalledFileLocator; 36 37 39 public final class ServerRegistry implements java.io.Serializable { 40 41 public static final String DIR_INSTALLED_SERVERS = "/J2EE/InstalledServers"; public static final String DIR_JSR88_PLUGINS = "/J2EE/DeploymentPlugins"; public static final String URL_ATTR = InstanceProperties.URL_ATTR; 44 public static final String USERNAME_ATTR = InstanceProperties.USERNAME_ATTR; 45 public static final String PASSWORD_ATTR = InstanceProperties.PASSWORD_ATTR; 46 public static final String FILE_DEFAULT_INSTANCE = "DefaultInstance.settings"; public static final String J2EE_DEFAULT_SERVER = "j2ee.defaultServer"; public static final String TARGETNAME_ATTR = "targetName"; public static final String SERVER_NAME = "serverName"; private static ServerRegistry instance = null; 51 public synchronized static ServerRegistry getInstance() { 52 if(instance == null) instance = new ServerRegistry(); 53 return instance; 54 55 } 58 59 62 public synchronized static boolean wasInitialized () { 63 return instance != null && instance.servers != null && instance.instances != null; 64 } 65 private transient Map servers = null; 66 private transient Map instances = null; 67 private transient Collection pluginListeners = new HashSet(); 68 private transient Collection instanceListeners = new ArrayList(); 69 private transient InstanceListener[] instanceListenersArray; 70 71 private ServerString defaultInstance; 73 74 public ServerRegistry() { 75 } 76 private synchronized void init() { 77 if (servers != null && instances != null) 78 return; 79 servers = new HashMap(); 81 instances = new HashMap(); 82 Repository rep = (Repository) Lookup.getDefault().lookup(Repository.class); 83 FileObject dir = rep.getDefaultFileSystem().findResource(DIR_JSR88_PLUGINS); 84 dir.addFileChangeListener(new PluginInstallListener()); 85 FileObject[] ch = dir.getChildren(); 86 for(int i = 0; i < ch.length; i++) { 87 addPlugin(ch[i]); 89 } 91 92 dir = rep.getDefaultFileSystem().findResource(DIR_INSTALLED_SERVERS); 93 dir.addFileChangeListener(new InstanceInstallListener()); 94 ch = dir.getChildren(); 95 96 for(int i = 0; i < ch.length; i++) { 97 addInstance(ch[i]); 99 } 101 } 103 private Map serversMap() { 104 init(); 105 return servers; 106 } 107 private Map instancesMap() { 108 init(); 109 return instances; 110 } 111 private synchronized void addPlugin(FileObject fo) { 112 String name = ""; try { 114 if(fo.isFolder()) { 115 name = fo.getName(); 116 if(serversMap().containsKey(name)) return; 117 Server server = new Server(fo); 118 serversMap().put(name,server); 119 firePluginListeners(server,true); 120 } 121 } catch (Exception e) { 122 ErrorManager.getDefault().log(ErrorManager.WARNING, ("Plugin "+name+" installation failed")); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 124 } 125 } 126 127 synchronized void removePlugin(FileObject fo) { 129 String name = fo.getName(); 130 if(serversMap().containsKey(name)) { 131 Server server = (Server) serversMap().get(name); 132 if (server != null) { 133 ServerInstance[] instances = getServerInstances(); 135 for (int i = 0; i < instances.length; i++) { 136 ServerInstance si = instances[i]; 137 if (server.equals(si.getServer())) { 138 removeServerInstance(si.getUrl()); 139 } 140 } 141 } 142 serversMap().remove(name); 143 firePluginListeners(server,false); 144 } 145 } 146 147 class PluginInstallListener extends LayerListener { 148 public void fileFolderCreated(FileEvent fe) { 149 super.fileFolderCreated(fe); 150 addPlugin(fe.getFile()); 151 } 152 public void fileDeleted(FileEvent fe) { 153 super.fileDeleted(fe); 154 removePlugin(fe.getFile()); 155 } 156 } 157 158 class InstanceInstallListener extends LayerListener { 159 public void fileDataCreated(FileEvent fe) { 160 super.fileDataCreated(fe); 161 addInstance(fe.getFile()); 162 } 163 } 165 166 class LayerListener implements FileChangeListener { 167 168 public void fileAttributeChanged(FileAttributeEvent fae) { 169 java.util.logging.Logger.getLogger(ServerRegistry.class.getName()).log(java.util.logging.Level.FINEST,"Attribute changed event"); } 171 public void fileChanged(FileEvent fe) { 172 } 173 public void fileFolderCreated(FileEvent fe) { 174 } 175 public void fileRenamed(FileRenameEvent fe) { 176 } 177 178 public void fileDataCreated(FileEvent fe) { 179 } 180 public void fileDeleted(FileEvent fe) { 181 } 182 183 } 184 185 public Collection getServers() { 186 return serversMap().values(); 187 } 188 189 public Collection getInstances() { 190 return instancesMap().values(); 191 } 192 193 public String [] getInstanceURLs() { 194 return (String []) instancesMap().keySet().toArray(new String [instancesMap().size()]); 195 } 196 197 public void checkInstanceAlreadyExists(String url) throws InstanceCreationException { 198 if (getServerInstance(url) != null) { 199 String msg = NbBundle.getMessage(ServerRegistry.class, "MSG_InstanceAlreadyExists", url); 200 throw new InstanceCreationException(msg); 201 } 202 } 203 204 public void checkInstanceExists(String url) { 205 if (getServerInstance(url) == null) { 206 String msg = NbBundle.getMessage(ServerRegistry.class, "MSG_InstanceNotExists", url); 207 throw new IllegalArgumentException (msg); 208 } 209 } 210 211 public Server getServer(String name) { 212 return (Server) serversMap().get(name); 213 } 214 215 public void addPluginListener(PluginListener pl) { 216 pluginListeners.add(pl); 217 } 218 219 public ServerInstance getServerInstance(String url) { 220 return (ServerInstance) instancesMap().get(url); 221 } 222 223 public void removeServerInstance(String url) { 224 if (url == null) 225 return; 226 227 ServerString def = getDefaultInstance(); 229 if (url.equals(def.getUrl())) { 230 defaultInstance = null; 231 } 232 233 ServerInstance instance = (ServerInstance) instancesMap().remove(url); 234 if (instance != null) { 235 fireInstanceListeners(url, false); 236 removeInstanceFromFile(url); 237 } 238 ServerString newinst = getDefaultInstance(false); 239 fireDefaultInstance(def != null ? def.getUrl() : null, 240 newinst != null ? newinst.getUrl() : null); 241 } 242 243 public ServerInstance[] getServerInstances() { 244 ServerInstance[] ret = new ServerInstance[instancesMap().size()]; 245 instancesMap().values().toArray(ret); 246 return ret; 247 } 248 249 public static FileObject getInstanceFileObject(String url) { 250 Repository rep = (Repository) Lookup.getDefault().lookup(Repository.class); 251 FileObject[] installedServers = rep.getDefaultFileSystem().findResource(DIR_INSTALLED_SERVERS).getChildren(); 252 for (int i=0; i<installedServers.length; i++) { 253 String val = (String ) installedServers[i].getAttribute(URL_ATTR); 254 if (val != null && val.equals(url)) 255 return installedServers[i]; 256 } 257 return null; 258 } 259 260 270 public void addInstance(String url, String username, String password, 271 String displayName) throws InstanceCreationException { 272 if (url == null || url.equals("")) { ErrorManager.getDefault().log(NbBundle.getMessage(ServerRegistry.class, "MSG_EmptyUrl")); 275 return; 276 } 277 278 checkInstanceAlreadyExists(url); 279 if (!addInstanceImpl(url, username, password, displayName)) { 280 throw new InstanceCreationException(NbBundle.getMessage(ServerRegistry.class, "MSG_FailedToCreateInstance", displayName)); 281 } 282 } 283 284 private synchronized void writeInstanceToFile(String url, String username, String password) throws IOException { 285 if (url == null) { 286 ErrorManager.getDefault().log(ErrorManager.ERROR, NbBundle.getMessage(ServerRegistry.class, "MSG_NullUrl")); 287 return; 288 } 289 Repository rep = (Repository) Lookup.getDefault().lookup(Repository.class); 290 FileObject dir = rep.getDefaultFileSystem().findResource(DIR_INSTALLED_SERVERS); 291 FileObject instanceFOs[] = dir.getChildren(); 292 FileObject instanceFO = null; 293 for (int i=0; i<instanceFOs.length; i++) { 294 if (url.equals(instanceFOs[i].getAttribute(URL_ATTR))) 295 instanceFO = instanceFOs[i]; 296 } 297 String name = FileUtil.findFreeFileName(dir,"instance",null); 298 if (instanceFO == null) 299 instanceFO = dir.createData(name); 300 instanceFO.setAttribute(URL_ATTR, url); 301 instanceFO.setAttribute(USERNAME_ATTR, username); 302 instanceFO.setAttribute(PASSWORD_ATTR, password); 303 } 304 305 private synchronized void removeInstanceFromFile(String url) { 306 FileObject instanceFO = getInstanceFileObject(url); 307 if (instanceFO == null) 308 return; 309 try { 310 instanceFO.delete(); 311 } catch (IOException ioe) { 312 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 313 } 314 } 315 316 327 private synchronized boolean addInstanceImpl(String url, String username, 328 String password, String displayName) { 329 if (instancesMap().containsKey(url)) return false; 330 for(Iterator i = serversMap().values().iterator(); i.hasNext();) { 331 Server server = (Server) i.next(); 332 try { 333 if(server.handlesUri(url)) { 334 ServerInstance instance = new ServerInstance(server,url); 335 instancesMap().put(url,instance); 337 ServerString str = new ServerString(server.getShortName(),url,null); 340 writeInstanceToFile(url,username,password); 341 if (displayName != null) instance.getInstanceProperties().setProperty( 342 InstanceProperties.DISPLAY_NAME_ATTR, displayName); 343 DeploymentManager manager = server.getDisconnectedDeploymentManager(url); 344 if (manager != null) { 345 fireInstanceListeners(url, true); 346 return true; 347 } else { 348 removeInstanceFromFile(url); 349 instancesMap().remove(url); 350 } 351 } 352 } catch (Exception e) { 353 if (instancesMap().containsKey(url)) { 354 removeInstanceFromFile(url); 355 instancesMap().remove(url); 356 } 357 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 358 } 359 } 360 return false; 361 } 362 363 public void addInstance(FileObject fo) { 364 String url = (String ) fo.getAttribute(URL_ATTR); 365 String username = (String ) fo.getAttribute(USERNAME_ATTR); 366 String password = (String ) fo.getAttribute(PASSWORD_ATTR); 367 String displayName = (String ) fo.getAttribute(InstanceProperties.DISPLAY_NAME_ATTR); 368 addInstanceImpl(url, username, password, displayName); 370 } 371 372 public Collection getInstances(InstanceListener il) { 373 if (il != null) { 374 synchronized(instanceListeners) { 375 instanceListenersArray = null; 376 instanceListeners.add(il); 377 } 378 } 379 return getInstances(); 380 } 381 382 public void addInstanceListener(InstanceListener il) { 383 synchronized(instanceListeners) { 384 instanceListenersArray = null; 385 instanceListeners.add(il); 386 } 387 } 388 389 public void removeInstanceListener(InstanceListener il) { 390 synchronized(instanceListeners) { 391 instanceListenersArray = null; 392 instanceListeners.remove(il); 393 } 394 } 395 396 public synchronized void removePluginListener(PluginListener pl) { 397 pluginListeners.remove(pl); 398 } 399 400 private void firePluginListeners(Server server, boolean add) { 401 for(Iterator i = pluginListeners.iterator();i.hasNext();) { 402 PluginListener pl = (PluginListener)i.next(); 403 if(add) pl.serverAdded(server); 404 else pl.serverRemoved(server); 405 } 406 configNamesByType = null; 407 } 408 409 410 private InstanceListener[] getInstanceListeners() { 411 InstanceListener[] retValue = null; 412 synchronized (instanceListeners) { 413 retValue = instanceListenersArray; 414 if (retValue == null) { 415 retValue = (InstanceListener[])instanceListeners.toArray(new InstanceListener[instanceListeners.size()]); 416 instanceListenersArray = retValue; 417 } 418 } 419 return retValue; 420 } 421 422 private void fireInstanceListeners(String instance, boolean add) { 423 InstanceListener[] instListeners = getInstanceListeners(); 424 for(int i = 0; i < instListeners.length; i++) { 425 if(add) { 426 instListeners[i].instanceAdded(instance); 427 } else { 428 instListeners[i].instanceRemoved(instance); 429 } 430 } 431 } 432 433 private void fireDefaultInstance(String oldInstance, String newInstance) { 434 InstanceListener[] instListeners = getInstanceListeners(); 435 for(int i = 0; i < instListeners.length; i++) { 436 instListeners[i].changeDefaultInstance(oldInstance, newInstance); 437 } 438 } 439 440 public void setDefaultInstance(ServerString instance) { 441 if (instance != null && instance.equals(defaultInstance)) { 442 return; 443 } 444 445 if (instance == null) { 446 removeDefaultInstanceFile(); 447 ServerString oldValue = defaultInstance; 448 defaultInstance = null; 449 fireDefaultInstance(oldValue != null ? oldValue.getUrl() : null, null); 450 } else { 451 if (ServerStringConverter.writeServerInstance(instance, DIR_INSTALLED_SERVERS, FILE_DEFAULT_INSTANCE)) { 452 ServerString oldValue = defaultInstance; 453 defaultInstance = instance; 454 fireDefaultInstance(oldValue != null ? oldValue.getUrl() : null, 455 instance != null ? instance.getUrl() : null); 456 } 457 } 458 } 459 460 static private void removeDefaultInstanceFile() { 461 FileLock lock = null; 462 Writer writer = null; 463 try { 464 String pathName = DIR_INSTALLED_SERVERS + "/" + FILE_DEFAULT_INSTANCE; FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(pathName); 466 if (fo != null) 467 fo.delete(); 468 } catch(Exception ioe) { 469 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, ioe); 470 } 471 } 472 473 private ServerString getInstallerDefaultPlugin() { 474 File propFile = InstalledFileLocator.getDefault ().locate ("config/install.properties", null, false); Properties installProp = readProperties(propFile); 477 String j2eeDefaultServerFileName = installProp.getProperty(J2EE_DEFAULT_SERVER); 478 if (j2eeDefaultServerFileName == null) 479 return null; 480 481 File serverFile = InstalledFileLocator.getDefault ().locate (j2eeDefaultServerFileName, null, false); 482 Properties defaultServerProp = readProperties(serverFile); 483 String serverName = defaultServerProp.getProperty(SERVER_NAME); 484 String url = defaultServerProp.getProperty(URL_ATTR); 485 String user = defaultServerProp.getProperty(USERNAME_ATTR); 486 String password = defaultServerProp.getProperty(PASSWORD_ATTR); 487 String targetName = defaultServerProp.getProperty(TARGETNAME_ATTR); 488 489 try { 490 if (url != null) { 491 InstanceProperties instProp = InstanceProperties.getInstanceProperties(url); 492 if (instProp == null) 493 instProp = InstanceProperties.createInstanceProperties(url, user, password); 494 instProp.setProperties(defaultServerProp); 495 496 ServerInstance inst = getServerInstance(url); 497 if (inst != null) 498 return new ServerString(inst, targetName); 499 500 } else if (serverName != null) { 501 Server server = getServer(serverName); 502 if (server != null) { 503 ServerInstance[] instances = server.getInstances(); 504 if (instances.length > 1) 505 return new ServerString(instances[0]); 506 } 507 } 508 } catch (Exception e) { 509 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 510 } 511 return null; 512 } 513 514 static private Properties readProperties(File propFile) { 515 Properties prop = new Properties(); 516 try { 517 if (propFile != null && propFile.exists()) 518 prop.load(new FileInputStream(propFile)); 519 } catch (IOException ioe) { 520 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.toString()); 521 } 522 return prop; 523 } 524 525 public ServerString getDefaultInstance() { 526 return getDefaultInstance(true); 527 } 528 529 public ServerString getDefaultInstance(boolean readFromFile) { 530 if (defaultInstance != null) 531 return defaultInstance; 532 533 if (readFromFile) { 534 defaultInstance = ServerStringConverter.readServerInstance(DIR_INSTALLED_SERVERS, FILE_DEFAULT_INSTANCE); 535 536 if (defaultInstance == null) { 537 defaultInstance = getInstallerDefaultPlugin(); 538 } 539 540 } 541 542 if (defaultInstance == null) { 543 ServerInstance[] instances = getServerInstances(); 544 if (instances != null && instances.length > 0) { 545 defaultInstance = new ServerString(instances[0]); 546 } 547 } 548 549 setDefaultInstance(defaultInstance); 550 return defaultInstance; 551 } 552 553 public interface PluginListener { 554 555 public void serverAdded(Server name); 556 557 public void serverRemoved(Server name); 558 559 } 560 561 private static HashMap configNamesByType = null; 562 private static final Object [] allTypes = new Object [] { 563 J2eeModule.EAR, J2eeModule.CLIENT, J2eeModule.CONN, J2eeModule.EJB, J2eeModule.WAR }; 564 565 private void initConfigNamesByType() { 566 if (configNamesByType != null) { 567 return; 568 } 569 configNamesByType = new HashMap(); 570 for (int i=0 ; i<allTypes.length; i++) { 571 Set configNames = new HashSet(); 572 for (Iterator j=servers.values().iterator(); j.hasNext();) { 573 Server s = (Server) j.next(); 574 String [] paths = s.getDeploymentPlanFiles(allTypes[i]); 575 if (paths == null) 576 continue; 577 for (int k=0 ; k<paths.length; k++) { 578 File path = new File(paths[k]); 579 configNames.add(path.getName()); 580 } 581 } 582 configNamesByType.put(allTypes[i], configNames); 583 } 584 } 585 public boolean isConfigFileName(String name, Object type) { 586 initConfigNamesByType(); 587 Set configNames = (Set) configNamesByType.get(type); 588 return (configNames != null && configNames.contains(name)); 589 } 590 591 public ServerInstance getInstanceOrDefault(String uri) { 592 ServerInstance instance = getServerInstance(uri); 593 if (instance == null) { 594 instance = getDefaultInstance().getServerInstance(); 595 } 596 if (instance != null) 597 return instance; 598 throw new RuntimeException (NbBundle.getMessage(ServerRegistry.class, "MSG_NoServerInstances", uri)); 599 } 600 601 602 public static Profiler getProfiler() { 603 return (Profiler)Lookup.getDefault().lookup(Profiler.class); 604 } 605 } 606 | Popular Tags |