View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration2.builder.combined;
18  
19  import java.util.Arrays;
20  
21  import org.apache.commons.configuration2.Configuration;
22  import org.apache.commons.configuration2.ConfigurationUtils;
23  import org.apache.commons.configuration2.HierarchicalConfiguration;
24  import org.apache.commons.configuration2.builder.BuilderConfigurationWrapperFactory;
25  import org.apache.commons.configuration2.builder.BuilderConfigurationWrapperFactory.EventSourceSupport;
26  import org.apache.commons.configuration2.builder.ConfigurationBuilder;
27  import org.apache.commons.configuration2.event.Event;
28  import org.apache.commons.configuration2.event.EventListener;
29  import org.apache.commons.configuration2.event.EventType;
30  import org.apache.commons.configuration2.ex.ConfigurationException;
31  import org.apache.commons.configuration2.reloading.ReloadingController;
32  import org.apache.commons.configuration2.reloading.ReloadingControllerSupport;
33  
34  /**
35   * <p>
36   * A specialized {@code ConfigurationBuilderProvider} implementation for integrating
37   * {@link MultiFileConfigurationBuilder} with {@code CombinedConfigurationBuilder}.
38   * </p>
39   * <p>
40   * When using a configuration source managed by {@code MultiFileConfigurationBuilder} it is not sufficient to store the
41   * configuration once obtained from the builder in the resulting combined configuration. Rather, it has to be ensured
42   * that each access to this configuration queries the builder anew so that it can evaluate its file pattern and return a
43   * different configuration if necessary. Therefore, this class returns a specialized wrapper over a
44   * {@code MultiFileConfigurationBuilder} which returns a configuration wrapping the builder; so accessing the
45   * configuration's properties actually calls back to the builder. This constellation is compatible with the way
46   * {@code DynamicCombinedConfiguration} manages its data.
47   * </p>
48   *
49   * @since 2.0
50   */
51  public class MultiFileConfigurationBuilderProvider extends BaseConfigurationBuilderProvider {
52      /** Constant for the name of the builder class. */
53      private static final String BUILDER_CLASS = "org.apache.commons.configuration2.builder.combined.MultiFileConfigurationBuilder";
54  
55      /** Constant for the name of the reloading builder class. */
56      private static final String RELOADING_BUILDER_CLASS = "org.apache.commons.configuration2.builder.combined.ReloadingMultiFileConfigurationBuilder";
57  
58      /** Constant for the name of the parameters class. */
59      private static final String PARAM_CLASS = "org.apache.commons.configuration2.builder.combined.MultiFileBuilderParametersImpl";
60  
61      /**
62       * Creates a new instance of {@code MultiFileConfigurationBuilderProvider} and sets the name of the configuration class
63       * to be returned by {@code MultiFileConfigurationBuilder}.
64       *
65       * @param configCls the name of the managed configuration class
66       * @param paramCls the name of the class of the parameters object to configure the managed configuration
67       */
68      public MultiFileConfigurationBuilderProvider(final String configCls, final String paramCls) {
69          super(BUILDER_CLASS, RELOADING_BUILDER_CLASS, configCls, Arrays.asList(paramCls, PARAM_CLASS));
70      }
71  
72      /**
73       * {@inheritDoc} This implementation lets the super class create a fully configured builder. Then it returns a special
74       * wrapper around it.
75       */
76      @Override
77      public ConfigurationBuilder<? extends Configuration> getConfigurationBuilder(final ConfigurationDeclaration decl) throws ConfigurationException {
78          final ConfigurationBuilder<? extends Configuration> multiBuilder = super.getConfigurationBuilder(decl);
79          final Configuration wrapConfig = createWrapperConfiguration(multiBuilder);
80          return createWrapperBuilder(multiBuilder, wrapConfig);
81      }
82  
83      /**
84       * Creates a configuration which wraps the specified builder.
85       *
86       * @param builder the builder
87       * @return the wrapping configuration
88       */
89      // It is safe to disable any type checks because we manually determine
90      // the interface class to be passed to BuilderConfigurationWrapperFactory
91      @SuppressWarnings({"unchecked", "rawtypes"})
92      private Configuration createWrapperConfiguration(final ConfigurationBuilder builder) {
93          final Class<?> configClass = ConfigurationUtils.loadClassNoEx(getConfigurationClass());
94          final Class ifcClass = HierarchicalConfiguration.class.isAssignableFrom(configClass) ? HierarchicalConfiguration.class : Configuration.class;
95          return (Configuration) BuilderConfigurationWrapperFactory.createBuilderConfigurationWrapper(ifcClass, builder, EventSourceSupport.BUILDER);
96      }
97  
98      /**
99       * Creates the {@code ConfigurationBuilder} to be returned by this provider. This is a very simple implementation which
100      * always returns the same wrapper configuration instance. The handling of builder listeners is delegated to the wrapped
101      * {@code MultiFileConfigurationBuilder}. If reloading is support, the builder returned by this method also implements
102      * the {@link ReloadingControllerSupport} interface.
103      *
104      * @param multiBuilder the {@code MultiFileConfigurationBuilder}
105      * @param wrapConfig the configuration to be returned
106      * @return the wrapper builder
107      */
108     private static ConfigurationBuilder<? extends Configuration> createWrapperBuilder(final ConfigurationBuilder<? extends Configuration> multiBuilder,
109         final Configuration wrapConfig) {
110         if (multiBuilder instanceof ReloadingControllerSupport) {
111             return new ReloadableWrapperBuilder(wrapConfig, multiBuilder);
112         }
113         return new WrapperBuilder(wrapConfig, multiBuilder);
114     }
115 
116     /**
117      * A simple wrapper implementation of the {@code ConfigurationBuilder} interface which returns a fix configuration and
118      * delegates to another builder for event listener management.
119      */
120     private static class WrapperBuilder implements ConfigurationBuilder<Configuration> {
121         /** The configuration managed by this builder. */
122         private final Configuration configuration;
123 
124         /** The builder to which this instance delegates. */
125         private final ConfigurationBuilder<? extends Configuration> builder;
126 
127         /**
128          * Creates a new instance of {@code WrapperBuilder}.
129          *
130          * @param conf the managed configuration
131          * @param bldr the underlying builder
132          */
133         public WrapperBuilder(final Configuration conf, final ConfigurationBuilder<? extends Configuration> bldr) {
134             configuration = conf;
135             builder = bldr;
136         }
137 
138         @Override
139         public Configuration getConfiguration() throws ConfigurationException {
140             return configuration;
141         }
142 
143         @Override
144         public <T extends Event> void addEventListener(final EventType<T> eventType, final EventListener<? super T> listener) {
145             builder.addEventListener(eventType, listener);
146         }
147 
148         @Override
149         public <T extends Event> boolean removeEventListener(final EventType<T> eventType, final EventListener<? super T> listener) {
150             return builder.removeEventListener(eventType, listener);
151         }
152     }
153 
154     /**
155      * A wrapper builder implementation which also provides a {@code ReloadingController}. This class assumes that the
156      * wrapped builder implements {@code ReloadingControllerSupport}. So the reloading controller can be obtained from this
157      * object.
158      */
159     private static final class ReloadableWrapperBuilder extends WrapperBuilder implements ReloadingControllerSupport {
160         /** The object for obtaining the reloading controller. */
161         private final ReloadingControllerSupport ctrlSupport;
162 
163         /**
164          * Creates a new instance of {@code ReloadableWrapperBuilder}.
165          *
166          * @param conf the managed configuration
167          * @param bldr the underlying builder (must implement {@code ReloadingControllerSupport})
168          */
169         public ReloadableWrapperBuilder(final Configuration conf, final ConfigurationBuilder<? extends Configuration> bldr) {
170             super(conf, bldr);
171             ctrlSupport = (ReloadingControllerSupport) bldr;
172         }
173 
174         @Override
175         public ReloadingController getReloadingController() {
176             return ctrlSupport.getReloadingController();
177         }
178     }
179 }