James Moger
2011-10-25 486ee115abb831b2ec78be6777fb1bca9e931df0
commit | author | age
5450d0 1 /*
JM 2  * Copyright 2011 gitblit.com.
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  */
22fc5e 16 package com.gitblit.build;
5450d0 17
669686 18 import java.io.BufferedReader;
5450d0 19 import java.io.File;
JM 20 import java.io.FileOutputStream;
21 import java.io.FileReader;
22 import java.text.MessageFormat;
23 import java.util.ArrayList;
24 import java.util.List;
669686 25 import java.util.Vector;
5450d0 26
85c2e6 27 import com.beust.jcommander.JCommander;
JM 28 import com.beust.jcommander.Parameter;
29 import com.beust.jcommander.ParameterException;
30 import com.beust.jcommander.Parameters;
22fc5e 31 import com.gitblit.Keys;
85c2e6 32 import com.gitblit.utils.StringUtils;
JM 33
892570 34 /**
JM 35  * Builds the Gitblit WAR web.xml file by merging the Gitblit GO web.xml file
36  * with the gitblit.properties comments, settings, and values.
37  * 
38  * @author James Moger
39  * 
40  */
5450d0 41 public class BuildWebXml {
JM 42     private static final String PARAMS = "<!-- PARAMS -->";
669686 43
JM 44     private static final String[] STRIP_TOKENS = { "<!-- STRIP", "STRIP -->" };
45
46     private static final String COMMENT_PATTERN = "\n\t<!-- {0} -->";
5450d0 47
JM 48     private static final String PARAM_PATTERN = "\n\t<context-param>\n\t\t<param-name>{0}</param-name>\n\t\t<param-value>{1}</param-value>\n\t</context-param>\n";
49
50     public static void main(String[] args) throws Exception {
85c2e6 51         Params params = new Params();
JM 52         JCommander jc = new JCommander(params);
53         try {
54             jc.parse(args);
55         } catch (ParameterException t) {
56             System.err.println(t.getMessage());
57             jc.usage();
58         }
59         generateWebXml(params);
60     }
61
62     private static void generateWebXml(Params params) throws Exception {
5450d0 63         // Read the current Gitblit properties
669686 64         BufferedReader propertiesReader = new BufferedReader(new FileReader(new File(
85c2e6 65                 params.propertiesFile)));
669686 66
JM 67         Vector<Setting> settings = new Vector<Setting>();
68         List<String> comments = new ArrayList<String>();
69         String line = null;
70         while ((line = propertiesReader.readLine()) != null) {
71             if (line.length() == 0) {
72                 comments.clear();
73             } else {
74                 if (line.charAt(0) == '#') {
75                     if (line.length() > 1) {
76                         comments.add(line.substring(1).trim());
77                     }
78                 } else {
79                     String[] kvp = line.split("=", 2);
80                     String key = kvp[0].trim();
81                     if (!skipKey(key)) {
82                         Setting s = new Setting(key, kvp[1].trim(), comments);
83                         settings.add(s);
84                     }
85                     comments.clear();
86                 }
5450d0 87             }
669686 88         }
JM 89         propertiesReader.close();
90
91         StringBuilder parameters = new StringBuilder();
92
93         for (Setting setting : settings) {
94             for (String comment : setting.comments) {
95                 parameters.append(MessageFormat.format(COMMENT_PATTERN, comment));
96             }
892570 97             parameters.append(MessageFormat.format(PARAM_PATTERN, setting.name,
JM 98                     StringUtils.escapeForHtml(setting.value, false)));
5450d0 99         }
JM 100
101         // Read the prototype web.xml file
85c2e6 102         File webxml = new File(params.sourceFile);
669686 103         char[] buffer = new char[(int) webxml.length()];
JM 104         FileReader webxmlReader = new FileReader(webxml);
105         webxmlReader.read(buffer);
106         webxmlReader.close();
5450d0 107         String webXmlContent = new String(buffer);
JM 108
109         // Insert the Gitblit properties into the prototype web.xml
669686 110         for (String stripToken : STRIP_TOKENS) {
5450d0 111             webXmlContent = webXmlContent.replace(stripToken, "");
JM 112         }
113         int idx = webXmlContent.indexOf(PARAMS);
114         StringBuilder sb = new StringBuilder();
115         sb.append(webXmlContent.substring(0, idx));
116         sb.append(parameters.toString());
117         sb.append(webXmlContent.substring(idx + PARAMS.length()));
118
119         // Save the merged web.xml to the war build folder
85c2e6 120         FileOutputStream os = new FileOutputStream(new File(params.destinationFile), false);
5450d0 121         os.write(sb.toString().getBytes());
JM 122         os.close();
123     }
124
125     private static boolean skipKey(String key) {
126         return key.startsWith(Keys.server._ROOT);
127     }
669686 128
88598b 129     /**
JM 130      * Setting represents a setting and its comments from the properties file.
131      */
669686 132     private static class Setting {
JM 133         final String name;
134         final String value;
135         final List<String> comments;
136
137         Setting(String name, String value, List<String> comments) {
138             this.name = name;
139             this.value = value;
140             this.comments = new ArrayList<String>(comments);
141         }
142     }
85c2e6 143
88598b 144     /**
JM 145      * JCommander Parameters class for BuildWebXml.
146      */
85c2e6 147     @Parameters(separators = " ")
JM 148     private static class Params {
149
150         @Parameter(names = { "--sourceFile" }, description = "Source web.xml file", required = true)
151         public String sourceFile;
152
153         @Parameter(names = { "--propertiesFile" }, description = "Properties settings file", required = true)
154         public String propertiesFile;
155
156         @Parameter(names = { "--destinationFile" }, description = "Destination web.xml file", required = true)
157         public String destinationFile;
158
159     }
5450d0 160 }