View Javadoc

1   /*
2    * Created on 29.04.2005 from Linke
3    *
4    */
5   package net.sf.crispy.impl;
6   
7   import java.beans.BeanInfo;
8   import java.beans.Introspector;
9   import java.beans.MethodDescriptor;
10  import java.lang.reflect.Method;
11  
12  import javax.naming.InitialContext;
13  import javax.naming.NamingException;
14  import javax.rmi.PortableRemoteObject;
15  
16  import net.sf.crispy.InvocationException;
17  import net.sf.crispy.Property;
18  import net.sf.crispy.proxy.StaticProxy;
19  
20  /**
21   * Remote call with EJB's (JNDI lookup).
22   * 
23   * @author Linke
24   *
25   */
26  public class StaticEjbProxy extends StaticProxy {
27  	
28  	private InitialContext initialContext = null;
29  
30      public StaticEjbProxy () {}
31  	
32      /**
33       * Get default url and port. If no url and port is in properties.
34       *  
35       * @return Default url and port.
36       */
37      public String getDefaultUrlAndPort() {
38      	return null;
39      }
40  
41  	public Object newInstance (Class pvClass) {
42  		return createProxy(pvClass);
43  	}
44  	
45  	private Object createProxy (Class pvProxyClass) {
46  		// initialContext einrichten
47  		try {
48  			initialContext = new InitialContext(getProperties());
49  		} catch (NamingException e) {
50  			throw new InvocationException("Exception by the InitialContext", e);
51  		} 
52  		// JNDI-Namen fuer lookup
53  		String lvJndi = getProperties().getProperty(pvProxyClass.getName());
54  		if (lvJndi == null) { 
55  			throw new InvocationException("For the Class: " + pvProxyClass.getName() + " is no JNDI-Entry."); 
56  		}
57  		Object lvHome = null;
58  		// JNDI-lookup
59  		try {
60  			lvHome = initialContext.lookup(lvJndi);
61  		} catch (NamingException e) {
62  			throw new InvocationException("Exception by JNDI-lookup: " + lvJndi, e);
63  		}				
64  		// HomeInterface bestimmen
65  		if (lvHome == null) {
66  			throw new InvocationException("For the JNDI-Entry " + lvJndi + " exist no HomeInterface.");
67  		}
68  		String lvEjbHomeStr = getProperties().getProperty(pvProxyClass.getName() + Property.EJB_HOME_INTERFACE);
69  		try {
70  			Class lvEjbHomeClass = Class.forName(lvEjbHomeStr);
71  			lvHome = PortableRemoteObject.narrow(lvHome, lvEjbHomeClass);
72  		} catch (Exception e) {
73  			throw new InvocationException("Exception by loading HomeInterface: " + lvEjbHomeStr 
74  								+ " (please check your properties and set the Home-Interface:" +
75  										"ServiceName + Property.EJB_HOME_INTERFACE - Home-Interface)", e);
76  		}				
77  		if (lvHome == null) {
78  			throw new InvocationException("With PortableRemoteObject.narrow can not create the HomeInterface: "
79  					+ lvJndi + " " + pvProxyClass.getName());
80  		}
81  		// Bean mit der Methode create() finden und aufrufen
82  		try {
83  			BeanInfo beanInfo = Introspector.getBeanInfo(lvHome.getClass());
84  			MethodDescriptor lvMethodDescriptor[] = beanInfo.getMethodDescriptors();
85  			for (int i = 0; i < lvMethodDescriptor.length; i++) {
86  				Method lvMethod = lvMethodDescriptor[i].getMethod();
87  				if (lvMethod.getName().equals("create")) {
88  					setProxyObject(lvMethod.invoke(lvHome, null));	
89  					setProxyClass(getProxyObject().getClass());
90  					return getProxyObject();				
91  				}
92  			}
93  		} catch (Exception e) {
94  			throw new InvocationException("Exception by Introspector, by search of create()-Method from: " 
95  											+ lvHome.getClass().getName(), e);
96  		}				
97  								
98  		return getProxyObject();
99  		
100 	}
101 	
102 
103 }