|
1 |
| package net.sf.crispy.impl.corba; |
|
2 |
| |
|
3 |
| import net.sf.crispy.impl.MiniServer; |
|
4 |
| import net.sf.crispy.impl.ServiceManager; |
|
5 |
| import net.sf.crispy.impl.StaticCorbaProxy; |
|
6 |
| import net.sf.crispy.util.Util; |
|
7 |
| |
|
8 |
| import org.apache.commons.logging.Log; |
|
9 |
| import org.apache.commons.logging.LogFactory; |
|
10 |
| import org.omg.CORBA.ORB; |
|
11 |
| import org.omg.CosNaming.NameComponent; |
|
12 |
| import org.omg.CosNaming.NamingContextExt; |
|
13 |
| import org.omg.CosNaming.NamingContextExtHelper; |
|
14 |
| import org.omg.PortableServer.POA; |
|
15 |
| import org.omg.PortableServer.POAHelper; |
|
16 |
| import org.omg.PortableServer.Servant; |
|
17 |
| |
|
18 |
| public class MiniCorbaServer implements MiniServer { |
|
19 |
| |
|
20 |
| public static final int PORT = 1057; |
|
21 |
| public static final String HOST = "127.0.0.1"; |
|
22 |
| |
|
23 |
| protected static final Log log = LogFactory.getLog (MiniCorbaServer.class); |
|
24 |
| |
|
25 |
| private static boolean isStarted = false; |
|
26 |
| private int port = PORT; |
|
27 |
| private String host = HOST; |
|
28 |
| private static Process orbdProcess = null; |
|
29 |
| private ORB orb = null; |
|
30 |
| private POA rootpoa = null; |
|
31 |
| private org.omg.CORBA.Object namingContext = null; |
|
32 |
| |
|
33 |
1
| public MiniCorbaServer() {
|
|
34 |
1
| init();
|
|
35 |
| } |
|
36 |
1
| public MiniCorbaServer(int pvPort) {
|
|
37 |
1
| port = pvPort;
|
|
38 |
1
| init();
|
|
39 |
| } |
|
40 |
2
| public MiniCorbaServer(String pvHost, int pvPort) {
|
|
41 |
2
| if (pvHost != null && pvHost.length() > 0) {
|
|
42 |
1
| host = pvHost;
|
|
43 |
| } |
|
44 |
2
| port = pvPort;
|
|
45 |
2
| init();
|
|
46 |
| } |
|
47 |
| |
|
48 |
2
| public int getPort() { return port; }
|
|
49 |
2
| public String getHost() { return host; }
|
|
50 |
| |
|
51 |
4
| private void init() {
|
|
52 |
4
| String lvJrePath = System.getProperty("java.home") + "/bin/orbd -ORBInitialPort " + port + " -ORBInitialHost " + host + " -port " + port;
|
|
53 |
4
| try {
|
|
54 |
4
| if (orbdProcess == null) {
|
|
55 |
1
| System.out.println("ORB-Path: " + lvJrePath);
|
|
56 |
1
| Runtime lvRuntime = Runtime.getRuntime();
|
|
57 |
1
| orbdProcess = lvRuntime.exec(lvJrePath);
|
|
58 |
1
| log.info("ORBD is started on port: " + port);
|
|
59 |
1
| System.out.println("ORB-Process: " + orbdProcess);
|
|
60 |
| } |
|
61 |
| } catch (Exception e) { |
|
62 |
| if (log.isWarnEnabled()) { |
|
63 |
| log.warn("Error by starting ORBD by path " + lvJrePath + " - " + e, e); |
|
64 |
| } |
|
65 |
| if (ServiceManager.DEBUG_MODE_ON) { |
|
66 |
| e.printStackTrace(); |
|
67 |
| } |
|
68 |
| } |
|
69 |
| |
|
70 |
| |
|
71 |
4
| try {
|
|
72 |
4
| if (isStarted == false) {
|
|
73 |
1
| String args[] = new String[] {"-ORBInitialPort", Integer.toString(port), "-ORBInitialHost", host};
|
|
74 |
| |
|
75 |
1
| orb = ORB.init(args, null);
|
|
76 |
1
| log.info("ORB is init on host: " + host + " on port: " + port);
|
|
77 |
| |
|
78 |
| |
|
79 |
1
| rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
|
|
80 |
1
| rootpoa.the_POAManager().activate();
|
|
81 |
1
| log.info("RootPOA: " + rootpoa);
|
|
82 |
| |
|
83 |
| |
|
84 |
1
| namingContext = orb.resolve_initial_references("NameService");
|
|
85 |
1
| log.info("NamingContext: " + namingContext);
|
|
86 |
| } |
|
87 |
| } catch (Exception e) { |
|
88 |
| if (log.isWarnEnabled()) { |
|
89 |
| log.warn("Error by init MiniCorbaServer: " + e, e); |
|
90 |
| } |
|
91 |
| if (ServiceManager.DEBUG_MODE_ON) { |
|
92 |
| e.printStackTrace(); |
|
93 |
| } |
|
94 |
| } |
|
95 |
| } |
|
96 |
| |
|
97 |
3
| public void start() {
|
|
98 |
| |
|
99 |
3
| try {
|
|
100 |
3
| if (isStarted == false) {
|
|
101 |
| |
|
102 |
1
| Thread lvThread = new Thread(new Runnable() {
|
|
103 |
| |
|
104 |
1
| public void run() {
|
|
105 |
1
| orb.run();
|
|
106 |
| } |
|
107 |
| |
|
108 |
| }); |
|
109 |
1
| lvThread.start();
|
|
110 |
1
| isStarted = true;
|
|
111 |
1
| log.info("MiniCorbaServer is on host: " + host + " on port: " + port + " started.");
|
|
112 |
| } |
|
113 |
| } catch (Exception e) { |
|
114 |
| if (log.isWarnEnabled()) { |
|
115 |
| log.warn("Error by starting the MiniCorbaServer: " + e, e); |
|
116 |
| } |
|
117 |
| if (ServiceManager.DEBUG_MODE_ON) { |
|
118 |
| e.printStackTrace(); |
|
119 |
| } |
|
120 |
| } |
|
121 |
| } |
|
122 |
| |
|
123 |
| public void stop() { |
|
124 |
| try { |
|
125 |
| if (orbdProcess != null) { |
|
126 |
| orbdProcess.destroy(); |
|
127 |
| } |
|
128 |
| } catch (Exception e) { |
|
129 |
| if (log.isWarnEnabled()) { |
|
130 |
| log.warn("Error by stopping the ORBD: " + e, e); |
|
131 |
| } |
|
132 |
| } |
|
133 |
| try { |
|
134 |
| orb.destroy(); |
|
135 |
| } catch (Exception e) { |
|
136 |
| if (log.isWarnEnabled()) { |
|
137 |
| log.warn("Error by stopping MiniCorbaServer: " + e, e); |
|
138 |
| } |
|
139 |
| } |
|
140 |
| isStarted = false; |
|
141 |
| } |
|
142 |
| |
|
143 |
2
| public void addService(String pvServiceInterface, String pvServiceObject) {
|
|
144 |
2
| try {
|
|
145 |
2
| Object lvServiceObject = Util.createObject(pvServiceObject);
|
|
146 |
2
| addService(pvServiceInterface, lvServiceObject);
|
|
147 |
| } catch (Exception e) { |
|
148 |
| if (ServiceManager.DEBUG_MODE_ON) { |
|
149 |
| e.printStackTrace(); |
|
150 |
| } |
|
151 |
| } |
|
152 |
| } |
|
153 |
| |
|
154 |
2
| public void addService (String pvLookName, Object pvServiceObject) {
|
|
155 |
2
| try {
|
|
156 |
2
| org.omg.CORBA.Object ref = rootpoa.servant_to_reference((Servant) pvServiceObject);
|
|
157 |
2
| NamingContextExt ncRef = NamingContextExtHelper.narrow(namingContext);
|
|
158 |
| |
|
159 |
| |
|
160 |
2
| String lvBindName = pvLookName.replaceAll("\\.", "_");
|
|
161 |
| if (log.isInfoEnabled()) { |
|
162 |
| log.info("Bind Service with name: " + lvBindName + " and Service-Object: " + pvServiceObject); |
|
163 |
| } |
|
164 |
2
| NameComponent path[] = ncRef.to_name( lvBindName );
|
|
165 |
2
| org.omg.CORBA.Object href = (org.omg.CORBA.Object) StaticCorbaProxy.createWithHelperCorbaObject(pvLookName, ref);
|
|
166 |
2
| ncRef.rebind(path, href);
|
|
167 |
| } catch (Exception e) { |
|
168 |
| if (ServiceManager.DEBUG_MODE_ON) { |
|
169 |
| e.printStackTrace(); |
|
170 |
| } |
|
171 |
| } |
|
172 |
| } |
|
173 |
| |
|
174 |
| public static void main(String[] args) { |
|
175 |
| Util.initJdkLogger(); |
|
176 |
| MiniServer lvMiniServer = new MiniCorbaServer(); |
|
177 |
| lvMiniServer.start(); |
|
178 |
| } |
|
179 |
| |
|
180 |
| |
|
181 |
| } |