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 public MiniCorbaServer() {
34 init();
35 }
36 public MiniCorbaServer(int pvPort) {
37 port = pvPort;
38 init();
39 }
40 public MiniCorbaServer(String pvHost, int pvPort) {
41 if (pvHost != null && pvHost.length() > 0) {
42 host = pvHost;
43 }
44 port = pvPort;
45 init();
46 }
47
48 public int getPort() { return port; }
49 public String getHost() { return host; }
50
51 private void init() {
52 String lvJrePath = System.getProperty("java.home") + "/bin/orbd -ORBInitialPort " + port + " -ORBInitialHost " + host + " -port " + port;
53 try {
54 if (orbdProcess == null) {
55 System.out.println("ORB-Path: " + lvJrePath);
56 Runtime lvRuntime = Runtime.getRuntime();
57 orbdProcess = lvRuntime.exec(lvJrePath);
58 log.info("ORBD is started on port: " + port);
59 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 try {
72 if (isStarted == false) {
73 String args[] = new String[] {"-ORBInitialPort", Integer.toString(port), "-ORBInitialHost", host};
74
75 orb = ORB.init(args, null);
76 log.info("ORB is init on host: " + host + " on port: " + port);
77
78
79 rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
80 rootpoa.the_POAManager().activate();
81 log.info("RootPOA: " + rootpoa);
82
83
84 namingContext = orb.resolve_initial_references("NameService");
85 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 public void start() {
98
99 try {
100 if (isStarted == false) {
101
102 Thread lvThread = new Thread(new Runnable() {
103
104 public void run() {
105 orb.run();
106 }
107
108 });
109 lvThread.start();
110 isStarted = true;
111 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 public void addService(String pvServiceInterface, String pvServiceObject) {
144 try {
145 Object lvServiceObject = Util.createObject(pvServiceObject);
146 addService(pvServiceInterface, lvServiceObject);
147 } catch (Exception e) {
148 if (ServiceManager.DEBUG_MODE_ON) {
149 e.printStackTrace();
150 }
151 }
152 }
153
154 public void addService (String pvLookName, Object pvServiceObject) {
155 try {
156 org.omg.CORBA.Object ref = rootpoa.servant_to_reference((Servant) pvServiceObject);
157 NamingContextExt ncRef = NamingContextExtHelper.narrow(namingContext);
158
159
160 String lvBindName = pvLookName.replaceAll("\\.", "_");
161 if (log.isInfoEnabled()) {
162 log.info("Bind Service with name: " + lvBindName + " and Service-Object: " + pvServiceObject);
163 }
164 NameComponent path[] = ncRef.to_name( lvBindName );
165 org.omg.CORBA.Object href = (org.omg.CORBA.Object) StaticCorbaProxy.createWithHelperCorbaObject(pvLookName, ref);
166 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 }