View Javadoc

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.configuration;
29  
30  import org.jcontainer.dna.Configuration;
31  import org.objectledge.pico.customization.CustomizedComponentProvider;
32  import org.objectledge.pico.customization.UnsupportedKeyTypeException;
33  import org.picocontainer.PicoContainer;
34  import org.picocontainer.PicoInitializationException;
35  import org.picocontainer.PicoIntrospectionException;
36  import org.picocontainer.PicoVerificationException;
37  
38  /***
39   * Provides Configuration objects to the components being initialized using ConfigurationFactory.
40   *
41   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
42   * @version $Id: CustomizedConfigurationProvider.java,v 1.4 2005/02/04 02:28:03 rafal Exp $
43   */
44  public class CustomizedConfigurationProvider
45  	implements CustomizedComponentProvider
46  {
47      private ConfigurationFactory configurationFactory;
48      
49      /***
50       * Creates new CustomizedConfigurationProvider instance.
51       * 
52       * @param configurationFactory the configuration factory.
53       */
54      public CustomizedConfigurationProvider(ConfigurationFactory configurationFactory)
55      {
56          this.configurationFactory = configurationFactory;
57      }
58  
59      /***
60       * {@inheritDoc}
61       */
62      public Object getCustomizedComponentInstance(PicoContainer container, Object componentKey, 
63          Class componentImplementation)
64          throws PicoInitializationException, PicoIntrospectionException
65      {
66          String componentName = getComponentName(componentKey);
67          return configurationFactory.getConfig(componentName, componentImplementation);
68      }
69      
70      /***
71       * {@inheritDoc}
72       */
73      public Class getCustomizedComponentImplementation()
74      {
75          return Configuration.class;
76      }
77  
78      /***
79       * {@inheritDoc}
80       */
81      public void verify(PicoContainer container) throws PicoVerificationException
82      {
83          // no dependencies
84      }
85      
86      //////////////////////////////////////////////////////////////////////////////////////////////
87      
88      /***
89       * Returns human readable name of the component.
90       * 
91       * @param componentKey the component key.
92       * @return human readable name of the component.
93       * @throws UnsupportedKeyTypeException if the component key is of unsupported type.
94       */
95      protected String getComponentName(Object componentKey)
96          throws UnsupportedKeyTypeException
97      {
98          if(componentKey instanceof Class)
99          {
100             return ((Class)componentKey).getName();
101         }
102         else if(componentKey instanceof String)
103         {
104             return componentKey.toString();
105         }
106         else
107         {
108             throw new UnsupportedKeyTypeException("unsupported component key type "+
109                 componentKey.getClass().getName());
110         }
111     }        
112 }