1 25 package org.snipsnap.config; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.OutputStream ; 32 import java.util.Properties ; 33 34 39 public class ServerConfiguration { 40 41 private Properties properties; 42 43 public final static String INIT_PARAM = "config"; 44 45 public final static String VERSION = "snipsnap.server.version"; 46 public final static String ENCODING = "snipsnap.server.encoding"; 47 public final static String ADMIN_USER = "snipsnap.server.admin.user"; 48 public final static String ADMIN_PASS = "snipsnap.server.admin.password"; 49 public final static String ADMIN_URL = "snipsnap.server.admin.rpc.url"; 50 public final static String ADMIN_EMAIL = "snipsnap.server.admin.email"; 51 public final static String WEBAPP_ROOT = "snipsnap.server.webapp.root"; 52 53 private File file = null; 54 55 58 public ServerConfiguration() { 59 properties = new Properties (); 60 } 61 62 67 public ServerConfiguration(String file) throws IOException { 68 this(new File (file)); 69 } 70 71 76 public ServerConfiguration(File file) throws IOException { 77 this(); 78 load(file); 79 } 80 81 82 public ServerConfiguration(ServerConfiguration config) { 83 properties = (Properties ) config.properties.clone(); 84 } 85 86 90 public void setFile(File file) { 91 this.file = file; 92 } 93 94 public File getFile() { 95 return file; 96 } 97 98 public void load() throws IOException { 99 if (file != null) { 100 load(file); 101 } else { 102 throw new IOException ("no configuration file known, use load(File file)"); 103 } 104 } 105 106 public void load(Properties properties) { 107 this.properties = properties; 108 } 109 110 public void load(File configFile) throws IOException { 111 setFile(configFile); 112 FileInputStream in = new FileInputStream (file); 113 load(in); 114 in.close(); 115 } 116 117 public void load(FileInputStream in) throws IOException { 118 properties.load(in); 119 } 120 121 125 public void store() throws IOException { 126 if (file != null) { 127 store(file); 128 } else { 129 throw new IOException ("no configuration file known, use store(File file)"); 130 } 131 } 132 133 138 public void store(File configFile) throws IOException { 139 setFile(configFile); 140 FileOutputStream out = new FileOutputStream (file); 141 store(out); 142 out.close(); 143 } 144 145 151 public void store(OutputStream out) throws IOException { 152 properties.store(out, "SnipSnap configuration $Revision: 1256 $"); 153 } 154 155 public void setAdminLogin(String login) { 156 properties.setProperty(ADMIN_USER, login); 157 } 158 159 public String getAdminLogin() { 160 return properties.getProperty(ADMIN_USER); 161 } 162 163 public void setAdminPassword(String password) { 164 properties.setProperty(ADMIN_PASS, password); 165 } 166 167 public String getAdminPassword() { 168 return properties.getProperty(ADMIN_PASS); 169 } 170 171 172 public void setAdminEmail(String email) { 173 properties.setProperty(ADMIN_EMAIL, email); 174 } 175 176 public String getAdminEmail() { 177 return properties.getProperty(ADMIN_EMAIL); 178 } 179 180 public String getVersion() { 181 String version = System.getProperty(VERSION); 182 if (null == version) { 183 version = getProperty(VERSION); 184 } 185 return version; 186 } 187 188 public void setProperty(String name, String value) { 189 if (name == null || value == null) { 190 return; 191 } 192 properties.setProperty(name, value); 193 } 194 195 public String getProperty(String name) { 196 return properties.getProperty(name); 197 } 198 199 public String getProperty(String name, String defaultValue) { 200 String value = getProperty(name); 201 if (value == null) { 202 return defaultValue; 203 } 204 return value; 205 } 206 207 public String toString() { 208 return properties.toString(); 209 } 210 } 211 | Popular Tags |