KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > ui > util > DerbyServerUtils


1 /*
2
3     Derby - Class org.apache.derby.ui.util.DerbyServerUtils
4     
5     Licensed to the Apache Software Foundation (ASF) under one or more
6     contributor license agreements. See the NOTICE file distributed with
7     this work for additional information regarding copyright ownership.
8     The ASF licenses this file to you under the Apache License, Version 2.0
9     (the "License"); you may not use this file except in compliance with
10     the License. You may obtain a copy of the License at
11     
12        http://www.apache.org/licenses/LICENSE-2.0
13     
14     Unless required by applicable law or agreed to in writing, software
15     distributed under the License is distributed on an "AS IS" BASIS,
16     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17     See the License for the specific language governing permissions and
18     limitations under the License.
19
20 */

21
22 package org.apache.derby.ui.util;
23
24 //import org.apache.ui.decorator.DerbyRunningDecorator;
25

26 import java.sql.SQLException JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.apache.derby.ui.common.CommonNames;
31 import org.apache.derby.ui.common.Messages;
32 import org.apache.derby.ui.decorate.DerbyIsRunningDecorator;
33 import org.apache.derby.ui.properties.DerbyProperties;
34 import org.eclipse.core.resources.IProject;
35 import org.eclipse.core.resources.IResourceChangeEvent;
36 import org.eclipse.core.resources.IResourceChangeListener;
37 import org.eclipse.core.resources.IWorkspace;
38 import org.eclipse.core.resources.ResourcesPlugin;
39 import org.eclipse.core.runtime.CoreException;
40 import org.eclipse.core.runtime.IStatus;
41 import org.eclipse.core.runtime.QualifiedName;
42 import org.eclipse.debug.core.DebugEvent;
43 import org.eclipse.debug.core.DebugPlugin;
44 import org.eclipse.debug.core.IDebugEventSetListener;
45 import org.eclipse.debug.core.ILaunch;
46 import org.eclipse.debug.core.model.IProcess;
47 import org.eclipse.jdt.core.IJavaProject;
48 import org.eclipse.jface.dialogs.MessageDialog;
49 import org.eclipse.swt.SWTException;
50 import org.eclipse.swt.widgets.Shell;
51
52
53
54 public class DerbyServerUtils {
55     
56     //Singleton Class
57
private static DerbyServerUtils dsUtils = new DerbyServerUtils();
58     private HashMap JavaDoc servers = new HashMap JavaDoc();
59  
60     private DerbyServerUtils() {
61         super();
62     }
63
64     public static DerbyServerUtils getDefault() {
65         if (dsUtils == null)
66             dsUtils = new DerbyServerUtils();
67         return dsUtils;
68     }
69     
70     // listener for DebugEvents, to know if a server was stopped by the client
71
// or died by itself
72

73     private IDebugEventSetListener listener = new IDebugEventSetListener() {
74         public void handleDebugEvents(DebugEvent[] events) {
75             // type of event was a terminate...
76
if(events.length>0){
77                 if (events[0].getKind() == DebugEvent.TERMINATE) {
78                     Object JavaDoc source = events[0].getSource();
79                     if (source instanceof IProcess) {
80                         // check for Derby Network Servre process.
81
Object JavaDoc proj = servers.get(source);
82                         if (proj != null) {
83                             try {
84                                 //remove it from the hashmap, update the ui
85
servers.remove(source);
86                                 if(proj instanceof IJavaProject){
87                                     setRunning(((IJavaProject)proj).getProject(), null);
88                                 }else if(proj instanceof IProject){
89                                     setRunning((IProject)proj,null);
90                                 }
91                             }
92                             catch (CoreException ce) {
93                                 Logger.log("DerbyServerTracker.handleDebugEvents: "+ce, IStatus.ERROR);
94                             }catch(Exception JavaDoc e){
95                                 Logger.log("DerbyServerTracker.handleDebugEvents: "+e, IStatus.ERROR);
96                             }
97                         }
98                     }
99                 }
100             }
101         }
102     };
103
104     private IResourceChangeListener rlistener = new IResourceChangeListener() {
105           public void resourceChanged(IResourceChangeEvent event){
106              if(event.getType()==IResourceChangeEvent.PRE_CLOSE){
107                 try{
108                     if(event.getResource().getProject().isNatureEnabled(CommonNames.DERBY_NATURE)){
109                         if(getRunning(event.getResource().getProject())){
110                             stopDerbyServer(event.getResource().getProject());
111                         }
112                     }
113                 }catch(SWTException swe){
114                     //The SWTException is thrown during the Shell creation
115
//Logger.log("Exception shutting down "+swe,IStatus.ERROR);
116
//e.printStackTrace();
117
}catch(Exception JavaDoc e){
118                     Logger.log("Exception shutting down "+e,IStatus.ERROR);
119                 }
120              }
121           }
122        };
123
124     public boolean getRunning(IProject proj) throws CoreException {
125         Object JavaDoc value = proj.getSessionProperty(new QualifiedName(CommonNames.UI_PATH, CommonNames.ISRUNNING));
126         
127         return value != null;
128     }
129     
130     public void setRunning(IProject proj, Boolean JavaDoc value) throws CoreException {
131         try{
132             if (value != null && value.equals(Boolean.FALSE)){
133                 value = null;
134             }
135             if(proj.isOpen()){
136                 proj.setSessionProperty(new QualifiedName(CommonNames.UI_PATH,CommonNames.ISRUNNING ),value);
137             }
138         }catch(Exception JavaDoc e){
139             Logger.log("DerbyServerUtils.setRunning() error: "+e, IStatus.ERROR);
140             
141         }
142         DerbyIsRunningDecorator.performUpdateDecor(proj);
143     }
144
145     public void startDerbyServer( IProject proj) throws CoreException {
146         String JavaDoc args = CommonNames.START_DERBY_SERVER;
147         String JavaDoc vmargs="";
148         DerbyProperties dprop=new DerbyProperties(proj);
149         //Starts the server as a Java app
150
args+=" -h "+dprop.getHost()+ " -p "+dprop.getPort();
151         
152         //Set Derby System Home from the Derby Properties
153
if((dprop.getSystemHome()!=null)&& !(dprop.getSystemHome().equals(""))){
154             vmargs=CommonNames.D_SYSTEM_HOME+dprop.getSystemHome();
155         }
156         String JavaDoc procName="["+proj.getName()+"] - "+CommonNames.DERBY_SERVER+" "+CommonNames.START_DERBY_SERVER+" ("+dprop.getHost()+ ", "+dprop.getPort()+")";
157         ILaunch launch = DerbyUtils.launch(proj, procName ,
158         CommonNames.DERBY_SERVER_CLASS, args, vmargs, CommonNames.START_DERBY_SERVER);
159         IProcess ip=launch.getProcesses()[0];
160         //set a name to be seen in the Console list
161
ip.setAttribute(IProcess.ATTR_PROCESS_LABEL,procName);
162         
163         // saves the mapping between (server) process and project
164
//servers.put(launch.getProcesses()[0], proj);
165
servers.put(ip, proj);
166         // register a listener to listen, when this process is finished
167
DebugPlugin.getDefault().addDebugEventListener(listener);
168         //Add resource listener
169
IWorkspace workspace = ResourcesPlugin.getWorkspace();
170         
171         workspace.addResourceChangeListener(rlistener);
172         setRunning(proj, Boolean.TRUE);
173         Shell shell = new Shell();
174         MessageDialog.openInformation(
175             shell,
176             CommonNames.PLUGIN_NAME,
177             Messages.D_NS_ATTEMPT_STARTED+dprop.getPort()+".");
178
179     }
180
181     public void stopDerbyServer( IProject proj) throws CoreException, ClassNotFoundException JavaDoc, SQLException JavaDoc {
182         String JavaDoc args = CommonNames.SHUTDOWN_DERBY_SERVER;
183         String JavaDoc vmargs="";
184         DerbyProperties dprop=new DerbyProperties(proj);
185         args+=" -h "+dprop.getHost()+ " -p "+dprop.getPort();
186         
187         // Set Derby System Home from the Derby Properties
188
if((dprop.getSystemHome()!=null)&& !(dprop.getSystemHome().equals(""))){
189             vmargs=CommonNames.D_SYSTEM_HOME+dprop.getSystemHome();
190         }
191         String JavaDoc procName="["+proj.getName()+"] - "+CommonNames.DERBY_SERVER+" "+CommonNames.SHUTDOWN_DERBY_SERVER+" ("+dprop.getHost()+ ", "+dprop.getPort()+")";
192         
193         // starts the server as a Java app
194
ILaunch launch = DerbyUtils.launch(proj, procName,
195         CommonNames.DERBY_SERVER_CLASS, args, vmargs,CommonNames.SHUTDOWN_DERBY_SERVER);
196         IProcess ip=launch.getProcesses()[0];
197         
198         //set a name to be seen in the Console list
199
ip.setAttribute(IProcess.ATTR_PROCESS_LABEL,procName);
200         
201         //update the objectState
202
setRunning(proj, Boolean.FALSE);
203         if(proj.isOpen()){
204             Shell shell = new Shell();
205             MessageDialog.openInformation(
206             shell,
207             CommonNames.PLUGIN_NAME,
208             Messages.D_NS_ATTEMPT_STOPPED+dprop.getPort()+"." );
209         }
210     }
211     public void shutdownAllServers() {
212         Iterator JavaDoc it = servers.values().iterator();
213         while (it.hasNext()) {
214             try {
215                 stopDerbyServer((IProject)it.next());
216             }
217             catch (Exception JavaDoc e) {
218                 Logger.log("DerbyServerUtils.shutdownServers",IStatus.ERROR);
219                 Logger.log(SelectionUtil.getStatusMessages(e), IStatus.ERROR);
220             }
221         }
222     }
223
224 }
225
Popular Tags