KickJava   Java API By Example, From Geeks To Geeks.

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


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

45 class OC4JStopRunnable implements Runnable JavaDoc {
46     
47     private OC4JDeploymentManager dm;
48     private OC4JStartServer startServer;
49     private InstanceProperties ip;
50     private String JavaDoc instanceName;
51     
52     /**
53      * The amount of time in milliseconds during which the server should
54      * stop
55      */

56     private static final int TIMEOUT = 120000;
57     
58     /**
59      * The amount of time in milliseconds that we should wait between checks
60      */

61     private static final int DELAY = 5000;
62     
63     OC4JStopRunnable(OC4JDeploymentManager dm, OC4JStartServer startServer) {
64         this.dm = dm;
65         this.ip = dm.getProperties().getInstanceProperties();
66         this.instanceName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
67         this.startServer = startServer;
68     }
69     
70     public void run() {
71         // save the current time so that we can deduct that the startup
72
// failed due to timeout
73
long start = System.currentTimeMillis();
74         
75         Process JavaDoc serverProcess = createProcess();
76         
77         if (serverProcess == null) {
78             return;
79         }
80         
81         fireStartProgressEvent(StateType.RUNNING, createProgressMessage("MSG_STOP_SERVER_IN_PROGRESS", instanceName));
82         
83         // create a logger to the server's output stream so that a user
84
// can observe the progress
85
OC4JLogger.getInstance(dm.getUri()).readInputStreams(new InputStream JavaDoc[] {
86             serverProcess.getInputStream(), serverProcess.getErrorStream()});
87         
88         // Waiting for server to start
89
while (System.currentTimeMillis() - start < TIMEOUT) {
90             // Send the 'completed' event and return when the server is running
91
if (!startServer.isRunning()) {
92                 fireStartProgressEvent(StateType.COMPLETED, createProgressMessage("MSG_SERVER_STOPPED", instanceName)); // NOI18N
93
return;
94             }
95             
96             // Sleep for a little so that we do not make our checks too
97
// Often
98
try {
99                 Thread.sleep(DELAY);
100             } catch (InterruptedException JavaDoc e) {}
101             
102             fireStartProgressEvent(StateType.RUNNING,createProgressMessage("MSG_STOP_SERVER_IN_PROGRESS", instanceName));
103         }
104         
105         fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_STOP_SERVER_FAILED", instanceName));//NOI18N
106

107         if(serverProcess != null)
108             serverProcess.destroy();
109     }
110     
111     private String JavaDoc[] createEnvironment() {
112         List JavaDoc<String JavaDoc> envp = new ArrayList JavaDoc<String JavaDoc>(3);
113         String JavaDoc rootDir = ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME);
114         JavaPlatform platform = dm.getProperties().getJavaPlatform();
115         FileObject fo = (FileObject) platform.getInstallFolders().iterator().next();
116         String JavaDoc javaHome = FileUtil.toFile(fo).getAbsolutePath();
117         envp.add("ORACLE_HOME=" + rootDir); // NOI18N
118
envp.add("JAVA_HOME=" + javaHome); // NOI18N
119
envp.add("VERBOSE=on"); // NOI18N
120

121         return (String JavaDoc[]) envp.toArray(new String JavaDoc[envp.size()]);
122     }
123     
124     private NbProcessDescriptor createProcessDescriptor() {
125         String JavaDoc serverLocation = ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME);
126         String JavaDoc scriptPath = serverLocation + File.separator + "bin" + File.separator +
127                 (Utilities.isWindows() ? OC4JStartRunnable.SCRIPT_WIN : OC4JStartRunnable.SCRIPT_UNIX);
128         String JavaDoc passwd = ip.getProperty(InstanceProperties.PASSWORD_ATTR);
129         String JavaDoc adminPort = ip.getProperty(OC4JPluginProperties.PROPERTY_ADMIN_PORT);
130         
131         if(!ip.getProperty(InstanceProperties.USERNAME_ATTR).equals("oc4jadmin"))
132             passwd = OC4JPluginUtils.requestPassword("oc4jadmin");
133         
134         if (!new File JavaDoc(scriptPath).exists()){
135             fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_STOP_SERVER_FAILED_FNF", instanceName));//NOI18N
136
return null;
137         }
138         
139         OC4JDebug.log(getClass().getName(), "EXEC: " + scriptPath +
140                 " -shutdown -port " + adminPort + " -password " + passwd);
141         
142         return new NbProcessDescriptor(scriptPath, "-shutdown -port " +
143                 adminPort + " -password " + passwd); // NOI18N
144
}
145     
146     private Process JavaDoc createProcess() {
147         NbProcessDescriptor pd = createProcessDescriptor();
148         
149         if (pd == null)
150             return null;
151         
152         try {
153             return pd.exec(null, createEnvironment(), true, new File JavaDoc(ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME)));
154         } catch (java.io.IOException JavaDoc ioe) {
155             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
156             fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_STOP_SERVER_FAILED_PD"));
157             return null;
158         }
159     }
160     
161     private String JavaDoc createProgressMessage(final String JavaDoc resName) {
162         return createProgressMessage(resName, null);
163     }
164     
165     private String JavaDoc createProgressMessage(final String JavaDoc resName, final String JavaDoc param) {
166         return NbBundle.getMessage(OC4JStartRunnable.class, resName, instanceName, param);
167     }
168     
169     private void fireStartProgressEvent(StateType JavaDoc stateType, String JavaDoc msg) {
170         startServer.fireHandleProgressEvent(null, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, stateType, msg));
171     }
172 }
Popular Tags