KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wmconfig


1 import java.io.*;
2 import java.util.*;
3
4 /*
5
6 PLEASE NOTE:
7     THIS IS A FAST HACK IN THE MID OF THE NIGHT.
8     NEEDS ABSTRACTION, ELEGANCE AND ALL THAT ...
9     but works for now :-)
10     
11 */

12
13
14 public class wmconfig {
15
16     String JavaDoc confPath;
17     Hashtable confTable = new Hashtable();
18
19     public static void main( String JavaDoc[] args ) {
20         if ( args.length < 1 ) {
21             System.err.println("Es wurde keine Konfigurationsdatei angegeben. Abbruch.");
22             System.exit(1);
23         }
24         wmconfig wmc = new wmconfig( args[0]);
25         wmc.readConfig();
26         wmc.checkConfig();
27         //
28
// defaults - please replace by args
29
//
30
String JavaDoc pWebmandbIni = "/jakarta-tomcat/webapps/ROOT/WEB-INF/classes/webmandb.ini";
31         String JavaDoc pServerTpl = "/jakarta-tomcat/conf/server.tpl";
32         String JavaDoc pServerXml = "/jakarta-tomcat/conf/server.xml";
33         pWebmandbIni.replace( '/', File.separatorChar);
34         pServerTpl.replace( '/', File.separatorChar);
35         pServerXml.replace( '/', File.separatorChar);
36         wmc.writeConfig( pWebmandbIni, pServerTpl, pServerXml);
37     }
38     
39
40     // register a set of config keys
41
//
42
wmconfig( String JavaDoc path) {
43         confPath = path;
44         confTable.put( "DB-Typ", "");
45         confTable.put( "DB-Lizenzen", "");
46         confTable.put( "DB-Nutzername", "");
47         confTable.put( "DB-Passwort", "");
48         confTable.put( "DB-ServerName", "");
49         confTable.put( "DB-ServerPort", "");
50         confTable.put( "DB-Name", "");
51         confTable.put( "WM-ServerPort", "");
52     }
53     
54     // read in the config: key,delimiter,value triples
55
//
56
void readConfig() {
57         try {
58             File f = new File( confPath);
59             confPath = f.getCanonicalPath();
60             BufferedReader reader = new BufferedReader ( new FileReader( f ) );
61             String JavaDoc s;
62             while ( (s=reader.readLine()) != null ) {
63                 StringTokenizer st = new StringTokenizer( s, "=;#", true );
64                 if ( st.countTokens() < 3 ) continue;
65                 setConfigValue( st.nextToken().trim(), st.nextToken().trim(), st.nextToken().trim() );
66             }
67             reader.close();
68         }
69         catch( IOException e) {
70             System.err.println("Die Konfigurationsdatei " + confPath + " konnte nicht gelesen werden. Abbruch.");
71             System.exit(1);
72         }
73     }
74     
75     // save key,value in config table
76
//
77
void setConfigValue( String JavaDoc k, String JavaDoc d, String JavaDoc v ) {
78         if ( ! d.equals( "=" ) ) return;
79         if ( ! confTable.containsKey( k ) ) {
80             System.err.println( "Fehlerhafter Konfigurationseintrag: '" + k + "' " + d + " '" + v + "'. Abbruch.");
81             System.exit(1);
82         }
83         confTable.put( k, v);
84     }
85     
86     // check completeness of config data
87
//
88
void checkConfig() {
89         Enumeration e = confTable.keys();
90         String JavaDoc k;
91         while( e.hasMoreElements() ) {
92             k = (String JavaDoc) e.nextElement();
93             if ( ((String JavaDoc) confTable.get( k )).length() == 0 ) {
94                 System.err.println( "Konfigurationseintrag für '" + k + "' fehlt. Abbruch.");
95                 System.exit(1);
96             }
97         }
98         
99     }
100     
101     
102     // write all neccessary webman files
103
//
104
void writeConfig( String JavaDoc pWebmandbIni, String JavaDoc pServerTpl, String JavaDoc pServerXml) {
105
106         String JavaDoc basePath;
107         String JavaDoc fp = "?";
108         String JavaDoc gp = "?";
109         
110         basePath = (new File( confPath)).getParent();
111         
112         // webmandb.ini
113

114         try {
115             fp = basePath + pWebmandbIni;
116             File f = new File( fp);
117             fp = f.getCanonicalPath();
118             System.out.println( "Erzeuge " + fp);
119             BufferedWriter writer = new BufferedWriter ( new FileWriter( f ) );
120             writer.write( "LICENCES=" + (String JavaDoc) confTable.get("DB-Lizenzen") + "\n" );
121             writer.write( "DATABASE=" + (String JavaDoc) confTable.get("DB-Typ") + "\n" );
122             writer.write( "USER=" + (String JavaDoc) confTable.get("DB-Nutzername") + "\n" );
123             writer.write( "PASSWD=" + (String JavaDoc) confTable.get("DB-Passwort") + "\n" );
124             
125             String JavaDoc host = "HOST="
126                 + (String JavaDoc) confTable.get("DB-ServerName") + ':'
127                 + (String JavaDoc) confTable.get("DB-ServerPort");
128             String JavaDoc dbType = ((String JavaDoc) confTable.get("DB-Typ")).toLowerCase();
129             String JavaDoc dbName = ((String JavaDoc) confTable.get("DB-Name"));
130             
131             if ( dbType.startsWith( "oracle")) {
132                 host = host + ':' + dbName;
133             }
134             else if ( dbType.startsWith( "sybase")) {
135                 
136             }
137             else if ( dbType.startsWith( "postgres")) {
138                 host = host + '/' + dbName + "?compatible=7.1";
139             }
140
141             writer.write( host + "\n");
142             writer.close();
143         }
144         catch( IOException e) {
145             System.err.println("Die Konfigurationsdatei " + fp + " konnte nicht erzeugt werden. Abbruch.");
146             System.exit(1);
147         }
148
149     
150         // server.xml
151

152         try {
153             gp = basePath + pServerXml;
154             File g = new File( gp);
155             gp = g.getCanonicalPath();
156             
157             fp = basePath + pServerTpl;
158             File f = new File( fp);
159             fp = f.getCanonicalPath();
160             
161             System.out.println( "Transformiere " + fp + " zu ");
162             System.out.println( " " +gp);
163             
164             BufferedReader reader = new BufferedReader ( new FileReader( f ) );
165             BufferedWriter writer = new BufferedWriter ( new FileWriter( g ) );
166             String JavaDoc s;
167             int i;
168             String JavaDoc k = "!webman-specified-serverport!";
169             while ( (s=reader.readLine()) != null ) {
170                 if ( (i = s.indexOf( k )) >= 0 ) {
171                     s = s.substring( 0, i ) + (String JavaDoc) confTable.get("WM-ServerPort") + s.substring( i + k.length() );
172                 }
173                 writer.write( s + "\n" );
174             }
175             writer.close();
176             reader.close();
177             System.out.println( "Konfiguration abgeschlossen.");
178         }
179         catch( IOException e) {
180             System.err.println("Die Konfigurationsdatei " + gp + " konnte nicht erzeugt werden. Abbruch.");
181             System.exit(1);
182         }
183         
184     }
185 }
186
Popular Tags