public class AnnotatedClassActionResolver extends java.lang.Object implements ActionResolver
Uses Annotations on classes to identify the ActionBean that corresponds to the current
request. ActionBeans are annotated with an @UrlBinding
annotation, which denotes the
web application relative URL that the ActionBean should respond to.
Individual methods on ActionBean classes are expected to be annotated with @HandlesEvent annotations, and potentially a @DefaultHandler annotation. Using these annotations the Resolver will determine which method should be executed for the current request.
UrlBinding
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
PACKAGES
Configuration key used to lookup a comma-separated list of package names.
|
RESOLVED_ACTION
Constructor and Description |
---|
AnnotatedClassActionResolver() |
Modifier and Type | Method and Description |
---|---|
protected void |
addActionBean(java.lang.Class<? extends ActionBean> clazz)
Adds an ActionBean class to the set that this resolver can resolve.
|
protected void |
assertGetContextWorks(ActionBean bean)
Since many down stream parts of Stripes rely on the ActionBean properly returning the
context it is given, we'll just test it up front.
|
protected java.util.Set<java.lang.Class<? extends ActionBean>> |
findClasses()
Helper method to find implementations of ActionBean in the packages specified in
Configuration using the
ResolverUtil class. |
ActionBean |
getActionBean(ActionBeanContext context)
Gets the logical name of the ActionBean that should handle the request.
|
ActionBean |
getActionBean(ActionBeanContext context,
java.lang.String path)
Returns the ActionBean class that is bound to the UrlBinding supplied.
|
java.util.Collection<java.lang.Class<? extends ActionBean>> |
getActionBeanClasses()
Get all the classes implementing
ActionBean that are recognized by this
ActionResolver . |
java.lang.Class<? extends ActionBean> |
getActionBeanType(java.lang.String path)
Fetches the Class representing the type of ActionBean that would respond were a
request made with the path specified.
|
protected Configuration |
getConfiguration()
Provides subclasses with access to the configuration object.
|
java.lang.reflect.Method |
getDefaultHandler(java.lang.Class<? extends ActionBean> bean)
Returns the Method that is the default handler for events in the ActionBean class supplied.
|
java.lang.String |
getEventName(java.lang.Class<? extends ActionBean> bean,
ActionBeanContext context)
Try various means to determine which event is to be executed on the current ActionBean.
|
protected java.lang.String |
getEventNameFromEventNameParam(java.lang.Class<? extends ActionBean> bean,
ActionBeanContext context)
Looks to see if there is a single non-empty parameter value for the parameter name
specified by
StripesConstants.URL_KEY_EVENT_NAME . |
protected java.lang.String |
getEventNameFromPath(java.lang.Class<? extends ActionBean> bean,
ActionBeanContext context)
Looks to see if there is extra path information beyond simply the url binding of the
bean.
|
protected java.lang.String |
getEventNameFromRequestAttribute(java.lang.Class<? extends ActionBean> bean,
ActionBeanContext context)
Checks a special request attribute to get the event name.
|
protected java.lang.String |
getEventNameFromRequestParams(java.lang.Class<? extends ActionBean> bean,
ActionBeanContext context)
Loops through the set of known events for the ActionBean to see if the event
names are present as parameter names in the request.
|
java.lang.String |
getHandledEvent(java.lang.reflect.Method handler)
Responsible for determining the name of the event handled by this method, if indeed
it handles one at all.
|
java.lang.reflect.Method |
getHandler(java.lang.Class<? extends ActionBean> bean,
java.lang.String eventName)
Uses the Maps constructed earlier to locate the Method which can handle the event.
|
protected java.lang.String |
getRequestedPath(javax.servlet.http.HttpServletRequest request)
Deprecated.
Use
HttpUtil.getRequestedPath(HttpServletRequest) instead. |
java.lang.String |
getUrlBinding(java.lang.Class<? extends ActionBean> clazz)
Takes a class that implements ActionBean and returns the URL binding of that class.
|
java.lang.String |
getUrlBindingFromPath(java.lang.String path)
Returns the URL binding that is a substring of the path provided.
|
void |
init(Configuration configuration)
Scans the classpath of the current classloader (not including parents) to find implementations
of the ActionBean interface.
|
protected ActionBean |
makeNewActionBean(java.lang.Class<? extends ActionBean> type,
ActionBeanContext context)
Helper method to construct and return a new ActionBean instance.
|
protected void |
processMethods(java.lang.Class<?> clazz,
java.util.Map<java.lang.String,java.lang.reflect.Method> classMappings)
Helper method that examines a class, starting at it's highest super class and
working it's way down again, to find method annotations and ensure that child
class annotations take precedence.
|
protected void |
removeActionBean(java.lang.Class<? extends ActionBean> clazz)
Removes an ActionBean class from the set that this resolver can resolve.
|
protected void |
setActionBeanContext(ActionBean bean,
ActionBeanContext context)
Calls
ActionBean.setContext(ActionBeanContext) with the given context only if
necessary. |
public static final java.lang.String PACKAGES
public void init(Configuration configuration) throws java.lang.Exception
init
in interface ConfigurableComponent
configuration
- the Configuration object being used by Stripesjava.lang.Exception
- should be thrown if the component cannot be configured well enough to use.protected void addActionBean(java.lang.Class<? extends ActionBean> clazz)
clazz
- a class that implements ActionBeanprotected void removeActionBean(java.lang.Class<? extends ActionBean> clazz)
clazz
- a class that implements ActionBeanpublic java.lang.String getUrlBindingFromPath(java.lang.String path)
/user/Profile.action
the path
/user/Profile.action/view
would return /user/Profile.action
.getUrlBindingFromPath
in interface ActionResolver
path
- the path being used to access an ActionBean, either in a form or link tag,
or in a request that is hitting the DispatcherServlet.public java.lang.String getUrlBinding(java.lang.Class<? extends ActionBean> clazz)
getUrlBinding
in interface ActionResolver
clazz
- a class that implements ActionBeanprotected void processMethods(java.lang.Class<?> clazz, java.util.Map<java.lang.String,java.lang.reflect.Method> classMappings)
public java.lang.String getHandledEvent(java.lang.reflect.Method handler)
getHandledEvent
in interface ActionResolver
handler
- a method that might or might not be a handler methodpublic java.lang.Class<? extends ActionBean> getActionBeanType(java.lang.String path)
Fetches the Class representing the type of ActionBean that would respond were a request made with the path specified. Checks to see if the full path matches any bean's UrlBinding. If no ActionBean matches then successively removes path segments (separated by slashes) from the end of the path until a match is found.
getActionBeanType
in interface ActionResolver
path
- the path segment of a URLpublic ActionBean getActionBean(ActionBeanContext context) throws StripesServletException
getActionBean
in interface ActionResolver
context
- the ActionBeanContext for the current requestStripesServletException
- thrown if a ActionBean cannot be resolved for any reason@Deprecated protected java.lang.String getRequestedPath(javax.servlet.http.HttpServletRequest request)
HttpUtil.getRequestedPath(HttpServletRequest)
instead.request
- the current HttpServletRequestpublic ActionBean getActionBean(ActionBeanContext context, java.lang.String path) throws StripesServletException
getActionBean
in interface ActionResolver
path
- a URL to which an ActionBean is bound, or a path starting with the URL
to which an ActionBean has been bound.context
- the current ActionBeanContextStripesServletException
- if the UrlBinding does not match an ActionBean bindingprotected void setActionBeanContext(ActionBean bean, ActionBeanContext context)
ActionBean.setContext(ActionBeanContext)
with the given context
only if
necessary. Subclasses should use this method instead of setting the context directly because
it can be somewhat tricky to determine when it needs to be done.bean
- The bean whose context may need to be set.context
- The context to pass to the bean if necessary.protected void assertGetContextWorks(ActionBean bean) throws StripesServletException
bean
- the ActionBean to test to see if getContext() works correctlyStripesServletException
- if getContext() returns nullprotected ActionBean makeNewActionBean(java.lang.Class<? extends ActionBean> type, ActionBeanContext context) throws java.lang.Exception
type
- the type of ActionBean to createcontext
- the current ActionBeanContextjava.lang.Exception
- if anything goes wrong!public java.lang.String getEventName(java.lang.Class<? extends ActionBean> bean, ActionBeanContext context)
Try various means to determine which event is to be executed on the current ActionBean. If a
'special' request attribute (StripesConstants.REQ_ATTR_EVENT_NAME
) is present in
the request, then return its value. This attribute is used to handle internal forwards, when
request parameters are merged and cannot reliably determine the desired event name.
If that doesn't work, the value of a 'special' request parameter (StripesConstants.URL_KEY_EVENT_NAME
)
is checked to see if contains a single value matching an event name.
Failing that, search for a parameter in the request whose name matches one of the named
events handled by the ActionBean. For example, if the ActionBean can handle events foo and
bar, this method will scan the request for foo=somevalue and bar=somevalue. If it finds a
request parameter with a matching name it will return that name. If there are multiple
matching names, the result of this method cannot be guaranteed and a
StripesRuntimeException
will be thrown.
Finally, if the event name cannot be determined through the parameter names and there is extra path information beyond the URL binding of the ActionBean, it is checked to see if it matches an event name.
getEventName
in interface ActionResolver
bean
- the ActionBean type bound to the requestcontext
- the ActionBeanContect for the current requestprotected java.lang.String getEventNameFromRequestAttribute(java.lang.Class<? extends ActionBean> bean, ActionBeanContext context)
bean
- the ActionBean type bound to the requestcontext
- the ActionBeanContect for the current requestStripesConstants.REQ_ATTR_EVENT_NAME
protected java.lang.String getEventNameFromRequestParams(java.lang.Class<? extends ActionBean> bean, ActionBeanContext context)
bean
- the ActionBean type bound to the requestcontext
- the ActionBeanContext for the current requestprotected java.lang.String getEventNameFromPath(java.lang.Class<? extends ActionBean> bean, ActionBeanContext context)
bean
- the ActionBean type bound to the requestcontext
- the ActionBeanContect for the current requestprotected java.lang.String getEventNameFromEventNameParam(java.lang.Class<? extends ActionBean> bean, ActionBeanContext context)
StripesConstants.URL_KEY_EVENT_NAME
. If there is, and it
matches a known event it is returned, otherwise returns null.bean
- the ActionBean type bound to the requestcontext
- the ActionBeanContect for the current requestpublic java.lang.reflect.Method getHandler(java.lang.Class<? extends ActionBean> bean, java.lang.String eventName) throws StripesServletException
getHandler
in interface ActionResolver
bean
- the subclass of ActionBean that is bound to the request.eventName
- the name of the event being handledStripesServletException
- thrown when no method handles the named event.public java.lang.reflect.Method getDefaultHandler(java.lang.Class<? extends ActionBean> bean) throws StripesServletException
getDefaultHandler
in interface ActionResolver
bean
- the ActionBean type bound to the requestStripesServletException
- if no default handler could be locatedprotected Configuration getConfiguration()
protected java.util.Set<java.lang.Class<? extends ActionBean>> findClasses()
ResolverUtil
class.public java.util.Collection<java.lang.Class<? extends ActionBean>> getActionBeanClasses()
ActionBean
that are recognized by this
ActionResolver
.getActionBeanClasses
in interface ActionResolver
? Copyright 2005-2006, Stripes Development Team.