View Javadoc

1   package net.sf.crispy.proxy;
2   
3   import java.util.Properties;
4   
5   import net.sf.crispy.InvocationStrategy;
6   
7   /**
8    * Decorate all StaticProxy-implementations.
9    * @author Linke
10   *
11   */
12  public class StaticProxyDecorator extends StaticProxy {
13  	
14  	private StaticProxy staticProxy = null;
15      
16  	public StaticProxyDecorator(StaticProxy pvStaticProxy) {
17  		setStaticProxy(pvStaticProxy);	
18  	}
19  
20  
21  	/**
22  	 * Delegate Method to StaticProxy.
23  	 * @see net.sf.crispy.proxy.StaticProxy#newInstance(Class)
24  	 */
25  	public Object newInstance(Class pvClass) {
26  		Properties lvProperties = new Properties(getProperties());
27  		lvProperties.put(PROPERTY_CURRENT_INTERFACE_CLASS, pvClass.getName());
28  		InvocationStrategy lvDefaultInvocationStrategy = getDefaultInvocationStrategy(lvProperties);
29  		getStaticProxy().setInvocationStrategy(lvDefaultInvocationStrategy);
30  		try {
31  			Object lvInvocationStrategy = Proxy.createInvocationStrategy(lvDefaultInvocationStrategy , 
32  					getProperties(), getStaticProxy().getDefaultUrlAndPort(),
33  					pvClass, null);
34  			getStaticProxy().setInvocationStrategy(lvInvocationStrategy);
35  		} catch (Exception e) {
36  			if (log.isWarnEnabled()) {
37  				log.warn("Error by create InvocationStrategy for Class: " + pvClass, e);
38  			}
39  		}
40  		
41  		Object lvReturn = getStaticProxy().newInstance(pvClass);
42  		return lvReturn;
43  	}
44  
45  	public final void setStaticProxy(StaticProxy pvStaticProxy) { staticProxy = pvStaticProxy; }
46  	public StaticProxy getStaticProxy() { return staticProxy; }
47  
48  	/**
49  	 * Delegate Method to StaticProxy.
50  	 * @see net.sf.crispy.proxy.StaticProxy#getProperties()
51  	 */
52  	public Properties getProperties() { return staticProxy.getProperties(); }
53  	/**
54  	 * Delegate Method to StaticProxy.
55  	 * @see net.sf.crispy.proxy.StaticProxy#setProperties(Properties)
56  	 */	
57  	public void setProperties(Properties pvProperties) { staticProxy.setProperties(pvProperties); }
58  
59  	/**
60  	 * Delegate Method to StaticProxy.
61  	 * @see net.sf.crispy.proxy.StaticProxy#getProxyClass()
62  	 */
63  	public Class getProxyClass() { return staticProxy.getProxyClass(); }
64  	
65  	/**
66  	 * Delegate Method to StaticProxy.
67  	 * @see net.sf.crispy.proxy.StaticProxy#getProxyObject()
68  	 */
69  	public Object getProxyObject() { return staticProxy.getProxyObject(); }
70  	
71      /**
72       * Get default url and port. If no url and port is in properties.
73       *  
74       * @return Default url and port.
75       */
76      public String getDefaultUrlAndPort() {
77      	return getStaticProxy().getDefaultUrlAndPort();
78      }
79      
80      public InvocationStrategy getDefaultInvocationStrategy(Properties pvProperties) {
81      	return getStaticProxy().getDefaultInvocationStrategy(pvProperties);
82      }
83  
84  }