1 19 20 package org.netbeans.modules.j2ee.sun.test; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileReader ; 25 import java.io.IOException ; 26 import java.io.Reader ; 27 import java.net.HttpURLConnection ; 28 import java.net.Proxy ; 29 import java.net.URL ; 30 import javax.enterprise.deploy.shared.ModuleType ; 31 import javax.enterprise.deploy.spi.Target ; 32 import javax.enterprise.deploy.spi.TargetModuleID ; 33 import javax.management.ObjectName ; 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 52 public class Util { 53 54 public static final String EJB_PROJECT_NAME = "SjsasTestEjb"; 56 public static final String WEB_PROJECT_NAME = "SjsasTestWeb"; 57 public static final String JSF_PROJECT_NAME = "SjsasJSFTest"; 58 public static final String MDB_PROJECT_NAME = "SimpleMessage"; 59 public static final String WEBSERVICE_PROJECT_NAME = "CalculatorWSApplication"; 60 public static final String CUSTOMER_APPLICATION_PROJECT_NAME = "customer-cmp-ear"; 61 public static final String CUSTOMER_CLIENT_PROJECT_NAME = "customer-cmp-ear-app-client"; 62 public static final String CUSTOMER_WEB_PROJECT_NAME = "customer-cmp-ear-war"; 63 public static final String STATEFUL_PROJECT_NAME = "duke-stateful"; 64 public static final String STATEFUL_CLIENT_PROJECT_NAME = "duke-stateful-app-client"; 65 public static final String EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME; 66 public static final String WEB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + WEB_PROJECT_NAME; 67 public static final String JSF_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + JSF_PROJECT_NAME; 68 public static final String MDB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + MDB_PROJECT_NAME; 69 public static final String STATEFUL_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + "duke_stateful"; 70 public static final String WEBSERVICE_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + WEBSERVICE_PROJECT_NAME; 71 public static final String CUSTOMER_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + "customer-cmp-ear"; 72 public static final int SLEEP = 15000; 73 74 public static final String _SEP = System.getProperty("file.separator"); 76 public static final String _DISPLAY_NAME = "Sun Java System Application Server"; 77 public static final String _PLATFORM_LOCATION = System.getProperty("sjsas.server.path"); 78 public static final String _INSTALL_LOCATION = _PLATFORM_LOCATION+_SEP+"domains"; 79 public static final String _DOMAIN = "domain1"; 80 public static final String _HOST = "localhost"; 81 public static final String _PORT = getPort(new File (_INSTALL_LOCATION+_SEP+_DOMAIN+_SEP+"config"+_SEP+"domain.xml")); 82 public static final String _USER_NAME = "admin"; 83 public static final String _PASSWORD = System.getProperty("sjsas.server.password"); 84 public static final String _URL = "["+_PLATFORM_LOCATION+"]deployer:Sun:AppServer::"+_HOST+":"+_PORT; 85 86 public static final String USER_NAME = "username"; 88 public static final String PASSWORD = "password"; 89 public static final String HOST = "host"; 90 public static final String PORT = "port"; 91 public static final String PLATFORM_LOCATION = "platform_location"; 92 public static final String INSTALL_LOCATION = "install_location"; 93 public static final String DOMAIN = "domain"; 94 public static final String TYPE = "type"; 95 public static final String PROP_DISPLAY_NAME = "ServInstWizard_displayName"; 96 97 100 public static String getPort(File domainXml){ 101 String adminPort = null; 102 String buffer = null; 103 104 try { 105 FileReader reader = new FileReader (domainXml); 106 BufferedReader br = new BufferedReader (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 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 e) { 130 } 132 } 133 134 public static TargetModuleID deployModule(final ModuleType moduleType, final String modulePath, final String moduleName) throws Exception { 135 Project project = (Project)openProject(new File (modulePath)); 136 TargetModuleID retVal = deployModule(moduleType, project, moduleName); 137 Util.closeProject(moduleName); 138 return retVal; 139 } 140 141 public static TargetModuleID deployModule(final ModuleType moduleType, final Project project, final String moduleName) throws Exception { 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 ()); 146 147 Runnable startCondition = new Runnable () { 148 public void run() { 149 System.out.println("startCond deployModule "+new java.util.Date ()); 150 while(!si.isConnected()) { 151 try { 152 System.out.print("S"); 153 Thread.sleep(1000); 154 } catch(Exception e) {} 155 } 156 System.out.println(""); 157 } 158 }; 159 160 Runnable deployCondition = new Runnable () { 161 public void run() { 162 System.out.println("deployCond deployModule "+new java.util.Date ()); 163 while(getModuleID(moduleType, moduleName, si, true) == null) { 164 try { 165 System.out.print("D"); 166 Thread.sleep(500); 167 } catch(Exception e) {} 168 } 169 System.out.println(""); 170 } 171 }; 172 173 Task t = RequestProcessor.getDefault().create(startCondition); 174 ap.invokeAction(EjbProjectConstants.COMMAND_REDEPLOY, project.getLookup()); 179 181 t.run(); 182 if(!t.waitFinished(300000)) 183 throw new Exception ("Server start timeout"); 184 185 186 t = RequestProcessor.getDefault().create(deployCondition); 187 t.run(); 188 if(!t.waitFinished(300000)) 189 throw new Exception ("Deploy has timeout"); 190 191 System.out.println("finish deployModule "+new java.util.Date ()); 192 193 TargetModuleID tmid = getModuleID(moduleType, moduleName, si, true); 194 195 if (null != tmid) { 196 si.getDeploymentManager().stop(new TargetModuleID [] {tmid}); 197 } else { 198 throw new Exception ("the module should be runnable"); 199 } 200 201 return null; 202 } 203 204 public static void undeployModule(final ModuleType moduleType, final String modulePath, final String moduleName, final TargetModuleID moduleID) throws Exception { 205 final ServerInstance si = ServerRegistry.getInstance().getServerInstance(_URL); 206 si.getDeploymentManager().undeploy(new TargetModuleID [] {moduleID}); 207 208 Runnable undeployCondition = new Runnable () { 209 public void run() { 210 try { 211 Thread.sleep(250); 212 } catch(Exception e) {} 213 System.out.println("undeployCond deployModule "+new java.util.Date ()); 214 while(getModuleID(moduleType, moduleName, si, false) != null) { 215 try { 216 System.out.print("U"); 217 Thread.sleep(500); 218 } catch(Exception e) {} 219 } 220 System.out.println(""); 221 } 222 }; 223 225 Task t = RequestProcessor.getDefault().create(undeployCondition); 226 t.run(); 227 if(!t.waitFinished(300000)) 228 throw new Exception ("Undeploy has timeout"); 229 if(getModuleID(moduleType, moduleName, si, false) != null) 230 throw new Exception ("Undeploy failed"); 231 } 232 233 public static TargetModuleID getModuleID(ModuleType moduleType, String moduleName, ServerInstance si, boolean running) { 234 try { 235 si.refresh(); 236 Target target = si.getTargets()[0].getTarget(); 237 TargetModuleID [] modules = null; 238 if (running) { 239 modules = si.getDeploymentManager().getRunningModules(moduleType, new Target [] {target}); 240 } else { 241 modules = si.getDeploymentManager().getAvailableModules(moduleType, new Target [] {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 e) { 251 return null; 252 } 253 254 } 255 public static int executeWebModule(ModuleType moduleType, String moduleName) throws Exception { 257 ServerInstance si = ServerRegistry.getInstance().getServerInstance(_URL); 258 si.refresh(); 259 Target target = si.getTargets()[0].getTarget(); 260 TargetModuleID [] modules = si.getDeploymentManager().getRunningModules(moduleType, new Target [] {target}); 261 for(int i=0;i<modules.length;i++){ 262 if(modules[i].getModuleID().equals(moduleName)) { 263 URL url=new URL (modules[i].getWebURL()); 264 HttpURLConnection httpConn = (HttpURLConnection )url.openConnection(Proxy.NO_PROXY); 265 return httpConn.getResponseCode(); 266 } 267 } 268 throw new Exception ("The module is not deployed"); 269 } 270 271 public static Object openProject(File projectDir) { 272 return ProjectSupport.openProject(projectDir); 273 } 274 275 public static void closeProject(String projectName) { 276 ProjectSupport.closeProject(projectName); 277 } 278 279 public static String [] getResourcesNames(String query, String keyProperty, ServerInterface mejb) throws Exception { 280 String MAP_RESOURCES = "com.sun.appserv:type=resources,category=config"; 281 ObjectName objName = new ObjectName (MAP_RESOURCES); 282 ObjectName [] beans = (ObjectName [])mejb.invoke(objName, query, null, null); 283 String [] resNames = new String [beans.length]; 284 for(int i=0; i<beans.length; i++){ 285 String resName = ((ObjectName )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 runAsadmin(String [] command) throws Exception { 298 String [] cmd = new String [command.length+1]; 299 cmd[0] = new File (Util._PLATFORM_LOCATION, "bin").getAbsolutePath(); 300 301 if(System.getProperty("os.name").startsWith("Windows")) 302 cmd[0] = new File (cmd[0], "asadmin.bat").getAbsolutePath(); 303 else 304 cmd[0] = new File (cmd[0], "asadmin").getAbsolutePath(); 305 306 for(int i=1;i<cmd.length;i++) { 307 cmd[i] = command[i-1]; 308 } 309 310 ProcessBuilder pb = new ProcessBuilder (cmd); 311 return pb.start(); 312 } 313 315 public static String readFile(File target) throws IOException { 316 char [] buffer = new char[100000]; 317 int filelength = 0; 318 Reader reader = null; 319 320 try { 321 reader = new BufferedReader (new FileReader (target)); 322 filelength = reader.read(buffer, 0, buffer.length); 323 } finally { 324 if(reader != null) { 325 try { 326 reader.close(); 327 } catch(IOException ex) { 328 System.out.println("IOException on closing file: " + target.getName()); 329 } 330 } 331 } 332 333 return new String (buffer, 0, filelength); 334 } 335 338 public static boolean compareFile(File beforeFile, File afterFile) throws IOException { 339 String before = readFile(beforeFile); 340 String after = readFile(afterFile); 341 342 return after.equals(before); 343 } 344 345 } 346 347 | Popular Tags |