KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > JBDeployer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.jboss4;
20
21 import java.io.IOException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.MissingResourceException JavaDoc;
26 import javax.management.ObjectName JavaDoc;
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 JavaDoc;
34 import java.io.File JavaDoc;
35 import java.util.Set JavaDoc;
36 import javax.enterprise.deploy.spi.Target JavaDoc;
37 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
38 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
39 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
40 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
41 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException JavaDoc;
42 import javax.enterprise.deploy.spi.status.ClientConfiguration JavaDoc;
43 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
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 JavaDoc;
52 import javax.enterprise.deploy.shared.CommandType JavaDoc;
53 import javax.enterprise.deploy.shared.StateType JavaDoc;
54 import javax.management.QueryExp JavaDoc;
55
56 import org.openide.util.NbBundle;
57
58 /**
59  *
60  * @author Ivan Sidorkin
61  */

62 public class JBDeployer implements ProgressObject JavaDoc, Runnable JavaDoc {
63     /** timeout for waiting for URL connection */
64     private static final int TIMEOUT = 60000;
65     
66     JBDeploymentManager dm;
67     
68     File JavaDoc file;
69     String JavaDoc uri;
70     JBTargetModuleID module_id;
71     /** Creates a new instance of JBDeployer */
72     public JBDeployer(String JavaDoc serverUri, JBDeploymentManager dm) {
73         uri = serverUri;
74         this.dm = dm;
75     }
76     
77     
78     public ProgressObject JavaDoc deploy(Target JavaDoc[] target, File JavaDoc file, File JavaDoc file2, String JavaDoc host, int port){
79         //PENDING: distribute to all targets!
80
module_id = new JBTargetModuleID(target[0], file.getName() );
81         
82         try{
83             String JavaDoc 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 JavaDoc 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 JavaDoc redeploy (TargetModuleID JavaDoc module_id[], File JavaDoc file, File JavaDoc file2) {
118         //PENDING: distribute all modules!
119
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 JavaDoc deployDir = InstanceProperties.getInstanceProperties(uri).getProperty(JBPluginProperties.PROPERTY_DEPLOY_DIR);
129         FileObject foIn = FileUtil.toFileObject(file);
130         FileObject foDestDir = FileUtil.toFileObject(new File JavaDoc(deployDir));
131         String JavaDoc fileName = file.getName();
132         
133         File JavaDoc toDeploy = new File JavaDoc(deployDir+File.separator+fileName);
134         if(toDeploy.exists())
135             toDeploy.delete();
136         
137         fileName = fileName.substring(0,fileName.lastIndexOf('.'));
138         String JavaDoc 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 JavaDoc 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); // copy version
150
TargetModuleID JavaDoc moduleID = module_id;
151             String JavaDoc webUrl = module_id.getWebURL();
152             if (webUrl == null) {
153                 TargetModuleID JavaDoc 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 JavaDoc url = new URL JavaDoc (webUrl);
167                 String JavaDoc waitingMsg = NbBundle.getMessage(JBDeployer.class, "MSG_Waiting_For_Url", url);
168                 fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, waitingMsg));
169                 //delay to prevent hitting the old content before reload
170
for (int i = 0; i < 3; i++) {
171                     try {
172                         Thread.sleep(1000);
173                     } catch (InterruptedException JavaDoc ex) {
174                         ex.printStackTrace();
175                     }
176                 }
177                 //wait until the url becomes active
178
checkUrlReady(moduleID);
179             }
180         } catch (MalformedURLException JavaDoc ex) {
181             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
182             fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.FAILED, "Failed"));
183         } catch (MissingResourceException JavaDoc ex) {
184             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
185             fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.FAILED, "Failed"));
186         } catch (IOException JavaDoc 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 JavaDoc moduleID) {
195         try {
196             String JavaDoc warName = moduleID.getModuleID();
197             ObjectName JavaDoc searchPattern = new ObjectName JavaDoc("jboss.web.deployment:war=" + warName + ",*"); // NOI18N
198
Object JavaDoc server = Util.getRMIServer(dm);
199             long start = System.currentTimeMillis();
200             Set JavaDoc managedObj = Collections.emptySet();
201             while (managedObj.size() == 0 && System.currentTimeMillis() - start < TIMEOUT) {
202                 managedObj = (Set JavaDoc) server.getClass().getMethod("queryMBeans", new Class JavaDoc[] {ObjectName JavaDoc.class, QueryExp JavaDoc.class}).invoke(server, new Object JavaDoc[] {searchPattern, null});
203             }
204         } catch (Exception JavaDoc ex) {
205             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
206         }
207     }
208     
209     // ---------- Implementation of ProgressObject interface
210
private Vector JavaDoc listeners = new Vector JavaDoc();
211     private DeploymentStatus JavaDoc deploymentStatus;
212     
213     public void addProgressListener(ProgressListener JavaDoc pl) {
214         listeners.add(pl);
215     }
216     
217     public void removeProgressListener(ProgressListener JavaDoc pl) {
218         listeners.remove(pl);
219     }
220     
221     public void stop() throws OperationUnsupportedException JavaDoc {
222         throw new OperationUnsupportedException JavaDoc("");
223     }
224     
225     public boolean isStopSupported() {
226         return false;
227     }
228     
229     public void cancel() throws OperationUnsupportedException JavaDoc {
230         throw new OperationUnsupportedException JavaDoc("");
231     }
232     
233     public boolean isCancelSupported() {
234         return false;
235     }
236     
237     public ClientConfiguration JavaDoc getClientConfiguration(TargetModuleID JavaDoc targetModuleID) {
238         return null;
239     }
240     
241     public TargetModuleID JavaDoc[] getResultTargetModuleIDs() {
242         return new TargetModuleID JavaDoc[]{ module_id };
243     }
244     
245     public DeploymentStatus JavaDoc getDeploymentStatus() {
246         return deploymentStatus;
247     }
248     
249     /** Report event to any registered listeners. */
250     public void fireHandleProgressEvent(TargetModuleID JavaDoc targetModuleID, DeploymentStatus JavaDoc deploymentStatus) {
251         ProgressEvent JavaDoc evt = new ProgressEvent JavaDoc(this, targetModuleID, deploymentStatus);
252         
253         this.deploymentStatus = deploymentStatus;
254         
255         java.util.Vector JavaDoc targets = null;
256         synchronized (this) {
257             if (listeners != null) {
258                 targets = (java.util.Vector JavaDoc) listeners.clone();
259             }
260         }
261         
262         if (targets != null) {
263             for (int i = 0; i < targets.size(); i++) {
264                 ProgressListener JavaDoc target = (ProgressListener JavaDoc)targets.elementAt(i);
265                 target.handleProgressEvent(evt);
266             }
267         }
268     }
269     
270     
271 }
272
273
274
275
Popular Tags