1 //---------------------------------------------------------------------- 2 // 3 // PerfectJPattern: "Design patterns are good but components are better!" 4 // Delegator.java Copyright (c) 2009 Giovanni Azua Garcia 5 // bravegag@hotmail.com 6 // 7 // This program is free software; you can redistribute it and/or 8 // modify it under the terms of the GNU General Public License 9 // as published by the Free Software Foundation; either version 3 10 // of the License, or (at your option) any later version. 11 // 12 // This program is distributed in the hope that it will be useful, 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 // GNU General Public License for more details. 16 // 17 // You should have received a copy of the GNU General Public License 18 // along with this program; if not, see <http://www.gnu.org/licenses/>. 19 // 20 //---------------------------------------------------------------------- 21 package org.perfectjpattern.core.extras.delegate; 22 23 import java.lang.reflect.*; 24 25 import org.apache.commons.lang.*; 26 import org.perfectjpattern.core.api.extras.delegate.*; 27 28 29 /** 30 * Implementation of <code>IDelegator</code> interface for creating 31 * Delegate instances based user-defined interface types. 32 * <br/><br/> 33 * <b>Notes</b>: Base source code implemented by Steve Lewis and Wilhelm 34 * Fitzpatrick and adapted to fit PerfectJPattern componentization 35 * criteria and code conventions. 36 * 37 * @param <I> Interface type built by this <code>Delegator</code> instance. 38 * 39 * @author <a href="mailto:smlewis@lordjoe.com">Steve Lewis</a> 40 * @author <a href="mailto:wilhelmf@agileinformatics.com">Wilhelm 41 * Fitzpatrick</a> 42 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a> 43 * @version $Revision: 1.0 $ $Date: Jun 25, 2007 7:20:28 AM $ 44 */ 45 @SuppressWarnings("unchecked") 46 public 47 class Delegator<I> 48 extends AbstractDelegator<I> 49 { 50 //------------------------------------------------------------------------ 51 // public 52 //------------------------------------------------------------------------ 53 /** 54 * Creates a <code>Delegator<E></code> from the type interface. 55 * 56 * @param anInterface The interface to build references from. 57 * @throws IllegalArgumentException 'anInterface' must not be null. 58 * @throws IllegalArgumentException 'anInterface' must be an interface type. 59 */ 60 public 61 Delegator(Class<I> anInterface) 62 { 63 super(anInterface); 64 65 Validate.notNull(anInterface, "'anInterface' must not be null"); 66 Validate.isTrue(anInterface.isInterface(), 67 "'anInterface' must be an interface type."); 68 69 theInterface = anInterface; 70 } 71 72 //------------------------------------------------------------------------ 73 /** 74 * {@inheritDoc} 75 */ 76 @SuppressWarnings("unchecked") 77 public I 78 build(Class aTarget, String aMethodName) 79 throws IllegalArgumentException, UnsupportedOperationException 80 { 81 Validate.notNull(aTarget, "'aTarget' must not be null"); 82 Validate.notNull(aMethodName, "'aMethodName' must not be null"); 83 84 DelegateProxy myProxy = new DelegateProxy(null, aTarget, aMethodName, 85 this); 86 87 Class myInterface = theInterface; 88 89 // build dynamic proxy 90 Class[] myInterfaces = {myInterface, IDelegate.class }; 91 I myDelegate = null; 92 try 93 { 94 // try with the target 95 myDelegate = (I) Proxy.newProxyInstance(aTarget.getClassLoader(), 96 myInterfaces, myProxy); 97 } 98 catch (IllegalArgumentException anException) 99 { 100 // retry with the Interface class 101 myDelegate = (I) Proxy.newProxyInstance(theInterface. 102 getClassLoader(), myInterfaces, myProxy); 103 } 104 105 return myDelegate; 106 } 107 108 //------------------------------------------------------------------------ 109 /** 110 * {@inheritDoc} 111 */ 112 @SuppressWarnings("unchecked") 113 public I 114 build(Object aTarget, String aMethodName) 115 throws IllegalArgumentException, UnsupportedOperationException 116 { 117 Validate.notNull(aTarget, "'aTarget' must not be null"); 118 Validate.notNull(aMethodName, "'aMethodName' must not be null"); 119 120 DelegateProxy myProxy = new DelegateProxy(aTarget, 121 aTarget.getClass(), aMethodName, this); 122 123 Class myInterface = theInterface; 124 125 // build dynamic proxy 126 Class[] myInterfaces = {myInterface, IDelegate.class }; 127 128 IDelegate myDelegate = null; 129 try 130 { 131 // try with the target 132 myDelegate = (IDelegate) java.lang.reflect.Proxy.newProxyInstance( 133 aTarget.getClass().getClassLoader(), myInterfaces, myProxy); 134 } 135 catch (IllegalArgumentException anException) 136 { 137 // retry with the Interface class 138 myDelegate = (IDelegate) java.lang.reflect.Proxy.newProxyInstance( 139 theInterface.getClassLoader(), myInterfaces, myProxy); 140 } 141 142 return (I) myDelegate; 143 } 144 145 //------------------------------------------------------------------------ 146 // members 147 //------------------------------------------------------------------------ 148 private final Class theInterface; 149 }