1 19 20 package org.netbeans.modules.j2ee.ant; 21 22 import org.apache.tools.ant.Task; 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.types.Environment; 25 import org.apache.tools.ant.types.Commandline; 26 import org.apache.tools.ant.types.CommandlineJava; 27 import org.netbeans.api.java.platform.JavaPlatform; 28 import org.netbeans.api.java.platform.JavaPlatformManager; 29 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 30 import org.netbeans.modules.j2ee.deployment.profiler.spi.Profiler; 31 import org.netbeans.modules.j2ee.deployment.profiler.api.ProfilerServerSettings; 32 import org.openide.filesystems.*; 33 import org.netbeans.api.project.FileOwnerQuery; 34 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 35 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance; 36 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; 37 import org.openide.util.NbBundle; 38 39 44 public class StartProfiledServer extends Task implements Deployment.Logger { 45 46 private static final String PLAT_PROP_ANT_NAME = "platform.ant.name"; 48 private boolean forceRestart; 49 50 private int startupTimeout = 180000; 51 52 private String javaPlatform; 53 private CommandlineJava jvmarg = new CommandlineJava(); 54 private Environment env = new Environment(); 55 56 public void execute() throws BuildException { 57 58 Profiler profiler = ServerRegistry.getProfiler(); 59 if (profiler == null) { 60 String msg = NbBundle.getMessage(StartProfiledServer.class, "MSG_ProfierNotFound"); 61 throw new BuildException(msg); 62 } 63 JavaPlatform[] installedPlatforms = JavaPlatformManager.getDefault().getInstalledPlatforms(); 64 JavaPlatform platform = null; 65 for (int i = 0; i < installedPlatforms.length; i++) { 66 String platformName = (String )installedPlatforms[i].getProperties().get(PLAT_PROP_ANT_NAME); 67 if (platformName != null && platformName.equals(javaPlatform)) { 68 platform = installedPlatforms[i]; 69 } 70 } 71 if (platform == null) { 72 String msg = NbBundle.getMessage(StartProfiledServer.class, "MSG_PlatformNotFound", javaPlatform); 73 throw new BuildException(msg); 74 } 75 String [] envvar = env.getVariables(); 76 if (envvar == null) { 77 envvar = new String [0]; 78 } 79 ProfilerServerSettings settings = new ProfilerServerSettings( 80 platform, 81 jvmarg.getVmCommand().getArguments(), 82 envvar); 83 FileObject fo = FileUtil.toFileObject(getProject().getBaseDir()); 84 fo.refresh(); J2eeModuleProvider jmp = (J2eeModuleProvider)FileOwnerQuery.getOwner(fo).getLookup().lookup(J2eeModuleProvider.class); 86 ServerInstance si = ServerRegistry.getInstance().getServerInstance(jmp.getServerInstanceID()); 87 if (!si.startProfile(settings, forceRestart, this)) { 88 String msg = NbBundle.getMessage(StartProfiledServer.class, "MSG_StartupFailed"); 89 throw new BuildException(msg); 90 } 91 log(NbBundle.getMessage(StartProfiledServer.class, "MSG_AttachingProfiler")); 92 if (!profiler.attachProfiler(getProject().getProperties())) { 93 String msg = NbBundle.getMessage(StartProfiledServer.class, "MSG_AttachFailed"); 94 throw new BuildException(msg); 95 } 96 log(NbBundle.getMessage(StartProfiledServer.class, "MSG_ProfilerAttached")); 97 long timeout = System.currentTimeMillis() + startupTimeout; 99 while (true) { 100 if (si.isRunning()) { 101 log(NbBundle.getMessage(StartProfiledServer.class, "MSG_ServerUp")); 102 si.refresh(); return; 104 } 105 if (System.currentTimeMillis() > timeout) { 107 String msg = NbBundle.getMessage(StartProfiledServer.class, "MSG_StartTimedOut", String.valueOf(Math.round(startupTimeout / 1000))); 108 throw new BuildException(msg); 109 } 110 try { 111 Thread.sleep(1000); } catch (Exception ex) {}; 113 } 114 } 115 116 public void setForceRestart(boolean forceRestart) { 117 this.forceRestart = forceRestart; 118 } 119 120 public void setStartupTimeout(int timeout) { 121 startupTimeout = timeout; 122 } 123 124 public void setJavaPlatform(String javaPlatform) { 125 this.javaPlatform = javaPlatform; 126 } 127 128 public Commandline.Argument createJvmarg() { 129 return jvmarg.createVmArgument(); 130 } 131 132 public void addEnv(Environment.Variable var) { 133 env.addVariable(var); 134 } 135 } 136 | Popular Tags |