001: package org.tigris.scarab.om;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2005 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by CollabNet <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of CollabNet.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of CollabNet.
047: */
048:
049: import java.util.List;
050: import java.io.Serializable;
051:
052: import org.apache.log4j.Logger;
053: import org.apache.torque.om.Persistent;
054: import org.apache.torque.TorqueException;
055: import org.apache.torque.util.Criteria;
056: import org.apache.turbine.Turbine;
057: import org.tigris.scarab.tools.localization.L10NKeySet;
058: import org.tigris.scarab.tools.localization.L10NMessage;
059: import org.tigris.scarab.util.ScarabRuntimeException;
060:
061: /**
062: * This class manages GlobalParameter objects. Global is used a bit
063: * loosely here. Parameters can be module scoped as well. for example,
064: * the email parameters have a global set which is the default, if the
065: * module does not provide alternatives.
066: *
067: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
068: * @version $Id: GlobalParameterManager.java 9977 2005-12-09 00:40:59Z hair $
069: */
070: public class GlobalParameterManager extends BaseGlobalParameterManager {
071: private static final String MANAGER_KEY = DEFAULT_MANAGER_CLASS;
072: private static final String GET_STRING = "getString";
073: private static final String GET_BOOLEAN = "getBoolean";
074:
075: private static final Logger LOG = Logger
076: .getLogger("org.tigris.scarab");
077:
078: /**
079: * Creates a new <code>GlobalParameterManager</code> instance.
080: *
081: * @exception TorqueException if an error occurs
082: */
083: public GlobalParameterManager() throws TorqueException {
084: super ();
085: setRegion(getClassName().replace('.', '_'));
086: }
087:
088: protected Persistent putInstanceImpl(Persistent om)
089: throws TorqueException {
090: Persistent oldOm = super .putInstanceImpl(om);
091: //Serializable obj = (Serializable)om;
092: GlobalParameter gp = (GlobalParameter) om;
093: Serializable moduleId = gp.getModuleId();
094: String name = gp.getName();
095: if (moduleId == null) {
096: // if altering a global parameter, its possible the
097: // module overrides are invalid.
098: getMethodResult().removeAll(MANAGER_KEY, name);
099: getMethodResult().removeAll(MANAGER_KEY, name);
100: } else {
101: getMethodResult().remove(MANAGER_KEY, name, GET_BOOLEAN,
102: moduleId);
103: getMethodResult().remove(MANAGER_KEY, name, GET_STRING,
104: moduleId);
105: }
106: /*
107: DEBUGGING
108: if (oldOm == null)
109: {
110: System.out.println("first put of value " + name + " to " + gp.getValue());
111:
112: }
113: else
114: {
115: System.out.println("changing value of " + name + " from " + ((GlobalParameter)oldOm).getValue() + " to " + gp.getValue());
116:
117: }
118: */
119: return oldOm;
120: }
121:
122: private static GlobalParameter getInstance(String name)
123: throws TorqueException {
124: // try to get a global without a module
125: GlobalParameter p = getInstance(name, null);
126: if (p == null) {
127: // get a local new instance
128: p = getInstance();
129: p.setName(name);
130: }
131: return p;
132: }
133:
134: private static GlobalParameter getInstance(String name,
135: Module module) throws TorqueException {
136: GlobalParameter result = null;
137: Criteria crit = new Criteria();
138: crit.add(GlobalParameterPeer.NAME, name);
139: if (module == null) {
140: crit.add(GlobalParameterPeer.MODULE_ID, null);
141: } else {
142: crit.add(GlobalParameterPeer.MODULE_ID, module
143: .getModuleId());
144: }
145: List parameters = GlobalParameterPeer.doSelect(crit);
146: if (!parameters.isEmpty()) {
147: result = (GlobalParameter) parameters.get(0);
148: }
149: return result;
150: }
151:
152: public static String getString(String key) throws TorqueException {
153: // we do not call getString(name, null) here because we do
154: // not want to cache results for every module if the parameter
155: // is global.
156: String result = null;
157: // reversing order because we want to be able to invalidate based
158: // on the parameter name, not the method name.
159: Object obj = getMethodResult()
160: .get(MANAGER_KEY, key, GET_STRING);
161: if (obj == null) {
162: result = getInstance(key).getValue();
163: if (result == null) {
164: result = Turbine.getConfiguration().getString(key);
165: if (result == null || result.trim().length() == 0) {
166: result = "";
167: }
168: }
169: if (!result.equals("")) {
170: getMethodResult().put(result, MANAGER_KEY, key,
171: GET_STRING);
172: }
173: } else {
174: result = (String) obj;
175: }
176: return result;
177: }
178:
179: public static String getString(String name, Module module)
180: throws TorqueException {
181: String result = null;
182: if (module == null) {
183: result = getString(name);
184: } else {
185: Object obj = getMethodResult().get(MANAGER_KEY, name,
186: GET_STRING, module);
187: if (obj == null) {
188: GlobalParameter p = getInstance(name, module);
189: if (p == null) {
190: // use global default
191: result = getString(name);
192: } else {
193: result = p.getValue();
194: getMethodResult().put(result, MANAGER_KEY, name,
195: GET_STRING, module);
196: }
197: } else {
198: result = (String) obj;
199: }
200: }
201: return result;
202: }
203:
204: public static void setString(String name, String value)
205: throws TorqueException {
206: GlobalParameter p = getInstance(name);
207: p.setValue(value);
208: p.save();
209: }
210:
211: public static void setString(String name, Module module,
212: String value) throws TorqueException {
213: if (module == null) {
214: setString(name, value);
215: } else {
216: GlobalParameter p = getInstance(name, module);
217: if (p == null) {
218: p = getInstance(name).copy();
219: p.setModuleId(module.getModuleId());
220: }
221: p.setValue(value);
222: p.save();
223:
224: getMethodResult().put(value, MANAGER_KEY, name, GET_STRING,
225: module);
226: }
227: }
228:
229: public static boolean getBoolean(String name)
230: throws TorqueException {
231: // we do not call getBoolean(name, null) here because we do
232: // not want to cache results for every module if the parameter
233: // is global.
234: Boolean result = null;
235: Object obj = getMethodResult().get(MANAGER_KEY, name,
236: GET_BOOLEAN);
237: if (obj == null) {
238: result = ("T".equals(getInstance(name).getValue())) ? Boolean.TRUE
239: : Boolean.FALSE;
240: getMethodResult().put(result, MANAGER_KEY, name,
241: GET_BOOLEAN);
242: } else {
243: result = (Boolean) obj;
244: }
245: return result.booleanValue();
246: }
247:
248: public static boolean getBoolean(String name, Module module)
249: throws TorqueException {
250: boolean b = false;
251: if (module == null) {
252: b = getBoolean(name);
253: } else {
254: Object obj = getMethodResult().get(MANAGER_KEY, name,
255: GET_BOOLEAN, module);
256: if (obj == null) {
257: GlobalParameter p = getInstance(name, module);
258: if (p == null) {
259: // use global default
260: b = getBoolean(name);
261: } else {
262: b = "T".equals(p.getValue());
263: getMethodResult().put(
264: (b ? Boolean.TRUE : Boolean.FALSE),
265: MANAGER_KEY, name, GET_BOOLEAN, module);
266: }
267: } else {
268: b = ((Boolean) obj).booleanValue();
269: }
270: }
271: return b;
272: }
273:
274: /**
275: * Recursively look up for the existence of the key.
276: * Further details, @see #getBooleanFromHierarchy(String key, Module module, boolean def)
277: *
278: * If no value was not found, return "def" instead.
279: *
280: * @param key
281: * @param module
282: * @param def
283: * @return
284: */
285: public static boolean getBooleanFromHierarchy(String key,
286: Module module, boolean def) {
287: String defAsString = (def) ? "T" : "F";
288: String bp = getStringFromHierarchy(key, module, defAsString);
289:
290: // bp is "[T|F] when it comes from the database,
291: // or [true|false] when it comes from Turbine
292: boolean result = (bp.equals("T") || bp.equals("true")) ? true
293: : false;
294:
295: return result;
296: }
297:
298: /**
299: * Recursively look up for the existence of the key in the
300: * module hierarchy. Backtrack towards the module root.
301: * If no value was found, check for the existence of a
302: * module-independent global parameter.
303: * If still no value found, check for the Turbine
304: * configuration property with the same key.
305: * If still no definition found, return the parameter
306: * "def" instead.
307: *
308: * @param key
309: * @param module
310: * @param def
311: * @return
312: */
313: public static String getStringFromHierarchy(String key,
314: Module module, String def) {
315: String result = null;
316: Module me = module;
317: try {
318: do {
319: Object obj = getMethodResult().get(MANAGER_KEY, key,
320: GET_STRING, me);
321: if (obj == null) {
322: GlobalParameter p = getInstance(key, me);
323: if (p != null) {
324: result = p.getValue();
325: getMethodResult().put(result, MANAGER_KEY, key,
326: GET_STRING, me);
327: }
328: } else {
329: result = (String) obj;
330: }
331: if (me == null) {
332: /* it doesn't make any sense to process here any further */
333: break;
334: }
335: Module parent = me.getParent();
336: if (parent == me) {
337: break;
338: }
339: me = parent;
340: } while (result == null || result.equals(""));
341:
342: if (result == null || result.equals("")) {
343: // here try to retrieve the module independent parameter,
344: // or as last resort get it from the Turbine config.
345: result = getString(key);
346:
347: // ok, give up and use the hard coded default value.
348: if (result == null || result.equals("")) {
349: result = def;
350: }
351: }
352:
353: } catch (Exception e) {
354: LOG
355: .warn("Internal error while retrieving data from GLOBAL_PRAMETER_TABLE: ["
356: + e.getMessage() + "]");
357: L10NMessage msg = new L10NMessage(
358: L10NKeySet.ExceptionTorqueGeneric, e.getMessage());
359: throw new ScarabRuntimeException(msg);
360: }
361:
362: return result;
363: }
364:
365: public static void setBoolean(String name, boolean value)
366: throws TorqueException {
367: setString(name, (value ? "T" : "F"));
368: }
369:
370: public static void setBoolean(String name, Module module,
371: boolean value) throws TorqueException {
372: if (module == null) {
373: setBoolean(name, value);
374: } else {
375: GlobalParameter p = getInstance(name, module);
376: if (p == null) {
377: p = getInstance(name).copy();
378: p.setModuleId(module.getModuleId());
379: }
380: String booleanString = (value) ? "T" : "F";
381: p.setValue(booleanString);
382: p.save();
383:
384: Boolean bool = new Boolean(value);
385: getMethodResult().put(bool, MANAGER_KEY, name, GET_BOOLEAN,
386: module);
387: }
388: }
389: }
|