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.apache.avalon.framework.configuration.Configuration;
22  import org.apache.avalon.framework.configuration.ConfigurationException;
23  import org.apache.avalon.phoenix.tools.configuration.ConfigurationBuilder;
24  import org.springframework.beans.factory.BeanDefinitionStoreException;
25  import org.springframework.beans.factory.config.BeanDefinitionHolder;
26  import org.springframework.beans.factory.support.AbstractBeanDefinitionReader;
27  import org.springframework.beans.factory.support.BeanDefinitionRegistry;
28  import org.springframework.core.io.Resource;
29  import org.springframework.util.ClassUtils;
30  import org.xml.sax.InputSource;
31  
32  import java.io.IOException;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  /**
37   */
38  public class AvalonBeanDefinitionReader extends AbstractBeanDefinitionReader {
39  
40      public AvalonBeanDefinitionReader(BeanDefinitionRegistry beanDefinitionRegistry) {
41          super(beanDefinitionRegistry);
42      }
43  
44      public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
45          Configuration configuration = null;
46          try {
47              configuration = getConfiguration(resource);
48          } catch (IOException e) {
49              throw new BeanDefinitionStoreException("could not read input resource", e);
50          }
51  
52          configuration.getChildren("assembly");
53          if (configuration == null) return 0;
54  
55          Configuration[] blocks = configuration.getChildren("block");
56          if (blocks == null) return 0;
57  
58          for (int i = 0; i < blocks.length; i++) {
59              Configuration block = blocks[i];
60              BeanDefinitionHolder definitionHolder = loadBeanDefinition(block);
61              getBeanFactory().registerBeanDefinition(definitionHolder.getBeanName(), definitionHolder.getBeanDefinition());
62          }
63  
64          return blocks.length;
65      }
66  
67      private BeanDefinitionHolder loadBeanDefinition(Configuration block) throws BeanDefinitionStoreException {
68          String name = null;
69          String className = null;
70          try {
71              name = block.getAttribute("name");
72          } catch (ConfigurationException e) {
73              throw new BeanDefinitionStoreException("avalon assembly block mandatory name attribute missing", e);
74          }
75          try {
76              className = block.getAttribute("class");
77          } catch (ConfigurationException e) {
78              throw new BeanDefinitionStoreException("avalon assembly block mandatory class attribute missing", e);
79          }
80  
81          
82          AvalonBeanDefinition beanDefinition = null;
83          try {
84              beanDefinition = createBeanDefinition(className);
85          } catch (ClassNotFoundException e) {
86              throw new BeanDefinitionStoreException("bean class not found", e);
87          }
88  
89          beanDefinition.addAllServiceReferences(loadServiceReferences(block));        
90  
91          return new BeanDefinitionHolder(beanDefinition, name);
92      }
93  
94      public AvalonBeanDefinition createBeanDefinition(String className) throws ClassNotFoundException {
95          AvalonBeanDefinition bd = new AvalonBeanDefinition();
96          if (className != null) {
97              if (getBeanClassLoader() != null) {
98                  bd.setBeanClass(ClassUtils.forName(className, getBeanClassLoader()));
99              }
100             else {
101                 bd.setBeanClassName(className);
102             }
103         }
104         return bd;
105     }
106     
107     private List loadServiceReferences(Configuration block) {
108         List serviceReferences = new ArrayList();
109         Configuration[] referencedComponentDefs  = block.getChildren("provide");
110         if (referencedComponentDefs == null) return serviceReferences;
111 
112         String name = null;
113         String roleClassname = null;
114         for (int i = 0; i < referencedComponentDefs.length; i++) {
115             Configuration referencedComponentDef = referencedComponentDefs[i];
116 
117             try {
118                 name = referencedComponentDef.getAttribute("name");
119             } catch (ConfigurationException e) {
120                 throw new BeanDefinitionStoreException("avalon assembly provide mandatory name attribute missing", e);
121             }
122             try {
123                 roleClassname = referencedComponentDef.getAttribute("role");
124             } catch (ConfigurationException e) {
125                 throw new BeanDefinitionStoreException("avalon assembly provide mandatory role attribute missing", e);
126             }
127             AvalonServiceReference avalonServiceReference = new AvalonServiceReference(name, roleClassname);
128             serviceReferences.add(avalonServiceReference);
129         }
130         return serviceReferences;
131     }
132 
133     private Configuration getConfiguration(Resource resource) throws IOException {
134         InputSource inputSource = new InputSource(resource.getInputStream());
135         inputSource.setSystemId(resource.getURL().toString());
136         try
137         {
138             Configuration configuration = ConfigurationBuilder.build(inputSource, null, null);
139             return configuration;
140         }
141         catch( final Exception e )
142         {
143             throw new RuntimeException("failed loading configuration ", e);
144         }
145     }
146 }