1 //
2 // Copyright (c) 2003,2004 , Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
14 // nor the names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
27 //
28 package org.objectledge.pico.customization;
29
30 import java.lang.reflect.Field;
31
32 import org.picocontainer.ComponentAdapter;
33 import org.picocontainer.Parameter;
34 import org.picocontainer.PicoContainer;
35 import org.picocontainer.PicoInstantiationException;
36 import org.picocontainer.defaults.ComponentParameter;
37
38 /***
39 *
40 *
41 * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
42 * @version $Id: CustomizingComponentParameter.java,v 1.1 2005/02/04 02:28:25 rafal Exp $
43 */
44 public class CustomizingComponentParameter
45 extends ComponentParameter
46 {
47 /*** A CustomizedComponentParameter with null componentKey */
48 public static final Parameter DEFAULT = new CustomizingComponentParameter();
49
50 // TODO copied from pico
51 private Object componentKey;
52
53 /***
54 * Creates new CustomizedComponentParameter instance.
55 */
56 public CustomizingComponentParameter()
57 {
58 super();
59 }
60
61 /***
62 * Creates new CustomizedComponentParameter instance.
63 *
64 * @param componentKey the component key.
65 */
66 public CustomizingComponentParameter(Object componentKey)
67 {
68 super(componentKey);
69 this.componentKey = componentKey;
70 }
71
72 //////////////////////////////////////////////////////////////////////////////////////////////
73
74 /***
75 * {@inheritDoc}
76 */
77 public Object resolveInstance(PicoContainer container, ComponentAdapter adapter,
78 Class expectedType)
79 throws PicoInstantiationException
80 {
81 ComponentAdapter resolvedAdapter = resolveAdapter(container, adapter, expectedType);
82 if(resolvedAdapter instanceof CustomizedComponentAdapter)
83 {
84 return ((CustomizedComponentAdapter)resolvedAdapter).getComponentInstance(container,
85 adapter.getComponentKey(), adapter.getComponentImplementation());
86 }
87 return super.resolveInstance(container, adapter, expectedType);
88 }
89
90 // private ///////////////////////////////////////////////////////////////////////////////////
91
92 // TODO copied from pico
93 private ComponentAdapter resolveAdapter(PicoContainer container, ComponentAdapter adapter,
94 Class expectedType)
95 {
96
97 final ComponentAdapter result = getTargetAdapter(container, expectedType);
98 if(result == null)
99 {
100 return null;
101 }
102
103 // can't depend on ourselves
104 if(adapter != null && adapter.getComponentKey().equals(result.getComponentKey()))
105 {
106 return null;
107 }
108
109 if(!expectedType.isAssignableFrom(result.getComponentImplementation()))
110 {
111 // check for primitive value
112 if(expectedType.isPrimitive())
113 {
114 try
115 {
116 final Field field = result.getComponentImplementation().getField("TYPE");
117 final Class type = (Class)field.get(result.getComponentInstance(null));
118 if(expectedType.isAssignableFrom(type))
119 {
120 return result;
121 }
122 }
123 catch(NoSuchFieldException e)
124 {
125 // ignored
126 }
127 catch(IllegalArgumentException e)
128 {
129 // ignored
130 }
131 catch(IllegalAccessException e)
132 {
133 // ignored
134 }
135 catch(ClassCastException e)
136 {
137 // ignored
138 }
139 }
140 return null;
141 }
142 return result;
143 }
144
145 // TODO copied from pico
146 private ComponentAdapter getTargetAdapter(PicoContainer container, Class expectedType)
147 {
148 if(componentKey != null)
149 {
150 // key tells us where to look so we follow
151 return container.getComponentAdapter(componentKey);
152 }
153 else
154 {
155 return container.getComponentAdapterOfType(expectedType);
156 }
157 }
158 }