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.geonet.services.config;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028: import java.util.StringTokenizer;
029: import jeeves.constants.Jeeves;
030: import jeeves.exceptions.OperationAbortedEx;
031: import jeeves.interfaces.Service;
032: import jeeves.resources.dbms.Dbms;
033: import jeeves.server.ServiceConfig;
034: import jeeves.server.context.ServiceContext;
035: import org.fao.geonet.GeonetContext;
036: import org.fao.geonet.constants.Geonet;
037: import org.fao.geonet.kernel.setting.SettingManager;
038: import org.jdom.Element;
039: import jeeves.exceptions.BadParameterEx;
040: import jeeves.exceptions.BadInputEx;
041: import org.fao.geonet.lib.Lib;
042:
043: //=============================================================================
044:
045: public class Set implements Service {
046: //--------------------------------------------------------------------------
047: //---
048: //--- Init
049: //---
050: //--------------------------------------------------------------------------
051:
052: public void init(String appPath, ServiceConfig params)
053: throws Exception {
054: }
055:
056: //--------------------------------------------------------------------------
057: //---
058: //--- Service
059: //---
060: //--------------------------------------------------------------------------
061:
062: public Element exec(Element params, ServiceContext context)
063: throws Exception {
064: GeonetContext gc = (GeonetContext) context
065: .getHandlerContext(Geonet.CONTEXT_NAME);
066: SettingManager sm = gc.getSettingManager();
067:
068: Dbms dbms = (Dbms) context.getResourceManager().open(
069: Geonet.Res.MAIN_DB);
070:
071: Map<String, Object> values = new HashMap<String, Object>();
072:
073: for (ConfigEntry ce : entries)
074: ce.eval(values, params);
075:
076: if (!sm.setValues(dbms, values))
077: throw new OperationAbortedEx("Cannot set all values");
078:
079: return new Element(Jeeves.Elem.RESPONSE).setText("ok");
080: }
081:
082: //--------------------------------------------------------------------------
083: //---
084: //--- Vars
085: //---
086: //--------------------------------------------------------------------------
087:
088: private ConfigEntry entries[] = {
089: new ConfigEntry(ConfigEntry.Type.STRING, true, "site/name",
090: "system/site/name"),
091: new ConfigEntry(ConfigEntry.Type.STRING, false,
092: "site/organization", "system/site/organization"),
093:
094: new ConfigEntry(ConfigEntry.Type.STRING, true,
095: "server/host", "system/server/host"),
096: new ConfigEntry(ConfigEntry.Type.INT, false, "server/port",
097: "system/server/port"),
098:
099: new ConfigEntry(ConfigEntry.Type.STRING, true,
100: "intranet/network", "system/intranet/network"),
101: new ConfigEntry(ConfigEntry.Type.STRING, true,
102: "intranet/netmask", "system/intranet/netmask"),
103:
104: new ConfigEntry(ConfigEntry.Type.BOOL, true,
105: "z3950/enable", "system/z3950/enable"),
106: new ConfigEntry(ConfigEntry.Type.INT, false, "z3950/port",
107: "system/z3950/port"),
108:
109: new ConfigEntry(ConfigEntry.Type.BOOL, true, "proxy/use",
110: "system/proxy/use"),
111: new ConfigEntry(ConfigEntry.Type.STRING, false,
112: "proxy/host", "system/proxy/host"),
113: new ConfigEntry(ConfigEntry.Type.INT, false, "proxy/port",
114: "system/proxy/port"),
115: new ConfigEntry(ConfigEntry.Type.STRING, false,
116: "proxy/username", "system/proxy/username"),
117: new ConfigEntry(ConfigEntry.Type.STRING, false,
118: "proxy/password", "system/proxy/password"),
119:
120: new ConfigEntry(ConfigEntry.Type.STRING, false,
121: "feedback/email", "system/feedback/email"),
122: new ConfigEntry(ConfigEntry.Type.STRING, false,
123: "feedback/mailServer/host",
124: "system/feedback/mailServer/host"),
125: new ConfigEntry(ConfigEntry.Type.INT, false,
126: "feedback/mailServer/port",
127: "system/feedback/mailServer/port"),
128:
129: new ConfigEntry(ConfigEntry.Type.STRING, true,
130: "removedMetadata/dir", "system/removedMetadata/dir"),
131:
132: new ConfigEntry(ConfigEntry.Type.BOOL, true, "ldap/use",
133: "system/ldap/use"),
134: new ConfigEntry(ConfigEntry.Type.STRING, false,
135: "ldap/host", "system/ldap/host"),
136: new ConfigEntry(ConfigEntry.Type.INT, false, "ldap/port",
137: "system/ldap/port"),
138: new ConfigEntry(ConfigEntry.Type.STRING, true,
139: "ldap/defaultProfile", "system/ldap/defaultProfile"),
140: new ConfigEntry(ConfigEntry.Type.STRING, true,
141: "ldap/distinguishedNames/base",
142: "system/ldap/distinguishedNames/base"),
143: new ConfigEntry(ConfigEntry.Type.STRING, true,
144: "ldap/distinguishedNames/users",
145: "system/ldap/distinguishedNames/users"),
146: new ConfigEntry(ConfigEntry.Type.STRING, true,
147: "ldap/userAttribs/name",
148: "system/ldap/userAttribs/name"),
149: new ConfigEntry(ConfigEntry.Type.STRING, true,
150: "ldap/userAttribs/password",
151: "system/ldap/userAttribs/password"),
152: new ConfigEntry(ConfigEntry.Type.STRING, false,
153: "ldap/userAttribs/profile",
154: "system/ldap/userAttribs/profile") };
155: }
156:
157: //=============================================================================
158:
159: class ConfigEntry {
160: /** @param mandatory Means that the value, if the element is present, cannot be empty */
161: public ConfigEntry(Type type, boolean mandatory, String srcPath,
162: String desPath) {
163: this .srcPath = srcPath;
164: this .desPath = desPath;
165: this .type = type;
166: this .mandatory = mandatory;
167: }
168:
169: //--------------------------------------------------------------------------
170: //---
171: //--- API methods
172: //---
173: //--------------------------------------------------------------------------
174:
175: public void eval(Map<String, Object> values, Element elem)
176: throws BadInputEx {
177: String value = Lib.element.eval(elem, srcPath);
178:
179: if (value == null)
180: return;
181:
182: //--- ok, the element is present
183:
184: if (mandatory) {
185: if (value.length() == 0)
186: throw new BadParameterEx("srcPath", value);
187:
188: checkValue(value);
189: } else {
190: if (value.length() != 0)
191: checkValue(value);
192: }
193:
194: values.put(desPath, value);
195: }
196:
197: //--------------------------------------------------------------------------
198: //---
199: //--- Private methods
200: //---
201: //--------------------------------------------------------------------------
202:
203: private void checkValue(String value) throws BadInputEx {
204: if (type == Type.INT && !Lib.type.isInteger(value))
205: throw new BadParameterEx("srcPath", value);
206:
207: else if (type == Type.BOOL && !Lib.type.isBoolean(value))
208: throw new BadParameterEx("srcPath", value);
209: }
210:
211: //--------------------------------------------------------------------------
212: //---
213: //--- Variables
214: //---
215: //--------------------------------------------------------------------------
216:
217: enum Type {
218: STRING, INT, BOOL
219: }
220:
221: //--------------------------------------------------------------------------
222:
223: private String srcPath;
224: private String desPath;
225: private Type type;
226: private boolean mandatory;
227: }
228:
229: //=============================================================================
|