-
New Feature
-
Resolution: Fixed
-
Major
-
None
-
None
If we are outside of an HTTP request (typically a daemon thread), the current caller is unknown.
But, we want in a few cases to have a non-null caller:
- for storing the lock owner in repository plugin ;
- for storing the workflow caller in workflow plugin.
As the default caller can changed from one application to the other and can be multiple (depending on the context), using a SingleExtPt is the right way.
The patch with admin as the default caller:
Index: main/plugin-core/src/org/ametys/runtime/plugins/core/user/AvalonContextCallerProvider.java =================================================================== --- main/plugin-core/src/org/ametys/runtime/plugins/core/user/AvalonContextCallerProvider.java (revision 0) +++ main/plugin-core/src/org/ametys/runtime/plugins/core/user/AvalonContextCallerProvider.java (revision 0) @@ -0,0 +1,86 @@ +package org.ametys.runtime.plugins.core.user; + +import java.util.Map; + +import org.apache.avalon.framework.configuration.Configurable; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.context.Context; +import org.apache.avalon.framework.context.ContextException; +import org.apache.avalon.framework.context.Contextualizable; +import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.avalon.framework.thread.ThreadSafe; +import org.apache.cocoon.components.ContextHelper; + +import org.ametys.runtime.user.ContextCallerProvider; +import org.ametys.runtime.user.UserHelper; + +/** + * Provide the caller by searching the current user in the object model.<br> + * If not found a default login is used (read from XML configuration). + */ +public class AvalonContextCallerProvider extends AbstractLogEnabled implements ContextCallerProvider, Contextualizable, Configurable, ThreadSafe +{ + private String _defaultCaller; + private Context _context; + + public void contextualize(Context context) throws ContextException + { + _context = context; + } + + public void configure(Configuration configuration) throws ConfigurationException + { + Configuration defaultCaller = configuration.getChild("caller", true); + _defaultCaller = defaultCaller.getAttribute("default", "unknown"); + } + + public String getCaller() + { + String caller = null; + + try + { + Map objectModel = ContextHelper.getObjectModel(_context); + + if (objectModel != null) + { + caller = UserHelper.getCurrentUser(objectModel); + } + } + catch (Exception e) + { + if (getLogger().isInfoEnabled()) + { + getLogger().info("Unable to retrieve current authenticated user, fallback to default user", e); + } + } + + if (caller == null) + { + return _getDefaultCaller(); + } + + if (getLogger().isDebugEnabled()) + { + getLogger().debug("Using caller: " + caller); + } + + return caller; + } + + /** + * Retrieve the default caller to use. + * @return the default caller. + */ + protected String _getDefaultCaller() + { + if (getLogger().isInfoEnabled()) + { + getLogger().info("Current user not found, use default caller: " + _defaultCaller); + } + + // Current user not found, use default caller + return _defaultCaller; + } +} Index: main/plugin-core/plugin.xml =================================================================== --- main/plugin-core/plugin.xml (revision 504) +++ main/plugin-core/plugin.xml (working copy) @@ -632,6 +632,10 @@ class="org.ametys.runtime.user.UsersManager" default-extension-id="org.ametys.runtime.plugins.core.user.Static"/> + <single-extension-point id="org.ametys.runtime.user.ContextCallerProvider" + class="org.ametys.runtime.user.ContextCallerProvider" + default-extension-id="org.ametys.runtime.plugins.core.user.AvalonContextCallerProvider"/> + <single-extension-point id="org.ametys.runtime.group.GroupsManager" class="org.ametys.runtime.group.GroupsManager" default-extension-id="org.ametys.runtime.plugins.core.group.Empty"/> @@ -1170,6 +1174,23 @@ </extension> </extensions> </feature> + + <!-- + + | CONTEXT CALLER PROVIDER + + --> + <feature name="runtime.context-caller"> + <!-- + This feature provides a non-null caller from the current context + --> + <extensions> + <!-- Default extension for selecting the caller from the current context --> + <extension point="org.ametys.runtime.user.ContextCallerProvider" + id="org.ametys.runtime.plugins.core.user.AvalonContextCallerProvider" + class="org.ametys.runtime.plugins.core.user.AvalonContextCallerProvider"> + <caller default="admin"/> + </extension> + </extensions> + </feature> <!-- + | GROUPS Index: main/kernel/src/org/ametys/runtime/user/ContextCallerProvider.java =================================================================== --- main/kernel/src/org/ametys/runtime/user/ContextCallerProvider.java (revision 0) +++ main/kernel/src/org/ametys/runtime/user/ContextCallerProvider.java (revision 0) @@ -0,0 +1,16 @@ +package org.ametys.runtime.user; + +/** + * Provide the caller of the current context. + */ +public interface ContextCallerProvider +{ + /** Avalon role. */ + public static final String ROLE = ContextCallerProvider.class.getName(); + + /** + * Provide the caller from current context. + * @return the caller which can not be <code>null</code>. + */ + String getCaller(); +}