001: package com.ice.jcvsweb.manager;
002:
003: import java.io.BufferedReader;
004: import java.io.File;
005: import java.io.FileReader;
006: import java.io.FileWriter;
007: import java.io.IOException;
008: import java.io.PrintWriter;
009: import java.util.ArrayList;
010: import java.util.HashMap;
011: import java.util.Properties;
012:
013: import javax.servlet.ServletContext;
014:
015: import com.ice.cvsc.CVSClient;
016: import com.ice.cvsc.CVSProject;
017: import com.ice.cvsc.CVSProjectDef;
018: import com.ice.cvsc.CVSEntry;
019:
020: import org.w3c.dom.Document;
021: import org.w3c.dom.Element;
022: import org.w3c.dom.Node;
023: import org.w3c.dom.NodeList;
024: import org.xml.sax.SAXException;
025:
026: import com.ice.jcvsweb.bean.*;
027: import com.ice.jcvsweb.helper.ContextHelper;
028: import com.ice.jcvsweb.helper.XMLHelper;
029:
030: /**
031: * This class Manages our Configuration.
032: *
033: * @author Tim Endres, <a href="mailto:time@jcvs.org">time@jcvs.org</a>
034: * @see com.ice.cvsc
035: */
036:
037: public class JCVSConfigManager {
038: public static final String ATTR_NAME = "jcvsConfigManager";
039:
040: private JCVSConfiguration config = null;
041: private File configLocFile = null;
042:
043: public JCVSConfigManager(ServletContext ctx) {
044: this .config = new JCVSConfiguration();
045:
046: try {
047: this .establishConfigDirectory(ctx);
048: this .loadConfiguration(this .getConfigFile(ctx));
049: } catch (Exception ex) {
050: // UNDONE I think we need to kill the servlet...?
051: ctx.log("loading JCVS Configuration", ex);
052: }
053: }
054:
055: public File getConfigFile(ServletContext ctx) {
056: String configPath = ContextHelper.getRealAbsolutePath(ctx,
057: this .config.getConfigDir());
058:
059: return new File(configPath, "jcvs-config.xml");
060: }
061:
062: public JCVSConfiguration getConfiguration() {
063: return this .config;
064: }
065:
066: private void loadConfiguration(File configFile) throws IOException {
067: JCVSConfiguration result = null;
068:
069: try {
070: Document xmlDoc = null;
071:
072: if (configFile != null && configFile.exists()) {
073: xmlDoc = XMLHelper.readXMLConfiguration(configFile);
074: }
075:
076: if (xmlDoc != null) {
077: Node configNode = XMLHelper.selectNode(xmlDoc
078: .getDocumentElement(), "/jcvs-configuration");
079:
080: this .config.loadConfiguration(configNode);
081: }
082: } catch (SAXException ex) {
083: throw new IOException(
084: "SAXException Loading Configuration: "
085: + ex.getMessage());
086: }
087: }
088:
089: public void saveConfiguration(ServletContext ctx)
090: throws IOException {
091: FileWriter fW = null;
092: PrintWriter pW = null;
093: try {
094: fW = new FileWriter(this .getConfigFile(ctx));
095: pW = new PrintWriter(fW);
096:
097: pW.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
098: pW.println("<jcvs-configuration>");
099: pW.println("");
100:
101: this .config.saveConfiguration(pW, "\t");
102:
103: pW.println("");
104: pW.println("</jcvs-configuration>");
105: } finally {
106: if (pW != null)
107: pW.close();
108: }
109: }
110:
111: private File getConfigLocationFile(ServletContext ctx) {
112: String webInfConfPath = ContextHelper.getRealAbsolutePath(ctx,
113: "WEB-INF/conf/");
114:
115: return new File(webInfConfPath, "confdir.txt");
116: }
117:
118: public void establishConfigDirectory(ServletContext ctx) {
119: String result = null;
120: BufferedReader rdr = null;
121: File configLocF = this .getConfigLocationFile(ctx);
122:
123: try {
124: FileReader fRdr = new FileReader(configLocF);
125: rdr = new BufferedReader(fRdr);
126: result = rdr.readLine();
127: } catch (IOException ex) {
128: ctx
129: .log("Error reading config directory from '"
130: + configLocF.getPath() + "' "
131: + ex.getMessage(), ex);
132: } finally {
133: if (rdr != null)
134: try {
135: rdr.close();
136: } catch (IOException ex) {
137: }
138: }
139:
140: if (result == null) {
141: result = "WEB-INF/conf/";
142: }
143:
144: this .config.setConfigDir(result);
145: }
146:
147: public void setConfigLocation(ServletContext ctx,
148: String configLocPath) throws IOException {
149: String result = null;
150: PrintWriter pW = null;
151: File configLocF = this .getConfigLocationFile(ctx);
152:
153: if (configLocPath == null) {
154: configLocPath = "WEB-INF/conf/";
155: }
156:
157: try {
158: FileWriter fW = new FileWriter(configLocF);
159: pW = new PrintWriter(fW);
160: pW.println(configLocPath);
161:
162: this .config.setConfigDir(configLocPath);
163:
164: // If the config file does not exist in the new location, we
165: // need to save the current configuration to that location...
166: //
167: File configF = this.getConfigFile(ctx);
168: if (!configF.exists()) {
169: this.saveConfiguration(ctx);
170: }
171:
172: this.loadConfiguration(this.getConfigFile(ctx));
173: } finally {
174: if (pW != null) {
175: pW.close();
176: }
177: }
178: }
179:
180: }
|