001: package net.sf.crispy.impl.jboss;
002:
003: import java.net.BindException;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.Map;
007:
008: import net.sf.crispy.impl.MiniServer;
009: import net.sf.crispy.impl.ServiceManager;
010: import net.sf.crispy.util.Util;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import org.jboss.remoting.InvokerLocator;
015: import org.jboss.remoting.transport.Connector;
016:
017: /**
018: * Mini server for JBoss remoting to answer to client requests.
019: *
020: * @author Linke
021: *
022: */
023: public class MiniJBossRemotingServer implements MiniServer {
024:
025: protected static final Log log = LogFactory
026: .getLog(MiniJBossRemotingServer.class);
027: public static final String DEFAULT_URL_AND_PORT = "socket://localhost:5411/?"
028: + InvokerLocator.BYVALUE + "=true";
029:
030: private String url = DEFAULT_URL_AND_PORT;
031: private Connector connector = null;
032: private Map services = new HashMap();
033:
034: public MiniJBossRemotingServer() {
035: }
036:
037: public MiniJBossRemotingServer(String pvUrl) {
038: url = pvUrl;
039: }
040:
041: public void addService(String pvServiceInterface,
042: String pvServiceObject) {
043: services.put(pvServiceInterface, pvServiceObject);
044: }
045:
046: public void addService(String pvServiceInterface,
047: Object pvServiceObject) {
048: services.put(pvServiceInterface, pvServiceObject);
049: }
050:
051: public void start() {
052: try {
053: connector = new Connector();
054: boolean isPortFree = Util.isPortFree(5411);
055: if (isPortFree == true) {
056: InvokerLocator locator = new InvokerLocator(url);
057: connector.setInvokerLocator(locator.getLocatorURI());
058: connector.create();
059:
060: JBossRemotingInvocationHandler lvInvocationHandler = new JBossRemotingInvocationHandler();
061:
062: Iterator it = services.keySet().iterator();
063: while (it.hasNext()) {
064: String lvKey = (String) it.next();
065: Object lvServiceObject = services.get(lvKey);
066: if (lvServiceObject instanceof String) {
067: String lvValue = (String) services.get(lvKey);
068: lvServiceObject = Util.createObject(lvValue);
069: }
070: lvInvocationHandler.addService(lvKey,
071: lvServiceObject);
072: }
073:
074: connector.addInvocationHandler(
075: "CrispyInvocationHandler", lvInvocationHandler);
076: try {
077: connector.start();
078: } catch (BindException e) {
079: if (log.isDebugEnabled()) {
080: log
081: .debug("Problem by starting the MiniJBossRemotingServer: "
082: + e);
083: }
084: }
085: log.debug("MiniJBossRemotingServrer is started ...");
086: }
087: } catch (Exception e) {
088: if (ServiceManager.DEBUG_MODE_ON) {
089: e.printStackTrace();
090: }
091: if (log.isErrorEnabled()) {
092: log.error("Error by starting MiniJBossRemotingServer: "
093: + e);
094: }
095: }
096:
097: }
098:
099: public void stop() {
100: connector.stop();
101: connector.destroy();
102: connector = null;
103: }
104:
105: public static void main(String[] args) {
106: Util.initJdkLogger();
107:
108: MiniJBossRemotingServer server = new MiniJBossRemotingServer();
109: server.start();
110: }
111:
112: }
|