View Javadoc

1   // 
2   // Copyright (c) 2003, 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  
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             // ignored
154         }
155         catch(IllegalAccessException e)
156         {
157             // ignored
158         }
159         return false;
160     }
161     
162 
163     /***
164      * {@inheritDoc}
165      */
166     public void accept(PicoVisitor visitor)
167     {
168         visitor.visitParameter(this);
169     }
170 }