1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.objectledge.pico;
30
31 import java.lang.reflect.Field;
32
33 import org.nanocontainer.reflection.InvalidConversionException;
34 import org.nanocontainer.reflection.StringToObjectConverter;
35 import org.picocontainer.ComponentAdapter;
36 import org.picocontainer.Parameter;
37 import org.picocontainer.PicoContainer;
38 import org.picocontainer.PicoInitializationException;
39 import org.picocontainer.PicoIntrospectionException;
40 import org.picocontainer.PicoVisitor;
41
42 /***
43 * A parameter that is intialized with a string, and is converted to the desired type on demand.
44 *
45 * <p>Created on Jan 8, 2004</p>
46 * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
47 * @version $Id: StringParameter.java,v 1.4 2005/02/04 02:28:13 rafal Exp $
48 */
49 public class StringParameter
50 implements Parameter
51 {
52 /*** the string representation of the value */
53 private String stringValue;
54
55 /*** the parmeter class */
56 private Class parameterType;
57
58 /***
59 * Creates a new StringParamter instance.
60 *
61 * @param stringValue the textual value of the component.
62 * @param parameterType the class of the parameter, or <code>null</code> to
63 * determine dynamically.
64 */
65 public StringParameter(String stringValue, Class parameterType)
66 {
67 this.stringValue = stringValue;
68 this.parameterType = parameterType;
69 }
70
71 /***
72 * Creates a new StringParamter instance.
73 *
74 * @param stringValue the textual value of the component.
75 */
76 public StringParameter(String stringValue)
77 {
78 this(stringValue, null);
79 }
80
81 /***
82 * {@inheritDoc}
83 */
84 public Object resolveInstance(PicoContainer container, ComponentAdapter adapter,
85 Class expectedType)
86 throws PicoInitializationException
87 {
88 StringToObjectConverter converter = (StringToObjectConverter)container.
89 getComponentInstance(StringToObjectConverter.class);
90 if(parameterType == null)
91 {
92 return converter.convertTo(expectedType, stringValue);
93 }
94 else
95 {
96 return converter.convertTo(parameterType, stringValue);
97 }
98 }
99
100 /***
101 * {@inheritDoc}
102 */
103 public boolean isResolvable(PicoContainer container, ComponentAdapter adapter,
104 Class expectedType)
105 {
106 try
107 {
108 verify(container, adapter, expectedType);
109 return true;
110 }
111 catch(PicoIntrospectionException e)
112 {
113 return false;
114 }
115 }
116
117 /***
118 * {@inheritDoc}
119 */
120 public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType)
121 throws PicoIntrospectionException
122 {
123 if(parameterType != null && !expectedType.isAssignableFrom(parameterType)
124 && !checkPrimitive(expectedType))
125 {
126 throw new PicoIntrospectionException(expectedType.getClass().getName()
127 + " is not assignable from "
128 + parameterType.getName());
129 }
130 try
131 {
132 resolveInstance(container, adapter, expectedType);
133 }
134 catch(InvalidConversionException e)
135 {
136 throw new PicoIntrospectionException("cannot resolve parameter from string", e);
137 }
138 }
139
140 private boolean checkPrimitive(Class expectedType)
141 {
142 try
143 {
144 if(expectedType.isPrimitive())
145 {
146 final Field field = parameterType.getField("TYPE");
147 final Class type = (Class)field.get(null);
148 return expectedType.isAssignableFrom(type);
149 }
150 }
151 catch(NoSuchFieldException e)
152 {
153
154 }
155 catch(IllegalAccessException e)
156 {
157
158 }
159 return false;
160 }
161
162
163 /***
164 * {@inheritDoc}
165 */
166 public void accept(PicoVisitor visitor)
167 {
168 visitor.visitParameter(this);
169 }
170 }