1 21 22 package org.apache.derby.ui.util; 23 24 26 import java.sql.SQLException ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 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 private static DerbyServerUtils dsUtils = new DerbyServerUtils(); 58 private HashMap servers = new HashMap (); 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 73 private IDebugEventSetListener listener = new IDebugEventSetListener() { 74 public void handleDebugEvents(DebugEvent[] events) { 75 if(events.length>0){ 77 if (events[0].getKind() == DebugEvent.TERMINATE) { 78 Object source = events[0].getSource(); 79 if (source instanceof IProcess) { 80 Object proj = servers.get(source); 82 if (proj != null) { 83 try { 84 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 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 }catch(Exception 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 value = proj.getSessionProperty(new QualifiedName(CommonNames.UI_PATH, CommonNames.ISRUNNING)); 126 127 return value != null; 128 } 129 130 public void setRunning(IProject proj, Boolean 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 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 args = CommonNames.START_DERBY_SERVER; 147 String vmargs=""; 148 DerbyProperties dprop=new DerbyProperties(proj); 149 args+=" -h "+dprop.getHost()+ " -p "+dprop.getPort(); 151 152 if((dprop.getSystemHome()!=null)&& !(dprop.getSystemHome().equals(""))){ 154 vmargs=CommonNames.D_SYSTEM_HOME+dprop.getSystemHome(); 155 } 156 String 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 ip.setAttribute(IProcess.ATTR_PROCESS_LABEL,procName); 162 163 servers.put(ip, proj); 166 DebugPlugin.getDefault().addDebugEventListener(listener); 168 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 , SQLException { 182 String args = CommonNames.SHUTDOWN_DERBY_SERVER; 183 String vmargs=""; 184 DerbyProperties dprop=new DerbyProperties(proj); 185 args+=" -h "+dprop.getHost()+ " -p "+dprop.getPort(); 186 187 if((dprop.getSystemHome()!=null)&& !(dprop.getSystemHome().equals(""))){ 189 vmargs=CommonNames.D_SYSTEM_HOME+dprop.getSystemHome(); 190 } 191 String procName="["+proj.getName()+"] - "+CommonNames.DERBY_SERVER+" "+CommonNames.SHUTDOWN_DERBY_SERVER+" ("+dprop.getHost()+ ", "+dprop.getPort()+")"; 192 193 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 ip.setAttribute(IProcess.ATTR_PROCESS_LABEL,procName); 200 201 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 it = servers.values().iterator(); 213 while (it.hasNext()) { 214 try { 215 stopDerbyServer((IProject)it.next()); 216 } 217 catch (Exception e) { 218 Logger.log("DerbyServerUtils.shutdownServers",IStatus.ERROR); 219 Logger.log(SelectionUtil.getStatusMessages(e), IStatus.ERROR); 220 } 221 } 222 } 223 224 } 225 | Popular Tags |