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.context.ApplicationContext;
22  import org.springframework.core.io.ClassPathResource;
23  import org.springframework.core.io.FileSystemResource;
24  import org.springframework.core.io.Resource;
25  
26  import java.io.File;
27  
28  /**
29   * Override the ResourceLoader capabilities from the AvalonApplicationContext
30   * supporting JAMES' conf/var specific behaviours and the "classpath:" prefix.
31   */
32  public class JamesApplicationContext extends AvalonApplicationContext {
33  
34      private static final String FILE_PROTOCOL = "file://";
35      private static final String FILE_PROTOCOL_AND_CONF = "file://conf/";
36      private static final String FILE_PROTOCOL_AND_VAR = "file://var/";
37      
38      public static final String JAMES_ASSEMBLY_CONF = "james-assembly.xml";
39  
40  
41      /**
42       * configuration-by-convention constructor, tries to find default config files on classpath
43       */
44      public static JamesApplicationContext newJamesApplicationContext() {
45          return newJamesApplicationContext(SPRING_BEANS_CONF, JAMES_ASSEMBLY_CONF);
46      }
47      
48      public static JamesApplicationContext newJamesApplicationContext(String containerConf, String applicationConf) {
49          return newJamesApplicationContext(new ClassPathResource(containerConf), new ClassPathResource(applicationConf));
50      }
51      
52      
53      public static JamesApplicationContext newJamesApplicationContext(Resource containerConfigurationResource,
54                                      Resource applicationConfigurationResource) {
55          JamesApplicationContext result = new JamesApplicationContext(null, containerConfigurationResource, applicationConfigurationResource);
56          result.refresh();
57          return result;
58      }
59  
60      
61      public JamesApplicationContext(ApplicationContext parent,
62              Resource containerConfigurationResource,
63              Resource applicationConfigurationResource) {
64          super(parent, containerConfigurationResource, applicationConfigurationResource);
65      }
66  
67      public ClassLoader getClassLoader() {
68          return Thread.currentThread().getContextClassLoader();
69      }
70  
71      public Resource getResource(String fileURL) {
72          Resource r = null;
73          if (fileURL.startsWith("classpath:")) {
74              String resourceName = fileURL.substring("classpath:".length());
75              r = new ClassPathResource(resourceName);
76          } else if (fileURL.startsWith(FILE_PROTOCOL)) {
77              File file = null;
78              if (fileURL.startsWith(FILE_PROTOCOL_AND_CONF)) {
79                  file = new File("../conf/" + fileURL.substring(FILE_PROTOCOL_AND_CONF.length()));
80              } else if (fileURL.startsWith(FILE_PROTOCOL_AND_VAR)) {
81                  file = new File("../var/" + fileURL.substring(FILE_PROTOCOL_AND_VAR.length()));
82              } else {
83                  file = new File("./" + fileURL.substring(FILE_PROTOCOL.length()));
84              }
85              r = new FileSystemResource(file);
86          } else {
87              r = super.getResource(fileURL);
88          }
89          return r;
90      }
91  
92  
93  }