001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.service;
009:
010: import java.rmi.Remote;
011: import java.rmi.MarshalledObject;
012: import java.net.ServerSocket;
013: import java.net.Socket;
014: import java.io.ObjectOutputStream;
015: import java.io.BufferedOutputStream;
016:
017: import javax.rmi.PortableRemoteObject;
018: import org.huihoo.jfox.logging.Logger;
019:
020: /**
021: * wrap a remote service and export it's stub to a speicific port
022: *
023: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
024: */
025:
026: public class ServiceWrapper extends ServiceSupport implements
027: ServiceWrapperMBean, Runnable {
028: private int port = 0;
029: private Remote remoteObject = null;
030: private ServerSocket ssocket = null;
031:
032: public ServiceWrapper(Remote remoteObject, int port) {
033: String className = remoteObject.getClass().getName();
034: this .setName(parseName(className) + "[ServiceWrapper]");
035: this
036: .setLogger(Logger.getLogger(className
037: + "[ServiceWrapper]"));
038: this .remoteObject = remoteObject;
039: this .setPort(port);
040: }
041:
042: public ServiceWrapper(Class remoteObjectClass, int port) {
043: String className = remoteObjectClass.getName();
044: this .setName(parseName(className) + "[ServiceWrapper]");
045: this
046: .setLogger(Logger.getLogger(className
047: + "[ServiceWrapper]"));
048:
049: if (remoteObjectClass.isAssignableFrom(Remote.class)) {
050: // throw new RuntimeException(remoteObjectClass.getName() + " is not a Remote class");
051: logger.fatal(remoteObjectClass.getName()
052: + " is not a Remote class");
053: return;
054: }
055:
056: try {
057: this .remoteObject = (Remote) remoteObjectClass
058: .newInstance();
059: } catch (Exception e) {
060: logger.fatal(remoteObjectClass.getName()
061: + " instantiator error", e);
062: }
063:
064: this .setPort(port);
065: }
066:
067: public ServiceWrapper(String remoteObjectClassName, int port) {
068: this .setName(parseName(remoteObjectClassName)
069: + "[ServiceWrapper]");
070: this .setLogger(Logger.getLogger(remoteObjectClassName
071: + "[ServiceWrapper]"));
072:
073: Class claz = null;
074: try {
075: claz = Thread.currentThread().getContextClassLoader()
076: .loadClass(remoteObjectClassName);
077: if (claz.isAssignableFrom(Remote.class)) {
078: // throw new RuntimeException(remoteObjectClass.getName() + " is not a Remote class");
079: logger.fatal(claz.getName() + " is not a Remote class");
080: return;
081: }
082:
083: this .remoteObject = (Remote) claz.newInstance();
084: } catch (ClassNotFoundException e) {
085: logger.fatal("Class " + remoteObjectClassName
086: + " not found!" + e);
087: } catch (Exception e) {
088: logger.fatal(claz.getName() + " instantiator error", e);
089: }
090: this .setPort(port);
091: // System.out.println("ServiceWrapper.constructor");
092: }
093:
094: protected void doInit() throws Exception {
095: try {
096: PortableRemoteObject.unexportObject(remoteObject);
097: } catch (Exception e) {
098:
099: }
100: ssocket = new ServerSocket(port);
101: }
102:
103: protected void doStart() throws Exception {
104: PortableRemoteObject.exportObject(remoteObject);
105: new Thread(this ).start();
106: }
107:
108: protected void doStop() throws Exception {
109: PortableRemoteObject.unexportObject(remoteObject);
110: }
111:
112: protected void doDestroy() throws Exception {
113: ssocket.close();
114: ssocket = null;
115: }
116:
117: private void setPort(int port) {
118: this .port = port;
119: }
120:
121: public int getPort() {
122: return port;
123: }
124:
125: public Remote getRemoteObject() {
126: return remoteObject;
127: }
128:
129: public void run() {
130: while (isRunning()) {
131: try {
132: Socket socket = ssocket.accept();
133: new Sender(socket).start();
134: } catch (Exception e) {
135: logger.warn(e.getMessage());
136: }
137: }
138: }
139:
140: /**
141: * Thread class to send stub
142: */
143: private class Sender extends Thread {
144: private Socket socket = null;
145:
146: public Sender(Socket socket) {
147: this .socket = socket;
148: }
149:
150: public void run() {
151: try {
152: ObjectOutputStream out = new ObjectOutputStream(
153: new BufferedOutputStream(socket
154: .getOutputStream()));
155: out.writeObject(new MarshalledObject(remoteObject));
156: out.flush();
157: socket.close();
158: } catch (Exception e) {
159: logger.warn(e.getMessage(), e);
160: }
161: }
162: }
163:
164: }
|