KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > oc4j > ide > OC4JStartRunnable


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.oc4j.ide;
21
22 import java.io.File JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.lang.Thread.UncaughtExceptionHandler;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.enterprise.deploy.shared.ActionType JavaDoc;
28 import javax.enterprise.deploy.shared.CommandType JavaDoc;
29 import javax.enterprise.deploy.shared.StateType JavaDoc;
30 import javax.swing.JOptionPane JavaDoc;
31 import org.netbeans.api.java.platform.JavaPlatform;
32 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
33 import org.netbeans.modules.j2ee.oc4j.OC4JDeploymentManager;
34 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginProperties;
35 import org.netbeans.modules.j2ee.oc4j.util.OC4JDebug;
36 import org.openide.ErrorManager;
37 import org.openide.execution.NbProcessDescriptor;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.util.NbBundle;
41 import org.openide.util.Utilities;
42
43 /**
44  * @author Michal Mocnak
45  */

46 class OC4JStartRunnable implements Runnable JavaDoc {
47     
48     static final String JavaDoc SCRIPT_UNIX = "oc4j";
49     static final String JavaDoc SCRIPT_WIN = "oc4j.cmd";
50     
51     private OC4JDeploymentManager dm;
52     private String JavaDoc instanceName;
53     private OC4JStartServer startServer;
54     private InstanceProperties ip;
55     
56     /**
57      * The amount of time in milliseconds during which the server should
58      * start
59      */

60     private static final int TIMEOUT = 120000;
61     
62     /**
63      * The amount of time in milliseconds that we should wait between checks
64      */

65     private static final int DELAY = 1000;
66     
67     public OC4JStartRunnable(OC4JDeploymentManager dm, OC4JStartServer startServer) {
68         this.dm = dm;
69         this.ip = dm.getProperties().getInstanceProperties();
70         this.instanceName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
71         this.startServer = startServer;
72     }
73     
74     public void run() {
75         // Save the current time so that we can deduct that the startup
76
// Failed due to timeout
77
long start = System.currentTimeMillis();
78                
79         Process JavaDoc serverProcess = createProcess();
80         
81         if (serverProcess == null) {
82             return;
83         }
84         
85         fireStartProgressEvent(StateType.RUNNING, createProgressMessage("MSG_START_SERVER_IN_PROGRESS"));
86         
87         // create a logger to the server's output stream so that a user
88
// can observe the progress
89
OC4JLogger.getInstance(dm.getUri()).readInputStreams(new InputStream JavaDoc[] {
90             serverProcess.getInputStream(), serverProcess.getErrorStream()});
91         
92         // Waiting for server to start
93
while (System.currentTimeMillis() - start < TIMEOUT) {
94             // Send the 'completed' event and return when the server is running
95
if (startServer.isRunning()) {
96                 fireStartProgressEvent(StateType.COMPLETED, createProgressMessage("MSG_SERVER_STARTED")); // NOI18N
97
return;
98             }
99             
100             // Sleep for a little so that we do not make our checks too
101
// Often
102
try {
103                 Thread.sleep(DELAY);
104             } catch (InterruptedException JavaDoc e) {}
105         }
106         
107         // If the server did not start in the designated time limits
108
// We consider the startup as failed and warn the user
109
fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED"));
110     }
111     
112     private String JavaDoc[] createEnvironment() {
113         StringBuilder JavaDoc javaOpts = new StringBuilder JavaDoc();
114         List JavaDoc<String JavaDoc> envp = new ArrayList JavaDoc<String JavaDoc>(3);
115         String JavaDoc rootDir = ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME);
116         JavaPlatform platform = dm.getProperties().getJavaPlatform();
117         FileObject fo = (FileObject) platform.getInstallFolders().iterator().next();
118         String JavaDoc javaHome = FileUtil.toFile(fo).getAbsolutePath();
119         
120         if(startServer.getMode() == OC4JStartServer.MODE.DEBUG) {
121             javaOpts.append(" -classic -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address="). // NOI18N
122
append(dm.getProperties().getDebugPort()).
123                     append(",server=y,suspend=n"); // NOI18N
124
}
125         
126         envp.add("ORACLE_HOME=" + rootDir); // NOI18N
127
envp.add("JAVA_HOME=" + javaHome); // NOI18N
128
envp.add("OC4J_JVM_ARGS=" + javaOpts); // NOI18N
129
envp.add("VERBOSE=on"); // NOI18N
130

131         return (String JavaDoc[]) envp.toArray(new String JavaDoc[envp.size()]);
132     }
133     
134     private NbProcessDescriptor createProcessDescriptor() {
135         final String JavaDoc serverLocation = ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME);
136         final String JavaDoc startScript = serverLocation + File.separator + "bin" + File.separator +
137                 (Utilities.isWindows() ? SCRIPT_WIN : SCRIPT_UNIX); //NOI18N
138
OC4JDebug.log(getClass().getName(), "serverLocation: " + serverLocation + ", startScript: " + startScript);
139         
140         if (!new File JavaDoc(startScript).exists()){
141             OC4JDebug.log(getClass().getName(), "startScript " + startScript + " doesn't exist");
142             fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_FNF")); //NOI18N
143
return null;
144         }
145         
146         OC4JDebug.log(getClass().getName(), "EXEC: " + startScript + " -start ");
147         return new NbProcessDescriptor(startScript, "-start"); //NOI18N
148
}
149     
150     private Process JavaDoc createProcess() {
151         NbProcessDescriptor pd = createProcessDescriptor();
152         
153         if (pd == null)
154             return null;
155         
156         try {
157             return pd.exec(null, createEnvironment(), true, new File JavaDoc(ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME)));
158         } catch (java.io.IOException JavaDoc ioe) {
159             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
160             fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_PD"));
161             return null;
162         }
163     }
164     
165     private String JavaDoc createProgressMessage(final String JavaDoc resName) {
166         return createProgressMessage(resName, null);
167     }
168     
169     private String JavaDoc createProgressMessage(final String JavaDoc resName, final String JavaDoc param) {
170         return NbBundle.getMessage(OC4JStartRunnable.class, resName, instanceName, param);
171     }
172     
173     private void fireStartProgressEvent(StateType JavaDoc stateType, String JavaDoc msg) {
174         startServer.fireHandleProgressEvent(null, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.START, stateType, msg));
175     }
176 }
Popular Tags