View Javadoc
1   package fr.ifremer.quadrige2.core.config;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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 Quadrige2Configurations {
40      /** Logger. */
41      private static final Log log = LogFactory.getLog(Quadrige2Configurations.class);
42   
43      /**
44       * <p>Constructor for Quadrige2Configurations.</p>
45       */
46      protected Quadrige2Configurations() {
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 fromProperty = property;
106                 String endPart = fromProperty.substring(fromKeyPrefix.length());
107                 String toProperty = toKeyPrefix + endPart;
108                 
109                 // This property could be override, so just do it !
110                 if (applicationConfig.hasOption(toProperty)) {
111                     remapOption(applicationConfig, fromProperty, toProperty);
112                 }
113             }
114         }
115     }
116 
117     /**
118      * <p>remapOptionsToPrefix.</p>
119      *
120      * @param applicationConfig a {@link org.nuiton.config.ApplicationConfig} object.
121      * @param oldPrefix a {@link java.lang.String} object.
122      * @param newOptionDefs an array of {@link org.nuiton.config.ConfigOptionDef} objects.
123      * @param newPrefix a {@link java.lang.String} object.
124      */
125     public static void remapOptionsToPrefix(
126             ApplicationConfig applicationConfig,
127             String oldPrefix,
128             ConfigOptionDef[] newOptionDefs,
129             String newPrefix) {
130 
131         for (ConfigOptionDef newOptionDef: newOptionDefs) {
132             String property = newOptionDef.getKey();
133 
134             // If prefix match
135             if (property.startsWith(newPrefix)) {
136                 String newProperty = property;
137                 String endPart = newProperty.substring(newPrefix.length());
138                 String oldProperty = oldPrefix + endPart;
139 
140                 remapOption(applicationConfig, oldProperty, newProperty);
141             }
142         }
143     }
144 }