001: /*
002:
003: Derby - Class org.apache.derby.ui.util.DerbyServerUtils
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.ui.util;
023:
024: //import org.apache.ui.decorator.DerbyRunningDecorator;
025:
026: import java.sql.SQLException;
027: import java.util.HashMap;
028: import java.util.Iterator;
029:
030: import org.apache.derby.ui.common.CommonNames;
031: import org.apache.derby.ui.common.Messages;
032: import org.apache.derby.ui.decorate.DerbyIsRunningDecorator;
033: import org.apache.derby.ui.properties.DerbyProperties;
034: import org.eclipse.core.resources.IProject;
035: import org.eclipse.core.resources.IResourceChangeEvent;
036: import org.eclipse.core.resources.IResourceChangeListener;
037: import org.eclipse.core.resources.IWorkspace;
038: import org.eclipse.core.resources.ResourcesPlugin;
039: import org.eclipse.core.runtime.CoreException;
040: import org.eclipse.core.runtime.IStatus;
041: import org.eclipse.core.runtime.QualifiedName;
042: import org.eclipse.debug.core.DebugEvent;
043: import org.eclipse.debug.core.DebugPlugin;
044: import org.eclipse.debug.core.IDebugEventSetListener;
045: import org.eclipse.debug.core.ILaunch;
046: import org.eclipse.debug.core.model.IProcess;
047: import org.eclipse.jdt.core.IJavaProject;
048: import org.eclipse.jface.dialogs.MessageDialog;
049: import org.eclipse.swt.SWTException;
050: import org.eclipse.swt.widgets.Shell;
051:
052: public class DerbyServerUtils {
053:
054: //Singleton Class
055: private static DerbyServerUtils dsUtils = new DerbyServerUtils();
056: private HashMap servers = new HashMap();
057:
058: private DerbyServerUtils() {
059: super ();
060: }
061:
062: public static DerbyServerUtils getDefault() {
063: if (dsUtils == null)
064: dsUtils = new DerbyServerUtils();
065: return dsUtils;
066: }
067:
068: // listener for DebugEvents, to know if a server was stopped by the client
069: // or died by itself
070:
071: private IDebugEventSetListener listener = new IDebugEventSetListener() {
072: public void handleDebugEvents(DebugEvent[] events) {
073: // type of event was a terminate...
074: if (events.length > 0) {
075: if (events[0].getKind() == DebugEvent.TERMINATE) {
076: Object source = events[0].getSource();
077: if (source instanceof IProcess) {
078: // check for Derby Network Servre process.
079: Object proj = servers.get(source);
080: if (proj != null) {
081: try {
082: //remove it from the hashmap, update the ui
083: servers.remove(source);
084: if (proj instanceof IJavaProject) {
085: setRunning(((IJavaProject) proj)
086: .getProject(), null);
087: } else if (proj instanceof IProject) {
088: setRunning((IProject) proj, null);
089: }
090: } catch (CoreException ce) {
091: Logger.log(
092: "DerbyServerTracker.handleDebugEvents: "
093: + ce, IStatus.ERROR);
094: } catch (Exception e) {
095: Logger.log(
096: "DerbyServerTracker.handleDebugEvents: "
097: + e, IStatus.ERROR);
098: }
099: }
100: }
101: }
102: }
103: }
104: };
105:
106: private IResourceChangeListener rlistener = new IResourceChangeListener() {
107: public void resourceChanged(IResourceChangeEvent event) {
108: if (event.getType() == IResourceChangeEvent.PRE_CLOSE) {
109: try {
110: if (event.getResource().getProject()
111: .isNatureEnabled(CommonNames.DERBY_NATURE)) {
112: if (getRunning(event.getResource().getProject())) {
113: stopDerbyServer(event.getResource()
114: .getProject());
115: }
116: }
117: } catch (SWTException swe) {
118: //The SWTException is thrown during the Shell creation
119: //Logger.log("Exception shutting down "+swe,IStatus.ERROR);
120: //e.printStackTrace();
121: } catch (Exception e) {
122: Logger.log("Exception shutting down " + e,
123: IStatus.ERROR);
124: }
125: }
126: }
127: };
128:
129: public boolean getRunning(IProject proj) throws CoreException {
130: Object value = proj.getSessionProperty(new QualifiedName(
131: CommonNames.UI_PATH, CommonNames.ISRUNNING));
132:
133: return value != null;
134: }
135:
136: public void setRunning(IProject proj, Boolean value)
137: throws CoreException {
138: try {
139: if (value != null && value.equals(Boolean.FALSE)) {
140: value = null;
141: }
142: if (proj.isOpen()) {
143: proj.setSessionProperty(new QualifiedName(
144: CommonNames.UI_PATH, CommonNames.ISRUNNING),
145: value);
146: }
147: } catch (Exception e) {
148: Logger.log("DerbyServerUtils.setRunning() error: " + e,
149: IStatus.ERROR);
150:
151: }
152: DerbyIsRunningDecorator.performUpdateDecor(proj);
153: }
154:
155: public void startDerbyServer(IProject proj) throws CoreException {
156: String args = CommonNames.START_DERBY_SERVER;
157: String vmargs = "";
158: DerbyProperties dprop = new DerbyProperties(proj);
159: //Starts the server as a Java app
160: args += " -h " + dprop.getHost() + " -p " + dprop.getPort();
161:
162: //Set Derby System Home from the Derby Properties
163: if ((dprop.getSystemHome() != null)
164: && !(dprop.getSystemHome().equals(""))) {
165: vmargs = CommonNames.D_SYSTEM_HOME + dprop.getSystemHome();
166: }
167: String procName = "[" + proj.getName() + "] - "
168: + CommonNames.DERBY_SERVER + " "
169: + CommonNames.START_DERBY_SERVER + " ("
170: + dprop.getHost() + ", " + dprop.getPort() + ")";
171: ILaunch launch = DerbyUtils.launch(proj, procName,
172: CommonNames.DERBY_SERVER_CLASS, args, vmargs,
173: CommonNames.START_DERBY_SERVER);
174: IProcess ip = launch.getProcesses()[0];
175: //set a name to be seen in the Console list
176: ip.setAttribute(IProcess.ATTR_PROCESS_LABEL, procName);
177:
178: // saves the mapping between (server) process and project
179: //servers.put(launch.getProcesses()[0], proj);
180: servers.put(ip, proj);
181: // register a listener to listen, when this process is finished
182: DebugPlugin.getDefault().addDebugEventListener(listener);
183: //Add resource listener
184: IWorkspace workspace = ResourcesPlugin.getWorkspace();
185:
186: workspace.addResourceChangeListener(rlistener);
187: setRunning(proj, Boolean.TRUE);
188: Shell shell = new Shell();
189: MessageDialog.openInformation(shell, CommonNames.PLUGIN_NAME,
190: Messages.D_NS_ATTEMPT_STARTED + dprop.getPort() + ".");
191:
192: }
193:
194: public void stopDerbyServer(IProject proj) throws CoreException,
195: ClassNotFoundException, SQLException {
196: String args = CommonNames.SHUTDOWN_DERBY_SERVER;
197: String vmargs = "";
198: DerbyProperties dprop = new DerbyProperties(proj);
199: args += " -h " + dprop.getHost() + " -p " + dprop.getPort();
200:
201: // Set Derby System Home from the Derby Properties
202: if ((dprop.getSystemHome() != null)
203: && !(dprop.getSystemHome().equals(""))) {
204: vmargs = CommonNames.D_SYSTEM_HOME + dprop.getSystemHome();
205: }
206: String procName = "[" + proj.getName() + "] - "
207: + CommonNames.DERBY_SERVER + " "
208: + CommonNames.SHUTDOWN_DERBY_SERVER + " ("
209: + dprop.getHost() + ", " + dprop.getPort() + ")";
210:
211: // starts the server as a Java app
212: ILaunch launch = DerbyUtils.launch(proj, procName,
213: CommonNames.DERBY_SERVER_CLASS, args, vmargs,
214: CommonNames.SHUTDOWN_DERBY_SERVER);
215: IProcess ip = launch.getProcesses()[0];
216:
217: //set a name to be seen in the Console list
218: ip.setAttribute(IProcess.ATTR_PROCESS_LABEL, procName);
219:
220: //update the objectState
221: setRunning(proj, Boolean.FALSE);
222: if (proj.isOpen()) {
223: Shell shell = new Shell();
224: MessageDialog.openInformation(shell,
225: CommonNames.PLUGIN_NAME,
226: Messages.D_NS_ATTEMPT_STOPPED + dprop.getPort()
227: + ".");
228: }
229: }
230:
231: public void shutdownAllServers() {
232: Iterator it = servers.values().iterator();
233: while (it.hasNext()) {
234: try {
235: stopDerbyServer((IProject) it.next());
236: } catch (Exception e) {
237: Logger.log("DerbyServerUtils.shutdownServers",
238: IStatus.ERROR);
239: Logger.log(SelectionUtil.getStatusMessages(e),
240: IStatus.ERROR);
241: }
242: }
243: }
244:
245: }
|