|
1 |
| package net.sf.crispy.impl; |
|
2 |
| |
|
3 |
| import java.lang.reflect.Method; |
|
4 |
| import java.util.Properties; |
|
5 |
| |
|
6 |
| import net.sf.crispy.InvocationStrategy; |
|
7 |
| import net.sf.crispy.proxy.StaticProxy; |
|
8 |
| import net.sf.crispy.strategy.NameInvocationStrategy; |
|
9 |
| import net.sf.crispy.util.Util; |
|
10 |
| |
|
11 |
| import org.omg.CORBA.ORB; |
|
12 |
| import org.omg.CosNaming.NamingContextExt; |
|
13 |
| import org.omg.CosNaming.NamingContextExtHelper; |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| public class StaticCorbaProxy extends StaticProxy { |
|
22 |
| |
|
23 |
| private ORB orb = null; |
|
24 |
| private org.omg.CORBA.Object namingContext = null; |
|
25 |
| private NamingContextExt namingContextExt = null; |
|
26 |
| |
|
27 |
39
| public String getDefaultUrlAndPort() {
|
|
28 |
39
| return "localhost";
|
|
29 |
| } |
|
30 |
| |
|
31 |
20
| public InvocationStrategy getDefaultInvocationStrategy(Properties pvProperties) {
|
|
32 |
20
| String lvInterfaceName = (String) pvProperties.get(PROPERTY_CURRENT_INTERFACE_CLASS);
|
|
33 |
20
| lvInterfaceName = lvInterfaceName.replaceAll("\\.", "_");
|
|
34 |
20
| return new NameInvocationStrategy(lvInterfaceName);
|
|
35 |
| } |
|
36 |
| |
|
37 |
20
| public Object newInstance(Class pvClass) {
|
|
38 |
20
| try {
|
|
39 |
20
| if (orb == null) {
|
|
40 |
19
| String lvUrlAndPort = getUrlAndPort();
|
|
41 |
19
| String lvUrlAndPortSeparated[] = null;
|
|
42 |
| |
|
43 |
19
| if (lvUrlAndPort != null && !lvUrlAndPort.equals("localhost")) {
|
|
44 |
1
| lvUrlAndPortSeparated = Util.separateHostAndPort(lvUrlAndPort);
|
|
45 |
| } |
|
46 |
| |
|
47 |
19
| String lvPort = null;
|
|
48 |
19
| String lvHost = null;
|
|
49 |
19
| if (lvUrlAndPortSeparated != null) {
|
|
50 |
1
| lvHost = lvUrlAndPortSeparated [0];
|
|
51 |
1
| lvPort = lvUrlAndPortSeparated [1];
|
|
52 |
| } else { |
|
53 |
18
| lvHost = getProperties().getProperty("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
|
|
54 |
18
| lvPort = getProperties().getProperty("org.omg.CORBA.ORBInitialPort", "1057");
|
|
55 |
| } |
|
56 |
| |
|
57 |
19
| Properties props = new Properties();
|
|
58 |
19
| props.put("org.omg.CORBA.ORBInitialPort", lvPort);
|
|
59 |
19
| props.put("org.omg.CORBA.ORBInitialHost", lvHost);
|
|
60 |
| |
|
61 |
19
| orb = ORB.init((String[]) null, props);
|
|
62 |
19
| namingContext = orb.resolve_initial_references("NameService");
|
|
63 |
19
| namingContextExt = NamingContextExtHelper.narrow(namingContext);
|
|
64 |
| } |
|
65 |
| |
|
66 |
20
| String lvBindName = getInvocationStrategy().toString();
|
|
67 |
| if (log.isInfoEnabled()) { |
|
68 |
| log.info("LookUp-Name: " + lvBindName); |
|
69 |
| } |
|
70 |
20
| org.omg.CORBA.Object lvCorbaObject = namingContextExt.resolve_str(lvBindName);
|
|
71 |
20
| String lvHelper = pvClass.getName();
|
|
72 |
20
| Object lvProxyObject = createWithHelperCorbaObject(lvHelper, lvCorbaObject);
|
|
73 |
20
| setProxyObject(lvProxyObject);
|
|
74 |
20
| setProxyClass(pvClass);
|
|
75 |
| |
|
76 |
| } catch (Exception e) { |
|
77 |
0
| e.printStackTrace();
|
|
78 |
| } |
|
79 |
| |
|
80 |
20
| return getProxyObject();
|
|
81 |
| } |
|
82 |
| |
|
83 |
22
| public static Object createWithHelperCorbaObject (String pvServiceInterface, org.omg.CORBA.Object pvCorbaObject) {
|
|
84 |
22
| try {
|
|
85 |
22
| String lvHelper = pvServiceInterface + "Helper";
|
|
86 |
| if (log.isInfoEnabled()) { |
|
87 |
| log.info("Create Helper: " + lvHelper); |
|
88 |
| } |
|
89 |
22
| Class lvHelperClass = Class.forName(lvHelper);
|
|
90 |
22
| Method lvNarrowMethod = lvHelperClass.getMethod("narrow", new Class[] {org.omg.CORBA.Object.class});
|
|
91 |
22
| Object lvReturnObject = (org.omg.CORBA.Object) lvNarrowMethod.invoke(null, new Object[] { pvCorbaObject });
|
|
92 |
22
| return lvReturnObject;
|
|
93 |
| } catch (Exception e) { |
|
94 |
0
| e.printStackTrace();
|
|
95 |
| } |
|
96 |
0
| return null;
|
|
97 |
| } |
|
98 |
| |
|
99 |
| } |