KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > config > ServerConfiguration


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.config;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 /**
35  * A configuration object. Contains information about server and admin login.
36  * @author Matthias L. Jugel
37  * @version $Id: ServerConfiguration.java 1256 2003-12-11 13:24:57Z leo $
38  */

39 public class ServerConfiguration {
40
41   private Properties JavaDoc properties;
42
43   public final static String JavaDoc INIT_PARAM = "config";
44
45   public final static String JavaDoc VERSION = "snipsnap.server.version";
46   public final static String JavaDoc ENCODING = "snipsnap.server.encoding";
47   public final static String JavaDoc ADMIN_USER = "snipsnap.server.admin.user";
48   public final static String JavaDoc ADMIN_PASS = "snipsnap.server.admin.password";
49   public final static String JavaDoc ADMIN_URL = "snipsnap.server.admin.rpc.url";
50   public final static String JavaDoc ADMIN_EMAIL = "snipsnap.server.admin.email";
51   public final static String JavaDoc WEBAPP_ROOT = "snipsnap.server.webapp.root";
52
53   private File JavaDoc file = null;
54
55   /**
56    * Create an instance of configuration, unconfigured
57    */

58   public ServerConfiguration() {
59     properties = new Properties JavaDoc();
60   }
61
62   /**
63    * Create an instance of configuration from a file given as string.
64    * @param file the config file to load
65    * @throws IOException
66    */

67   public ServerConfiguration(String JavaDoc file) throws IOException JavaDoc {
68     this(new File JavaDoc(file));
69   }
70
71   /**
72    * Create an instance of configuration from a file.
73    * @param file the config file to load
74    * @throws IOException
75    */

76   public ServerConfiguration(File JavaDoc file) throws IOException JavaDoc {
77     this();
78     load(file);
79   }
80
81
82   public ServerConfiguration(ServerConfiguration config) {
83     properties = (Properties JavaDoc) config.properties.clone();
84   }
85
86   /**
87    * Change the file to store the configuration in.
88    * @param file the new file
89    */

90   public void setFile(File JavaDoc file) {
91     this.file = file;
92   }
93
94   public File JavaDoc getFile() {
95     return file;
96   }
97
98   public void load() throws IOException JavaDoc {
99     if (file != null) {
100       load(file);
101     } else {
102       throw new IOException JavaDoc("no configuration file known, use load(File file)");
103     }
104   }
105
106   public void load(Properties JavaDoc properties) {
107     this.properties = properties;
108   }
109
110   public void load(File JavaDoc configFile) throws IOException JavaDoc {
111     setFile(configFile);
112     FileInputStream JavaDoc in = new FileInputStream JavaDoc(file);
113     load(in);
114     in.close();
115   }
116
117   public void load(FileInputStream JavaDoc in) throws IOException JavaDoc {
118     properties.load(in);
119   }
120
121   /**
122    * Store configuration in the file it was loaded from.
123    * @throws IOException
124    */

125   public void store() throws IOException JavaDoc {
126     if (file != null) {
127       store(file);
128     } else {
129       throw new IOException JavaDoc("no configuration file known, use store(File file)");
130     }
131   }
132
133   /**
134    * Store configuration file explicitely in a specifified file.
135    * @param configFile the file to store in
136    * @throws IOException
137    */

138   public void store(File JavaDoc configFile) throws IOException JavaDoc {
139     setFile(configFile);
140     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
141     store(out);
142     out.close();
143   }
144
145   /**
146    * Store using an output stream. This is used to override defaults in Properties and adds
147    * a header.
148    * @param out the output stream
149    * @throws IOException
150    */

151   public void store(OutputStream JavaDoc out) throws IOException JavaDoc {
152     properties.store(out, "SnipSnap configuration $Revision: 1256 $");
153   }
154
155   public void setAdminLogin(String JavaDoc login) {
156     properties.setProperty(ADMIN_USER, login);
157   }
158
159   public String JavaDoc getAdminLogin() {
160     return properties.getProperty(ADMIN_USER);
161   }
162
163   public void setAdminPassword(String JavaDoc password) {
164     properties.setProperty(ADMIN_PASS, password);
165   }
166
167   public String JavaDoc getAdminPassword() {
168     return properties.getProperty(ADMIN_PASS);
169   }
170
171
172   public void setAdminEmail(String JavaDoc email) {
173     properties.setProperty(ADMIN_EMAIL, email);
174   }
175
176   public String JavaDoc getAdminEmail() {
177     return properties.getProperty(ADMIN_EMAIL);
178   }
179
180   public String JavaDoc getVersion() {
181     String JavaDoc version = System.getProperty(VERSION);
182     if (null == version) {
183       version = getProperty(VERSION);
184     }
185     return version;
186   }
187
188   public void setProperty(String JavaDoc name, String JavaDoc value) {
189     if (name == null || value == null) {
190       return;
191     }
192     properties.setProperty(name, value);
193   }
194
195   public String JavaDoc getProperty(String JavaDoc name) {
196     return properties.getProperty(name);
197   }
198
199   public String JavaDoc getProperty(String JavaDoc name, String JavaDoc defaultValue) {
200     String JavaDoc value = getProperty(name);
201     if (value == null) {
202       return defaultValue;
203     }
204     return value;
205   }
206
207   public String JavaDoc toString() {
208     return properties.toString();
209   }
210 }
211
Popular Tags