001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: ServerConfig.java$
021: * $FileID: 4525$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $VerID: 10366$
026: * $Date: 9/17/03 1:16 PM$
027: * $Comment: $
028: */
029:
030: package org.sourcejammer.server;
031:
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Element;
034: import org.w3c.dom.Node;
035: import org.sourcejammer.xml.XMLUtil;
036: import java.io.IOException;
037: import java.io.File;
038:
039: import javax.xml.parsers.ParserConfigurationException;
040:
041: import org.xml.sax.SAXException;
042: import org.sourcejammer.util.ConfigurationException;
043:
044: /**
045: * Title: $FileName: ServerConfig.java$
046: * @version $VerNum: 9$
047: * @author $AuthorName: Rob MacGrogan$<br><br>
048: * $Description: $<br>
049: * $KeyWordsOff: $
050: */
051: public class ServerConfig {
052:
053: private ServerConfig() {
054: }
055:
056: private static final String CONF_FILE = "serverconf.xml";
057:
058: public static final class ConfXMLNodes {
059: public static final String MAX_CACHE_SIZE = "MaxCacheSize";
060: public static final String ENABLE_ANONYMOUS_ACCESS = "EnableAnonymousAccess";
061: public static final String ANONYMOUS_USER_NAME = "AnonymousUserName";
062: public static final String ENABLE_WEB_ACCESS = "EnableWebAccess";
063: public static final String WEB_INTERFACE_TITLE = "WebInterfaceTitle";
064: public static final String WEB_INTERFACE_INFO = "WebInterfaceInfo";
065:
066: public static final String COMP_CHUNK_SIZE = "ComparisonChunkSize";
067: public static final String BINARY_RANGE_SIZE = "BinaryRangeSize";
068: public static final String DEFAULT_ARCHIVE_ROOT = "DefaultArchiveRoot";
069: public static final String SERVER_DIR = "ServerDirectory";
070: public static final String FILESYS_DIR = "FileSysDirectory";
071: }
072:
073: private static ServerConfig instance = null;
074:
075: //Hard code this. Remember to update for each release.
076: private static String msServerVersion = "2.1.0.1";
077: private int miMaxCacheSize = -1;
078: private boolean mbEnableAnonymous = false;
079: private boolean mbEnableWeb = false;
080: private String msAnonymousUserName = "anon";
081: private String msWebInterfaceTitle = "SourceJammer";
082: private String msWebInterfaceInfo = "The free, open-source source control and versioning system.";
083: private String serverDirectory = "/usr/local/sourcejammer/server";
084: private String filesysDirectory = "/usr/local/sourcejammer/server/filesys";
085:
086: private int miComparisonChunkSize = 100;
087: private int miBinaryRangeSize = 1000;
088: private String msDefaultArchiveRoot = "";
089:
090: private synchronized void load(String sBasePath)
091: throws IOException, SAXException {
092:
093: File flConf = null;
094: if (sBasePath == null) {
095: flConf = new File(CONF_FILE);
096: } else {
097: flConf = new File(sBasePath, CONF_FILE);
098: }
099: System.out.println("sc - " + flConf.getAbsolutePath());
100: if (flConf.exists()) {
101: Document docConf = null;
102: try {
103: docConf = XMLUtil.getXMLDoc(flConf);
104: } catch (ParserConfigurationException ex) {
105: throw new SAXException(
106: "Parser configuration exception in reading server conf file.",
107: ex);
108: }
109: Element elmRoot = docConf.getDocumentElement();
110:
111: //Cache size
112: String sMaxCacheSize = XMLUtil.getValue(
113: ConfXMLNodes.MAX_CACHE_SIZE, elmRoot);
114: miMaxCacheSize = Integer.parseInt(sMaxCacheSize.trim());
115:
116: //Enable anon SOAP access
117: String sTF = XMLUtil.getValue(
118: ConfXMLNodes.ENABLE_ANONYMOUS_ACCESS, elmRoot);
119: if (sTF.startsWith("t") || sTF.startsWith("T")) {
120: mbEnableAnonymous = true;
121: }
122:
123: //Enable anon Web access
124: sTF = XMLUtil.getValue(ConfXMLNodes.ENABLE_WEB_ACCESS,
125: elmRoot);
126: if (sTF.startsWith("t") || sTF.startsWith("T")) {
127: mbEnableWeb = true;
128: }
129:
130: //Anon user name
131: msAnonymousUserName = XMLUtil.getValue(
132: ConfXMLNodes.ANONYMOUS_USER_NAME, elmRoot);
133:
134: //Compare chunk size
135: String sCompareSize = XMLUtil.getValue(
136: ConfXMLNodes.COMP_CHUNK_SIZE, elmRoot);
137: miComparisonChunkSize = Integer.parseInt(sCompareSize
138: .trim());
139:
140: //Range size
141: String sRangeSize = XMLUtil.getValue(
142: ConfXMLNodes.BINARY_RANGE_SIZE, elmRoot);
143: miBinaryRangeSize = Integer.parseInt(sRangeSize.trim());
144:
145: //Server directory -- inside WEB-INF if not defined.
146: serverDirectory = XMLUtil.getValue(ConfXMLNodes.SERVER_DIR,
147: elmRoot);
148: if (serverDirectory == null || serverDirectory.equals("")) {
149: java.io.File confDir = new java.io.File(
150: org.sourcejammer.util.AppConfig.getInstance()
151: .getConfigFilePath());
152: java.io.File webinfDir = confDir.getParentFile();
153: java.io.File flServerDir = new java.io.File(webinfDir,
154: "server");
155: if (!flServerDir.exists()) {
156: flServerDir.mkdir();
157: }
158: serverDirectory = flServerDir.getAbsolutePath();
159: }
160:
161: //Archives directory -- inside server directory if not defined.
162: msDefaultArchiveRoot = XMLUtil.getValue(
163: ConfXMLNodes.DEFAULT_ARCHIVE_ROOT, elmRoot);
164: if (msDefaultArchiveRoot == null
165: || msDefaultArchiveRoot.equals("")) {
166: java.io.File archivesDir = new java.io.File(
167: serverDirectory, "archives");
168: if (!archivesDir.exists()) {
169: archivesDir.mkdir();
170: }
171: msDefaultArchiveRoot = archivesDir.getAbsolutePath();
172: }
173:
174: //FileSys directory -- inside server directory if not defined.
175: filesysDirectory = XMLUtil.getValue(
176: ConfXMLNodes.FILESYS_DIR, elmRoot);
177: if (filesysDirectory == null || filesysDirectory.equals("")) {
178: java.io.File flFileSysDir = new java.io.File(
179: serverDirectory, "filesys");
180: if (!flFileSysDir.exists()) {
181: flFileSysDir.mkdir();
182: }
183: filesysDirectory = flFileSysDir.getAbsolutePath();
184: }
185:
186: }
187: }
188:
189: public static ServerConfig getInstance(String basePath) {
190: //assume that we need to reload when this is called.
191: try {
192: instance = new ServerConfig();
193: instance.load(basePath);
194: } catch (Exception ex) {
195: System.out.println("!!!Error in ServerCofig!!!");
196: ex.printStackTrace();
197: }
198: return instance;
199: }
200:
201: public static ServerConfig getInstance() {
202: if (instance == null) {
203: throw new ConfigurationException(
204: "ServerConfig has not been intialized.");
205: }
206: return instance;
207: }
208:
209: public int getComparisonChunckSize() {
210: return miComparisonChunkSize;
211: }
212:
213: public int getBinaryRangeSize() {
214: return miBinaryRangeSize;
215: }
216:
217: public String getDefaultArchiveRoot() {
218: return msDefaultArchiveRoot;
219: }
220:
221: public int getMaxCacheSize() {
222: return miMaxCacheSize;
223: }
224:
225: public boolean isEnableAnonymousAccess() {
226: return mbEnableAnonymous;
227: }
228:
229: public boolean isEnableWebAccess() {
230: return mbEnableWeb;
231: }
232:
233: public String getAnonymousUserName() {
234: return msAnonymousUserName;
235: }
236:
237: public static String getServerVersion() {
238: return msServerVersion;
239: }
240:
241: public String getWebInterfaceTitle() {
242: return msWebInterfaceTitle;
243: }
244:
245: public String getWebInterfaceInfo() {
246: return msWebInterfaceInfo;
247: }
248:
249: /**
250: * Returns the filesysDirectory.
251: * @return String
252: */
253: public String getFilesysDirectory() {
254: return filesysDirectory;
255: }
256:
257: /**
258: * Returns the serverDirectory.
259: * @return String
260: */
261: public String getServerDirectory() {
262: return serverDirectory;
263: }
264:
265: }
|