001/* 002 * jDTAUS Core ⁑ Reference Implementation 003 * Copyright (C) 2005 Christian Schulte <cs@schulte.it> 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library; if not, write to the Free Software 017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 018 * 019 * $JDTAUS: FactoryBean.java.vm 8525 2012-05-07 08:32:23Z schulte2005 $ 020 */ 021package org.jdtaus.core.container.ri.spring; 022 023import org.jdtaus.core.container.ContainerFactory; 024import org.jdtaus.core.container.ModelFactory; 025import org.jdtaus.core.container.Specification; 026import org.springframework.beans.factory.FactoryBean; 027 028/** 029 * Spring {@code FactoryBean} implementation delegating to the {@code Container} 030 * singleton. 031 * <p>Property {@code specificationIdentifier} holds the identifier of the 032 * specification to return an object for. Property {@code implementationName} 033 * holds the optional name of the implementation of the object to return.</p> 034 * 035 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 036 * @version $JDTAUS: FactoryBean.java.vm 8525 2012-05-07 08:32:23Z schulte2005 $ 037 * 038 * @see org.jdtaus.core.container.ContainerFactory#newContainer() 039 */ 040public class ContainerFactoryBean implements FactoryBean 041{ 042 /** Identifier of the specification of this factory bean. */ 043 private String specificationIdentifier; 044 045 /** Implementation name of this factory bean. */ 046 private String implementationName; 047 048 /** 049 * Gets the value of property {@code specificationIdentifier}. 050 * 051 * @return the identifier of the specification of this factory bean. 052 */ 053 public String getSpecificationIdentifier() 054 { 055 return this.specificationIdentifier; 056 } 057 058 /** 059 * Gets the value of property {@code implementationName}. 060 * 061 * @return the implementation name of this factory bean. 062 */ 063 public String getImplementationName() 064 { 065 return this.implementationName; 066 } 067 068 public Object getObject() throws Exception 069 { 070 final Object o; 071 final Class specificationClass = Class.forName( 072 this.getSpecificationIdentifier(), true, 073 this.getClassLoader( this.getClass() ) ); 074 075 if ( this.getImplementationName() != null ) 076 { 077 o = ContainerFactory.getContainer().getObject( 078 specificationClass, this.getImplementationName() ); 079 080 } 081 else 082 { 083 o = ContainerFactory.getContainer().getObject( specificationClass ); 084 } 085 086 return o; 087 } 088 089 public Class getObjectType() 090 { 091 return null; 092 } 093 094 public boolean isSingleton() 095 { 096 return ModelFactory.getModel().getModules(). 097 getSpecification( this.getSpecificationIdentifier() ). 098 getScope() == Specification.SCOPE_SINGLETON; 099 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}