1 package net.sf.crispy.impl.jboss;
2
3 import java.net.BindException;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.Map;
7
8 import net.sf.crispy.impl.MiniServer;
9 import net.sf.crispy.impl.ServiceManager;
10 import net.sf.crispy.util.Util;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.jboss.remoting.InvokerLocator;
15 import org.jboss.remoting.transport.Connector;
16
17 /**
18 * Mini server for JBoss remoting to answer to client requests.
19 *
20 * @author Linke
21 *
22 */
23 public class MiniJBossRemotingServer implements MiniServer {
24
25 protected static final Log log = LogFactory.getLog (MiniJBossRemotingServer.class);
26 public static final String DEFAULT_URL_AND_PORT = "socket://localhost:5411/?" + InvokerLocator.BYVALUE + "=true";
27
28 private String url = DEFAULT_URL_AND_PORT;
29 private Connector connector = null;
30 private Map services = new HashMap();
31
32 public MiniJBossRemotingServer() { }
33 public MiniJBossRemotingServer(String pvUrl) {
34 url = pvUrl;
35 }
36
37 public void addService (String pvServiceInterface, String pvServiceObject) {
38 services.put(pvServiceInterface, pvServiceObject);
39 }
40
41 public void addService (String pvServiceInterface, Object pvServiceObject) {
42 services.put(pvServiceInterface, pvServiceObject);
43 }
44
45 public void start() {
46 try {
47 connector = new Connector();
48 boolean isPortFree = Util.isPortFree(5411);
49 if (isPortFree == true) {
50 InvokerLocator locator = new InvokerLocator(url);
51 connector.setInvokerLocator(locator.getLocatorURI());
52 connector.create();
53
54 JBossRemotingInvocationHandler lvInvocationHandler = new JBossRemotingInvocationHandler();
55
56 Iterator it = services.keySet().iterator();
57 while (it.hasNext()) {
58 String lvKey = (String) it.next();
59 Object lvServiceObject = services.get(lvKey);
60 if (lvServiceObject instanceof String) {
61 String lvValue = (String) services.get(lvKey);
62 lvServiceObject = Util.createObject(lvValue);
63 }
64 lvInvocationHandler.addService(lvKey, lvServiceObject);
65 }
66
67 connector.addInvocationHandler("CrispyInvocationHandler", lvInvocationHandler);
68 try {
69 connector.start();
70 } catch (BindException e) {
71 if (log.isDebugEnabled()) {
72 log.debug("Problem by starting the MiniJBossRemotingServer: " + e);
73 }
74 }
75 log.debug("MiniJBossRemotingServrer is started ...");
76 }
77 } catch (Exception e) {
78 if (ServiceManager.DEBUG_MODE_ON) {
79 e.printStackTrace();
80 }
81 if (log.isErrorEnabled()) {
82 log.error("Error by starting MiniJBossRemotingServer: " + e);
83 }
84 }
85
86
87 }
88
89 public void stop() {
90 connector.stop();
91 connector.destroy();
92 connector = null;
93 }
94
95 public static void main(String[] args) {
96 Util.initJdkLogger();
97
98 MiniJBossRemotingServer server = new MiniJBossRemotingServer();
99 server.start();
100 }
101
102 }