View Javadoc
1   package fr.ifremer.quadrige3.core.config;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.nuiton.config.ApplicationConfig;
31  import org.nuiton.config.ConfigOptionDef;
32  
33  /**
34   * helper class for configuration
35   *
36   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
37   * @since 1.0
38   */
39  public class QuadrigeConfigurations {
40      /** Logger. */
41      private static final Log log = LogFactory.getLog(QuadrigeConfigurations.class);
42   
43      /**
44       * <p>Constructor for QuadrigeConfigurations.</p>
45       */
46      protected QuadrigeConfigurations() {
47          // helper class
48      }
49      
50      /**
51       * <p>remapOption.</p>
52       *
53       * @param applicationConfig a {@link org.nuiton.config.ApplicationConfig} object.
54       * @param fromKey a {@link org.nuiton.config.ConfigOptionDef} object.
55       * @param toKey a {@link org.nuiton.config.ConfigOptionDef} object.
56       */
57      public static void remapOption(ApplicationConfig applicationConfig,
58              ConfigOptionDef fromKey,
59              ConfigOptionDef toKey
60              ) {
61          remapOption(applicationConfig, fromKey.getKey(), toKey.getKey());
62      }
63      
64      /**
65       * <p>remapOption.</p>
66       *
67       * @param applicationConfig a {@link org.nuiton.config.ApplicationConfig} object.
68       * @param fromKey a {@link java.lang.String} object.
69       * @param toKey a {@link java.lang.String} object.
70       */
71      public static void remapOption(ApplicationConfig applicationConfig,
72              String fromKey,
73              String toKey
74              ) {
75          
76          if (log.isDebugEnabled()) {
77              log.debug(String.format("Change default value of config option: %s=${%s}", fromKey, toKey));
78          }
79          applicationConfig.setDefaultOption(fromKey,
80                  String.format("${%s}", 
81                          toKey));
82      }
83      
84      /**
85       * <p>remapOptionsFromPrefix.</p>
86       *
87       * @param applicationConfig a {@link org.nuiton.config.ApplicationConfig} object.
88       * @param optionDefs an array of {@link org.nuiton.config.ConfigOptionDef} objects.
89       * @param fromKeyPrefix a {@link java.lang.String} object.
90       * @param toKeyPrefix a {@link java.lang.String} object.
91       */
92      public static void remapOptionsFromPrefix(
93              ApplicationConfig applicationConfig,
94              ConfigOptionDef[] optionDefs,
95              String fromKeyPrefix,
96              String toKeyPrefix
97              ) {
98          
99          
100         for (ConfigOptionDef optionDef: optionDefs) {
101             String property = optionDef.getKey();
102             
103             // If prefix match
104             if (property.startsWith(fromKeyPrefix)) {
105                 String endPart = property.substring(fromKeyPrefix.length());
106                 String toProperty = toKeyPrefix + endPart;
107                 
108                 // This property could be override, so just do it !
109                 if (applicationConfig.hasOption(toProperty)) {
110                     remapOption(applicationConfig, property, toProperty);
111                 }
112             }
113         }
114     }
115 
116     /**
117      * <p>remapOptionsToPrefix.</p>
118      *
119      * @param applicationConfig a {@link org.nuiton.config.ApplicationConfig} object.
120      * @param oldPrefix a {@link java.lang.String} object.
121      * @param newOptionDefs an array of {@link org.nuiton.config.ConfigOptionDef} objects.
122      * @param newPrefix a {@link java.lang.String} object.
123      */
124     public static void remapOptionsToPrefix(
125             ApplicationConfig applicationConfig,
126             String oldPrefix,
127             ConfigOptionDef[] newOptionDefs,
128             String newPrefix) {
129 
130         for (ConfigOptionDef newOptionDef: newOptionDefs) {
131             String property = newOptionDef.getKey();
132 
133             // If prefix match
134             if (property.startsWith(newPrefix)) {
135                 String endPart = property.substring(newPrefix.length());
136                 String oldProperty = oldPrefix + endPart;
137 
138                 remapOption(applicationConfig, oldProperty, property);
139             }
140         }
141     }
142 }