KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > weblogic9 > WLDeployer


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.weblogic9;
20
21 import java.net.URL JavaDoc;
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 JavaDoc;
27 import java.io.File JavaDoc;
28 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
29 import javax.enterprise.deploy.spi.Target JavaDoc;
30 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
31 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
32 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
33 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
34 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException JavaDoc;
35 import javax.enterprise.deploy.spi.status.ClientConfiguration JavaDoc;
36 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
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 JavaDoc;
44 import javax.enterprise.deploy.shared.CommandType JavaDoc;
45 import javax.enterprise.deploy.shared.StateType JavaDoc;
46
47 import org.openide.util.NbBundle;
48
49 /**
50  *
51  * @author Ivan Sidorkin
52  */

53 public class WLDeployer implements ProgressObject JavaDoc, Runnable JavaDoc {
54     private static final String JavaDoc AUTO_DEPLOY_DIR = "/autodeploy"; //NOI18N
55
/** timeout for waiting for URL connection */
56     private static final int TIMEOUT = 60000;
57     
58     Target JavaDoc[] target;
59     File JavaDoc file;
60     File JavaDoc file2;
61     String JavaDoc uri;
62     TargetModuleID JavaDoc module_id;
63     /** Creates a new instance of JBDeployer */
64     public WLDeployer(String JavaDoc serverUri) {
65         uri = serverUri;
66     }
67     
68     
69     public ProgressObject JavaDoc deploy(Target JavaDoc[] target, File JavaDoc file, File JavaDoc file2, String JavaDoc host, String JavaDoc port){
70         //PENDING: distribute to all targets!
71
WLTargetModuleID module_id = new WLTargetModuleID(target[0], file.getName() );
72
73         try{
74             String JavaDoc server_url = "http://" + host+":"+port;
75             
76             if (file.getName().endsWith(".war")) {
77                 String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc(deployDir));
121         String JavaDoc fileName = file.getName();
122         
123         File JavaDoc toDeploy = new File JavaDoc(deployDir+File.separator+fileName);
124         if(toDeploy.exists())
125             toDeploy.delete();
126         
127         fileName = fileName.substring(0,fileName.lastIndexOf('.'));
128         String JavaDoc 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); // copy version
133
System.out.println("Copying 1 file to: " + foDestDir.getPath());
134             String JavaDoc webUrl = module_id.getWebURL();
135             if (webUrl == null) {
136                 TargetModuleID JavaDoc 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 JavaDoc url = new URL JavaDoc (webUrl);
149                 String JavaDoc waitingMsg = NbBundle.getMessage(WLDeployer.class, "MSG_Waiting_For_Url", url);
150                 
151                 fireHandleProgressEvent(null, new WLDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, waitingMsg));
152                 //delay to prevent hitting the old content before reload
153
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 JavaDoc 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     // ---------- Implementation of ProgressObject interface
172
private Vector JavaDoc listeners = new Vector JavaDoc();
173     private DeploymentStatus JavaDoc deploymentStatus;
174     
175     public void addProgressListener(ProgressListener JavaDoc pl) {
176         listeners.add(pl);
177     }
178     
179     public void removeProgressListener(ProgressListener JavaDoc pl) {
180         listeners.remove(pl);
181     }
182     
183     public void stop() throws OperationUnsupportedException JavaDoc {
184         throw new OperationUnsupportedException JavaDoc("");
185     }
186     
187     public boolean isStopSupported() {
188         return false;
189     }
190     
191     public void cancel() throws OperationUnsupportedException JavaDoc {
192         throw new OperationUnsupportedException JavaDoc("");
193     }
194     
195     public boolean isCancelSupported() {
196         return false;
197     }
198     
199     public ClientConfiguration JavaDoc getClientConfiguration(TargetModuleID JavaDoc targetModuleID) {
200         return null;
201     }
202     
203     public TargetModuleID JavaDoc[] getResultTargetModuleIDs() {
204         return new TargetModuleID JavaDoc[]{ module_id };
205     }
206     
207     public DeploymentStatus JavaDoc getDeploymentStatus() {
208         return deploymentStatus;
209     }
210     
211     /** Report event to any registered listeners. */
212     public void fireHandleProgressEvent(TargetModuleID JavaDoc targetModuleID, DeploymentStatus JavaDoc deploymentStatus) {
213         ProgressEvent JavaDoc evt = new ProgressEvent JavaDoc(this, targetModuleID, deploymentStatus);
214         
215         this.deploymentStatus = deploymentStatus;
216         
217         java.util.Vector JavaDoc targets = null;
218         synchronized (this) {
219             if (listeners != null) {
220                 targets = (java.util.Vector JavaDoc) listeners.clone();
221             }
222         }
223         
224         if (targets != null) {
225             for (int i = 0; i < targets.size(); i++) {
226                 ProgressListener JavaDoc target = (ProgressListener JavaDoc)targets.elementAt(i);
227                 target.handleProgressEvent(evt);
228             }
229         }
230     }
231     
232     
233 }
234
235
236
237
Popular Tags