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.lifecycle;
20  
21  import org.apache.avalon.framework.configuration.Configurable;
22  import org.apache.avalon.framework.configuration.Configuration;
23  import org.apache.avalon.framework.configuration.ConfigurationException;
24  import org.apache.james.container.spring.adaptor.ConfigurationProvider;
25  import org.springframework.beans.factory.config.BeanDefinition;
26  import org.springframework.beans.factory.config.BeanPostProcessor;
27  import org.springframework.core.Ordered;
28  
29  /**
30   * calls configure() for all avalon components
31   */
32  public class ConfigurationPropagator extends AbstractPropagator implements BeanPostProcessor, Ordered {
33  
34      private Configuration configuration;
35  
36      public void setConfigurationProvider(ConfigurationProvider configurationProvider) {
37          this.configuration = configurationProvider.getConfiguration();
38      }
39  
40      private boolean isConfigurationEmpty(Configuration componentConfiguration) {
41          return     (componentConfiguration.getChildren() == null || componentConfiguration.getChildren().length == 0)
42                  && (componentConfiguration.getAttributeNames() == null || componentConfiguration.getAttributeNames().length == 0);
43      }
44  
45      public int getOrder() {
46          return 3;
47      }
48  
49      protected Class getLifecycleInterface() {
50          return Configurable.class;
51      }
52  
53      protected void invokeLifecycleWorker(String beanName, Object bean, BeanDefinition beanDefinition) {
54          if (!(bean instanceof Configurable)) return;
55          Configurable configurable = (Configurable)bean;
56           try {
57               Configuration componentConfiguration = configuration.getChild(beanName);
58               if (isConfigurationEmpty(componentConfiguration)) {
59                   // heuristic: try lowercase
60                   componentConfiguration = configuration.getChild(beanName.toLowerCase());
61               }
62               if (isConfigurationEmpty(componentConfiguration)) {
63                   System.out.println("configuraton empty for bean " + beanName);
64               }
65               configurable.configure(componentConfiguration);
66           } catch (ConfigurationException e) {
67               throw new RuntimeException("could not configure component of type " + configurable.getClass(), e);
68           }
69  //         catch (Exception e) {
70  //             throw new RuntimeException("could not configure component of type " + serviceable.getClass(), e);
71  //         }
72       }
73  }