KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > bridge > DirectoryDeploymentFacility


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
20 package org.netbeans.modules.j2ee.sun.bridge;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.StringReader JavaDoc;
26
27
28 import javax.enterprise.deploy.spi.Target JavaDoc;
29 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
30 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
31
32 // necessary imports from App Server Jars: they need to be there at runtime:
33
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 JavaDoc;
39 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42 import org.xml.sax.EntityResolver JavaDoc;
43 import org.xml.sax.InputSource JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45
46
47
48
49 /**
50  *
51  * @author Ludo Champenois
52  * small class to invoke App Server APIS to really do the directory based deployment.
53  * It has to be a separate class because of class loader closure issues if the app server jars
54  * are not around wihtin the IDE.
55  */

56 public class DirectoryDeploymentFacility {
57     
58     private String JavaDoc host, user, passwd;
59     private int port;
60     private boolean secure;
61     // class name that might differ between AS 8 and AS 9...
62
final static private String JavaDoc FILEARCHIVEA81= "com.sun.enterprise.deployment.archivist.FileArchive"; //NOI18N
63
final static private String JavaDoc FILEARCHIVEA9= "com.sun.enterprise.deployment.deploy.shared.FileArchive";//NOI18N
64

65     
66     public DirectoryDeploymentFacility(String JavaDoc host, int port, String JavaDoc user, String JavaDoc 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     /**
76      * @param targetModuleID
77      * @return a progress object representing the incrmental dpeloy action.
78      */

79     final public ProgressObject JavaDoc incrementalDeploy( final TargetModuleID JavaDoc tmid) {
80         ProgressObject JavaDoc progressObject = null;
81          File JavaDoc dirLocation = AppServerBridge.getDirLocation( tmid);
82       // long tt = System.currentTimeMillis();
83
// ClassLoader origClassLoader = origClassLoader=Thread.currentThread().getContextClassLoader();
84
try {
85      // Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
86

87             Archive fa= getFileArchive(dirLocation);
88             if (fa==null){
89                 IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc("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 JavaDoc deploymentOptions = new java.util.Properties JavaDoc();
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 JavaDoc[] targets =new Target JavaDoc[1];
108             targets[0] = (Target JavaDoc)tmid;
109             
110             
111             progressObject = df.deploy( targets, fa, null, deploymentOptions );
112 // System.out.println("redeploy in="+(System.currentTimeMillis()-tt));
113

114         } catch(Exception JavaDoc e) {
115             e.printStackTrace();
116             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc(e.getMessage());
117             ise.initCause(e);
118             throw ise;
119             
120         } //finally {
121
// Thread.currentThread().setContextClassLoader(origClassLoader);
122

123       // }
124
return progressObject;
125     }
126     
127     
128     
129     
130     
131     
132     
133     /**
134      * First time deployment file distribution.
135      * Before this method is called the files are copied into the target
136      * folder provided by plugin.
137      * @param target target of deployment
138      * @param file the destination directory for the given deploy app
139      * @return the object for feedback on progress of deployment
140      */

141     public ProgressObject JavaDoc initialDeploy(Target JavaDoc target, File JavaDoc file, String JavaDoc moduleID) {
142         
143         
144         ProgressObject JavaDoc progressObject = null;
145         try{
146             Archive fa= getFileArchive(file);
147             if (fa==null){
148                 IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc("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 JavaDoc deploymentOptions = new java.util.Properties JavaDoc();
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 JavaDoc[] targets =new Target JavaDoc[1];
166             targets[0] = target;
167             
168             progressObject = df.deploy( targets, fa, null, deploymentOptions );
169             
170         } catch(Exception JavaDoc e) {
171             e.printStackTrace();
172             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc(e.getMessage());
173             ise.initCause(e);
174             throw ise;
175         }
176         return progressObject;
177     }
178     
179     private static void setContextRoot(final java.util.Properties JavaDoc deploymentOptions, final String JavaDoc moduleID, final Archive fa) { // throws IOException {
180
InputStream JavaDoc webXml = null;
181         InputStream JavaDoc 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                     // parse sun-web for the context-root value
190
Document JavaDoc 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 JavaDoc 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 JavaDoc ioe) {
205             // do nothing here...
206
} finally {
207             if (null != webXml) {
208                 try {
209                     webXml.close();
210                 } catch (IOException JavaDoc ioe) {
211                 }
212             }
213             if (null != sunWebXml) {
214                 try {
215                     sunWebXml.close();
216                 } catch (IOException JavaDoc ioe) {
217                 }
218             }
219             
220         }
221     }
222     
223     private Archive getFileArchive(File JavaDoc file){
224         try{
225             Class JavaDoc 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 JavaDoc ex) {
230                 try {
231                     fileArchiveClass = this.getClass().getClassLoader().loadClass(FILEARCHIVEA9);
232                 } catch (ClassNotFoundException JavaDoc ex2) {
233                     ex2.printStackTrace();
234                     return null;
235                 }
236             }
237             Object JavaDoc fa =fileArchiveClass.newInstance();
238             
239             java.lang.reflect.Method JavaDoc method =fileArchiveClass.getMethod("open", new Class JavaDoc[]{ String JavaDoc.class});//NOI18N
240
method.invoke(fa, new Object JavaDoc[] {file.getAbsolutePath() });
241             return (Archive)fa;
242         } catch (Exception JavaDoc e) {
243             e.printStackTrace();
244             return null;
245         }
246     }
247     
248     
249     static private Document JavaDoc loadSunWeb(InputStream JavaDoc is) {
250         try {
251             DocumentBuilderFactory JavaDoc dbFactory = DocumentBuilderFactory.newInstance();
252             dbFactory.setValidating(false);
253             DocumentBuilder JavaDoc dBuilder = dbFactory.newDocumentBuilder();
254             
255             dBuilder.setEntityResolver(new EntityResolver JavaDoc() {
256                 public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
257                     StringReader JavaDoc reader = new StringReader JavaDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // NOI18N
258
InputSource JavaDoc source = new InputSource JavaDoc(reader);
259                     source.setPublicId(publicId);
260                     source.setSystemId(systemId);
261                     return source;
262                 }
263             });
264             
265             return dBuilder.parse(is);
266         } catch (Exception JavaDoc e) {
267             return null;
268         }
269     }
270 }
271
272
273
Popular Tags