View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the 'License')
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an 'AS IS' BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.javagen.agile.core.ioc.spring;
17  
18  
19  import java.io.IOException;
20  import java.util.Properties;
21  
22  import org.javagen.agile.core.model.Model;
23  import org.javagen.agile.core.util.ModelUtil;
24  import org.javagen.agile.core.util.NanoTemplate;
25  import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
26  
27  /***
28   * Add a third properties source <code>overrideProperties</code>, so we can 
29   * override these properties instead of having to redefine every property as using
30   * the <code>setProperties</code> method would require.
31   * <p>
32   * This allows overriding properties in the Maven 2 Plugin config.
33   * 
34   * @author Richard Easterling
35   */
36  public class GlobalPropertyHolder extends PropertyPlaceholderConfigurer {
37  
38      
39      protected Properties overrideProperties;
40      protected Model globalPropertiesModel;
41      private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(GlobalPropertyHolder.class);
42      
43      public GlobalPropertyHolder() {}
44      
45      /***
46       * Return a merged Properties instance containing the
47       * loaded properties, properties set on this FactoryBean
48       * and the overrideProperties.
49       */
50      public Properties mergeProperties() throws IOException {
51          Properties _mergeProperties = super.mergeProperties();
52          if (overrideProperties!=null) {
53              //any property added here overrides previous values
54              for(Object key : overrideProperties.keySet()) {
55                  Object value = overrideProperties.get(key);
56                  _mergeProperties.put(key, value);
57              }
58          }
59          return _mergeProperties;
60      }
61  
62      /***
63       * load global properties into global properties model
64       */
65      public void injectPropertiesIntoNode() {
66          if (globalPropertiesModel==null) {
67              log.warn("GlobalPropertyHolder.globalPropertiesModel is null - No properties will be propagated to the model or templates");
68          } else {
69              try {
70                  ModelUtil.injectPropertiesIntoModelContext(mergeProperties(), globalPropertiesModel);
71                  NanoTemplate.bindContextVariables("${", globalPropertiesModel.getContext());
72              } catch (IOException e) {
73                  throw new RuntimeException(e);
74              }
75          }
76      }
77      
78      public Properties getOverrideProperties() {
79          return overrideProperties;
80      }
81  
82      public void setOverrideProperties(Properties overrideProperties) {
83          this.overrideProperties = overrideProperties;
84      }
85                      
86      public Model getGlobalPropertiesModel() {
87          return globalPropertiesModel;
88      }
89  
90      public void setGlobalPropertiesModel(Model globalPropertiesModel) {
91          this.globalPropertiesModel = globalPropertiesModel;
92      }
93  
94  }