|
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 |
| |
|
19 |
| |
|
20 |
| |
|
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 |
1
| public MiniJBossRemotingServer() { }
|
|
33 |
1
| public MiniJBossRemotingServer(String pvUrl) {
|
|
34 |
1
| url = pvUrl;
|
|
35 |
| } |
|
36 |
| |
|
37 |
2
| public void addService (String pvServiceInterface, String pvServiceObject) {
|
|
38 |
2
| services.put(pvServiceInterface, pvServiceObject);
|
|
39 |
| } |
|
40 |
| |
|
41 |
1
| public void addService (String pvServiceInterface, Object pvServiceObject) {
|
|
42 |
1
| services.put(pvServiceInterface, pvServiceObject);
|
|
43 |
| } |
|
44 |
| |
|
45 |
2
| public void start() {
|
|
46 |
2
| try {
|
|
47 |
2
| connector = new Connector();
|
|
48 |
2
| boolean isPortFree = Util.isPortFree(5411);
|
|
49 |
2
| if (isPortFree == true) {
|
|
50 |
2
| InvokerLocator locator = new InvokerLocator(url);
|
|
51 |
2
| connector.setInvokerLocator(locator.getLocatorURI());
|
|
52 |
2
| connector.create();
|
|
53 |
| |
|
54 |
2
| JBossRemotingInvocationHandler lvInvocationHandler = new JBossRemotingInvocationHandler();
|
|
55 |
| |
|
56 |
2
| Iterator it = services.keySet().iterator();
|
|
57 |
2
| while (it.hasNext()) {
|
|
58 |
2
| String lvKey = (String) it.next();
|
|
59 |
2
| Object lvServiceObject = services.get(lvKey);
|
|
60 |
2
| if (lvServiceObject instanceof String) {
|
|
61 |
2
| String lvValue = (String) services.get(lvKey);
|
|
62 |
2
| lvServiceObject = Util.createObject(lvValue);
|
|
63 |
| } |
|
64 |
2
| lvInvocationHandler.addService(lvKey, lvServiceObject);
|
|
65 |
| } |
|
66 |
| |
|
67 |
2
| connector.addInvocationHandler("CrispyInvocationHandler", lvInvocationHandler);
|
|
68 |
2
| try {
|
|
69 |
2
| connector.start();
|
|
70 |
| } catch (BindException e) { |
|
71 |
| if (log.isDebugEnabled()) { |
|
72 |
| log.debug("Problem by starting the MiniJBossRemotingServer: " + e); |
|
73 |
| } |
|
74 |
| } |
|
75 |
2
| 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 |
| } |