KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ant > StartProfiledServer


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.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 /**
40  * Ant task that starts the server in profile mode.
41  *
42  * @author sherold
43  */

44 public class StartProfiledServer extends Task implements Deployment.Logger {
45     
46     private static final String JavaDoc PLAT_PROP_ANT_NAME = "platform.ant.name"; //NOI18N
47

48     private boolean forceRestart;
49     /** timeout on waiting for server startup, default = 3 min */
50     private int startupTimeout = 180000;
51     /** java platform "platform.ant.name" property */
52     private String JavaDoc 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 JavaDoc 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 JavaDoc platformName = (String JavaDoc)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 JavaDoc msg = NbBundle.getMessage(StartProfiledServer.class, "MSG_PlatformNotFound", javaPlatform);
73             throw new BuildException(msg);
74         }
75         String JavaDoc[] envvar = env.getVariables();
76         if (envvar == null) {
77             envvar = new String JavaDoc[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(); // without this the "build" directory is not found in filesystems
85
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 JavaDoc 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 JavaDoc msg = NbBundle.getMessage(StartProfiledServer.class, "MSG_AttachFailed");
94             throw new BuildException(msg);
95         }
96         log(NbBundle.getMessage(StartProfiledServer.class, "MSG_ProfilerAttached"));
97         // wait for the server to finish its startup
98
long timeout = System.currentTimeMillis() + startupTimeout;
99         while (true) {
100             if (si.isRunning()) {
101                 log(NbBundle.getMessage(StartProfiledServer.class, "MSG_ServerUp"));
102                 si.refresh(); // update the server status
103
return;
104             }
105             // if time-out ran out, suppose command failed
106
if (System.currentTimeMillis() > timeout) {
107                 String JavaDoc 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); // take a nap before next retry
112
} catch (Exception JavaDoc 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 JavaDoc 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