1 19 20 package org.netbeans.modules.j2ee.sun.bridge; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.StringReader ; 26 27 28 import javax.enterprise.deploy.spi.Target ; 29 import javax.enterprise.deploy.spi.TargetModuleID ; 30 import javax.enterprise.deploy.spi.status.ProgressObject ; 31 32 import com.sun.enterprise.deployment.client.DeploymentFacilityFactory; 34 import com.sun.enterprise.deployment.client.DeploymentFacility; 35 import com.sun.enterprise.deployment.client.ServerConnectionIdentifier; 36 37 import com.sun.enterprise.deployment.deploy.shared.Archive; 38 import javax.xml.parsers.DocumentBuilder ; 39 import javax.xml.parsers.DocumentBuilderFactory ; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.NodeList ; 42 import org.xml.sax.EntityResolver ; 43 import org.xml.sax.InputSource ; 44 import org.xml.sax.SAXException ; 45 46 47 48 49 56 public class DirectoryDeploymentFacility { 57 58 private String host, user, passwd; 59 private int port; 60 private boolean secure; 61 final static private String FILEARCHIVEA81= "com.sun.enterprise.deployment.archivist.FileArchive"; final static private String FILEARCHIVEA9= "com.sun.enterprise.deployment.deploy.shared.FileArchive"; 65 66 public DirectoryDeploymentFacility(String host, int port, String user, String passwd,boolean secure) { 67 this.host =host; 68 this.port =port; 69 this.user =user; 70 this.passwd =passwd; 71 this.secure = secure; 72 } 73 74 75 79 final public ProgressObject incrementalDeploy( final TargetModuleID tmid) { 80 ProgressObject progressObject = null; 81 File dirLocation = AppServerBridge.getDirLocation( tmid); 82 try { 85 87 Archive fa= getFileArchive(dirLocation); 88 if (fa==null){ 89 IllegalStateException ise = new IllegalStateException ("cannot find FileArchive class..."); 90 throw ise; 91 } 92 DeploymentFacility df = DeploymentFacilityFactory.getDeploymentFacility(); 93 ServerConnectionIdentifier sci = new ServerConnectionIdentifier(); 94 sci.setHostName(host); 95 sci.setHostPort(port); 96 sci.setUserName(user); 97 sci.setPassword(passwd); 98 sci.setSecure(secure); 99 df.connect(sci); 100 java.util.Properties deploymentOptions = new java.util.Properties (); 101 102 deploymentOptions.put("force","true"); 103 deploymentOptions.put("name", tmid.getModuleID()); 104 setContextRoot(deploymentOptions, tmid.getModuleID(), fa); 105 System.out.println("moduleID="+tmid.getModuleID()); 106 deploymentOptions.put("archiveName", dirLocation.getAbsolutePath()); 107 Target [] targets =new Target [1]; 108 targets[0] = (Target )tmid; 109 110 111 progressObject = df.deploy( targets, fa, null, deploymentOptions ); 112 114 } catch(Exception e) { 115 e.printStackTrace(); 116 IllegalStateException ise = new IllegalStateException (e.getMessage()); 117 ise.initCause(e); 118 throw ise; 119 120 } 123 return progressObject; 125 } 126 127 128 129 130 131 132 133 141 public ProgressObject initialDeploy(Target target, File file, String moduleID) { 142 143 144 ProgressObject progressObject = null; 145 try{ 146 Archive fa= getFileArchive(file); 147 if (fa==null){ 148 IllegalStateException ise = new IllegalStateException ("cannot find FileArchive class..."); 149 throw ise; 150 } 151 DeploymentFacility df = DeploymentFacilityFactory.getDeploymentFacility(); 152 ServerConnectionIdentifier sci = new ServerConnectionIdentifier(); 153 sci.setHostName(host); 154 sci.setHostPort(port); 155 sci.setUserName(user); 156 sci.setPassword(passwd); 157 sci.setSecure(secure); 158 df.connect(sci); 159 java.util.Properties deploymentOptions = new java.util.Properties (); 160 161 deploymentOptions.put("force","true"); 162 setContextRoot(deploymentOptions, moduleID, fa); 163 System.out.println("moduleID="+moduleID); 164 deploymentOptions.put("archiveName", file.getAbsolutePath()); 165 Target [] targets =new Target [1]; 166 targets[0] = target; 167 168 progressObject = df.deploy( targets, fa, null, deploymentOptions ); 169 170 } catch(Exception e) { 171 e.printStackTrace(); 172 IllegalStateException ise = new IllegalStateException (e.getMessage()); 173 ise.initCause(e); 174 throw ise; 175 } 176 return progressObject; 177 } 178 179 private static void setContextRoot(final java.util.Properties deploymentOptions, final String moduleID, final Archive fa) { InputStream webXml = null; 181 InputStream sunWebXml = null; 182 try { 183 webXml = fa.getEntry("WEB-INF/web.xml"); 184 sunWebXml = fa.getEntry("WEB-INF/sun-web.xml"); 185 186 deploymentOptions.put("name", moduleID); 187 if (null != webXml) { 188 if (null != sunWebXml) { 189 Document swx = loadSunWeb(sunWebXml); 191 if (null == swx) { 192 System.out.println("this should not happen here"); 193 deploymentOptions.put("contextRoot", moduleID); 194 } else { 195 NodeList contextRootNodeList = swx.getElementsByTagName("context-root"); 196 if (null == contextRootNodeList || contextRootNodeList.getLength() < 1) { 197 deploymentOptions.put("contextRoot", moduleID); 198 } 199 } 200 } else { 201 deploymentOptions.put("contextRoot", moduleID); 202 } 203 } 204 } catch (IOException ioe) { 205 } finally { 207 if (null != webXml) { 208 try { 209 webXml.close(); 210 } catch (IOException ioe) { 211 } 212 } 213 if (null != sunWebXml) { 214 try { 215 sunWebXml.close(); 216 } catch (IOException ioe) { 217 } 218 } 219 220 } 221 } 222 223 private Archive getFileArchive(File file){ 224 try{ 225 Class fileArchiveClass; 226 try { 227 fileArchiveClass = this.getClass().getClassLoader().loadClass(FILEARCHIVEA81); 228 java.util.logging.Logger.getLogger("javax.enterprise.system.tools.deployment").setLevel(java.util.logging.Level.SEVERE); 229 } catch (ClassNotFoundException ex) { 230 try { 231 fileArchiveClass = this.getClass().getClassLoader().loadClass(FILEARCHIVEA9); 232 } catch (ClassNotFoundException ex2) { 233 ex2.printStackTrace(); 234 return null; 235 } 236 } 237 Object fa =fileArchiveClass.newInstance(); 238 239 java.lang.reflect.Method method =fileArchiveClass.getMethod("open", new Class []{ String .class}); method.invoke(fa, new Object [] {file.getAbsolutePath() }); 241 return (Archive)fa; 242 } catch (Exception e) { 243 e.printStackTrace(); 244 return null; 245 } 246 } 247 248 249 static private Document loadSunWeb(InputStream is) { 250 try { 251 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 252 dbFactory.setValidating(false); 253 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 254 255 dBuilder.setEntityResolver(new EntityResolver () { 256 public InputSource resolveEntity(String publicId, String systemId) throws SAXException , IOException { 257 StringReader reader = new StringReader ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); InputSource source = new InputSource (reader); 259 source.setPublicId(publicId); 260 source.setSystemId(systemId); 261 return source; 262 } 263 }); 264 265 return dBuilder.parse(is); 266 } catch (Exception e) { 267 return null; 268 } 269 } 270 } 271 272 273 | Popular Tags |