001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.container.integration.spring;
027:
028: import org.picocontainer.ComponentAdapter;
029: import org.picocontainer.PicoContainer;
030: import org.picocontainer.PicoException;
031: import org.picocontainer.defaults.InstanceComponentAdapter;
032: import org.springframework.beans.BeansException;
033: import org.springframework.beans.factory.BeanFactory;
034: import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
035: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
036:
037: /**
038: *
039: *
040: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
041: * @version $Id: PicoBasedSpringFactory.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
042: */
043: public class PicoBasedSpringFactory implements BeanFactory {
044: protected final PicoContainer m_container;
045:
046: public PicoBasedSpringFactory(final PicoContainer container) {
047: m_container = container;
048: }
049:
050: public Object getBean(final String name) throws BeansException {
051: if (name == null)
052: throw new NoSuchBeanDefinitionException(name,
053: "The name parameter was null, which is not allowed!");
054:
055: if (!m_container.hasComponent(name))
056: throw new NoSuchBeanDefinitionException(name,
057: "No entry exists for the bean name '" + name + "'");
058:
059: try {
060: return m_container.getComponentInstance(name);
061: } catch (PicoException pe) {
062: throw new SpringPicoBeansException(pe.getMessage(), pe);
063: }
064: }
065:
066: public Object getBean(final String name, final Class requiredType)
067: throws BeansException {
068: if (name == null)
069: throw new NoSuchBeanDefinitionException(name,
070: "The name parameter was null, which is not allowed!");
071:
072: if (requiredType == null)
073: throw new NoSuchBeanDefinitionException(name,
074: "The requiredTupe parameter was null, which is not "
075: + "allowed!");
076:
077: if (!m_container.hasComponent(name))
078: throw new NoSuchBeanDefinitionException(name,
079: "No entry exists for the bean name '" + name + "'");
080:
081: try {
082: final Object result = m_container
083: .getComponentInstance(name);
084: if (requiredType.isInstance(result))
085: return result;
086: else
087: throw new BeanNotOfRequiredTypeException(name,
088: requiredType, result);
089: } catch (PicoException pe) {
090: throw new SpringPicoBeansException(pe.getMessage(), pe);
091: }
092: }
093:
094: public boolean containsBean(final String name)
095: throws BeansException {
096: return m_container.hasComponent(name);
097: }
098:
099: public boolean isSingleton(final String name)
100: throws NoSuchBeanDefinitionException {
101: final ComponentAdapter adapter = m_container
102: .findComponentAdapter(name);
103: if (adapter instanceof InstanceComponentAdapter)
104: return true;
105:
106: return false;
107: }
108:
109: public String[] getAliases(final String name)
110: throws NoSuchBeanDefinitionException {
111: return new String[0];
112: }
113: }
|