1 19 package org.netbeans.modules.j2ee.jboss4; 20 21 import java.io.IOException ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.Collections ; 25 import java.util.MissingResourceException ; 26 import javax.management.ObjectName ; 27 import org.netbeans.modules.j2ee.dd.api.application.Application; 28 import org.netbeans.modules.j2ee.dd.api.application.DDProvider; 29 import org.netbeans.modules.j2ee.dd.api.application.Module; 30 import org.netbeans.modules.j2ee.jboss4.config.gen.JbossWeb; 31 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties; 32 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 33 import java.util.Vector ; 34 import java.io.File ; 35 import java.util.Set ; 36 import javax.enterprise.deploy.spi.Target ; 37 import javax.enterprise.deploy.spi.TargetModuleID ; 38 import javax.enterprise.deploy.spi.status.ProgressEvent ; 39 import javax.enterprise.deploy.spi.status.ProgressListener ; 40 import javax.enterprise.deploy.spi.status.ProgressObject ; 41 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 42 import javax.enterprise.deploy.spi.status.ClientConfiguration ; 43 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 44 import org.netbeans.modules.j2ee.jboss4.nodes.Util; 45 import org.openide.ErrorManager; 46 import org.openide.filesystems.JarFileSystem; 47 import org.openide.util.RequestProcessor; 48 import org.openide.filesystems.FileObject; 49 import org.openide.filesystems.FileUtil; 50 import org.netbeans.modules.j2ee.jboss4.ide.JBDeploymentStatus; 51 import javax.enterprise.deploy.shared.ActionType ; 52 import javax.enterprise.deploy.shared.CommandType ; 53 import javax.enterprise.deploy.shared.StateType ; 54 import javax.management.QueryExp ; 55 56 import org.openide.util.NbBundle; 57 58 62 public class JBDeployer implements ProgressObject , Runnable { 63 64 private static final int TIMEOUT = 60000; 65 66 JBDeploymentManager dm; 67 68 File file; 69 String uri; 70 JBTargetModuleID module_id; 71 72 public JBDeployer(String serverUri, JBDeploymentManager dm) { 73 uri = serverUri; 74 this.dm = dm; 75 } 76 77 78 public ProgressObject deploy(Target [] target, File file, File file2, String host, int port){ 79 module_id = new JBTargetModuleID(target[0], file.getName() ); 81 82 try{ 83 String server_url = "http://" + host+":"+port; 84 85 if (file.getName().endsWith(".war")) { 86 module_id.setContextURL( server_url + JbossWeb.createGraph(file2).getContextRoot()); 87 } else if (file.getName().endsWith(".ear")) { 88 JarFileSystem jfs = new JarFileSystem(); 89 jfs.setJarFile(file); 90 FileObject appXml = jfs.getRoot().getFileObject("META-INF/application.xml"); 91 if (appXml != null) { 92 Application ear = DDProvider.getDefault().getDDRoot(appXml); 93 Module modules [] = ear.getModule(); 94 for (int i = 0; i < modules.length; i++) { 95 JBTargetModuleID mod_id = new JBTargetModuleID(target[0]); 96 if (modules[i].getWeb() != null) { 97 mod_id.setContextURL(server_url + modules[i].getWeb().getContextRoot()); 98 } 99 module_id.addChild(mod_id); 100 } 101 } else { 102 System.out.println("Cannot file META-INF/application.xml in " + file); 103 } 104 } 105 106 }catch(Exception e){ 107 e.printStackTrace(); 108 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 109 } 110 111 this.file = file; 112 fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, NbBundle.getMessage(JBDeployer.class, "MSG_DEPLOYING", file.getAbsolutePath()))); 113 RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY); 114 return this; 115 } 116 117 public ProgressObject redeploy (TargetModuleID module_id[], File file, File file2) { 118 this.file = file; 120 this.module_id = (JBTargetModuleID) module_id[0]; 121 fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, NbBundle.getMessage(JBDeployer.class, "MSG_DEPLOYING", file.getAbsolutePath()))); 122 RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY); 123 return this; 124 } 125 126 public void run(){ 127 128 String deployDir = InstanceProperties.getInstanceProperties(uri).getProperty(JBPluginProperties.PROPERTY_DEPLOY_DIR); 129 FileObject foIn = FileUtil.toFileObject(file); 130 FileObject foDestDir = FileUtil.toFileObject(new File (deployDir)); 131 String fileName = file.getName(); 132 133 File toDeploy = new File (deployDir+File.separator+fileName); 134 if(toDeploy.exists()) 135 toDeploy.delete(); 136 137 fileName = fileName.substring(0,fileName.lastIndexOf('.')); 138 String msg = NbBundle.getMessage(JBDeployer.class, "MSG_DEPLOYING", file.getAbsolutePath()); 139 fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, msg)); 140 141 try{ 142 wait(2000); 143 }catch(Exception e){ 144 } 145 146 fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, msg)); 147 148 try{ 149 org.openide.filesystems.FileUtil.copyFile(foIn, foDestDir, fileName); TargetModuleID moduleID = module_id; 151 String webUrl = module_id.getWebURL(); 152 if (webUrl == null) { 153 TargetModuleID ch [] = module_id.getChildTargetModuleID(); 154 if (ch != null) { 155 for (int i = 0; i < ch.length; i++) { 156 webUrl = ch [i].getWebURL(); 157 if (webUrl != null) { 158 moduleID = ch[i]; 159 break; 160 } 161 } 162 } 163 164 } 165 if (webUrl!= null) { 166 URL url = new URL (webUrl); 167 String waitingMsg = NbBundle.getMessage(JBDeployer.class, "MSG_Waiting_For_Url", url); 168 fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, waitingMsg)); 169 for (int i = 0; i < 3; i++) { 171 try { 172 Thread.sleep(1000); 173 } catch (InterruptedException ex) { 174 ex.printStackTrace(); 175 } 176 } 177 checkUrlReady(moduleID); 179 } 180 } catch (MalformedURLException ex) { 181 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 182 fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.FAILED, "Failed")); 183 } catch (MissingResourceException ex) { 184 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 185 fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.FAILED, "Failed")); 186 } catch (IOException ex) { 187 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 188 fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.FAILED, "Failed")); 189 } 190 191 fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.COMPLETED, "Applicaton Deployed")); 192 } 193 194 private void checkUrlReady(TargetModuleID moduleID) { 195 try { 196 String warName = moduleID.getModuleID(); 197 ObjectName searchPattern = new ObjectName ("jboss.web.deployment:war=" + warName + ",*"); Object server = Util.getRMIServer(dm); 199 long start = System.currentTimeMillis(); 200 Set managedObj = Collections.emptySet(); 201 while (managedObj.size() == 0 && System.currentTimeMillis() - start < TIMEOUT) { 202 managedObj = (Set ) server.getClass().getMethod("queryMBeans", new Class [] {ObjectName .class, QueryExp .class}).invoke(server, new Object [] {searchPattern, null}); 203 } 204 } catch (Exception ex) { 205 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 206 } 207 } 208 209 private Vector listeners = new Vector (); 211 private DeploymentStatus deploymentStatus; 212 213 public void addProgressListener(ProgressListener pl) { 214 listeners.add(pl); 215 } 216 217 public void removeProgressListener(ProgressListener pl) { 218 listeners.remove(pl); 219 } 220 221 public void stop() throws OperationUnsupportedException { 222 throw new OperationUnsupportedException (""); 223 } 224 225 public boolean isStopSupported() { 226 return false; 227 } 228 229 public void cancel() throws OperationUnsupportedException { 230 throw new OperationUnsupportedException (""); 231 } 232 233 public boolean isCancelSupported() { 234 return false; 235 } 236 237 public ClientConfiguration getClientConfiguration(TargetModuleID targetModuleID) { 238 return null; 239 } 240 241 public TargetModuleID [] getResultTargetModuleIDs() { 242 return new TargetModuleID []{ module_id }; 243 } 244 245 public DeploymentStatus getDeploymentStatus() { 246 return deploymentStatus; 247 } 248 249 250 public void fireHandleProgressEvent(TargetModuleID targetModuleID, DeploymentStatus deploymentStatus) { 251 ProgressEvent evt = new ProgressEvent (this, targetModuleID, deploymentStatus); 252 253 this.deploymentStatus = deploymentStatus; 254 255 java.util.Vector targets = null; 256 synchronized (this) { 257 if (listeners != null) { 258 targets = (java.util.Vector ) listeners.clone(); 259 } 260 } 261 262 if (targets != null) { 263 for (int i = 0; i < targets.size(); i++) { 264 ProgressListener target = (ProgressListener )targets.elementAt(i); 265 target.handleProgressEvent(evt); 266 } 267 } 268 } 269 270 271 } 272 273 274 275 | Popular Tags |