|
What this is
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.j2ee.deployment.impl;
import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceCreationException;
import javax.enterprise.deploy.spi.DeploymentManager;
import org.openide.filesystems.*;
import org.openide.*;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import java.util.*;
import java.io.*;
import org.openide.modules.InstalledFileLocator;
//import java.util.logging.*;
public final class ServerRegistry implements java.io.Serializable {
public static final String DIR_INSTALLED_SERVERS = "/J2EE/InstalledServers"; //NOI18N
public static final String DIR_JSR88_PLUGINS = "/J2EE/DeploymentPlugins"; //NOI18N
public static final String URL_ATTR = InstanceProperties.URL_ATTR;
public static final String USERNAME_ATTR = InstanceProperties.USERNAME_ATTR;
public static final String PASSWORD_ATTR = InstanceProperties.PASSWORD_ATTR;
public static final String FILE_DEFAULT_INSTANCE = "DefaultInstance.settings"; //NOI18N
public static final String J2EE_DEFAULT_SERVER = "j2ee.defaultServer"; //NOI18N
public static final String TARGETNAME_ATTR = "targetName"; //NOI18N
public static final String SERVER_NAME = "serverName"; //NOI18N
private static ServerRegistry instance = null;
public synchronized static ServerRegistry getInstance() {
if(instance == null) instance = new ServerRegistry();
return instance;
//PENDING need to get this from lookup
// return (ServerRegistry) Lookup.getDefault().lookup(ServerRegistry.class);
}
/** Utility method that returns true if the ServerRegistry was initialized
* during the current IDE session and false otherwise.
*/
public synchronized static boolean wasInitialized () {
return instance != null && instance.servers != null && instance.instances != null;
}
private transient Map servers = null;
private transient Map instances = null;
private transient Collection pluginListeners = new HashSet();
private transient Collection instanceListeners = new LinkedList();
// This is the serializable portion of ServerRegistry
private ServerString defaultInstance;
public ServerRegistry() {
}
private synchronized void init() {
if (servers != null && instances != null)
return;
//long t0 = System.currentTimeMillis();
servers = new HashMap();
instances = new HashMap();
Repository rep = (Repository) Lookup.getDefault().lookup(Repository.class);
FileObject dir = rep.getDefaultFileSystem().findResource(DIR_JSR88_PLUGINS);
dir.addFileChangeListener(new PluginInstallListener());
FileObject[] ch = dir.getChildren();
for(int i = 0; i < ch.length; i++) {
//long t1=System.currentTimeMillis();
addPlugin(ch[i]);
//System.out.println("ServerRegistry.addPlugin("+ch[i]+")="+(System.currentTimeMillis()-t1));
}
dir = rep.getDefaultFileSystem().findResource(DIR_INSTALLED_SERVERS);
dir.addFileChangeListener(new InstanceInstallListener());
ch = dir.getChildren();
for(int i = 0; i < ch.length; i++) {
//long t1=System.currentTimeMillis();
addInstance(ch[i]);
//System.out.println("ServerRegistry.addInstance("+ch[i]+")="+(System.currentTimeMillis()-t1));
}
//System.out.println("ServerRegistry.init="+(System.currentTimeMillis()-t0));
}
private Map serversMap() {
init();
return servers;
}
private Map instancesMap() {
init();
return instances;
}
private synchronized void addPlugin(FileObject fo) {
String name = ""; //NOI18N
try {
if(fo.isFolder()) {
name = fo.getName();
if(serversMap().containsKey(name)) return;
Server server = new Server(fo);
serversMap().put(name,server);
firePluginListeners(server,true);
}
} catch (Exception e) {
ErrorManager.getDefault().log(ErrorManager.WARNING, ("Plugin "+name+" installation failed")); //NOI18N
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
}
}
// PENDING should be private
synchronized void removePlugin(FileObject fo) {
String name = fo.getName();
if(serversMap().containsKey(name)) {
Server server = (Server) serversMap().get(name);
serversMap().remove(name);
firePluginListeners(server,false);
}
}
class PluginInstallListener extends LayerListener {
public void fileFolderCreated(FileEvent fe) {
super.fileFolderCreated(fe);
addPlugin(fe.getFile());
}
public void fileDeleted(FileEvent fe) {
super.fileDeleted(fe);
removePlugin(fe.getFile());
}
}
class InstanceInstallListener extends LayerListener {
public void fileDataCreated(FileEvent fe) {
super.fileDataCreated(fe);
addInstance(fe.getFile());
}
// PENDING should support removing of instances?
}
class LayerListener implements FileChangeListener {
public void fileAttributeChanged(FileAttributeEvent fae) {
java.util.logging.Logger.global.log(java.util.logging.Level.FINEST,"Attribute changed event");
}
public void fileChanged(FileEvent fe) {
}
public void fileFolderCreated(FileEvent fe) {
}
public void fileRenamed(FileRenameEvent fe) {
}
public void fileDataCreated(FileEvent fe) {
}
public void fileDeleted(FileEvent fe) {
}
}
public Collection getServers() {
return serversMap().values();
}
public Collection getInstances() {
return instancesMap().values();
}
public String[] getInstanceURLs() {
return (String[]) instancesMap().keySet().toArray(new String[instancesMap().size()]);
}
public void checkInstanceAlreadyExists(String url) throws InstanceCreationException {
if (getServerInstance(url) != null) {
String msg = NbBundle.getMessage(ServerRegistry.class, "MSG_InstanceAlreadyExists", url);
throw new InstanceCreationException(msg);
}
}
public void checkInstanceExists(String url) {
if (getServerInstance(url) == null) {
String msg = NbBundle.getMessage(ServerRegistry.class, "MSG_InstanceNotExists", url);
throw new IllegalArgumentException(msg);
}
}
public Server getServer(String name) {
return (Server) serversMap().get(name);
}
public void addPluginListener(PluginListener pl) {
pluginListeners.add(pl);
}
public ServerInstance getServerInstance(String url) {
return (ServerInstance) instancesMap().get(url);
}
public void removeServerInstance(String url) {
if (url == null)
return;
// Make sure defaultInstance cache is reset
ServerString def = getDefaultInstance();
if (url.equals(def.getUrl())) {
defaultInstance = null;
}
ServerInstance instance = (ServerInstance) instancesMap().remove(url);
if (instance != null) {
ServerString ss = new ServerString(instance);
fireInstanceListeners(ss, false);
removeInstanceFromFile(instance.getUrl());
}
ServerString newinst = getDefaultInstance(false);
fireDefaultInstance(def, newinst);
}
public ServerInstance[] getServerInstances() {
ServerInstance[] ret = new ServerInstance[instancesMap().size()];
instancesMap().values().toArray(ret);
return ret;
}
public static FileObject getInstanceFileObject(String url) {
Repository rep = (Repository) Lookup.getDefault().lookup(Repository.class);
FileObject[] installedServers = rep.getDefaultFileSystem().findResource(DIR_INSTALLED_SERVERS).getChildren();
for (int i=0; i
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.