KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > om > GlobalParameterManager


1 package org.tigris.scarab.om;
2
3 /* ================================================================
4  * Copyright (c) 2000-2003 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by CollabNet <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of CollabNet.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of CollabNet.
47  */

48
49 import java.util.List JavaDoc;
50 import java.io.Serializable JavaDoc;
51
52 import org.apache.log4j.Logger;
53 import org.apache.torque.om.Persistent;
54 import org.apache.torque.TorqueException;
55 import org.apache.torque.util.Criteria;
56 import org.apache.turbine.Turbine;
57 import org.tigris.scarab.tools.localization.L10NKeySet;
58 import org.tigris.scarab.tools.localization.L10NMessage;
59 import org.tigris.scarab.util.ScarabRuntimeException;
60
61
62 /**
63  * This class manages GlobalParameter objects. Global is used a bit
64  * loosely here. Parameters can be module scoped as well. for example,
65  * the email parameters have a global set which is the default, if the
66  * module does not provide alternatives.
67  *
68  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
69  * @version $Id: GlobalParameterManager.java 9516 2005-03-25 01:12:25Z dabbous $
70  */

71 public class GlobalParameterManager
72     extends BaseGlobalParameterManager
73 {
74     private static final String JavaDoc MANAGER_KEY = DEFAULT_MANAGER_CLASS;
75     private static final String JavaDoc GET_STRING = "getString";
76     private static final String JavaDoc GET_BOOLEAN = "getBoolean";
77
78     private static final Logger LOG = Logger.getLogger("org.tigris.scarab");
79
80     /**
81      * Creates a new <code>GlobalParameterManager</code> instance.
82      *
83      * @exception TorqueException if an error occurs
84      */

85     public GlobalParameterManager()
86         throws TorqueException
87     {
88         super();
89         setRegion(getClassName().replace('.', '_'));
90     }
91
92     protected Persistent putInstanceImpl(Persistent om)
93         throws TorqueException
94     {
95         Persistent oldOm = super.putInstanceImpl(om);
96         //Serializable obj = (Serializable)om;
97
GlobalParameter gp = (GlobalParameter)om;
98         Serializable JavaDoc moduleId = gp.getModuleId();
99         String JavaDoc name = gp.getName();
100         if (moduleId == null)
101         {
102             // if altering a global parameter, its possible the
103
// module overrides are invalid.
104
getMethodResult().removeAll(MANAGER_KEY, name);
105             getMethodResult().removeAll(MANAGER_KEY, name);
106         }
107         else
108         {
109             getMethodResult().remove(MANAGER_KEY, name, GET_BOOLEAN,
110                                      moduleId);
111             getMethodResult().remove(MANAGER_KEY, name, GET_STRING,
112                                      moduleId);
113         }
114 /*
115     DEBUGGING
116         if (oldOm == null)
117         {
118         System.out.println("first put of value " + name + " to " + gp.getValue());
119             
120         }
121         else
122         {
123         System.out.println("changing value of " + name + " from " + ((GlobalParameter)oldOm).getValue() + " to " + gp.getValue());
124             
125         }
126 */

127         return oldOm;
128     }
129
130     private static GlobalParameter getInstance(String JavaDoc name)
131         throws TorqueException
132     {
133         // try to get a global without a module
134
GlobalParameter p = getInstance(name, null);
135         if (p == null)
136         {
137             // get a local new instance
138
p = getInstance();
139             p.setName(name);
140         }
141         return p;
142     }
143
144     private static GlobalParameter getInstance(String JavaDoc name, Module module)
145         throws TorqueException
146     {
147         GlobalParameter result = null;
148         Criteria crit = new Criteria();
149         crit.add(GlobalParameterPeer.NAME, name);
150         if (module == null)
151         {
152             crit.add(GlobalParameterPeer.MODULE_ID, null);
153         }
154         else
155         {
156             crit.add(GlobalParameterPeer.MODULE_ID, module.getModuleId());
157         }
158         List JavaDoc parameters = GlobalParameterPeer.doSelect(crit);
159         if (!parameters.isEmpty())
160         {
161             result = (GlobalParameter)parameters.get(0);
162         }
163         return result;
164     }
165
166     public static String JavaDoc getString(String JavaDoc key)
167         throws TorqueException
168     {
169         // we do not call getString(name, null) here because we do
170
// not want to cache results for every module if the parameter
171
// is global.
172
String JavaDoc result = null;
173         // reversing order because we want to be able to invalidate based
174
// on the parameter name, not the method name.
175
Object JavaDoc obj = getMethodResult().get(MANAGER_KEY, key, GET_STRING);
176         if (obj == null)
177         {
178             result = getInstance(key).getValue();
179             if (result == null)
180             {
181                 result = Turbine.getConfiguration().getString(key);
182                 if (result == null || result.trim().length() == 0)
183                 {
184                     result = "";
185                 }
186             }
187             if(!result.equals(""))
188             {
189                 getMethodResult().put(result, MANAGER_KEY, key, GET_STRING);
190             }
191         }
192         else
193         {
194             result = (String JavaDoc)obj;
195         }
196         return result;
197     }
198
199     public static String JavaDoc getString(String JavaDoc name, Module module)
200         throws TorqueException
201     {
202         String JavaDoc result = null;
203         if (module == null)
204         {
205             result = getString(name);
206         }
207         else
208         {
209             Object JavaDoc obj = getMethodResult()
210                 .get(MANAGER_KEY, name, GET_STRING, module);
211             if (obj == null)
212             {
213                 GlobalParameter p = getInstance(name, module);
214                 if (p == null)
215                 {
216                     // use global default
217
result = getString(name);
218                 }
219                 else
220                 {
221                     result = p.getValue();
222                     getMethodResult()
223                         .put(result, MANAGER_KEY, name, GET_STRING, module);
224                 }
225             }
226             else
227             {
228                 result = (String JavaDoc)obj;
229             }
230         }
231         return result;
232     }
233
234     public static void setString(String JavaDoc name, String JavaDoc value)
235         throws Exception JavaDoc
236     {
237         GlobalParameter p = getInstance(name);
238         p.setValue(value);
239         p.save();
240     }
241
242     public static void setString(String JavaDoc name, Module module, String JavaDoc value)
243         throws Exception JavaDoc
244     {
245         if (module == null)
246         {
247             setString(name, value);
248         }
249         else
250         {
251             GlobalParameter p = getInstance(name, module);
252             if (p == null)
253             {
254                 p = getInstance(name).copy();
255                 p.setModuleId(module.getModuleId());
256             }
257             p.setValue(value);
258             p.save();
259
260             getMethodResult().put(value, MANAGER_KEY, name, GET_STRING, module);
261         }
262     }
263
264     public static boolean getBoolean(String JavaDoc name)
265         throws TorqueException
266     {
267         // we do not call getBoolean(name, null) here because we do
268
// not want to cache results for every module if the parameter
269
// is global.
270
Boolean JavaDoc result = null;
271         Object JavaDoc obj = getMethodResult().get(MANAGER_KEY, name, GET_BOOLEAN);
272         if (obj == null)
273         {
274             result = ("T".equals(getInstance(name).getValue())) ?
275                 Boolean.TRUE : Boolean.FALSE;
276             getMethodResult()
277                 .put(result, MANAGER_KEY, name, GET_BOOLEAN);
278         }
279         else
280         {
281             result = (Boolean JavaDoc)obj;
282         }
283         return result.booleanValue();
284     }
285
286     public static boolean getBoolean(String JavaDoc name, Module module)
287         throws TorqueException
288     {
289         boolean b = false;
290         if (module == null)
291         {
292             b = getBoolean(name);
293         }
294         else
295         {
296             Object JavaDoc obj = getMethodResult()
297                 .get(MANAGER_KEY, name, GET_BOOLEAN, module);
298             if (obj == null)
299             {
300                 GlobalParameter p = getInstance(name, module);
301                 if (p == null)
302                 {
303                     // use global default
304
b = getBoolean(name);
305                 }
306                 else
307                 {
308                     b = "T".equals(p.getValue());
309                     getMethodResult().put((b ? Boolean.TRUE : Boolean.FALSE),
310                         MANAGER_KEY, name, GET_BOOLEAN, module);
311                 }
312             }
313             else
314             {
315                 b = ((Boolean JavaDoc)obj).booleanValue();
316             }
317         }
318         return b;
319     }
320     
321     /**
322      * Recursively look up for the existence of the key.
323      * Further details, @see #getBooleanFromHierarchy(String key, Module module, boolean def)
324      *
325      * If no value was not found, return "def" instead.
326      *
327      * @param key
328      * @param module
329      * @param def
330      * @return
331      */

332     public static boolean getBooleanFromHierarchy(String JavaDoc key, Module module, boolean def)
333     {
334         String JavaDoc defAsString = (def)? "T":"F";
335         String JavaDoc bp = getStringFromHierarchy(key,module, defAsString );
336
337         // bp is "[T|F] when it comes from the database,
338
// or [true|false] when it comes from Turbine
339
boolean result = (bp.equals("T") || bp.equals("true"))? true:false;
340
341         return result;
342     }
343
344     /**
345      * Recursively look up for the existence of the key in the
346      * module hierarchy. Backtrack towards the module root.
347      * If no value was found, check for the existence of a
348      * module-independent global parameter.
349      * If still no value found, check for the Turbine
350      * configuration property with the same key.
351      * If still no definition found, return the parameter
352      * "def" instead.
353      *
354      * @param key
355      * @param module
356      * @param def
357      * @return
358      */

359     public static String JavaDoc getStringFromHierarchy(String JavaDoc key, Module module, String JavaDoc def)
360     {
361         String JavaDoc result = null;
362         Module me = module;
363         try
364         {
365             do
366             {
367                 Object JavaDoc obj = getMethodResult().get(MANAGER_KEY, key, GET_STRING, me);
368                 if (obj == null)
369                 {
370                     GlobalParameter p = getInstance(key, me);
371                     if(p != null)
372                     {
373                         result = p.getValue();
374                         getMethodResult()
375                             .put(result, MANAGER_KEY, key, GET_STRING, me);
376                     }
377                 }
378                 else
379                 {
380                     result = (String JavaDoc)obj;
381                 }
382                 if (me == null) {
383                     /* it doesn't make any sense to process here any further */
384                     break;
385                 }
386                 Module parent = me.getParent();
387                 if(parent==me)
388                 {
389                     break;
390                 }
391                 me = parent;
392             } while (result==null || result.equals(""));
393
394             if(result==null || result.equals(""))
395             {
396                 // here try to retrieve the module independent parameter,
397
// or as last resort get it from the Turbine config.
398
result = getString(key);
399                 
400                 // ok, give up and use the hard coded default value.
401
if(result == null || result.equals(""))
402                 {
403                     result = def;
404                 }
405             }
406
407         }
408         catch (Exception JavaDoc e)
409         {
410             LOG.warn("Internal error while retrieving data from GLOBAL_PRAMETER_TABLE: ["+e.getMessage()+"]");
411             L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionTorqueGeneric,e.getMessage());
412             throw new ScarabRuntimeException(msg);
413         }
414             
415         return result;
416     }
417
418     public static void setBoolean(String JavaDoc name, boolean value)
419         throws Exception JavaDoc
420     {
421         setString(name, (value ? "T" : "F"));
422     }
423
424     public static void setBoolean(String JavaDoc name, Module module, boolean value)
425         throws Exception JavaDoc
426     {
427         if (module == null)
428         {
429             setBoolean(name, value);
430         }
431         else
432         {
433             GlobalParameter p = getInstance(name, module);
434             if (p == null)
435             {
436                 p = getInstance(name).copy();
437                 p.setModuleId(module.getModuleId());
438             }
439             String JavaDoc booleanString =(value)?"T":"F";
440             p.setValue(booleanString);
441             p.save();
442
443             Boolean JavaDoc bool =new Boolean JavaDoc(value);
444             getMethodResult().put(bool, MANAGER_KEY, name, GET_BOOLEAN, module);
445         }
446     }
447 }
448
Popular Tags