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.apache.avalon.framework.activity.Disposable;
23  import org.apache.avalon.framework.activity.Initializable;
24  import org.apache.avalon.framework.logger.AbstractLogEnabled;
25  import org.apache.avalon.framework.service.ServiceException;
26  import org.apache.avalon.framework.service.ServiceManager;
27  import org.apache.avalon.framework.configuration.Configurable;
28  import org.apache.avalon.framework.configuration.ConfigurationException;
29  import org.apache.avalon.framework.configuration.Configuration;
30  import org.springframework.context.support.AbstractApplicationContext;
31  import org.springframework.context.support.FileSystemXmlApplicationContext;
32  
33  /**
34   * Starts an instance of the Spring Service Framework as Avalon service.
35   *
36   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
37   */
38  
39  public class SpringFrameworkServiceImpl
40      extends AbstractLogEnabled
41      implements SpringFrameworkService, Configurable, Initializable, Disposable, ServiceManager
42  {
43      /** the list of configuration files passed to the Spring container */
44      private String[] configLocations;
45  
46      /** the Spring service container */
47      private AbstractApplicationContext ctx;
48  
49      /** ServiceManager facade to lookup Spring services */
50      private BeanFactoryServiceManager beanFactoryServiceManager;
51  
52      /////////////////////////////////////////////////////////////////////////
53      // Avalon Service Lifecycle Implementation
54      /////////////////////////////////////////////////////////////////////////
55  
56      /**
57       * Constructor
58       */
59      public SpringFrameworkServiceImpl()
60      {
61          this.configLocations = new String[0];
62      }
63  
64      /**
65       * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
66       */
67      public void configure(Configuration configuration) throws ConfigurationException
68      {
69  
70          // parse the 'configLocations' to passed to Spring
71  
72          Configuration[] configLocationConfigurationList = configuration.getChild("configurations").getChildren("configuration");
73          this.configLocations = new String[configLocationConfigurationList.length];
74  
75          for(int i=0; i<configLocations.length; i++)
76          {
77              this.configLocations[i] = configLocationConfigurationList[i].getValue();
78          }
79  
80          if(this.configLocations.length == 0)
81          {
82              String msg = "No configuration files for the Spring container are defined";
83              throw new ConfigurationException(msg);
84          }
85      }
86  
87      /**
88       * @see org.apache.avalon.framework.activity.Initializable#initialize()
89       */
90      public void initialize() throws Exception
91      {
92          this.ctx = new FileSystemXmlApplicationContext(this.configLocations);
93          this.beanFactoryServiceManager = new BeanFactoryServiceManager(ctx);
94      }
95  
96      /**
97       * @see org.apache.avalon.framework.activity.Disposable#dispose()
98       */
99      public void dispose()
100     {
101         if(this.ctx != null)
102         {
103             try
104             {
105                 this.ctx.close();
106             }
107             catch(Exception e)
108             {
109                 String msg = "Failed to dispose the Spring service";
110                 this.getLogger().error(msg, e);
111             }
112             finally
113             {
114                 this.ctx = null;
115             }
116         }
117 
118         this.beanFactoryServiceManager = null;        
119     }
120 
121     /////////////////////////////////////////////////////////////////////////
122     // Service interface implementation
123     /////////////////////////////////////////////////////////////////////////
124 
125     /** @see SpringFrameworkService#getAbstractApplicationContext() */ 
126     public AbstractApplicationContext getAbstractApplicationContext()
127     {
128         return this.ctx;
129     }
130 
131     /** @see org.apache.avalon.framework.service.ServiceManager#lookup(String) */
132     public Object lookup(String key) throws ServiceException
133     {
134         if(this.beanFactoryServiceManager == null)
135         {
136             throw new RuntimeException("The SpringFrameworkService is not yet initialized");
137         }
138 
139         return this.beanFactoryServiceManager.lookup(key);
140     }
141 
142     /** @see org.apache.avalon.framework.service.ServiceManager#hasService(String)  */
143     public boolean hasService(String key)
144     {
145         if(this.beanFactoryServiceManager == null)
146         {
147             throw new RuntimeException("The SpringFrameworkService is not yet initialized");
148         }
149 
150         return this.beanFactoryServiceManager.hasService(key);
151     }
152 
153     /** @see org.apache.avalon.framework.service.ServiceManager#release(Object)  */
154     public void release(Object o)
155     {
156         if(this.beanFactoryServiceManager == null)
157         {
158             throw new RuntimeException("The SpringFrameworkService is not yet initialized");
159         }
160 
161         this.beanFactoryServiceManager.release(o);
162     }    
163 }