KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > test > Util


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.test;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.net.HttpURLConnection JavaDoc;
28 import java.net.Proxy JavaDoc;
29 import java.net.URL JavaDoc;
30 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
31 import javax.enterprise.deploy.spi.Target JavaDoc;
32 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.junit.ide.ProjectSupport;
36 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
37 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
38 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
39 import org.netbeans.modules.j2ee.sun.api.ServerInterface;
40 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.Resources;
41 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.BaseResourceNode;
42 import org.netbeans.modules.j2ee.sun.ide.sunresources.resourcesloader.SunResourceDataObject;
43 import org.netbeans.spi.project.ActionProvider;
44 import org.openide.util.RequestProcessor;
45 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants;
46 import org.openide.util.RequestProcessor.Task;
47
48 /**
49  *
50  * @author Michal Mocnak
51  */

52 public class Util {
53     
54     // TEST PROPERTIES
55
public static final String JavaDoc EJB_PROJECT_NAME = "SjsasTestEjb";
56     public static final String JavaDoc WEB_PROJECT_NAME = "SjsasTestWeb";
57     public static final String JavaDoc JSF_PROJECT_NAME = "SjsasJSFTest";
58     public static final String JavaDoc MDB_PROJECT_NAME = "SimpleMessage";
59     public static final String JavaDoc WEBSERVICE_PROJECT_NAME = "CalculatorWSApplication";
60     public static final String JavaDoc CUSTOMER_APPLICATION_PROJECT_NAME = "customer-cmp-ear";
61     public static final String JavaDoc CUSTOMER_CLIENT_PROJECT_NAME = "customer-cmp-ear-app-client";
62     public static final String JavaDoc CUSTOMER_WEB_PROJECT_NAME = "customer-cmp-ear-war";
63     public static final String JavaDoc STATEFUL_PROJECT_NAME = "duke-stateful";
64     public static final String JavaDoc STATEFUL_CLIENT_PROJECT_NAME = "duke-stateful-app-client";
65     public static final String JavaDoc EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME;
66     public static final String JavaDoc WEB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + WEB_PROJECT_NAME;
67     public static final String JavaDoc JSF_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + JSF_PROJECT_NAME;
68     public static final String JavaDoc MDB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + MDB_PROJECT_NAME;
69     public static final String JavaDoc STATEFUL_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + "duke_stateful";
70     public static final String JavaDoc WEBSERVICE_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + WEBSERVICE_PROJECT_NAME;
71     public static final String JavaDoc CUSTOMER_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + "customer-cmp-ear";
72     public static final int SLEEP = 15000;
73     
74     // SERVER PROPERTIES FOR TESTS
75
public static final String JavaDoc _SEP = System.getProperty("file.separator");
76     public static final String JavaDoc _DISPLAY_NAME = "Sun Java System Application Server";
77     public static final String JavaDoc _PLATFORM_LOCATION = System.getProperty("sjsas.server.path");
78     public static final String JavaDoc _INSTALL_LOCATION = _PLATFORM_LOCATION+_SEP+"domains";
79     public static final String JavaDoc _DOMAIN = "domain1";
80     public static final String JavaDoc _HOST = "localhost";
81     public static final String JavaDoc _PORT = getPort(new File JavaDoc(_INSTALL_LOCATION+_SEP+_DOMAIN+_SEP+"config"+_SEP+"domain.xml"));
82     public static final String JavaDoc _USER_NAME = "admin";
83     public static final String JavaDoc _PASSWORD = System.getProperty("sjsas.server.password");
84     public static final String JavaDoc _URL = "["+_PLATFORM_LOCATION+"]deployer:Sun:AppServer::"+_HOST+":"+_PORT;
85     
86     // SERVER PROPERTIES FOR APP SERVER REGISTRATION
87
public static final String JavaDoc USER_NAME = "username";
88     public static final String JavaDoc PASSWORD = "password";
89     public static final String JavaDoc HOST = "host";
90     public static final String JavaDoc PORT = "port";
91     public static final String JavaDoc PLATFORM_LOCATION = "platform_location";
92     public static final String JavaDoc INSTALL_LOCATION = "install_location";
93     public static final String JavaDoc DOMAIN = "domain";
94     public static final String JavaDoc TYPE = "type";
95     public static final String JavaDoc PROP_DISPLAY_NAME = "ServInstWizard_displayName";
96     
97     /**
98      * It returns admin port number if the server.
99      */

100     public static String JavaDoc getPort(File JavaDoc domainXml){
101         String JavaDoc adminPort = null;
102         String JavaDoc buffer = null;
103         
104         try {
105             FileReader JavaDoc reader = new FileReader JavaDoc(domainXml);
106             BufferedReader JavaDoc br = new BufferedReader JavaDoc(reader);
107             
108             while((buffer = br.readLine()) != null) {
109                 if(buffer.indexOf("admin-listener") > -1) {
110                     int x = buffer.indexOf(34, buffer.indexOf("port"));
111                     int y = buffer.indexOf(34, ++x);
112                     adminPort = buffer.substring(x, y);
113                     break;
114                 }
115             }
116             
117             br.close();
118             reader.close();
119         } catch (Exception JavaDoc ex) {
120             ex.printStackTrace();
121         }
122         
123         return adminPort;
124     }
125     
126     public static void sleep(int i) {
127         try {
128             Thread.sleep(i);
129         } catch(Exception JavaDoc e) {
130             // Nothing to do
131
}
132     }
133     
134     public static TargetModuleID JavaDoc deployModule(final ModuleType JavaDoc moduleType, final String JavaDoc modulePath, final String JavaDoc moduleName) throws Exception JavaDoc {
135         Project project = (Project)openProject(new File JavaDoc(modulePath));
136         TargetModuleID JavaDoc retVal = deployModule(moduleType, project, moduleName);
137         Util.closeProject(moduleName);
138         return retVal;
139     }
140     
141     public static TargetModuleID JavaDoc deployModule(final ModuleType JavaDoc moduleType, final Project project, final String JavaDoc moduleName) throws Exception JavaDoc {
142         ActionProvider ap = (ActionProvider)project.getLookup().lookup(ActionProvider.class);
143         J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
144         final ServerInstance si = ServerRegistry.getInstance().getServerInstance(_URL);
145         System.out.println("start deployModule "+new java.util.Date JavaDoc());
146         
147         Runnable JavaDoc startCondition = new Runnable JavaDoc() {
148             public void run() {
149                 System.out.println("startCond deployModule "+new java.util.Date JavaDoc());
150                 while(!si.isConnected()) {
151                     try {
152                         System.out.print("S");
153                         Thread.sleep(1000);
154                     } catch(Exception JavaDoc e) {}
155                 }
156                 System.out.println("");
157             }
158         };
159         
160         Runnable JavaDoc deployCondition = new Runnable JavaDoc() {
161             public void run() {
162                 System.out.println("deployCond deployModule "+new java.util.Date JavaDoc());
163                 while(getModuleID(moduleType, moduleName, si, true) == null) {
164                     try {
165                         System.out.print("D");
166                         Thread.sleep(500);
167                     } catch(Exception JavaDoc e) {}
168                 }
169                 System.out.println("");
170             }
171         };
172         
173         Task t = RequestProcessor.getDefault().create(startCondition);
174         // TODO : get this to work
175
// if (moduleType == ModuleType.WAR) {
176
// ap.invokeAction(ap.COMMAND_RUN,project.getLookup());
177
// } else {
178
ap.invokeAction(EjbProjectConstants.COMMAND_REDEPLOY, project.getLookup());
179         // }
180

181         t.run();
182         if(!t.waitFinished(300000))
183             throw new Exception JavaDoc("Server start timeout");
184         
185         
186         t = RequestProcessor.getDefault().create(deployCondition);
187         t.run();
188         if(!t.waitFinished(300000))
189             throw new Exception JavaDoc("Deploy has timeout");
190         
191         System.out.println("finish deployModule "+new java.util.Date JavaDoc());
192         
193         TargetModuleID JavaDoc tmid = getModuleID(moduleType, moduleName, si, true);
194         
195         if (null != tmid) {
196             si.getDeploymentManager().stop(new TargetModuleID JavaDoc[] {tmid});
197         } else {
198             throw new Exception JavaDoc("the module should be runnable");
199         }
200         
201         return null;
202     }
203     
204     public static void undeployModule(final ModuleType JavaDoc moduleType, final String JavaDoc modulePath, final String JavaDoc moduleName, final TargetModuleID JavaDoc moduleID) throws Exception JavaDoc {
205         final ServerInstance si = ServerRegistry.getInstance().getServerInstance(_URL);
206         si.getDeploymentManager().undeploy(new TargetModuleID JavaDoc[] {moduleID});
207         
208         Runnable JavaDoc undeployCondition = new Runnable JavaDoc() {
209             public void run() {
210                     try {
211                         Thread.sleep(250);
212                     } catch(Exception JavaDoc e) {}
213                 System.out.println("undeployCond deployModule "+new java.util.Date JavaDoc());
214                 while(getModuleID(moduleType, moduleName, si, false) != null) {
215                     try {
216                         System.out.print("U");
217                         Thread.sleep(500);
218                     } catch(Exception JavaDoc e) {}
219                 }
220                 System.out.println("");
221             }
222         };
223         //sleep(SLEEP);
224

225         Task t = RequestProcessor.getDefault().create(undeployCondition);
226         t.run();
227         if(!t.waitFinished(300000))
228             throw new Exception JavaDoc("Undeploy has timeout");
229         if(getModuleID(moduleType, moduleName, si, false) != null)
230             throw new Exception JavaDoc("Undeploy failed");
231     }
232     
233     public static TargetModuleID JavaDoc getModuleID(ModuleType JavaDoc moduleType, String JavaDoc moduleName, ServerInstance si, boolean running) {
234         try {
235             si.refresh();
236             Target JavaDoc target = si.getTargets()[0].getTarget();
237             TargetModuleID JavaDoc[] modules = null;
238             if (running) {
239                 modules = si.getDeploymentManager().getRunningModules(moduleType, new Target JavaDoc[] {target});
240             } else {
241                 modules = si.getDeploymentManager().getAvailableModules(moduleType, new Target JavaDoc[] {target});
242             }
243             
244             for(int i=0;i<modules.length;i++) {
245                 if(modules[i].getModuleID().equals(moduleName))
246                     return modules[i];
247             }
248             
249             return null;
250         } catch(Exception JavaDoc e) {
251             return null;
252         }
253         
254     }
255     //return http error code if module is deployed and -1 if module is not deployed
256
public static int executeWebModule(ModuleType JavaDoc moduleType, String JavaDoc moduleName) throws Exception JavaDoc{
257         ServerInstance si = ServerRegistry.getInstance().getServerInstance(_URL);
258         si.refresh();
259         Target JavaDoc target = si.getTargets()[0].getTarget();
260         TargetModuleID JavaDoc[] modules = si.getDeploymentManager().getRunningModules(moduleType, new Target JavaDoc[] {target});
261         for(int i=0;i<modules.length;i++){
262             if(modules[i].getModuleID().equals(moduleName)) {
263                 URL JavaDoc url=new URL JavaDoc(modules[i].getWebURL());
264                 HttpURLConnection JavaDoc httpConn = (HttpURLConnection JavaDoc)url.openConnection(Proxy.NO_PROXY);
265                 return httpConn.getResponseCode();
266             }
267         }
268         throw new Exception JavaDoc("The module is not deployed");
269     }
270     
271     public static Object JavaDoc openProject(File JavaDoc projectDir) {
272         return ProjectSupport.openProject(projectDir);
273     }
274     
275     public static void closeProject(String JavaDoc projectName) {
276         ProjectSupport.closeProject(projectName);
277     }
278     
279     public static String JavaDoc[] getResourcesNames(String JavaDoc query, String JavaDoc keyProperty, ServerInterface mejb) throws Exception JavaDoc {
280         String JavaDoc MAP_RESOURCES = "com.sun.appserv:type=resources,category=config";
281         ObjectName JavaDoc objName = new ObjectName JavaDoc(MAP_RESOURCES);
282         ObjectName JavaDoc[] beans = (ObjectName JavaDoc[])mejb.invoke(objName, query, null, null);
283         String JavaDoc[] resNames = new String JavaDoc[beans.length];
284         for(int i=0; i<beans.length; i++){
285             String JavaDoc resName = ((ObjectName JavaDoc)beans[i]).getKeyProperty(keyProperty);
286             resNames[i] = resName;
287         }
288         
289         return resNames;
290     }
291     
292     public static Resources getResourcesObject(SunResourceDataObject resourceObj) {
293         BaseResourceNode resNode = (BaseResourceNode)resourceObj.getNodeDelegate();
294         return resNode.getBeanGraph();
295     }
296     
297     public static Process JavaDoc runAsadmin(String JavaDoc[] command) throws Exception JavaDoc {
298         String JavaDoc[] cmd = new String JavaDoc[command.length+1];
299         cmd[0] = new File JavaDoc(Util._PLATFORM_LOCATION, "bin").getAbsolutePath();
300         
301         if(System.getProperty("os.name").startsWith("Windows"))
302             cmd[0] = new File JavaDoc(cmd[0], "asadmin.bat").getAbsolutePath();
303         else
304             cmd[0] = new File JavaDoc(cmd[0], "asadmin").getAbsolutePath();
305         
306         for(int i=1;i<cmd.length;i++) {
307             cmd[i] = command[i-1];
308         }
309         
310         ProcessBuilder JavaDoc pb = new ProcessBuilder JavaDoc(cmd);
311         return pb.start();
312     }
313     /** Read a file (< 100Kb) into a String and return the contents.
314      */

315     public static String JavaDoc readFile(File JavaDoc target) throws IOException JavaDoc {
316         char [] buffer = new char[100000];
317         int filelength = 0;
318         Reader JavaDoc reader = null;
319         
320         try {
321             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(target));
322             filelength = reader.read(buffer, 0, buffer.length);
323         } finally {
324             if(reader != null) {
325                 try {
326                     reader.close();
327                 } catch(IOException JavaDoc ex) {
328                     System.out.println("IOException on closing file: " + target.getName());
329                 }
330             }
331         }
332         
333         return new String JavaDoc(buffer, 0, filelength);
334     }
335     /* Quick & dirty implementation that reads two files an compares for exact match, regardless
336      * of white space. Files must be < 100K each.
337      */

338     public static boolean compareFile(File JavaDoc beforeFile, File JavaDoc afterFile) throws IOException JavaDoc {
339         String JavaDoc before = readFile(beforeFile);
340         String JavaDoc after = readFile(afterFile);
341         
342         return after.equals(before);
343     }
344     
345 }
346
347
Popular Tags