KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > spring > kernel > LazyLocator


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.spring.kernel;
24
25 import org.springframework.aop.TargetSource;
26 import org.springframework.aop.framework.ProxyFactory;
27
28 /**
29  * Avoid hitting delegate locator until actual bean invocation.
30  * Must have delegating locator set.
31  * By default NullLocator is delegating locator.
32  *
33  * @author <a HREF="mailto:ales.justin@genera-lynx.com">Ales Justin</a>
34  * @see NullLocator
35  */

36 public class LazyLocator extends MicrocontainerLocatorSupport implements Locator
37 {
38
39    public Object JavaDoc locateBean(String JavaDoc beanName, Class JavaDoc targetType)
40    {
41       TargetSource targetSource = new LocatorTargetSource(beanName, targetType);
42       ProxyFactory proxyFactory = new ProxyFactory();
43       proxyFactory.addInterface(targetType);
44       proxyFactory.setTargetSource(targetSource);
45       return proxyFactory.getProxy();
46    }
47
48    private class LocatorTargetSource implements TargetSource
49    {
50
51       private String JavaDoc beanName;
52       private Class JavaDoc targetClass;
53
54       private Object JavaDoc cachedObject;
55
56       public LocatorTargetSource(String JavaDoc beanName, Class JavaDoc targetClass)
57       {
58          this.beanName = beanName;
59          this.targetClass = targetClass;
60       }
61
62       public Class JavaDoc getTargetClass()
63       {
64          return (this.cachedObject != null ? this.cachedObject.getClass() : this.targetClass);
65       }
66
67       public boolean isStatic()
68       {
69          return (this.cachedObject != null);
70       }
71
72       public Object JavaDoc getTarget() throws Exception JavaDoc
73       {
74          synchronized (this)
75          {
76             if (this.cachedObject == null)
77             {
78                this.cachedObject = locateBean(beanName, targetClass);
79             }
80             return this.cachedObject;
81          }
82       }
83
84       public void releaseTarget(Object JavaDoc target)
85       {
86       }
87
88    }
89
90 }
91
Popular Tags