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.jicarilla.container.Adapter;
029: import org.jicarilla.container.DefaultContainer;
030: import org.jicarilla.container.JicarillaClassNotFoundException;
031: import org.jicarilla.container.JicarillaIllegalAccessException;
032: import org.jicarilla.container.JicarillaInstantiationException;
033: import org.jicarilla.container.JicarillaInvocationTargetException;
034: import org.jicarilla.container.adapters.SingletonAdapter;
035: import org.springframework.beans.BeansException;
036: import org.springframework.beans.FatalBeanException;
037: import org.springframework.beans.factory.AutowireCapableBeanFactory;
038: import org.springframework.beans.factory.BeanFactory;
039: import org.springframework.beans.factory.BeanFactoryAware;
040: import org.springframework.beans.factory.BeanIsNotAFactoryException;
041: import org.springframework.beans.factory.BeanNameAware;
042: import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
043: import org.springframework.beans.factory.FactoryBean;
044: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
045:
046: import java.util.Map;
047:
048: /**
049: *
050: *
051: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
052: * @version $Id: JicarillaBasedSpringFactory.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
053: */
054: public class JicarillaBasedSpringFactory extends DefaultContainer
055: implements BeanFactory, AutowireCapableBeanFactory {
056: public Object getBean(final String name) throws BeansException {
057: if (name == null)
058: throw new NoSuchBeanDefinitionException(name,
059: "The name parameter was null, which is not allowed!");
060:
061: if (!name.startsWith("&")
062: && !super .getResolver().contains(name))
063: throw new NoSuchBeanDefinitionException(name,
064: "No entry exists for the bean name '" + name + "'");
065:
066: try {
067: Object instance = super .getResolver().get(name);
068:
069: if (instance == null) {
070: throw new NoSuchBeanDefinitionException(name,
071: "No entry exists for the bean name '" + name
072: + "'");
073: }
074:
075: if (instance instanceof FactoryBean) {
076: if (!name.startsWith("&")) {
077: try {
078: instance = ((FactoryBean) instance).getObject();
079: if (instance instanceof BeanNameAware) {
080: ((BeanNameAware) instance)
081: .setBeanName(name);
082: }
083:
084: if (instance instanceof BeanFactoryAware) {
085: ((BeanFactoryAware) instance)
086: .setBeanFactory(this );
087: }
088:
089: } catch (Exception e) {
090: throw new FatalBeanException(
091: "Unable to get instance for "
092: + name
093: + " from FactoryBean associated with it",
094: e);
095: }
096: }
097: } else if (name.startsWith("&")) {
098: throw new BeanIsNotAFactoryException(name, instance);
099: }
100:
101: return instance;
102: } catch (JicarillaIllegalAccessException e) {
103: throw new SpringIllegalAccessBeansException(e.getMessage(),
104: e);
105: } catch (JicarillaInvocationTargetException e) {
106: throw new SpringInvocationTargetBeansException(e
107: .getMessage(), e);
108: } catch (JicarillaInstantiationException e) {
109: throw new SpringInstantiationBeansException(e.getMessage(),
110: e);
111: } catch (JicarillaClassNotFoundException e) {
112: throw new SpringClassNotFoundBeansException(e.getMessage(),
113: e);
114: }
115: }
116:
117: public Object getBean(final String name, final Class requiredType)
118: throws BeansException {
119: if (name == null)
120: throw new NoSuchBeanDefinitionException(name,
121: "The name parameter was null, which is not allowed!");
122:
123: if (requiredType == null)
124: throw new NoSuchBeanDefinitionException(name,
125: "The requiredTupe parameter was null, which is not "
126: + "allowed!");
127:
128: if (!super .getResolver().contains(name))
129: throw new NoSuchBeanDefinitionException(name,
130: "No entry exists for the bean name '" + name + "'");
131:
132: Object result = null;
133: try {
134: result = super .getResolver().get(name);
135: if (requiredType.isInstance(result))
136: return result;
137: else
138: throw new BeanNotOfRequiredTypeException(name,
139: requiredType, result);
140: } catch (JicarillaIllegalAccessException e) {
141: throw new SpringIllegalAccessBeansException(e.getMessage(),
142: e);
143: } catch (JicarillaInvocationTargetException e) {
144: throw new SpringInvocationTargetBeansException(e
145: .getMessage(), e);
146: } catch (JicarillaInstantiationException e) {
147: throw new SpringInstantiationBeansException(e.getMessage(),
148: e);
149: } catch (JicarillaClassNotFoundException e) {
150: throw new SpringClassNotFoundBeansException(e.getMessage(),
151: e);
152: } finally {
153: try {
154: super .getResolver().releaseInstance(result);
155: } catch (Exception e) {
156: }
157: }
158: }
159:
160: public boolean containsBean(final String name)
161: throws BeansException {
162: return super .getResolver().contains(name);
163: }
164:
165: public boolean isSingleton(final String name)
166: throws NoSuchBeanDefinitionException {
167: if (!super .getResolver().contains(name))
168: throw new NoSuchBeanDefinitionException(name,
169: "No entry exists for the bean name '" + name + "'");
170:
171: final Adapter adapter = (Adapter) super .getSwitch().get(name);
172:
173: if (adapter instanceof SpringComponentAdapterForPrototypeFactories) {
174: if (name.startsWith("&"))
175: return true;
176: else
177: return false;
178: }
179:
180: if (adapter instanceof SingletonAdapter)
181: return true;
182:
183: return false;
184: }
185:
186: public String[] getAliases(final String name)
187: throws NoSuchBeanDefinitionException {
188: return new String[0];
189: }
190:
191: // ----------------------------------------------------------------------
192: // Autowire interfaces
193: // ----------------------------------------------------------------------
194:
195: public Object autowire(final Class beanClass) throws BeansException {
196: return null;
197: }
198:
199: public Object autowireConstructor(final Class beanClass)
200: throws BeansException {
201: return null;
202: }
203:
204: public void autowireBeanProperties(final Object existingBean,
205: final int autowireMode, final boolean dependencyCheck)
206: throws BeansException {
207: }
208:
209: public Object applyBeanPostProcessors(final Object existingBean,
210: final String name) throws BeansException {
211: return null;
212: }
213:
214: public int getBeanDefinitionCount() {
215: return 0;
216: }
217:
218: public String[] getBeanDefinitionNames() {
219: return new String[0];
220: }
221:
222: public String[] getBeanDefinitionNames(final Class type) {
223: return new String[0];
224: }
225:
226: public boolean containsBeanDefinition(final String name) {
227: return false;
228: }
229:
230: public Map getBeansOfType(final Class type,
231: final boolean includePrototypes,
232: final boolean includeFactoryBeans) throws BeansException {
233: return null;
234: }
235: }
|