1 19 package org.netbeans.modules.j2ee.weblogic9; 20 21 import java.net.URL ; 22 import org.netbeans.modules.j2ee.dd.api.application.Application; 23 import org.netbeans.modules.j2ee.dd.api.application.DDProvider; 24 import org.netbeans.modules.j2ee.dd.api.application.Module; 25 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 26 import java.util.Vector ; 27 import java.io.File ; 28 import javax.enterprise.deploy.spi.DeploymentManager ; 29 import javax.enterprise.deploy.spi.Target ; 30 import javax.enterprise.deploy.spi.TargetModuleID ; 31 import javax.enterprise.deploy.spi.status.ProgressEvent ; 32 import javax.enterprise.deploy.spi.status.ProgressListener ; 33 import javax.enterprise.deploy.spi.status.ProgressObject ; 34 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 35 import javax.enterprise.deploy.spi.status.ClientConfiguration ; 36 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 37 import org.netbeans.modules.j2ee.weblogic9.config.gen.WeblogicWebApp; 38 import org.openide.ErrorManager; 39 import org.openide.filesystems.JarFileSystem; 40 import org.openide.util.RequestProcessor; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import javax.enterprise.deploy.shared.ActionType ; 44 import javax.enterprise.deploy.shared.CommandType ; 45 import javax.enterprise.deploy.shared.StateType ; 46 47 import org.openide.util.NbBundle; 48 49 53 public class WLDeployer implements ProgressObject , Runnable { 54 private static final String AUTO_DEPLOY_DIR = "/autodeploy"; 56 private static final int TIMEOUT = 60000; 57 58 Target [] target; 59 File file; 60 File file2; 61 String uri; 62 TargetModuleID module_id; 63 64 public WLDeployer(String serverUri) { 65 uri = serverUri; 66 } 67 68 69 public ProgressObject deploy(Target [] target, File file, File file2, String host, String port){ 70 WLTargetModuleID module_id = new WLTargetModuleID(target[0], file.getName() ); 72 73 try{ 74 String server_url = "http://" + host+":"+port; 75 76 if (file.getName().endsWith(".war")) { 77 String ctx [] = WeblogicWebApp.createGraph(file2).getContextRoot(); 78 if (ctx != null && ctx.length > 0) { 79 module_id.setContextURL( server_url + ctx[0]); 80 } 81 } else if (file.getName().endsWith(".ear")) { 82 JarFileSystem jfs = new JarFileSystem(); 83 jfs.setJarFile(file); 84 FileObject appXml = jfs.getRoot().getFileObject("META-INF/application.xml"); 85 if (appXml != null) { 86 Application ear = DDProvider.getDefault().getDDRoot(appXml); 87 Module modules [] = ear.getModule(); 88 for (int i = 0; i < modules.length; i++) { 89 WLTargetModuleID mod_id = new WLTargetModuleID(target[0]); 90 if (modules[i].getWeb() != null) { 91 mod_id.setContextURL(server_url + modules[i].getWeb().getContextRoot()); 92 } 93 module_id.addChild(mod_id); 94 } 95 } else { 96 System.out.println("Cannot file META-INF/application.xml in " + file); 97 } 98 } 99 100 }catch(Exception e){ 101 e.printStackTrace(); 102 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 103 } 104 105 106 this.target = target; 107 this.file = file; 108 this.file2 = file2; 109 this.module_id = module_id; 110 fireHandleProgressEvent(null, new WLDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, NbBundle.getMessage(WLDeployer.class, "MSG_DEPLOYING", file.getAbsolutePath()))); 111 RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY); 112 return this; 113 } 114 115 116 public void run(){ 117 118 String deployDir = InstanceProperties.getInstanceProperties(uri).getProperty(WLPluginProperties.DOMAIN_ROOT_ATTR) + AUTO_DEPLOY_DIR; 119 FileObject foIn = FileUtil.toFileObject(file); 120 FileObject foDestDir = FileUtil.toFileObject(new File (deployDir)); 121 String fileName = file.getName(); 122 123 File toDeploy = new File (deployDir+File.separator+fileName); 124 if(toDeploy.exists()) 125 toDeploy.delete(); 126 127 fileName = fileName.substring(0,fileName.lastIndexOf('.')); 128 String msg = NbBundle.getMessage(WLDeployer.class, "MSG_DEPLOYING", file.getAbsolutePath()); 129 fireHandleProgressEvent(null, new WLDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, msg)); 130 131 try{ 132 org.openide.filesystems.FileUtil.copyFile(foIn, foDestDir, fileName); System.out.println("Copying 1 file to: " + foDestDir.getPath()); 134 String webUrl = module_id.getWebURL(); 135 if (webUrl == null) { 136 TargetModuleID ch [] = module_id.getChildTargetModuleID(); 137 if (ch != null) { 138 for (int i = 0; i < ch.length; i++) { 139 webUrl = ch [i].getWebURL(); 140 if (webUrl != null) { 141 break; 142 } 143 } 144 } 145 146 } 147 if (webUrl!= null) { 148 URL url = new URL (webUrl); 149 String waitingMsg = NbBundle.getMessage(WLDeployer.class, "MSG_Waiting_For_Url", url); 150 151 fireHandleProgressEvent(null, new WLDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, waitingMsg)); 152 for (int i = 0; i < 3; i++) { 154 Thread.sleep(1000); 155 } 156 long start = System.currentTimeMillis(); 157 while (System.currentTimeMillis() - start < TIMEOUT) { 158 if (URLWait.waitForUrlReady(url, 1000)) { 159 break; 160 } 161 } 162 } 163 }catch(Exception e){ 164 fireHandleProgressEvent(null, new WLDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.FAILED, "Failed")); 165 } 166 167 fireHandleProgressEvent(null, new WLDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.COMPLETED, "Applicaton Deployed")); 168 } 169 170 171 private Vector listeners = new Vector (); 173 private DeploymentStatus deploymentStatus; 174 175 public void addProgressListener(ProgressListener pl) { 176 listeners.add(pl); 177 } 178 179 public void removeProgressListener(ProgressListener pl) { 180 listeners.remove(pl); 181 } 182 183 public void stop() throws OperationUnsupportedException { 184 throw new OperationUnsupportedException (""); 185 } 186 187 public boolean isStopSupported() { 188 return false; 189 } 190 191 public void cancel() throws OperationUnsupportedException { 192 throw new OperationUnsupportedException (""); 193 } 194 195 public boolean isCancelSupported() { 196 return false; 197 } 198 199 public ClientConfiguration getClientConfiguration(TargetModuleID targetModuleID) { 200 return null; 201 } 202 203 public TargetModuleID [] getResultTargetModuleIDs() { 204 return new TargetModuleID []{ module_id }; 205 } 206 207 public DeploymentStatus getDeploymentStatus() { 208 return deploymentStatus; 209 } 210 211 212 public void fireHandleProgressEvent(TargetModuleID targetModuleID, DeploymentStatus deploymentStatus) { 213 ProgressEvent evt = new ProgressEvent (this, targetModuleID, deploymentStatus); 214 215 this.deploymentStatus = deploymentStatus; 216 217 java.util.Vector targets = null; 218 synchronized (this) { 219 if (listeners != null) { 220 targets = (java.util.Vector ) listeners.clone(); 221 } 222 } 223 224 if (targets != null) { 225 for (int i = 0; i < targets.size(); i++) { 226 ProgressListener target = (ProgressListener )targets.elementAt(i); 227 target.handleProgressEvent(evt); 228 } 229 } 230 } 231 232 233 } 234 235 236 237 | Popular Tags |