001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.gast.lib;
025:
026: import java.io.FileNotFoundException;
027: import java.io.IOException;
028: import jeeves.constants.ConfigFile;
029: import org.fao.geonet.constants.Geonet;
030: import org.jdom.Document;
031: import org.jdom.Element;
032: import org.jdom.JDOMException;
033:
034: //=============================================================================
035:
036: public class ConfigLib {
037: //---------------------------------------------------------------------------
038: //---
039: //--- Constructor
040: //---
041: //---------------------------------------------------------------------------
042:
043: public ConfigLib(String appPath) throws JDOMException, IOException {
044: this .appPath = appPath;
045:
046: config = Lib.xml.load(appPath
047: + "/web/geonetwork/WEB-INF/config.xml");
048: dbmsElem = retrieveDbms(config);
049: appHandElem = config.getRootElement().getChild(
050: ConfigFile.Child.APP_HANDLER);
051: }
052:
053: //---------------------------------------------------------------------------
054: //---
055: //--- API methods
056: //---
057: //---------------------------------------------------------------------------
058:
059: public String getDbmsURL() {
060: return dbmsElem.getChild(ConfigFile.Resource.Child.CONFIG)
061: .getChildText("url");
062: }
063:
064: //---------------------------------------------------------------------------
065:
066: public String getDbmsDriver() {
067: return dbmsElem.getChild(ConfigFile.Resource.Child.CONFIG)
068: .getChildText("driver");
069: }
070:
071: //---------------------------------------------------------------------------
072:
073: public String getDbmsUser() {
074: return dbmsElem.getChild(ConfigFile.Resource.Child.CONFIG)
075: .getChildText("user");
076: }
077:
078: //---------------------------------------------------------------------------
079:
080: public String getDbmsPassword() {
081: return dbmsElem.getChild(ConfigFile.Resource.Child.CONFIG)
082: .getChildText("password");
083: }
084:
085: //---------------------------------------------------------------------------
086:
087: public String getLuceneDir() {
088: return findInHandler(Geonet.Config.LUCENE_DIR);
089: }
090:
091: //---------------------------------------------------------------------------
092:
093: public String getHandlerProp(String name) {
094: return findInHandler(name);
095: }
096:
097: //---------------------------------------------------------------------------
098: //--- Dbms setters
099: //---------------------------------------------------------------------------
100:
101: public void setDbmsURL(String url) {
102: dbmsElem.getChild(ConfigFile.Resource.Child.CONFIG).getChild(
103: "url").setText(url);
104: }
105:
106: //---------------------------------------------------------------------------
107:
108: public void setDbmsDriver(String driver) {
109: dbmsElem.getChild(ConfigFile.Resource.Child.CONFIG).getChild(
110: "driver").setText(driver);
111: }
112:
113: //---------------------------------------------------------------------------
114:
115: public void setDbmsUser(String user) {
116: dbmsElem.getChild(ConfigFile.Resource.Child.CONFIG).getChild(
117: "user").setText(user);
118: }
119:
120: //---------------------------------------------------------------------------
121:
122: public void setDbmsPassword(String password) {
123: dbmsElem.getChild(ConfigFile.Resource.Child.CONFIG).getChild(
124: "password").setText(password);
125: }
126:
127: //---------------------------------------------------------------------------
128: //--- Activator
129: //---------------------------------------------------------------------------
130:
131: public void addActivator() {
132: removeActivator();
133:
134: Element activ = new Element(ConfigFile.Resource.Child.ACTIVATOR);
135: activ.setAttribute("class",
136: "org.fao.geonet.activators.McKoiActivator");
137:
138: Element config = new Element("configFile");
139: config.setText("WEB-INF/db/db.conf");
140:
141: activ.addContent(config);
142: dbmsElem.addContent(activ);
143: }
144:
145: //---------------------------------------------------------------------------
146:
147: public void removeActivator() {
148: dbmsElem.removeChild(ConfigFile.Resource.Child.ACTIVATOR);
149: }
150:
151: //---------------------------------------------------------------------------
152: //--- Other
153: //---------------------------------------------------------------------------
154:
155: public void save() throws FileNotFoundException, IOException {
156: Lib.xml.save(appPath + "/web/geonetwork/WEB-INF/config.xml",
157: config);
158: }
159:
160: //---------------------------------------------------------------------------
161:
162: public Resource createResource() throws Exception {
163: return new Resource(appPath + "/web/geonetwork/", dbmsElem);
164: }
165:
166: //---------------------------------------------------------------------------
167: //---
168: //--- Private methods
169: //---
170: //---------------------------------------------------------------------------
171:
172: private Element retrieveDbms(Document config) {
173: Element resources = config.getRootElement().getChild(
174: ConfigFile.Child.RESOURCES);
175:
176: for (Object res : resources
177: .getChildren(ConfigFile.Resources.Child.RESOURCE)) {
178: Element resource = (Element) res;
179: String enabled = resource.getAttributeValue("enabled");
180:
181: if ("true".equals(enabled))
182: return resource;
183: }
184:
185: //--- we should not arrive here
186:
187: return null;
188: }
189:
190: //---------------------------------------------------------------------------
191:
192: private String findInHandler(String paramName) {
193: for (Object o : appHandElem.getChildren("param")) {
194: Element param = (Element) o;
195:
196: String name = param.getAttributeValue("name");
197: String value = param.getAttributeValue("value");
198:
199: if (paramName.equals(name))
200: return value;
201: }
202:
203: return null;
204: }
205:
206: //---------------------------------------------------------------------------
207: //---
208: //--- Variables
209: //---
210: //---------------------------------------------------------------------------
211:
212: private String appPath;
213: private Document config;
214: private Element dbmsElem;
215: private Element appHandElem;
216: }
217:
218: //=============================================================================
|