1 19 package org.netbeans.modules.j2ee.weblogic9; 20 21 import java.io.*; 22 import java.lang.reflect.Method ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.net.URLClassLoader ; 26 import java.security.AllPermission ; 27 import java.security.CodeSource ; 28 import java.security.PermissionCollection ; 29 import java.security.Permissions ; 30 import java.util.Collections ; 31 import java.util.Enumeration ; 32 import java.util.WeakHashMap ; 33 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager ; 34 import org.netbeans.modules.j2ee.weblogic9.util.WLDebug; 35 import org.openide.ErrorManager; 36 37 import javax.enterprise.deploy.spi.*; 38 import javax.enterprise.deploy.spi.factories.*; 39 import javax.enterprise.deploy.spi.exceptions.*; 40 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 41 42 import org.openide.util.NbBundle; 43 44 51 public class WLDeploymentFactory implements DeploymentFactory { 52 53 public static final String URI_PREFIX = "deployer:WebLogic:http://"; 55 58 private static WLDeploymentFactory instance; 59 60 private DeploymentFactory wlFactory = null; 61 62 private static final WeakHashMap <InstanceProperties, WLDeploymentManager> managerCache = 63 new WeakHashMap <InstanceProperties, WLDeploymentManager>(); 64 65 70 public static synchronized DeploymentFactory getInstance() { 71 if (instance == null) { 72 instance = new WLDeploymentFactory(); 73 DeploymentFactoryManager.getInstance().registerDeploymentFactory(instance); 74 } 75 return instance; 76 } 77 78 public static class WLClassLoader extends URLClassLoader { 79 80 public WLClassLoader(URL [] urls, ClassLoader parent) throws MalformedURLException , RuntimeException { 81 super(urls, parent); 82 } 83 84 public void addURL(File f) throws MalformedURLException , RuntimeException { 85 if (f.isFile()) { 86 addURL(f.toURL()); 87 } 88 } 89 90 protected PermissionCollection getPermissions(CodeSource codeSource) { 91 Permissions p = new Permissions (); 92 p.add(new AllPermission ()); 93 return p; 94 } 95 96 public Enumeration <URL > getResources(String name) throws IOException { 97 if (name.indexOf("jndi.properties") != -1 || name.indexOf("i18n_user.properties") != -1) { return Collections.enumeration(Collections.<URL >emptyList()); 100 } 101 102 return super.getResources(name); 103 } 104 } 105 106 private static WLClassLoader loader; 107 108 public static ClassLoader getWLClassLoader (String serverRoot) { 109 if (loader == null) { 110 resetWLClassLoader(serverRoot); 111 } 112 return loader; 113 } 114 115 private static void resetWLClassLoader (String serverRoot) { 116 loader = null; 117 try { 118 URL [] urls = new URL [] { new File(serverRoot + "/server/lib/weblogic.jar").toURI().toURL()}; loader = new WLClassLoader(urls, WLDeploymentFactory.class.getClassLoader()); 120 } catch (Exception e) { 121 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 122 } 123 } 124 125 private DeploymentManager getDM(String uri, String username, String password, String host, String port) throws DeploymentManagerCreationException { 126 if (WLDebug.isEnabled()) { 127 WLDebug.notify(WLDeploymentFactory.class, "getDM, uri:" + uri+" username:" + username+" password:"+password+" host:"+host+" port:"+port); 128 } 129 DeploymentManagerCreationException dmce = null; 130 ClassLoader orig = Thread.currentThread().getContextClassLoader(); 131 try { 132 String serverRoot = InstanceProperties.getInstanceProperties(uri). 133 getProperty(WLPluginProperties.SERVER_ROOT_ATTR); 134 if (serverRoot == null) 138 serverRoot = WLPluginProperties.getInstance().getInstallLocation(); 139 140 ClassLoader loader = getWLClassLoader(serverRoot); 141 Thread.currentThread().setContextClassLoader(loader); 142 Class helperClazz = loader.loadClass("weblogic.deploy.api.tools.SessionHelper"); Method m = helperClazz.getDeclaredMethod("getDeploymentManager", new Class [] {String .class,String .class,String .class,String .class}); 144 Object o = m.invoke(null, new Object [] {host, port, username, password}); 145 if (DeploymentManager.class.isAssignableFrom(o.getClass())) { 146 return (DeploymentManager) o; 147 } else { 148 dmce = new DeploymentManagerCreationException ("Instance created by weblogic is not DeploymentManager instance."); 149 } 150 } catch (Exception e) { 151 dmce = new DeploymentManagerCreationException ("Cannot create weblogic DeploymentManager instance."); 152 ErrorManager.getDefault().annotate(dmce, e); 153 } finally { 154 Thread.currentThread().setContextClassLoader(orig); 155 } 156 throw dmce; 157 } 158 159 private DeploymentManager getDiscoDM(String uri) throws DeploymentManagerCreationException { 160 DeploymentManagerCreationException dmce = null; 161 ClassLoader orig = Thread.currentThread().getContextClassLoader(); 162 try { 163 String serverRoot = InstanceProperties.getInstanceProperties(uri). 164 getProperty(WLPluginProperties.SERVER_ROOT_ATTR); 165 if (serverRoot == null) 169 serverRoot = WLPluginProperties.getInstance().getInstallLocation(); 170 171 ClassLoader loader = getWLClassLoader(serverRoot); 172 Thread.currentThread().setContextClassLoader(loader); 173 Class helperClazz = loader.loadClass("weblogic.deploy.api.tools.SessionHelper"); Method m = helperClazz.getDeclaredMethod("getDisconnectedDeploymentManager", new Class [] {}); 175 Object o = m.invoke(null, new Object [] {}); 176 if (DeploymentManager.class.isAssignableFrom(o.getClass())) { 177 return (DeploymentManager) o; 178 } else { 179 dmce = new DeploymentManagerCreationException ("Instance created by weblogic is not disconnected DeploymentManager instance."); 180 } 181 } catch (Exception e) { 182 dmce = new DeploymentManagerCreationException ("Cannot create weblogic disconnected DeploymentManager instance."); 183 ErrorManager.getDefault().annotate(dmce, e); 184 } finally { 185 Thread.currentThread().setContextClassLoader(orig); 186 } 187 throw dmce; 188 } 189 190 public boolean handlesURI(String uri) { 191 if (uri != null && uri.startsWith(URI_PREFIX)) { 192 return true; 193 } 194 195 return false; 196 } 197 198 public DeploymentManager getDeploymentManager(String uri, String username, 199 String password) throws DeploymentManagerCreationException { 200 if (WLDebug.isEnabled()) { 201 WLDebug.notify(WLDeploymentFactory.class, "getDeploymentManager, uri:" + uri+" username:" + username+" password:"+password); 202 } 203 204 String [] parts = uri.split(":"); String host = parts[3].substring(2); 206 String port = parts[4]; 207 WLDeploymentManager dm = new WLDeploymentManager(getDM (uri, username, password, host, port), uri, username, password, host, port); 208 updateManagerCache(dm, uri); 209 return dm; 210 } 211 212 public DeploymentManager getDisconnectedDeploymentManager(String uri) 213 throws DeploymentManagerCreationException { 214 if (WLDebug.isEnabled()) { 215 WLDebug.notify(WLDeploymentFactory.class, "getDisconnectedDeploymentManager, uri:" + uri); 216 } 217 String [] parts = uri.split(":"); String host = parts[3].substring(2); 219 String port = parts[4]; 220 WLDeploymentManager dm = new WLDeploymentManager(getDiscoDM(uri), uri, host, port); 221 updateManagerCache(dm, uri); 222 return dm; 223 } 224 225 private void updateManagerCache(WLDeploymentManager dm, String uri) { 226 InstanceProperties ip = InstanceProperties.getInstanceProperties(uri); 227 if (managerCache.get(ip) != null) { 228 dm.setServerProcess(managerCache.get(ip).getServerProcess()); 229 } 230 managerCache.put(ip, dm); 231 } 232 233 public String getProductVersion() { 234 return NbBundle.getMessage(WLDeploymentFactory.class, 235 "TXT_productVersion"); } 237 238 public String getDisplayName() { 239 return NbBundle.getMessage(WLDeploymentFactory.class, 240 "TXT_displayName"); } 242 } 243 | Popular Tags |