001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.dav.server.webservice;
020:
021: import java.rmi.*;
022: import java.util.*;
023: import java.util.logging.*;
024:
025: import org.openharmonise.rm.config.*;
026:
027: /**
028: * This class provides configuration operations for config properties.
029: *
030: * @author Michael Bell
031: * @version $Revision: 1.1 $
032: *
033: */
034: public class ConfigSettingsService {
035:
036: /**
037: * Logger for this class.
038: */
039: private static final Logger m_logger = Logger
040: .getLogger(ConfigSettingsService.class.getName());
041:
042: /**
043: * Construct a new <code>ConfigSettingsService</code>.
044: */
045: public ConfigSettingsService() {
046: super ();
047: }
048:
049: /**
050: * Return an array of all config properties.
051: *
052: * @param username the user name
053: * @param password the password
054: * @return an array of all config properties
055: * @throws RemoteException
056: */
057: public static ConfigProperty[] getAllProperties(String username,
058: String password) throws RemoteException {
059: ConfigProperty[] props = null;
060:
061: try {
062: authenticateSuperUser(username, password);
063:
064: List propNames = ConfigSettings.getPropertyNameArray();
065:
066: props = new ConfigProperty[propNames.size()];
067:
068: for (int i = 0; i < propNames.size(); i++) {
069: String sName = (String) propNames.get(i);
070: String sVal = ConfigSettings.getProperty(sName);
071: props[i] = new ConfigProperty(sName, sVal);
072: System.out.println(props[i]);
073: }
074:
075: System.out.println(props.toString());
076:
077: } catch (ConfigException e) {
078: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
079: }
080:
081: return props;
082: }
083:
084: /**
085: * Get the specified properties.
086: *
087: * @param username the user name
088: * @param password the user password
089: * @param propNames the names of the properties to return
090: * @return an array of config properties
091: * @throws RemoteException
092: */
093: public static ConfigProperty[] getProperties(String username,
094: String password, String[] propNames) throws RemoteException {
095: ConfigProperty[] props = null;
096:
097: try {
098: authenticateSuperUser(username, password);
099:
100: props = new ConfigProperty[propNames.length];
101:
102: for (int i = 0; i < propNames.length; i++) {
103: String sName = (String) propNames[i];
104: String sVal = ConfigSettings.getProperty(sName);
105: props[i] = new ConfigProperty(sName, sVal);
106: System.out.println(props[i]);
107: }
108:
109: System.out.println(props.toString());
110:
111: } catch (ConfigException e) {
112: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
113: }
114:
115: return props;
116: }
117:
118: /**
119: * Set the values of the given config properties.
120: *
121: * @param username the user name
122: * @param password the user password
123: * @param props the properties to set
124: * @throws ConfigException
125: * @throws RemoteException
126: */
127: public static void setProperties(String username, String password,
128: ConfigProperty[] props) throws ConfigException,
129: RemoteException {
130:
131: authenticateSuperUser(username, password);
132:
133: for (int i = 0; i < props.length; i++) {
134:
135: try {
136: ConfigSettings.setProperty(props[i].getName(), props[i]
137: .getValue());
138: } catch (ConfigException e) {
139: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
140: }
141: }
142:
143: }
144:
145: /**
146: * Add the given properties.
147: *
148: * @param username the user name
149: * @param password the user password
150: * @param props the properties to add
151: * @throws ConfigException
152: * @throws RemoteException
153: */
154: public static void addProperties(String username, String password,
155: ConfigProperty[] props) throws ConfigException,
156: RemoteException {
157:
158: authenticateSuperUser(username, password);
159:
160: for (int i = 0; i < props.length; i++) {
161:
162: try {
163: ConfigSettings.insertProperty(props[i].getName(),
164: props[i].getValue());
165: } catch (ConfigException e) {
166: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
167: }
168:
169: }
170:
171: }
172:
173: /**
174: * Removes the specified configuration properties from
175: * this Harmonise installation.
176: *
177: * @param username the user name
178: * @param password the user password
179: * @param propNames the properties to remove
180: * @throws ConfigException
181: * @throws RemoteException
182: */
183: public static void removeProperties(String username,
184: String password, String[] propNames)
185: throws ConfigException, RemoteException {
186:
187: authenticateSuperUser(username, password);
188:
189: for (int i = 0; i < propNames.length; i++) {
190:
191: try {
192: ConfigSettings.removeProperty(propNames[i]);
193: } catch (ConfigException e) {
194: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
195: }
196:
197: }
198:
199: }
200:
201: /**
202: * Authenticate that the user with the specified user name and
203: * password is a super user.
204: *
205: * @param username the user name
206: * @param password the user password
207: * @throws ConfigException
208: * @throws RemoteException
209: */
210: static private void authenticateSuperUser(String username,
211: String password) throws ConfigException, RemoteException {
212: if (UserConfigService.isSuperUser(username, password) == false) {
213: throw new ConfigException(
214: "Forbidden: User is not super user");
215: }
216: }
217: }
|