KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyBuild > propertyconfig


1 /*
2
3    Derby - Class org.apache.derbyBuild.propertyconfig
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyBuild;
23 import java.util.Properties JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.File JavaDoc;
28
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Enumeration JavaDoc;
32
33
34 /**
35  *
36  * Usage:
37  * java propertyConfig <master file> <config> <output file>
38  * <B> e.g., java propertyConfig dbms.properties cloudsync
39  * cloudsync.dbms.properties
40  *
41  * <P>
42  * This program takes a master property file, and using the configuration
43  * specification, generate an output file that only contains the properties
44  * for that particular configuration.
45  *
46  * <P>
47  * For the different types of legitamite configurations, please see
48  * org.apache.derby.modules.properties
49  *
50  * <P>
51  * PropertySplitter will look at cloudscape.config.<tag> to see which
52  * configuration a particular module belongs to.
53  * <B>E.g., cloudscape.config.dataComm.listen=cloudtarget,cloudsync
54  * <B>this means all properties associated with dataComm.listen will be
55  * in the output properties file only if we are generating for the cloudsync or
56  * cloudtarget configuration. They will not be in the output properties file
57  * if we are generating for the cloud or cloudscape configuration.
58  *
59  */

60
61 public class propertyconfig {
62
63     public static String JavaDoc header =
64         "######## This is a generated file, do not edit.\n" +
65         "#\n# This file is generated as by propertyConfig\n" +
66         "#\n";
67
68     public static String JavaDoc footer =
69         "\n######## This is a generated file, do not edit.\n";
70
71     public static void main(String JavaDoc[] args) throws IOException JavaDoc
72     {
73         if (args.length != 3)
74             printUsageAndExit();
75
76         File JavaDoc masterfile = new File JavaDoc(args[0]);
77         File JavaDoc outputfile = new File JavaDoc(args[2]);
78
79         if (!masterfile.exists())
80             printUsageAndExit();
81
82         // OK, got the input cleared up, now do the processing
83
Properties JavaDoc masterProp = new Properties JavaDoc();
84         FileInputStream JavaDoc is = new FileInputStream JavaDoc(masterfile);
85
86         try
87         {
88             masterProp.load(is);
89         }
90         finally
91         {
92             if (is != null)
93                 is.close();
94         }
95
96         process(masterProp, args[1], outputfile);
97     }
98
99     /**
100      * For each module with a particular tag in derby.module.<tag>, see if
101      * there is any configuration restriction. If there is no
102      * cloudscape.config.<tag> property, then this module should be present in
103      * all configurations. If there is a cloudscape.config.<tag>, then this
104      * module should only be present in the configurations listed.
105      *
106      * <br>If this module should be present or this configuration, then gather
107      * up all the properties belong to this module and send it to the output
108      * file.
109      *
110      */

111     private static void process(Properties JavaDoc moduleList, String JavaDoc config,
112                                 File JavaDoc outputfile)
113                     throws IOException JavaDoc
114     {
115         Properties JavaDoc outputProp = new Properties JavaDoc();
116
117         // copy this code from
118
// org.apache.derby.impl.services.monitor.BaseMonitor
119
//
120
for (Enumeration JavaDoc e = moduleList.propertyNames(); e.hasMoreElements(); )
121         {
122             String JavaDoc key = (String JavaDoc) e.nextElement();
123             if (key.startsWith("derby.module."))
124             {
125                 String JavaDoc tag = key.substring("derby.module.".length());
126
127                 // Check to see if it has any configuration requirements
128
String JavaDoc configKey = "cloudscape.config.".concat(tag);
129                 String JavaDoc configProp = moduleList.getProperty(configKey);
130                 
131                 boolean match = false;
132
133                 if (configProp != null)
134                 {
135                     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(configProp, ",");
136                     while(st.hasMoreTokens())
137                     {
138
139                         String JavaDoc s = st.nextToken().trim();
140
141                         // if config spec says all, it should not have other
142
// configurations
143
if (s.equalsIgnoreCase("all") &&
144                             !configProp.trim().equals("all"))
145                         {
146                             System.out.println("illegal config specification "
147                                                + key);
148                             System.exit(3);
149                         }
150
151                         // if config spec says none, it should not have other
152
// configurations
153
if (s.equalsIgnoreCase("none") &&
154                             !configProp.trim().equals("none"))
155                         {
156                             System.out.println("illegal config specification "
157                                                + key);
158                             System.exit(4);
159                         }
160
161                         if (s.equalsIgnoreCase(config) ||
162                             s.equalsIgnoreCase("all"))
163                         {
164                             match = true;
165                             break;
166                         }
167                     }
168                 }
169                 else
170                 {
171                     // no config property, this module goes to all configs
172
System.out.println("Need config specification for " + key);
173                     System.exit(2);
174                 }
175
176                 if (match)
177                 {
178                     // gather up all relavant properties and put it in
179
// outputProp
180

181                     // derby.module.<tag>
182
outputProp.put(key, moduleList.getProperty(key));
183
184                     // don't output cloudscape.config.<tag>
185
// that line only has meaning to this program
186

187                     // derby.env.classes.<tag>
188
String JavaDoc envKey = "derby.env.classes.".concat(tag);
189                     if (moduleList.getProperty(envKey) != null)
190                         outputProp.put(envKey, moduleList.getProperty(envKey));
191
192                     // derby.env.jdk.<tag>
193
//
194
// some of the configs only support one java env. Some modules
195
// have alternate implementation for running on java1 and
196
// java2 platforms. If we get rid of, say, the java2
197
// implementation, then the monitor won't load the java1
198
// implementation if that module specifies that it should
199
// only be loaded in a java1 environment. The result is
200
// that some critical modules will be missing and the
201
// database won't boot.
202
//
203
// the convention is, for modules that have both java1 and
204
// java2 implementation, they must named the module as
205
// derby.env.jdk.<name>J1 or
206
// derby.env.jdk.<name>J2
207
// in other words, the <tag> must end with J1 or J2.
208
//
209
// If a config only use one of the two implementation, then
210
// this program will not put the one env.jdk line to the
211
// output properties. As a result, this one implementation
212
// will be loaded when run in any environment.
213
//
214
// This does not apply to any module that only has one
215
// implementation that runs on a specific jdk environment.
216
//
217
//derby.env.jdk.<tag>
218
envKey = "derby.env.jdk.".concat(tag);
219
220                     if (moduleList.getProperty(envKey) != null)
221                     {
222                         // by default keep the jdk env specification with the
223
// config
224
boolean saveEnvKey = true;
225
226                         // figure out if this is a tag of the form <name>J1 or
227
// <name>J2.
228
if (tag.endsWith("J1") || tag.endsWith("J2"))
229                         {
230                             // ok, this is a module with alternate
231
// implementation for java 1 and java 2. If this
232
// config ditches one of them, then do not output
233
// the env line
234
int length = tag.length() - 2;
235                             String JavaDoc alternateTag = tag.substring(0, length);
236                             
237                             if (tag.endsWith("J1"))
238                                 alternateTag += "J2";
239                             else
240                                 alternateTag += "J1";
241
242                             // see if
243
// 1) this module has an alternate impl for the
244
// other jdk and
245
// 2) this config is not going to pick it up.
246
//
247

248                             String JavaDoc alternateImplKey =
249                                 "derby.module."+ alternateTag;
250                             String JavaDoc alternateJDKEnv =
251                                 "derby.env.jdk."+ alternateTag;
252                             String JavaDoc alternateImplConfigKey =
253                                 "cloudscape.config."+alternateTag;
254
255                             // if any of of these are not present, then we
256
// don't have a problem because either there is no
257
// alternate implementation, or the alternate
258
// implementation is not based on jdk, or the
259
// alternate jdk based implemenation will also be
260
// present in this configuration
261

262                             if ((moduleList.getProperty(alternateImplKey) != null) &&
263                                 (moduleList.getProperty(alternateJDKEnv) != null) &&
264                                 (moduleList.getProperty(alternateImplConfigKey) != null))
265                             {
266                                 // there is an alternate impl that is jdk based
267
// and it has a config tag. Let's see if it is
268
// part of this config.
269
String JavaDoc alternateConfigProp =
270                                     moduleList.getProperty(alternateImplConfigKey);
271
272                                 // we know that there are
273
// derby.module.<tag>J2 and
274
// derby.module.<tag>J1 and
275
// derby.env.jdk.<tag>J2 and
276
// derby.env.jdk.<tag>J1 and
277
// cloudscape.config.<tag>J2 and
278
// cloudscape.config.<tag>J1
279
StringTokenizer JavaDoc st2 = new
280                                     StringTokenizer JavaDoc(alternateConfigProp, ",");
281
282                                 boolean ok = false;
283                                 while(st2.hasMoreTokens())
284                                 {
285                                     String JavaDoc s = st2.nextToken().trim();
286
287                                     if (s.equalsIgnoreCase(config) ||
288                                         s.equalsIgnoreCase("all"))
289                                     {
290                                         ok = true;
291                                         break;
292                                     }
293                                 }
294                                 // the alternate module impl is not part of
295
// this config, do not save the jdk env key
296
if (!ok)
297                                     saveEnvKey = false;
298                             }
299                         }
300
301                         if (saveEnvKey)
302                             outputProp.put(envKey, moduleList.getProperty(envKey));
303                     }
304                     
305
306
307                     // NOTE, if other types of properties are added to
308
// modules.properties, be sure to add it here too.
309
}
310             }
311         }
312
313         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(outputfile);
314         try
315         {
316             outputProp.save(os,
317                             header.
318                             concat("# config is ").concat(config).
319                             concat(footer));
320         }
321         finally
322         {
323             if (os != null)
324                 os.close();
325         }
326     }
327
328
329     private static void printUsageAndExit()
330     {
331         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(400);
332
333         buf.append("Usage propertyConfig <masterFile> <config> <outputFile>\n")
334             .append("masterFile must be a pre-existing properties file ")
335             .append("containing all the modules properites\n")
336             .append("config must be a configuration defined in ")
337             .append("org.apache.derby.modules.properties.\n")
338             .append("outputFile must not be a pre-existing properties file.\n\n")
339             .append("propertyConfig will generate the outputFile based on")
340             .append("the masterfile and the configuration specified.")
341             .append("\n\nE.g., java propertyConfig dbms.properties cloudsync dbms.cloudsync.properties\n");
342         
343
344         System.out.println(buf.toString());
345         System.exit(1);
346     }
347 }
348
Popular Tags