View Javadoc

1   package org.apache.fulcrum.spring;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.springframework.beans.factory.BeanFactory;
23  import org.apache.avalon.framework.service.ServiceManager;
24  import org.apache.avalon.framework.service.ServiceException;
25  
26  /**
27   * Wraps a Spring bean factory to implement an Avalon service
28   * lookup - this allows to delegate a service lookup to Spring.
29   *
30   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
31   */
32  public class BeanFactoryServiceManager implements ServiceManager
33  {
34      /** Spring's bean factory */
35      private BeanFactory beanFactory;
36  
37      /**
38       * Constructor
39       *
40       * @param beanFactory Spring's bean factory
41       */
42      public BeanFactoryServiceManager(BeanFactory beanFactory)
43      {
44          this.beanFactory = beanFactory;
45      }
46  
47      /** @see org.apache.avalon.framework.service.ServiceManager#lookup(String) */
48      public Object lookup(String key) throws ServiceException
49      {
50          try
51          {
52              return beanFactory.getBean(key);
53          }
54          catch(Exception e)
55          {
56              throw new ServiceException(key, "Unable to lookup service using Spring's BeanFactory", e);
57          }
58      }
59  
60      /** @see org.apache.avalon.framework.service.ServiceManager#hasService(String) */
61      public boolean hasService(String key)
62      {
63          return beanFactory.containsBean(key);
64      }
65  
66      /** @see org.apache.avalon.framework.service.ServiceManager#release(Object) */
67      public void release(Object o)
68      {
69          // nothing to do for Spring beans
70      }
71  }