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.adaptor;
20  
21  import org.apache.avalon.framework.configuration.Configuration;
22  import org.apache.avalon.phoenix.tools.configuration.ConfigurationBuilder;
23  import org.apache.james.container.spring.configuration.ConfigurationInterceptor;
24  import org.springframework.context.ResourceLoaderAware;
25  import org.springframework.core.io.Resource;
26  import org.springframework.core.io.ResourceLoader;
27  import org.xml.sax.InputSource;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * loads the well-known classic avalon/phoenix configuration file
36   *
37    * TODO make this thing be based on Resource class and inject resource.getInputStream() into InputSource 
38   */
39  public class AvalonConfigurationFileProvider implements ConfigurationProvider, ResourceLoaderAware {
40  
41      private List configurationInterceptors;
42      private String configuration;
43      private ResourceLoader resourceLoader;
44      
45      public Configuration getConfiguration() {
46          InputStream inputStream = null;
47          String systemId = null;
48          
49          Resource resource = resourceLoader.getResource(configuration);
50          if (!resource.exists()) {
51              throw new RuntimeException("could not locate configuration file " + configuration);
52          }
53          try {
54              inputStream = resource.getInputStream();
55              systemId = resource.getURL().toString();
56          } catch (IOException e1) {
57              throw new RuntimeException("could not open configuration file " + configuration, e1);
58          }
59          InputSource inputSource = new InputSource(inputStream);
60          Configuration configuration;
61          try
62          {
63              inputSource.setSystemId(systemId);
64              configuration = ConfigurationBuilder.build(inputSource, null, null);
65          }
66          catch( final Exception e )
67          {
68  //            getLogger().error( message, e );
69              throw new RuntimeException("failed loading configuration ", e);
70          }
71  
72          // apply all interceptors
73          if (configuration != null && configurationInterceptors != null) {
74              Iterator interceptorsIterator = configurationInterceptors.iterator();
75              while (interceptorsIterator.hasNext()) {
76                  ConfigurationInterceptor configurationInterceptor = (ConfigurationInterceptor) interceptorsIterator.next();
77                  configuration = configurationInterceptor.intercept(configuration);
78              }
79          }
80          
81          return configuration;
82      }
83      public void setConfigurationResource(String configuration) {
84          this.configuration = configuration;
85      }
86  
87      public void setConfigurationInterceptors(List configurationInterceptors) {
88          this.configurationInterceptors = configurationInterceptors;
89      }
90  
91      public synchronized void setResourceLoader(ResourceLoader resourceLoader) {
92          this.resourceLoader = resourceLoader;
93      }
94  }