dain 2004/04/07 20:05:52
Modified: modules/core/src/java/org/openejb/server ServiceDaemon.java
ServiceManager.java
Log:
Converted ServiceDaemon to a GBean
Revision Changes Path
1.3 +120 -127
openejb/modules/core/src/java/org/openejb/server/ServiceDaemon.java
Index: ServiceDaemon.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/server/ServiceDaemon.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ServiceDaemon.java 10 Mar 2004 09:20:19 -0000 1.2
+++ ServiceDaemon.java 8 Apr 2004 00:05:52 -0000 1.3
@@ -44,164 +44,157 @@
*/
package org.openejb.server;
-import java.io.*;
-import java.net.*;
-import java.util.*;
-import org.openejb.*;
-import org.openejb.util.SafeProperties;
-
-/**
- * The Server will call the following methods.
- *
- * newInstance()
- * init( port, properties)
- * start()
- * stop()
- *
- * All ServerService implementations must have a no argument
- * constructor.
- *
- */
-public class ServiceDaemon implements ServerService, Runnable {
-
- ServerService next;
-
- Properties props;
- String ip;
- int port;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
- ServerSocket serverSocket;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.geronimo.gbean.GBean;
+import org.apache.geronimo.gbean.GBeanContext;
+import org.apache.geronimo.gbean.GBeanInfo;
+import org.apache.geronimo.gbean.GBeanInfoFactory;
- /**
- * We start out in a "stopped" state until someone
- * calls the start method.
- */
- boolean stop = true;
+public class ServiceDaemon implements GBean {
+ private static final Log log = LogFactory.getLog(ServiceDaemon.class);
+
+ private final ServerService serverService;
+ private final String inetAddress;
+ private final int port;
+
+ private SocketDaemon socketDaemon;
- public ServiceDaemon(ServerService next){
- this.next = next;
+ public ServiceDaemon(ServerService serverService, String inetAddress, int port)
{
+ this.serverService = serverService;
+ this.inetAddress = inetAddress;
+ this.port = port;
}
- /**
- * Pulls out the access log information
- *
- * @param props
- *
- * @exception ServiceException
- */
- public void init(Properties props) throws Exception{
- // Do our stuff
- this.props = props;
-
- String p = props.getProperty("port");
- ip = props.getProperty("bind");
-
- port = Integer.parseInt(p);
- // Then call the next guy
- next.init(props);
- }
-
- public void start() throws ServiceException{
- synchronized (this){
- // Don't bother if we are already started/starting
- if (!stop) return;
-
- stop = false;
- // Do our stuff
- try{
- serverSocket = new ServerSocket(port, 20,
InetAddress.getByName(ip));
- Thread d = new Thread(this);
- d.setName("service."+next.getName()+"@"+d.hashCode());
- d.setDaemon(true);
- d.start();
- } catch (Exception e){
- throw new ServiceException("Service failed to start.",e);
- //e.printStackTrace();
- }
-
- // Then call the next guy
- next.start();
- }
- }
-
- public void stop() throws ServiceException{
- // Do our stuff
- synchronized (this){
- // Don't bother if we are already stopped/stopping
- if (stop) return;
-
- //System.out.println("[] sending stop signal");
- stop = true;
- try{
- this.notifyAll();
- } catch (Throwable t){
- t.printStackTrace();
- //logger.error("Unable to notify the server thread to stop.
Received exception: "+t.getClass().getName()+" : "+t.getMessage());
- }
- // Then call the next guy
- next.stop();
+ public void setGBeanContext(GBeanContext context) {
+ }
+
+ public synchronized void doStart() throws ServiceException {
+ // Don't bother if we are already started/starting
+ if (socketDaemon != null) {
+ return;
+ }
+
+ ServerSocket serverSocket;
+ try {
+ serverSocket = new ServerSocket(port, 20,
InetAddress.getByName(inetAddress));
+ } catch (Exception e) {
+ throw new ServiceException("Service failed to open socket", e);
}
+
+ socketDaemon = new SocketDaemon(serverService, serverSocket);
+ Thread thread = new Thread(socketDaemon);
+ thread.setName("service." + serverService.getName() + "@" +
socketDaemon.hashCode());
+ thread.setDaemon(true);
+ thread.start();
}
- public void service(Socket socket) throws ServiceException, IOException{
- // Do our stuff
- // Check authorization
+ public synchronized void doStop() {
+ if (socketDaemon != null) {
+ socketDaemon.stop();
+ socketDaemon = null;
+ }
+ }
- // Then call the next guy
- next.service(socket);
+ public void doFail() {
+ doStop();
}
- /**
- * Gets the name of the service.
- * Used for display purposes only
- */
- public String getName(){
- return next.getName();
+ public String getServiceName() {
+ return serverService.getName();
}
/**
- * Gets the ip number that the
+ * Gets the inetAddress number that the
* daemon is listening on.
*/
- public String getIP(){
- return ip;
+ public String getInetAddress() {
+ return inetAddress;
}
-
+
/**
- * Gets the port number that the
+ * Gets the port number that the
* daemon is listening on.
*/
- public int getPort(){
+ public int getPort() {
return port;
}
+ private static class SocketDaemon implements Runnable {
+ private ServerService serverService;
+ private ServerSocket serverSocket;
+ private boolean stopped;
+
+ public SocketDaemon(ServerService serverService, ServerSocket serverSocket)
{
+ this.serverService = serverService;
+ this.serverSocket = serverSocket;
+ stopped = false;
+ }
+
+ public synchronized void stop() {
+ stopped = true;
+ }
- public void run() {
+ private synchronized boolean shouldStop() {
+ return stopped;
+ }
- Socket socket = null;
-
- while ( !stop ) {
- try {
-
- socket = serverSocket.accept();
-
- if (!stop) service(socket);
-
- } catch ( SecurityException e ) {
- //logger.error( "Security error: "+ e.getMessage() );
- } catch ( Throwable e ) {
- //logger.error( "Unexpected error", e );
+ public void run() {
+ while (!shouldStop()) {
+ Socket socket = null;
+ try {
+ socket = serverSocket.accept();
+ if (!shouldStop()) {
+ serverService.service(socket);
+ }
+ } catch (Throwable e) {
+ log.error("Unexpected error", e);
+ } finally {
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (Throwable t) {
+ log.error("Encountered problem while closing connection
with client: " + t.getMessage());
+ }
+ }
+ }
+ }
- } finally {
+ if (serverSocket != null) {
try {
- if ( socket != null ) socket.close();
- } catch ( Throwable t ){
- //logger.error("Encountered problem while closing connection
with client: "+t.getMessage());
+ serverSocket.close();
+ } catch (IOException ioException) {
+ log.debug("Error cleaning up socked", ioException);
}
+ serverSocket = null;
}
+ serverService = null;
}
+ }
+
+ public static final GBeanInfo GBEAN_INFO;
+
+ static {
+ GBeanInfoFactory infoFactory = new GBeanInfoFactory(ServiceDaemon.class);
+
+ infoFactory.setConstructor(
+ new String[]{"ServerService", "InetAddress", "Port"},
+ new Class[]{ServerService.class, String.class, Integer.TYPE});
+ infoFactory.addReference("ServerService", ServerService.class);
+ infoFactory.addAttribute("InetAddress", true);
+ infoFactory.addAttribute("Port", true);
+
+ infoFactory.addAttribute("ServiceName", false);
+
+ GBEAN_INFO = infoFactory.getBeanInfo();
}
}
+
1.3 +110 -110
openejb/modules/core/src/java/org/openejb/server/ServiceManager.java
Index: ServiceManager.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/server/ServiceManager.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ServiceManager.java 10 Mar 2004 09:20:19 -0000 1.2
+++ ServiceManager.java 8 Apr 2004 00:05:52 -0000 1.3
@@ -44,43 +44,47 @@
*/
package org.openejb.server;
-import java.io.*;
-import java.net.*;
-import java.util.*;
-import org.openejb.*;
-import org.openejb.util.*;
+import java.net.InetAddress;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Properties;
+import java.util.Vector;
+
+import org.openejb.util.Logger;
+import org.openejb.util.Messages;
/**
- * This is the base class for orcistrating the other daemons
- * which actually accept and react to calls coming in from
+ * This is the base class for orcistrating the other daemons
+ * which actually accept and react to calls coming in from
* different protocols or channels.
- *
+ *
* To perform this task, this class will
* newInstance()
* init( port, properties)
* start()
* stop()
- *
- *
+ *
+ *
*/
public class ServiceManager {
- static Messages messages = new Messages( "org.openejb.server" );
- static Logger logger = Logger.getInstance( "OpenEJB.server.remote",
"org.openejb.server" );
+ static Messages messages = new Messages("org.openejb.server");
+ static Logger logger = Logger.getInstance("OpenEJB.server.remote",
"org.openejb.server");
private static ServiceManager manager;
-
+
private static HashMap propsByFile = new HashMap();
private static HashMap fileByProps = new HashMap();
- private static ServerService[] daemons;
+ private static ServiceDaemon[] daemons;
private boolean stop = false;
- private ServiceManager(){}
+ private ServiceManager() {
+ }
- public static ServiceManager getManager(){
+ public static ServiceManager getManager() {
if (manager == null) {
manager = new ServiceManager();
}
@@ -88,7 +92,7 @@
return manager;
}
- // Have properties files (like xinet.d) that specifies what daemons to
+ // Have properties files (like xinet.d) that specifies what daemons to
// Look into the xinet.d file structure again
// conf/server.d/
// admin.properties
@@ -105,42 +109,46 @@
// The port to use
// whether it's turned on
public void init() throws Exception {
- try{
+ try {
org.apache.log4j.MDC.put("SERVER", "main");
InetAddress localhost = InetAddress.getLocalHost();
org.apache.log4j.MDC.put("HOST", localhost.getHostName());
- } catch (Exception e){
+ } catch (Exception e) {
}
-
+
// Get the properties files
// - hard coded for now -
- String[] serviceFiles = new String[] {
+ String[] serviceFiles = new String[]{
"admin.properties",
"ejbd.properties",
"telnet.properties",
"webadmin.properties"
};
-
+
Vector enabledServers = new Vector();
- for (int i=0; i < serviceFiles.length; i++){
- try{
+ for (int i = 0; i < serviceFiles.length; i++) {
+ try {
//Properties props =
getProperties("conf/server.d/"+serviceFiles[i]);
Properties props = getProperties(serviceFiles[i]);
- if (isEnabled(props)){
+ if (isEnabled(props)) {
ServerService server = createService(props);
server = wrapService(server);
server.init(props);
- enabledServers.add(server);
+
+ String ip = props.getProperty("bind");
+ int port = Integer.parseInt(props.getProperty("port"));
+ ServiceDaemon daemon = new ServiceDaemon(server, ip, port);
+ enabledServers.add(daemon);
}
- } catch (Throwable e){
+ } catch (Throwable e) {
logger.i18n.error("service.not.loaded", serviceFiles[i],
e.getMessage());
}
}
- daemons = new ServerService[enabledServers.size()];
+ daemons = new ServiceDaemon[enabledServers.size()];
enabledServers.copyInto(daemons);
@@ -157,60 +165,59 @@
}
- private static Properties getProperties(String file) throws ServiceException{
- Properties props = (Properties)propsByFile.get(file);
+ private static Properties getProperties(String file) throws ServiceException {
+ Properties props = (Properties) propsByFile.get(file);
if (props == null) {
props = loadProperties(file);
- propsByFile.put(file,props);
- fileByProps.put(props,file);
+ propsByFile.put(file, props);
+ fileByProps.put(props, file);
}
-
+
return props;
}
- private static Properties loadProperties(String file) throws ServiceException{
+ private static Properties loadProperties(String file) throws ServiceException {
Properties props = new Properties();
- try{
- URL url = new URL("resource:/"+file);
+ try {
+ URL url = new URL("resource:/" + file);
props.load(url.openStream());
- } catch (Exception e){
+ } catch (Exception e) {
//e.printStackTrace();
- throw new ServiceException("Cannot load properties",e);
+ throw new ServiceException("Cannot load properties", e);
}
return props;
}
-
+
private ServerService createService(Properties props) throws ServiceException {
ServerService service = null;
-
- String serviceClassName = getRequiredProperty("server",props);
+
+ String serviceClassName = getRequiredProperty("server", props);
Class serviceClass = loadClass(serviceClassName);
checkImplementation(serviceClass);
service = instantiateService(serviceClass);
-
+
return service;
}
-
- private ServerService wrapService(ServerService service){
+
+ private ServerService wrapService(ServerService service) {
service = new ServiceLogger(service);
service = new ServiceAccessController(service);
- service = new ServiceDaemon(service);
return service;
}
public synchronized void start() throws ServiceException {
System.out.println(" ** Starting Services **");
printRow("NAME", "IP", "PORT");
-
- for (int i=0; i < daemons.length; i++){
- ServerService d = daemons[i];
- try{
- d.start();
- printRow(d.getName(), d.getIP(), d.getPort()+"");
- } catch (Exception e){
- logger.error(d.getName()+" "+d.getIP()+" "+d.getPort()+":
"+e.getMessage());
- printRow(d.getName(), "----", "FAILED");
+
+ for (int i = 0; i < daemons.length; i++) {
+ ServiceDaemon d = daemons[i];
+ try {
+ d.doStart();
+ printRow(d.getServiceName(), d.getInetAddress(), d.getPort() + "");
+ } catch (Exception e) {
+ logger.error(d.getServiceName() + " " + d.getInetAddress() + " " +
d.getPort() + ": " + e.getMessage());
+ printRow(d.getServiceName(), "----", "FAILED");
}
}
@@ -225,13 +232,13 @@
* To stop the thread (and the VM), just call the stop method
* which will set 'stop' to true and notify the user thread.
*/
- try{
- while ( !stop ) {
+ try {
+ while (!stop) {
//System.out.println("[] waiting to stop
\t["+Thread.currentThread().getName()+"]");
this.wait(Long.MAX_VALUE);
}
- } catch (Throwable t){
- logger.fatal("Unable to keep the server thread alive. Received
exception: "+t.getClass().getName()+" : "+t.getMessage());
+ } catch (Throwable t) {
+ logger.fatal("Unable to keep the server thread alive. Received
exception: " + t.getClass().getName() + " : " + t.getMessage());
}
System.out.println("[] exiting vm");
logger.info("Stopping Remote Server");
@@ -241,25 +248,25 @@
public synchronized void stop() throws ServiceException {
System.out.println("[] received stop signal");
stop = true;
- for (int i=0; i < daemons.length; i++){
- daemons[i].stop();
+ for (int i = 0; i < daemons.length; i++) {
+ daemons[i].doStop();
}
notifyAll();
}
- public void printRow(String col1, String col2, String col3){
+ public void printRow(String col1, String col2, String col3) {
// column 1 is 20 chars long
col1 += " ";
- col1 = col1.substring(0,20);
-
+ col1 = col1.substring(0, 20);
+
// column 2 is 15 chars long
col2 += " ";
- col2 = col2.substring(0,15);
-
+ col2 = col2.substring(0, 15);
+
// column 3 is 6 chars long
col3 += " ";
- col3 = col3.substring(0,6);
+ col3 = col3.substring(0, 6);
StringBuffer sb = new StringBuffer(50);
sb.append(" ").append(col1);
@@ -271,37 +278,36 @@
/**
* Loads the service class passed in
- *
+ *
* @param className
- *
- * @return
+ *
+ * @return
* @exception ServiceException
*/
- private Class loadClass(String className) throws ServiceException{
+ private Class loadClass(String className) throws ServiceException {
ClassLoader loader =
org.openejb.util.ClasspathUtils.getContextClassLoader();
Class clazz = null;
- try{
- clazz = Class.forName(className,true,loader);
- }
- catch (ClassNotFoundException cnfe){
+ try {
+ clazz = Class.forName(className, true, loader);
+ } catch (ClassNotFoundException cnfe) {
String msg = messages.format("service.no.class", className);
- throw new ServiceException( msg );
- }
+ throw new ServiceException(msg);
+ }
return clazz;
}
/**
* Does this class implement the ServerService interface?
- *
+ *
* @param clazz
- *
+ *
* @exception ServiceException
*/
- private void checkImplementation(Class clazz) throws ServiceException{
+ private void checkImplementation(Class clazz) throws ServiceException {
Class intrfce = org.openejb.server.ServerService.class;
- if ( !intrfce.isAssignableFrom(clazz) ){
- String msg = messages.format("service.bad.impl",
clazz.getName(),intrfce.getName());
+ if (!intrfce.isAssignableFrom(clazz)) {
+ String msg = messages.format("service.bad.impl", clazz.getName(),
intrfce.getName());
throw new ServiceException(msg);
}
}
@@ -309,57 +315,51 @@
/**
* Instantiates the specified service
- *
+ *
* @param clazz
- *
- * @return
+ *
+ * @return
* @exception ServiceException
*/
- private ServerService instantiateService(Class clazz) throws ServiceException{
+ private ServerService instantiateService(Class clazz) throws ServiceException {
ServerService service = null;
-
- try{
- service = (ServerService)clazz.newInstance();
- }
- catch (Throwable t){
+
+ try {
+ service = (ServerService) clazz.newInstance();
+ } catch (Throwable t) {
String msg = messages.format(
- "service.instantiation.err",
- clazz.getName(),
- t.getClass().getName(),
- t.getMessage());
+ "service.instantiation.err",
+ clazz.getName(),
+ t.getClass().getName(),
+ t.getMessage());
- throw new ServiceException( msg, t );
- }
+ throw new ServiceException(msg, t);
+ }
return service;
}
- private boolean isEnabled(Properties props) throws ServiceException{
- // if it should be started, continue
- String dissabled = props.getProperty("dissabled","");
-
+ private boolean isEnabled(Properties props) {
+ // if it should be started, continue
+ String dissabled = props.getProperty("dissabled", "");
+
if (dissabled.equalsIgnoreCase("yes") ||
dissabled.equalsIgnoreCase("true")) {
return false;
} else {
return true;
}
}
-
-
- public static String getRequiredProperty(String name, Properties props) throws
ServiceException{
-
+ public static String getRequiredProperty(String name, Properties props) throws
ServiceException {
String value = props.getProperty(name);
if (value == null) {
String msg = messages.format(
- "service.missing.property",
- name, fileByProps.get(props));
+ "service.missing.property",
+ name, fileByProps.get(props));
- throw new ServiceException( msg );
+ throw new ServiceException(msg);
}
return value;
}
-
-
}