Author: ronvoe122
Date: 2006-11-05 09:32:40-0800
New Revision: 10325
Modified:
trunk/src/java/org/tigris/scarab/om/GlobalParameterManager.java
trunk/src/java/org/tigris/scarab/om/ScarabModule.java
Log:
[SCB1691] Refactored the GlobalParameterManager for better cache utilizations and code reuse.
Now an empty String is put into the cache, if a parameter has no entry in the database.
Before that, each query for an unset parameter resulted in a db hit because nothing was cached.
At present most parameters are not stored in the db - at least not specific for a module.
So this saves a lot of db-hits.
There are now only two central private methods left which are cached. The public methods are calling this methods.
Modified: trunk/src/java/org/tigris/scarab/om/GlobalParameterManager.java
Url: http://scarab.tigris.org/source/browse/scarab/trunk/src/java/org/tigris/scarab/om/GlobalParameterManager.java?view=diff&rev=10325&p1=trunk/src/java/org/tigris/scarab/om/GlobalParameterManager.java&p2=trunk/src/java/org/tigris/scarab/om/GlobalParameterManager.java&r1=10324&r2=10325
==============================================================================
--- trunk/src/java/org/tigris/scarab/om/GlobalParameterManager.java (original)
+++ trunk/src/java/org/tigris/scarab/om/GlobalParameterManager.java 2006-11-05 09:32:40-0800
@@ -49,23 +49,20 @@
import java.util.List;
import java.io.Serializable;
-import org.apache.log4j.Logger;
import org.apache.torque.om.Persistent;
import org.apache.torque.TorqueException;
import org.apache.torque.util.Criteria;
import org.apache.turbine.Turbine;
-import org.tigris.scarab.tools.localization.L10NKeySet;
-import org.tigris.scarab.tools.localization.L10NMessage;
-import org.tigris.scarab.util.ScarabRuntimeException;
-/**
+/**
* This class manages GlobalParameter objects. Global is used a bit
* loosely here. Parameters can be module scoped as well. for example,
* the email parameters have a global set which is the default, if the
* module does not provide alternatives.
*
* @author <a href="mailto:[email protected]">John McNally</a>
+ * @author <a href="mailto:[email protected]">Ronny Völker</a>
* @version $Id$
*/
public class GlobalParameterManager
@@ -73,9 +70,6 @@
{
private static final String MANAGER_KEY = DEFAULT_MANAGER_CLASS;
private static final String GET_STRING = "getString";
- private static final String GET_BOOLEAN = "getBoolean";
-
- private static final Logger LOG = Logger.getLogger("org.tigris.scarab");
/**
* Creates a new <code>GlobalParameterManager</code> instance.
@@ -93,52 +87,27 @@
throws TorqueException
{
Persistent oldOm = super.putInstanceImpl(om);
- //Serializable obj = (Serializable)om;
GlobalParameter gp = (GlobalParameter)om;
Serializable moduleId = gp.getModuleId();
String name = gp.getName();
- if (moduleId == null)
+ if (moduleId == null)
{
- // if altering a global parameter, its possible the
+ // if altering a global parameter, its possible the
// module overrides are invalid.
getMethodResult().removeAll(MANAGER_KEY, name);
- getMethodResult().removeAll(MANAGER_KEY, name);
}
- else
+ else
{
- getMethodResult().remove(MANAGER_KEY, name, GET_BOOLEAN,
+ getMethodResult().remove(MANAGER_KEY, name, GET_STRING,
moduleId);
- getMethodResult().remove(MANAGER_KEY, name, GET_STRING,
- moduleId);
- }
-/*
- DEBUGGING
- if (oldOm == null)
- {
- System.out.println("first put of value " + name + " to " + gp.getValue());
-
- }
- else
- {
- System.out.println("changing value of " + name + " from " + ((GlobalParameter)oldOm).getValue() + " to " + gp.getValue());
-
}
-*/
return oldOm;
}
private static GlobalParameter getInstance(String name)
throws TorqueException
{
- // try to get a global without a module
- GlobalParameter p = getInstance(name, null);
- if (p == null)
- {
- // get a local new instance
- p = getInstance();
- p.setName(name);
- }
- return p;
+ return getInstance(name, null);
}
private static GlobalParameter getInstance(String name, Module module)
@@ -147,274 +116,210 @@
GlobalParameter result = null;
Criteria crit = new Criteria();
crit.add(GlobalParameterPeer.NAME, name);
- if (module == null)
+ if (module == null)
{
crit.add(GlobalParameterPeer.MODULE_ID, null);
}
- else
+ else
{
crit.add(GlobalParameterPeer.MODULE_ID, module.getModuleId());
}
List parameters = GlobalParameterPeer.doSelect(crit);
- if (!parameters.isEmpty())
+ if (!parameters.isEmpty())
{
result = (GlobalParameter)parameters.get(0);
}
return result;
}
- public static String getString(String key)
+ private static String getStringModule(String name, Module module )
throws TorqueException
{
- // we do not call getString(name, null) here because we do
- // not want to cache results for every module if the parameter
- // is global.
- String result = null;
- // reversing order because we want to be able to invalidate based
- // on the parameter name, not the method name.
- Object obj = getMethodResult().get(MANAGER_KEY, key, GET_STRING);
- if (obj == null)
+ String value = (String) getMethodResult()
+ .get(MANAGER_KEY, name, GET_STRING, module.getModuleId());
+
+ if(value == null)
{
- result = getInstance(key).getValue();
- if (result == null)
+ GlobalParameter p = getInstance(name, module);
+
+ if (p != null)
{
- result = Turbine.getConfiguration().getString(key);
- if (result == null || result.trim().length() == 0)
- {
- result = "";
- }
+ value = p.getValue();
}
- if(!result.equals(""))
+ if(value==null)
{
- getMethodResult().put(result, MANAGER_KEY, key, GET_STRING);
+ value = "";
}
+ getMethodResult().put(value, MANAGER_KEY, name, GET_STRING, module.getModuleId());
}
- else
- {
- result = (String)obj;
- }
- return result;
+ return value;
}
- public static String getString(String name, Module module)
+ private static String getStringGlobal(String name)
throws TorqueException
{
- String result = null;
- if (module == null)
- {
- result = getString(name);
- }
- else
+ String value = (String) getMethodResult()
+ .get(MANAGER_KEY, name, GET_STRING );
+
+ if(value == null)
{
- Object obj = getMethodResult()
- .get(MANAGER_KEY, name, GET_STRING, module);
- if (obj == null)
+ GlobalParameter p = getInstance(name);
+
+ if (p != null)
{
- GlobalParameter p = getInstance(name, module);
- if (p == null)
- {
- // use global default
- result = getString(name);
- }
- else
- {
- result = p.getValue();
- getMethodResult()
- .put(result, MANAGER_KEY, name, GET_STRING, module);
- }
+ value = p.getValue();
}
- else
+ if(value==null || value.equals(""))
{
- result = (String)obj;
+ value = Turbine.getConfiguration().getString(name);
}
+ if(value==null)
+ {
+ value = "";
+ }
+ getMethodResult().put(value, MANAGER_KEY, name, GET_STRING);
}
- return result;
+ return value;
}
- public static void setString(String name, String value)
+ /**
+ * return the global value of parameter <name>
+ *
+ * @param name
+ * @return "" if the parameter is not defined
+ */
+ public static String getString(String name)
throws TorqueException
{
- GlobalParameter p = getInstance(name);
- p.setValue(value);
- p.save();
+ return getStringGlobal(name);
}
- public static void setString(String name, Module module, String value)
+ /**
+ * return the value of parameter <name> for the module <module>
+ * if it's not defined return the global value of this parameter
+ *
+ * @param name
+ * @param module
+ * @return "" if the parameter is not defined
+ */
+ public static String getString(String name, Module module)
throws TorqueException
{
- if (module == null)
+ String value = getStringModule(name, module);
+ if (value.equals(""))
{
- setString(name, value);
- }
- else
- {
- GlobalParameter p = getInstance(name, module);
- if (p == null)
- {
- p = getInstance(name).copy();
- p.setModuleId(module.getModuleId());
- }
- p.setValue(value);
- p.save();
-
- getMethodResult().put(value, MANAGER_KEY, name, GET_STRING, module);
+ value = getStringGlobal(name);
}
+ return value;
}
- public static boolean getBoolean(String name)
+ /**
+ * Recursively look up for the existence of the name in the
+ * module hierarchy. Backtrack towards the module root.
+ * If no value was found, check for the existence of a
+ * module-independent global parameter.
+ * If still no value found, check for the Turbine
+ * configuration property with the same name.
+ * If still no definition found, return the parameter
+ * "def" instead.
+ *
+ * @param name
+ * @param module
+ * @param def
+ * @return
+ */
+ public static String getStringFromHierarchy(String name, Module module, String def)
throws TorqueException
{
- // we do not call getBoolean(name, null) here because we do
- // not want to cache results for every module if the parameter
- // is global.
- Boolean result = null;
- Object obj = getMethodResult().get(MANAGER_KEY, name, GET_BOOLEAN);
- if (obj == null)
- {
- result = ("T".equals(getInstance(name).getValue())) ?
- Boolean.TRUE : Boolean.FALSE;
- getMethodResult()
- .put(result, MANAGER_KEY, name, GET_BOOLEAN);
+ String value = getStringModule( name, module );
+
+ if(value.equals(""))
+ {
+ Module parentModule = module.getParent();
+ if(parentModule != null)
+ {
+ value = getStringFromHierarchy(name, parentModule, def);
+ }
+ }
+
+ if(value.equals(""))
+ {
+ value = getStringGlobal(name);
}
- else
+
+ if(value.equals(""))
{
- result = (Boolean)obj;
+ value = def;
}
- return result.booleanValue();
+
+ return value;
}
- public static boolean getBoolean(String name, Module module)
+ public static void setString(String name, String value)
throws TorqueException
{
- boolean b = false;
- if (module == null)
- {
- b = getBoolean(name);
- }
- else
+ setString(name, null, value);
+ }
+
+ public static void setString(String name, Module module, String value)
+ throws TorqueException
+ {
+ GlobalParameter p = getInstance(name, module);
+ if (p == null)
{
- Object obj = getMethodResult()
- .get(MANAGER_KEY, name, GET_BOOLEAN, module);
- if (obj == null)
- {
- GlobalParameter p = getInstance(name, module);
- if (p == null)
- {
- // use global default
- b = getBoolean(name);
- }
- else
- {
- b = "T".equals(p.getValue());
- getMethodResult().put((b ? Boolean.TRUE : Boolean.FALSE),
- MANAGER_KEY, name, GET_BOOLEAN, module);
- }
- }
- else
- {
- b = ((Boolean)obj).booleanValue();
- }
+ p = getInstance();
+ p.setName(name);
+ p.setModuleId(module.getModuleId());
}
- return b;
+ p.setValue(value);
+ p.save();
}
-
+
/**
- * Recursively look up for the existence of the key.
- * Further details, @see #getBooleanFromHierarchy(String key, Module module, boolean def)
- *
- * If no value was not found, return "def" instead.
- *
- * @param key
- * @param module
- * @param def
- * @return
+ * return the global value of parameter <name>
+ *
+ * @param name
+ * @return "" if the parameter is not defined
*/
- public static boolean getBooleanFromHierarchy(String key, Module module, boolean def)
+ public static boolean getBoolean(String name)
+ throws TorqueException
{
- String defAsString = (def)? "T":"F";
- String bp = getStringFromHierarchy(key,module, defAsString );
-
- // bp is "[T|F] when it comes from the database,
- // or [true|false] when it comes from Turbine
- boolean result = (bp.equals("T") || bp.equals("true"))? true:false;
+ return "T".equals(getString(name));
+ }
- return result;
+ /**
+ * return the value of parameter <name> for the module <module>
+ * if it's not defined return the global value of this parameter
+ *
+ * @param name
+ * @param module
+ * @return "" if the parameter is not defined
+ */
+ public static boolean getBoolean(String name, Module module)
+ throws TorqueException
+ {
+ return "T".equals(getString(name, module));
}
/**
- * Recursively look up for the existence of the key in the
- * module hierarchy. Backtrack towards the module root.
- * If no value was found, check for the existence of a
- * module-independent global parameter.
- * If still no value found, check for the Turbine
- * configuration property with the same key.
- * If still no definition found, return the parameter
- * "def" instead.
- *
- * @param key
+ * Recursively look up for the existence of the name.
+ * Further details, @see #getBooleanFromHierarchy(String name, Module module, boolean def)
+ *
+ * If no value was not found, return "def" instead.
+ *
+ * @param name
* @param module
* @param def
* @return
*/
- public static String getStringFromHierarchy(String key, Module module, String def)
+ public static boolean getBooleanFromHierarchy(String name, Module module, boolean def)
+ throws TorqueException
{
- String result = null;
- Module me = module;
- try
- {
- do
- {
- Object obj = getMethodResult().get(MANAGER_KEY, key, GET_STRING, me);
- if (obj == null)
- {
- GlobalParameter p = getInstance(key, me);
- if(p != null)
- {
- result = p.getValue();
- getMethodResult()
- .put(result, MANAGER_KEY, key, GET_STRING, me);
- }
- }
- else
- {
- result = (String)obj;
- }
- if (me == null) {
- /* it doesn't make any sense to process here any further */
- break;
- }
- Module parent = me.getParent();
- if(parent==me)
- {
- break;
- }
- me = parent;
- } while (result==null || result.equals(""));
-
- if(result==null || result.equals(""))
- {
- // here try to retrieve the module independent parameter,
- // or as last resort get it from the Turbine config.
- result = getString(key);
-
- // ok, give up and use the hard coded default value.
- if(result == null || result.equals(""))
- {
- result = def;
- }
- }
-
- }
- catch (Exception e)
- {
- LOG.warn("Internal error while retrieving data from GLOBAL_PRAMETER_TABLE: ["+e.getMessage()+"]");
- L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionTorqueGeneric,e.getMessage());
- throw new ScarabRuntimeException(msg);
- }
-
- return result;
+ String defAsString = (def)? "T":"F";
+ return "T".equals(getStringFromHierarchy(name, module, defAsString ));
}
+
public static void setBoolean(String name, boolean value)
throws TorqueException
{
@@ -424,24 +329,6 @@
public static void setBoolean(String name, Module module, boolean value)
throws TorqueException
{
- if (module == null)
- {
- setBoolean(name, value);
- }
- else
- {
- GlobalParameter p = getInstance(name, module);
- if (p == null)
- {
- p = getInstance(name).copy();
- p.setModuleId(module.getModuleId());
- }
- String booleanString =(value)?"T":"F";
- p.setValue(booleanString);
- p.save();
-
- Boolean bool =new Boolean(value);
- getMethodResult().put(bool, MANAGER_KEY, name, GET_BOOLEAN, module);
- }
+ setString(name, module, (value ? "T" : "F"));
}
}
Modified: trunk/src/java/org/tigris/scarab/om/ScarabModule.java
Url: http://scarab.tigris.org/source/browse/scarab/trunk/src/java/org/tigris/scarab/om/ScarabModule.java?view=diff&rev=10325&p1=trunk/src/java/org/tigris/scarab/om/ScarabModule.java&p2=trunk/src/java/org/tigris/scarab/om/ScarabModule.java&r1=10324&r2=10325
==============================================================================
--- trunk/src/java/org/tigris/scarab/om/ScarabModule.java (original)
+++ trunk/src/java/org/tigris/scarab/om/ScarabModule.java 2006-11-05 09:32:40-0800
@@ -66,10 +66,8 @@
import java.sql.Connection;
import org.apache.fulcrum.security.TurbineSecurity;
-import org.apache.fulcrum.security.util.DataBackendException;
import org.apache.fulcrum.security.util.RoleSet;
import org.apache.fulcrum.security.util.TurbineSecurityException;
-import org.apache.fulcrum.security.util.UnknownEntityException;
import org.apache.fulcrum.security.entity.User;
import org.apache.fulcrum.security.entity.Group;
import org.apache.fulcrum.security.entity.Role;
@@ -84,9 +82,7 @@
import org.tigris.scarab.util.ScarabException;
import org.tigris.scarab.util.ScarabPaginatedList;
import org.tigris.scarab.util.ScarabLocalizedTorqueException;
-import org.tigris.scarab.util.ScarabLocalizedTurbineSecurityException;
import org.tigris.scarab.services.cache.ScarabCache;
-import org.tigris.scarab.services.security.ScarabSecurity;
// FIXME! do not like referencing servlet inside of business objects
// though I have forgotten how I might avoid it
@@ -215,7 +211,6 @@
* @param v Value to assign to port.
*/
public void setPort(String v)
- throws TorqueException
{
if (v != null)
{
@@ -243,7 +238,6 @@
* @param v Value to assign to scheme.
*/
public void setScheme(String v)
- throws TorqueException
{
if (v != null)
{
@@ -271,7 +265,6 @@
* @param v Value to assign to scriptName.
*/
public void setScriptName(String v)
- throws TorqueException
{
if (v != null)
{
@@ -594,7 +587,15 @@
public Module getParent()
throws TorqueException
{
- return super.getModuleRelatedByParentId();
+ Module parent = super.getModuleRelatedByParentId();
+
+ // The top level module has itself as parent.
+ // Return null in this case, to avoid endless loops.
+ if (this.getModuleId() == parent.getModuleId())
+ {
+ parent = null;
+ }
+ return parent;
}
/**
@@ -636,7 +637,7 @@
* @exception Exception if an error occurs
*/
public int getIssueCount(ScarabUser user, AttributeOption attributeOption)
- throws TorqueException, ScarabException, DataSetException
+ throws TorqueException, DataSetException
{
Criteria crit = new Criteria();
@@ -669,7 +670,7 @@
* @exception Exception if an error occurs
*/
public int getIssueCount(ScarabUser user)
- throws TorqueException, ScarabException, DataSetException
+ throws TorqueException, DataSetException
{
Criteria crit = new Criteria();
crit.add(IssuePeer.MODULE_ID,getModuleId());
@@ -985,8 +986,16 @@
public boolean isIssueReasonRequired()
{
String key = GlobalParameter.ISSUE_REASON_REQUIRED;
- boolean result = GlobalParameterManager.
- getBooleanFromHierarchy(key, this, true);
+ boolean result = true;
+ try
+ {
+ result = GlobalParameterManager.
+ getBooleanFromHierarchy(key, this, true);
+ }
+ catch (TorqueException te)
+ {
+ getLog().error("isIssueReasonRequired(): " + te);
+ }
return result;
}
@@ -1037,20 +1046,19 @@
*/
public Role getRequiredRole()
{
- String key = GlobalParameter.REQUIRED_ROLE_FOR_REQUESTING_ACCESS;
- Role result = null;
+ Role role = null;
try
{
- String val = GlobalParameterManager.
- getString(key, this);
- if (val != null && val.length() > 0)
- result = TurbineSecurity.getRole(val);
+ String roleName = GlobalParameterManager
+ .getString(GlobalParameter.REQUIRED_ROLE_FOR_REQUESTING_ACCESS, this);
+ if (roleName != null && roleName.length() > 0)
+ role = TurbineSecurity.getRole(roleName);
}
catch (Exception e)
{
- getLog().error("getRequiredRole(): " + e);
+ throw new RuntimeException(e);
}
- return result;
+ return role;
}
/**
@@ -1086,7 +1094,6 @@
* Gets all module roles.
*/
public List getRoles()
- throws TorqueException
{
return new ArrayList(0);
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.