1 19 package org.netbeans.modules.j2ee.jboss4; 20 21 import java.util.HashMap ; 22 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 23 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.net.URLClassLoader ; 29 import java.security.AllPermission ; 30 import java.security.CodeSource ; 31 import java.security.PermissionCollection ; 32 import java.security.Permissions ; 33 import java.util.ArrayList ; 34 import java.util.Collections ; 35 import java.util.Enumeration ; 36 import java.util.HashSet ; 37 import java.util.List ; 38 import java.util.Set ; 39 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginUtils; 40 import org.openide.ErrorManager; 41 import org.openide.util.NbBundle; 42 import javax.enterprise.deploy.spi.DeploymentManager ; 43 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException ; 44 import javax.enterprise.deploy.spi.factories.DeploymentFactory ; 45 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager ; 46 import org.openide.filesystems.FileObject; 47 import org.openide.filesystems.FileUtil; 48 import org.openide.filesystems.Repository; 49 import org.openide.util.Lookup; 50 51 55 public class JBDeploymentFactory implements DeploymentFactory { 56 57 public static final String URI_PREFIX = "jboss-deployer:"; 59 private static final String DISCONNECTED_URI = "jboss-deployer:http://localhost:8080&"; 61 private static JBDeploymentFactory instance; 62 63 public static synchronized DeploymentFactory create() { 64 if (instance == null) { 65 instance = new JBDeploymentFactory(); 66 DeploymentFactoryManager.getInstance().registerDeploymentFactory(instance); 67 68 registerDefaultServerInstance(); 69 } 70 71 return instance; 72 } 73 74 78 private HashMap jbossFactories = new HashMap (); 79 80 public static class JBClassLoader extends URLClassLoader { 81 82 public JBClassLoader(URL [] urls, ClassLoader parent) throws MalformedURLException , RuntimeException { 83 super(urls, parent); 84 } 85 86 protected PermissionCollection getPermissions(CodeSource codeSource) { 87 Permissions p = new Permissions (); 88 p.add(new AllPermission ()); 89 return p; 90 } 91 92 public Enumeration <URL > getResources(String name) throws IOException { 93 if (name.indexOf("jndi.properties") != -1) { return Collections.enumeration(Collections.<URL >emptyList()); 96 } 97 98 return super.getResources(name); 99 } 100 } 101 102 public static URLClassLoader getJBClassLoader(String serverRoot, String domainRoot){ 103 try { 104 File dom404 = new File (serverRoot + "/lib/dom4j.jar"); 107 File dom405 = new File (domainRoot + "/lib/dom4j.jar"); 110 File client40 = new File (serverRoot + "/client/jboss-common-client.jar"); 113 File client50 = new File (serverRoot + "/client/jboss-client.jar"); 116 File core50 = new File (serverRoot + "/client/jboss-common-core.jar"); 119 File logging50 = new File (serverRoot + "/client/jboss-logging-spi.jar"); 122 List <URL > urlList = new ArrayList <URL >(); 123 124 urlList.add(new File (serverRoot + "/client/jbossall-client.jar").toURI().toURL()); urlList.add(new File (serverRoot + "/client/jboss-deployment.jar").toURI().toURL()); urlList.add(new File (serverRoot + "/client/jnp-client.jar").toURI().toURL()); urlList.add((dom404.exists()) ? (dom404.toURI().toURL()) : (dom405.toURI().toURL())); 129 if(client40.exists()) 130 urlList.add(client40.toURI().toURL()); 131 132 if(client50.exists()) 133 urlList.add(client50.toURI().toURL()); 134 135 if(core50.exists()) 136 urlList.add(core50.toURI().toURL()); 137 138 if(logging50.exists()) 139 urlList.add(logging50.toURI().toURL()); 140 141 URLClassLoader loader = new JBClassLoader(urlList.toArray(new URL [] {}), JBDeploymentFactory.class.getClassLoader()); 142 return loader; 143 } catch (Exception e) { 144 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 145 } 146 return null; 147 } 148 149 public DeploymentFactory getFactory(String instanceURL) { 150 DeploymentFactory jbossFactory = null; 151 try { 152 String jbossRoot = InstanceProperties.getInstanceProperties(instanceURL). 153 getProperty(JBPluginProperties.PROPERTY_ROOT_DIR); 154 155 String domainRoot = InstanceProperties.getInstanceProperties(instanceURL). 156 getProperty(JBPluginProperties.PROPERTY_SERVER_DIR); 157 158 if (jbossRoot == null) 162 jbossRoot = JBPluginProperties.getInstance().getInstallLocation(); 163 164 if (domainRoot == null) 168 domainRoot = JBPluginProperties.getInstance().getDomainLocation(); 169 170 jbossFactory = (DeploymentFactory ) jbossFactories.get(jbossRoot); 171 if ( jbossFactory == null ) { 172 URLClassLoader loader = getJBClassLoader(jbossRoot, domainRoot); 173 jbossFactory = (DeploymentFactory ) loader.loadClass("org.jboss.deployment.spi.factories.DeploymentFactoryImpl").newInstance(); 175 jbossFactories.put(jbossRoot, jbossFactory); 176 } 177 } catch (Exception e) { 178 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 179 } 180 181 return jbossFactory; 182 } 183 184 public boolean handlesURI(String uri) { 185 if (uri != null && uri.startsWith(URI_PREFIX)) { 186 return true; 187 } 188 189 return false; 190 } 191 192 public DeploymentManager getDeploymentManager(String uri, String uname, String passwd) throws DeploymentManagerCreationException { 193 if (!handlesURI(uri)) { 194 throw new DeploymentManagerCreationException (NbBundle.getMessage(JBDeploymentFactory.class, "MSG_INVALID_URI", uri)); } 196 197 DeploymentFactory df = getFactory(uri); 198 if (df == null) 199 throw new DeploymentManagerCreationException (NbBundle.getMessage(JBDeploymentFactory.class, "MSG_ERROR_CREATING_DM", uri)); 201 String jbURI = uri; 202 try { 203 jbURI = uri.substring(0, uri.indexOf("&")); } 205 catch (Exception e) { 206 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 207 } 208 209 return new JBDeploymentManager(df.getDeploymentManager(jbURI, uname, passwd), uri, uname, passwd); 210 } 211 212 public DeploymentManager getDisconnectedDeploymentManager(String uri) throws DeploymentManagerCreationException { 213 if (!handlesURI(uri)) { 214 throw new DeploymentManagerCreationException (NbBundle.getMessage(JBDeploymentFactory.class, "MSG_INVALID_URI", uri)); } 216 217 DeploymentFactory df = null; 218 InstanceProperties ip = InstanceProperties.getInstanceProperties(uri); 219 if (ip == null) { 220 if (!DISCONNECTED_URI.equals(uri)) { 222 throw new DeploymentManagerCreationException ("JBoss instance " + uri + " is not registered in the IDE."); } 224 } 225 else { 226 df = getFactory(uri); 227 if (df == null) { 228 throw new DeploymentManagerCreationException (NbBundle.getMessage(JBDeploymentFactory.class, "MSG_ERROR_CREATING_DM", uri)); } 230 } 231 232 String jbURI = uri; 233 try { 234 jbURI = uri.substring(0, uri.indexOf("&")); } 236 catch (Exception e) { 237 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 238 } 239 240 return new JBDeploymentManager((df != null ? df.getDisconnectedDeploymentManager(jbURI) : null), uri, null, null); 241 } 242 243 public String getProductVersion() { 244 245 return NbBundle.getMessage (JBDeploymentFactory.class, "LBL_JBossFactoryVersion"); 246 } 247 248 public String getDisplayName() { 249 return NbBundle.getMessage(JBDeploymentFactory.class, "SERVER_NAME"); } 251 252 private static final String INSTALL_ROOT_PROP_NAME = "org.netbeans.modules.j2ee.jboss4.installRoot"; 254 private static void registerDefaultServerInstance() { 255 try { 256 FileObject serverInstanceDir = getServerInstanceDir(); 257 String serverLocation = getDefaultInstallLocation(); 258 String domainLocation = serverLocation + File.separator + "server" + File.separator + "default"; setRemovability(serverInstanceDir, domainLocation); 260 if (JBPluginUtils.isGoodJBServerLocation4x(new File (serverLocation)) && 261 JBPluginUtils.isGoodJBInstanceLocation4x(new File (domainLocation)) || 262 JBPluginUtils.isGoodJBServerLocation5x(new File (serverLocation)) && 263 JBPluginUtils.isGoodJBInstanceLocation5x(new File (domainLocation))) 264 { 265 if (!isAlreadyRegistered(serverInstanceDir, domainLocation)) { 266 String host = "localhost"; String port = JBPluginUtils.getHTTPConnectorPort(domainLocation); register(serverInstanceDir, serverLocation, domainLocation, host, port); 269 } 270 } 271 } 272 catch (IOException ioe) { 273 ErrorManager.getDefault().log(ErrorManager.EXCEPTION, ioe.getMessage()); 274 } 275 } 276 277 private static String getDefaultInstallLocation() { 278 String installRoot = System.getProperty(INSTALL_ROOT_PROP_NAME); 279 if (installRoot != null && new File (installRoot).exists()) { 280 return installRoot; 281 } 282 283 return ""; 284 } 285 286 private static boolean isAlreadyRegistered(FileObject serverInstanceDir, String domainLocation) throws IOException { 287 String domainLocationCan = new File (domainLocation).getCanonicalPath(); 288 for (FileObject instanceFO : serverInstanceDir.getChildren()) { 289 String installedLocation = (String )instanceFO.getAttribute(JBPluginProperties.PROPERTY_SERVER_DIR); 290 if (installedLocation != null) { 291 String installedLocationCan = new File (installedLocation).getCanonicalPath(); 292 if (domainLocationCan.equals(installedLocationCan)) { 293 return true; } 295 } 296 } 297 298 return false; 299 } 300 301 private static void setRemovability(FileObject serverInstanceDir, String domainLocation) throws IOException { 302 String domainLocationCan = new File (domainLocation).getCanonicalPath(); 303 for (FileObject instanceFO : serverInstanceDir.getChildren()) { 304 String url = (String )instanceFO.getAttribute(InstanceProperties.URL_ATTR); 305 if (url.startsWith(URI_PREFIX)) { String installedLocation = (String )instanceFO.getAttribute(JBPluginProperties.PROPERTY_SERVER_DIR); 307 String installedLocationCan = new File (installedLocation).getCanonicalPath(); 308 if (domainLocationCan.equals(installedLocationCan)) { 309 instanceFO.setAttribute(InstanceProperties.REMOVE_FORBIDDEN, Boolean.TRUE); 310 } 311 else { 312 if (instanceFO.getAttribute(InstanceProperties.REMOVE_FORBIDDEN) != null) { 313 instanceFO.setAttribute(InstanceProperties.REMOVE_FORBIDDEN, Boolean.FALSE); 314 } 315 } 316 } 317 } 318 } 319 320 private static void register(FileObject serverInstanceDir, String serverLocation, String domainLocation, String host, String port) throws IOException { 321 String displayName = generateDisplayName(serverInstanceDir); 322 323 String url = URI_PREFIX + host + ":" + port + "#default&" + serverLocation; 325 String name = FileUtil.findFreeFileName(serverInstanceDir, "instance", null); FileObject instanceFO = serverInstanceDir.createData(name); 327 328 instanceFO.setAttribute(InstanceProperties.URL_ATTR, url); 329 instanceFO.setAttribute(InstanceProperties.USERNAME_ATTR, ""); 330 instanceFO.setAttribute(InstanceProperties.PASSWORD_ATTR, ""); 331 instanceFO.setAttribute(InstanceProperties.DISPLAY_NAME_ATTR, displayName); 332 instanceFO.setAttribute(InstanceProperties.REMOVE_FORBIDDEN, "true"); 333 334 instanceFO.setAttribute(JBPluginProperties.PROPERTY_SERVER, "default"); String deployDir = JBPluginUtils.getDeployDir(domainLocation); 336 instanceFO.setAttribute(JBPluginProperties.PROPERTY_DEPLOY_DIR, deployDir); 337 instanceFO.setAttribute(JBPluginProperties.PROPERTY_SERVER_DIR, domainLocation); 338 instanceFO.setAttribute(JBPluginProperties.PROPERTY_ROOT_DIR, serverLocation); 339 instanceFO.setAttribute(JBPluginProperties.PROPERTY_HOST, host); 340 instanceFO.setAttribute(JBPluginProperties.PROPERTY_PORT, port); 341 } 342 343 private static FileObject getServerInstanceDir() { 344 Repository rep = (Repository)Lookup.getDefault().lookup(Repository.class); 345 FileObject dir = rep.getDefaultFileSystem().findResource("/J2EE/InstalledServers"); return dir; 347 } 348 349 private static String generateDisplayName(FileObject serverInstanceDir) { 350 final String serverName = NbBundle.getMessage(JBDeploymentFactory.class, "SERVER_NAME"); 352 String instanceName = serverName; 353 int counter = 1; 354 Set <String > registeredInstances = getServerInstancesNames(serverInstanceDir); 355 356 while (registeredInstances.contains(instanceName.toUpperCase())) { 357 instanceName = serverName + " (" + String.valueOf(counter++) + ")"; 358 } 359 360 return instanceName; 361 } 362 363 private static Set <String > getServerInstancesNames(FileObject serverInstanceDir) { 364 Set <String > names = new HashSet <String >(); 365 for (FileObject instanceFO : serverInstanceDir.getChildren()) { 366 String instanceName = (String )instanceFO.getAttribute(InstanceProperties.DISPLAY_NAME_ATTR); 367 names.add(instanceName.toUpperCase()); 368 } 369 370 return names; 371 } 372 373 } 374 375 | Popular Tags |