View Javadoc

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   * This implementation is <b>experimental</b>.
17   * <code>idlj -fall [idl_file_name].idl</code>
18   * @author Linke
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  	public String getDefaultUrlAndPort() {
28  		return "localhost";
29  	}
30  	
31  	public InvocationStrategy getDefaultInvocationStrategy(Properties pvProperties) {
32      	String lvInterfaceName = (String) pvProperties.get(PROPERTY_CURRENT_INTERFACE_CLASS);
33      	lvInterfaceName = lvInterfaceName.replaceAll("\\.", "_");
34      	return new NameInvocationStrategy(lvInterfaceName);
35  	}
36  
37  	public Object newInstance(Class pvClass) {
38  		try {
39  			if (orb == null) {
40  				String lvUrlAndPort = getUrlAndPort();
41  				String lvUrlAndPortSeparated[] = null;
42  				
43  				if (lvUrlAndPort != null && !lvUrlAndPort.equals("localhost")) {
44  					lvUrlAndPortSeparated = Util.separateHostAndPort(lvUrlAndPort);
45  				}
46  				
47  				String lvPort = null;
48  				String lvHost = null;
49  				if (lvUrlAndPortSeparated != null) {
50  					lvHost = lvUrlAndPortSeparated [0];
51  					lvPort = lvUrlAndPortSeparated [1];
52  				} else {				
53  					lvHost = getProperties().getProperty("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
54  					lvPort = getProperties().getProperty("org.omg.CORBA.ORBInitialPort", "1057");
55  				}
56  			        
57  		  	  	Properties props = new Properties();
58  		  	  	props.put("org.omg.CORBA.ORBInitialPort", lvPort);
59  		  	  	props.put("org.omg.CORBA.ORBInitialHost", lvHost);
60  			
61  		  	  	orb = ORB.init((String[]) null, props);
62  				namingContext = orb.resolve_initial_references("NameService");
63  				namingContextExt = NamingContextExtHelper.narrow(namingContext);
64  			}
65  				
66  	        String lvBindName = getInvocationStrategy().toString();
67  	        if (log.isInfoEnabled()) {
68  	        	log.info("LookUp-Name: " + lvBindName);
69  	        }
70  	        org.omg.CORBA.Object lvCorbaObject = namingContextExt.resolve_str(lvBindName);
71  	        String lvHelper = pvClass.getName();
72  	        Object lvProxyObject = createWithHelperCorbaObject(lvHelper, lvCorbaObject);
73  			setProxyObject(lvProxyObject);
74  			setProxyClass(pvClass);
75  		        
76  		} catch (Exception e) {
77  			e.printStackTrace();
78  		}
79  
80  		return getProxyObject();
81  	}
82  	
83  	public static Object createWithHelperCorbaObject (String pvServiceInterface, org.omg.CORBA.Object pvCorbaObject) {
84  		try {
85  			String lvHelper = pvServiceInterface + "Helper";
86  	        if (log.isInfoEnabled()) {
87  	        	log.info("Create Helper: " + lvHelper);
88  	        }	        				
89  	        Class lvHelperClass = Class.forName(lvHelper);
90  	        Method lvNarrowMethod = lvHelperClass.getMethod("narrow", new Class[] {org.omg.CORBA.Object.class});
91  	        Object lvReturnObject = (org.omg.CORBA.Object) lvNarrowMethod.invoke(null, new Object[] { pvCorbaObject });
92  			return lvReturnObject;
93  		} catch (Exception e) {
94  			e.printStackTrace();
95  		}
96  		return null;
97  	}
98  
99  }