1 45 package org.exolab.jms.service; 46 47 48 57 public abstract class Service implements Serviceable { 58 59 62 private String _name = null; 63 64 67 private volatile ServiceState _state = ServiceState.STOPPED; 68 69 72 protected Service() { 73 } 74 75 80 protected Service(String name) { 81 _name = name; 82 } 83 84 90 public synchronized void start() throws ServiceException { 91 if (_state.isStopped()) { 92 _state = ServiceState.RUNNING; 93 } else { 94 throw new ServiceException("Failed to start service " + this); 95 } 96 } 97 98 104 public synchronized void stop() throws ServiceException { 105 if (_state.isRunning()) { 106 _state = ServiceState.STOPPED; 107 } else { 108 throw new ServiceException("Failed to stop service " + this); 109 } 110 } 111 112 118 public synchronized void restart() throws ServiceException { 119 if (_state.isRunning()) { 120 stop(); 121 } 122 start(); 123 } 124 125 130 public ServiceState getState() { 131 return _state; 132 } 133 134 139 public String getName() { 140 return _name; 141 } 142 143 148 public String toString() { 149 StringBuffer buf = new StringBuffer ("Service:["); 150 buf.append("name="); 151 buf.append(_name); 152 buf.append("state="); 153 buf.append(_state); 154 buf.append("]"); 155 return buf.toString(); 156 } 157 158 163 protected void setState(ServiceState state) { 164 _state = state; 165 } 166 167 } 168 | Popular Tags |