View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  package org.apache.james.container.spring.beanfactory;
20  
21  import org.springframework.web.context.support.AbstractRefreshableWebApplicationContext;
22  import org.springframework.beans.factory.support.DefaultListableBeanFactory;
23  import org.springframework.beans.BeansException;
24  import org.springframework.core.io.Resource;
25  
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /**
31   * application context which can be initialized in a web container environment
32   */
33  public class AvalonWebApplicationContext extends AbstractRefreshableWebApplicationContext {
34  
35      private List jamesAssemblyResources = new ArrayList();
36      private List springBeanLocations = new ArrayList();
37      
38      protected void loadBeanDefinitions(DefaultListableBeanFactory defaultListableBeanFactory) throws IOException, BeansException {
39          // for the sake of simplicity, support only one james assembly and one bean definition currently
40          if (jamesAssemblyResources.size() != 1) throw new RuntimeException("can only load one Avalon-type assembly file");
41          if (springBeanLocations.size() != 1) throw new RuntimeException("can only load one Spring bean definition file");
42          Resource springBeanResource = getResourceByPath((String) springBeanLocations.get(0));
43          Resource jamesAssemblyResource = (Resource) jamesAssemblyResources.get(0);
44          AvalonApplicationContext.loadAvalonBasedBeanDefinitions(defaultListableBeanFactory, springBeanResource, jamesAssemblyResource);
45      }
46  
47      public void setConfigLocations(String[] locationStrings) {
48          if (locationStrings == null) locationStrings = new String[] {"/WEB-INF/james-assembly.xml", "/WEB-INF/spring-beans.xml"};
49          for (int i = 0; i < locationStrings.length; i++) {
50              String locationString = locationStrings[i];
51              if (locationString.indexOf("james-assembly") != -1) jamesAssemblyResources.add(getResourceByPath(locationString));
52              else springBeanLocations.add(locationString);
53          }
54      }
55  }