View Javadoc

1   /*
2    *  jDTAUS Core ⁑ Reference Implementation
3    *  Copyright (C) 2005 Christian Schulte <cs@schulte.it>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   *  $JDTAUS: FactoryBean.java.vm 8525 2012-05-07 08:32:23Z schulte2005 $
20   */
21  package org.jdtaus.core.container.ri.spring;
22  
23  import org.jdtaus.core.container.ContainerFactory;
24  import org.jdtaus.core.container.ModelFactory;
25  import org.jdtaus.core.container.Specification;
26  import org.springframework.beans.factory.FactoryBean;
27  
28  /**
29   * Spring {@code FactoryBean} implementation delegating to the {@code Container}
30   * singleton.
31   * <p>Property {@code specificationIdentifier} holds the identifier of the
32   * specification to return an object for. Property {@code implementationName}
33   * holds the optional name of the implementation of the object to return.</p>
34   *
35   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
36   * @version $JDTAUS: FactoryBean.java.vm 8525 2012-05-07 08:32:23Z schulte2005 $
37   *
38   * @see org.jdtaus.core.container.ContainerFactory#newContainer()
39   */
40  public class ContainerFactoryBean implements FactoryBean
41  {
42      /** Identifier of the specification of this factory bean. */
43      private String specificationIdentifier;
44  
45      /** Implementation name of this factory bean. */
46      private String implementationName;
47  
48      /**
49       * Gets the value of property {@code specificationIdentifier}.
50       *
51       * @return the identifier of the specification of this factory bean.
52       */
53      public String getSpecificationIdentifier()
54      {
55          return this.specificationIdentifier;
56      }
57  
58      /**
59       * Gets the value of property {@code implementationName}.
60       *
61       * @return the implementation name of this factory bean.
62       */
63      public String getImplementationName()
64      {
65          return this.implementationName;
66      }
67  
68      public Object getObject() throws Exception
69      {
70          final Object o;
71          final Class specificationClass = Class.forName(
72              this.getSpecificationIdentifier(), true,
73              this.getClassLoader( this.getClass() ) );
74  
75          if ( this.getImplementationName() != null )
76          {
77              o = ContainerFactory.getContainer().getObject(
78                  specificationClass, this.getImplementationName() );
79  
80          }
81          else
82          {
83              o = ContainerFactory.getContainer().getObject( specificationClass );
84          }
85  
86          return o;
87      }
88  
89      public Class getObjectType()
90      {
91          return null;
92      }
93  
94      public boolean isSingleton()
95      {
96          return ModelFactory.getModel().getModules().
97              getSpecification( this.getSpecificationIdentifier() ).
98              getScope() == Specification.SCOPE_SINGLETON;
99  
100     }
101 
102     protected ClassLoader getClassLoader( final Class clazz )
103     {
104         ClassLoader classLoader = clazz.getClassLoader();
105         if( classLoader == null )
106         {
107             classLoader = ClassLoader.getSystemClassLoader();
108         }
109 
110         return classLoader;
111     }
112 }