| Method from com.opensymphony.webwork.dispatcher.DispatcherUtils Detail: |
public void cleanup() {
ObjectFactory objectFactory = ObjectFactory.getObjectFactory();
// inform ShutDownListener, we are shuting down
if (Configuration.isSet(WebWorkConstants.WEBWORK_DISPATCHER_SHUTDOWN_LISTENER)) {
String[] shutdownListenerClassNames = Configuration.getString(WebWorkConstants.WEBWORK_DISPATCHER_SHUTDOWN_LISTENER).split(",");
for (int a=0; a< shutdownListenerClassNames.length; a++) {
String shutdownListenerClassName = shutdownListenerClassNames[a].trim();
try {
ShutDownListener shutDownListener = (ShutDownListener) objectFactory.buildBean(shutdownListenerClassName, Collections.EMPTY_MAP);
if (LOG.isDebugEnabled()) {
LOG.debug("notifying shutdown listener ["+shutDownListener+"]");
}
shutDownListener.shutdown();
}
catch(Exception e) { // we might also get ClassCastException
LOG.warn("shutdown listener ["+shutdownListenerClassName+"] failed to be initialized, it will be ignored", e);
}
}
}
// clean up ObjectFactory
if (objectFactory == null) {
LOG.warn("Object Factory is null, something is seriously wrong, no clean up will be performed");
}
if (objectFactory instanceof ObjectFactoryDestroyable) {
try {
((ObjectFactoryDestroyable)objectFactory).destroy();
}
catch(Exception e) {
// catch any exception that may occured during destroy() and log it
LOG.error("exception occurred while destroying ObjectFactory ["+objectFactory+"]", e);
}
}
// clean up ConfigurationManager
ConfigurationManager.destroyConfiguration();
}
|
public Map createContextMap(HttpServletRequest request,
HttpServletResponse response,
ActionMapping mapping,
ServletContext context) {
// request map wrapping the http request objects
Map requestMap = new RequestMap(request);
// parameters map wrapping the http paraneters.
Map params = null;
if (mapping != null) {
params = mapping.getParams();
}
Map requestParams = new HashMap(request.getParameterMap());
if (params != null) {
params.putAll(requestParams);
} else {
params = requestParams;
}
// session map wrapping the http session
Map session = new SessionMap(request);
// application map wrapping the ServletContext
Map application = new ApplicationMap(context);
return createContextMap(requestMap, params, session, application, request, response, context);
}
|
public HashMap createContextMap(Map requestMap,
Map parameterMap,
Map sessionMap,
Map applicationMap,
HttpServletRequest request,
HttpServletResponse response,
ServletContext servletContext) {
HashMap extraContext = new HashMap();
extraContext.put(ActionContext.PARAMETERS, new HashMap(parameterMap));
extraContext.put(ActionContext.SESSION, sessionMap);
extraContext.put(ActionContext.APPLICATION, applicationMap);
Locale locale = null;
if (Configuration.isSet(WebWorkConstants.WEBWORK_LOCALE)) {
locale = LocalizedTextUtil.localeFromString(Configuration.getString(WebWorkConstants.WEBWORK_LOCALE), request.getLocale());
} else {
locale = request.getLocale();
}
extraContext.put(ActionContext.LOCALE, locale);
extraContext.put(ActionContext.DEV_MODE, Boolean.valueOf(devMode));
extraContext.put(WebWorkStatics.HTTP_REQUEST, request);
extraContext.put(WebWorkStatics.HTTP_RESPONSE, response);
extraContext.put(WebWorkStatics.SERVLET_CONTEXT, servletContext);
extraContext.put(ComponentInterceptor.COMPONENT_MANAGER, request.getAttribute(ComponentManager.COMPONENT_MANAGER_KEY));
// helpers to get access to request/session/application scope
extraContext.put("request", requestMap);
extraContext.put("session", sessionMap);
extraContext.put("application", applicationMap);
extraContext.put("parameters", parameterMap);
AttributeMap attrMap = new AttributeMap(extraContext);
extraContext.put("attr", attrMap);
return extraContext;
}
Merges all application and servlet attributes into a single HashMap to represent the entire
Action context. |
public static DispatcherUtils getInstance() {
return instance;
}
|
public static int getMaxSize() {
Integer maxSize = new Integer(Integer.MAX_VALUE);
try {
String maxSizeStr = Configuration.getString(WebWorkConstants.WEBWORK_MULTIPART_MAXSIZE);
if (maxSizeStr != null) {
try {
maxSize = new Integer(maxSizeStr);
} catch (NumberFormatException e) {
LOG.warn("Unable to format 'webwork.multipart.maxSize' property setting. Defaulting to Integer.MAX_VALUE");
}
} else {
LOG.warn("Unable to format 'webwork.multipart.maxSize' property setting. Defaulting to Integer.MAX_VALUE");
}
} catch (IllegalArgumentException e1) {
LOG.warn("Unable to format 'webwork.multipart.maxSize' property setting. Defaulting to Integer.MAX_VALUE");
}
if (LOG.isDebugEnabled()) {
LOG.debug("maxSize=" + maxSize);
}
return maxSize.intValue();
}
Returns the maximum upload size allowed for multipart requests (this is configurable). |
public String getSaveDir(ServletContext servletContext) {
String saveDir = Configuration.getString(WebWorkConstants.WEBWORK_MULTIPART_SAVEDIR).trim();
if (saveDir.equals("")) {
File tempdir = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
LOG.info("Unable to find 'webwork.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir");
if (tempdir != null) {
saveDir = tempdir.toString();
}
} else {
File multipartSaveDir = new File(saveDir);
if (!multipartSaveDir.exists()) {
multipartSaveDir.mkdir();
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("saveDir=" + saveDir);
}
return saveDir;
}
Returns the path to save uploaded files to (this is configurable). |
protected void init(ServletContext servletContext) {
boolean reloadi18n = Boolean.valueOf((String) Configuration.get(WebWorkConstants.WEBWORK_I18N_RELOAD)).booleanValue();
LocalizedTextUtil.setReloadBundles(reloadi18n);
// initialize ObjectFactory
if (Configuration.isSet(WebWorkConstants.WEBWORK_OBJECTFACTORY)) {
String className = (String) Configuration.get(WebWorkConstants.WEBWORK_OBJECTFACTORY);
if (className.equals("spring")) {
// note: this class name needs to be in string form so we don't put hard
// dependencies on spring, since it isn't technically required.
className = "com.opensymphony.webwork.spring.WebWorkSpringObjectFactory";
} else if (className.equals("plexus")) {
// note: this class name needs to be in string form so we don't put hard
// dependencies on spring, since it isn't technically required.
className = "com.opensymphony.webwork.plexus.PlexusObjectFactory";
}
try {
Class clazz = ClassLoaderUtil.loadClass(className, DispatcherUtils.class);
ObjectFactory objectFactory = (ObjectFactory) clazz.newInstance();
if (objectFactory instanceof ObjectFactoryInitializable) {
((ObjectFactoryInitializable) objectFactory).init(servletContext);
}
ObjectFactory.setObjectFactory(objectFactory);
} catch (Exception e) {
LOG.error("Could not load ObjectFactory named " + className + ". Using default ObjectFactory.", e);
}
}
// Intialize ObjecTypeDeterminer
if (Configuration.isSet(WebWorkConstants.WEBWORK_OBJECTTYPEDETERMINER)) {
String className = (String) Configuration.get(WebWorkConstants.WEBWORK_OBJECTTYPEDETERMINER);
if (className.equals("tiger")) {
// note: this class name needs to be in string form so we don't put hard
// dependencies on xwork-tiger, since it isn't technically required.
className = "com.opensymphony.xwork.util.GenericsObjectTypeDeterminer";
}
else if (className.equals("notiger")) {
className = "com.opensymphony.xwork.util.DefaultObjectTypeDeterminer";
}
try {
Class clazz = ClassLoaderUtil.loadClass(className, DispatcherUtils.class);
ObjectTypeDeterminer objectTypeDeterminer = (ObjectTypeDeterminer) clazz.newInstance();
ObjectTypeDeterminerFactory.setInstance(objectTypeDeterminer);
} catch (Exception e) {
LOG.error("Could not load ObjectTypeDeterminer named " + className + ". Using default DefaultObjectTypeDeterminer.", e);
}
}
if ("true".equals(Configuration.get(WebWorkConstants.WEBWORK_DEVMODE))) {
devMode = true;
Configuration.set(WebWorkConstants.WEBWORK_I18N_RELOAD, "true");
Configuration.set(WebWorkConstants.WEBWORK_CONFIGURATION_XML_RELOAD, "true");
}
//check for configuration reloading
if (Configuration.isSet(WebWorkConstants.WEBWORK_CONFIGURATION_XML_RELOAD) &&
"true".equalsIgnoreCase(Configuration.getString(WebWorkConstants.WEBWORK_CONFIGURATION_XML_RELOAD))) {
FileManager.setReloadingConfigs(true);
}
if (Configuration.isSet(WebWorkConstants.WEBWORK_CONTINUATIONS_PACKAGE)) {
String pkg = Configuration.getString(WebWorkConstants.WEBWORK_CONTINUATIONS_PACKAGE);
ObjectFactory.setContinuationPackage(pkg);
}
// test wether param-access workaround needs to be enabled
if (servletContext.getServerInfo().indexOf("WebLogic") >= 0) {
LOG.info("WebLogic server detected. Enabling WebWork parameter access work-around.");
paramsWorkaroundEnabled = true;
} else if (Configuration.isSet(WebWorkConstants.WEBWORK_DISPATCHER_PARAMETERSWORKAROUND)) {
paramsWorkaroundEnabled = "true".equals(Configuration.get(WebWorkConstants.WEBWORK_DISPATCHER_PARAMETERSWORKAROUND));
} else {
LOG.debug("Parameter access work-around disabled.");
}
// inform startup listeners
if (Configuration.isSet(WebWorkConstants.WEBWORK_DISPATCHER_START_UP_LISTENER)) {
String[] startupListenerClassNames = Configuration.getString(WebWorkConstants.WEBWORK_DISPATCHER_START_UP_LISTENER).split(",");
for (int a=0; a< startupListenerClassNames.length; a++) {
String startupListenerClassName = startupListenerClassNames[a].trim();
try {
ObjectFactory objectFactory = ObjectFactory.getObjectFactory();
StartUpListener startUpListener = (StartUpListener) objectFactory.buildBean(startupListenerClassName, Collections.EMPTY_MAP);
if (LOG.isDebugEnabled()) {
LOG.debug("notifying start up listener ["+startUpListener+"]");
}
startUpListener.startup();
}
catch(Exception e) { // we might also get ClassCastException
LOG.warn("startup listener ["+startupListenerClassName+"] failed to be initialized, it will be ignored", e);
}
}
}
}
|
public static void initialize(ServletContext servletContext) {
synchronized (DispatcherUtils.class) {
if (instance == null) {
instance = new DispatcherUtils(servletContext);
}
}
}
|
public static boolean isPortletSupportActive() {
return portletSupportActive;
}
Returns true, if portlet support is active, false otherwise. |
public void prepare(HttpServletRequest request,
HttpServletResponse response) {
String encoding = null;
if (Configuration.isSet(WebWorkConstants.WEBWORK_I18N_ENCODING)) {
encoding = Configuration.getString(WebWorkConstants.WEBWORK_I18N_ENCODING);
}
Locale locale = null;
if (Configuration.isSet(WebWorkConstants.WEBWORK_LOCALE)) {
locale = LocalizedTextUtil.localeFromString(Configuration.getString(WebWorkConstants.WEBWORK_LOCALE), request.getLocale());
}
if (encoding != null) {
try {
request.setCharacterEncoding(encoding);
} catch (Exception e) {
LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);
}
}
if (locale != null) {
response.setLocale(locale);
}
if (paramsWorkaroundEnabled) {
request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request
}
}
|
public void sendError(HttpServletRequest request,
HttpServletResponse response,
ServletContext ctx,
int code,
Exception e) {
if (devMode) {
response.setContentType("text/html");
try {
freemarker.template.Configuration config = FreemarkerManager.getInstance().getConfiguration(ctx);
Template template = config.getTemplate("/com/opensymphony/webwork/dispatcher/error.ftl");
List chain = new ArrayList();
Throwable cur = e;
chain.add(cur);
while ((cur = cur.getCause()) != null) {
chain.add(cur);
}
HashMap data = new HashMap();
data.put("exception", e);
data.put("unknown", Location.UNKNOWN);
data.put("chain", chain);
data.put("locator", new Locator());
template.process(data, response.getWriter());
response.getWriter().close();
} catch (Exception exp) {
try {
response.sendError(code, "Unable to show problem report: " + exp);
} catch (IOException ex) {
// we're already sending an error, not much else we can do if more stuff breaks
}
}
} else {
try {
// send a http error response to use the servlet defined error handler
// make the exception availible to the web.xml defined error page
request.setAttribute("javax.servlet.error.exception", e);
// for compatibility
request.setAttribute("javax.servlet.jsp.jspException", e);
// send the error response
response.sendError(code, e.getMessage());
} catch (IOException e1) {
// we're already sending an error, not much else we can do if more stuff breaks
}
}
}
Sends an HTTP error response code. |
public void serviceAction(HttpServletRequest request,
HttpServletResponse response,
ServletContext context,
ActionMapping mapping) throws ServletException {
Map extraContext = createContextMap(request, response, mapping, context);
// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);
if (stack != null) {
extraContext.put(ActionContext.VALUE_STACK, new OgnlValueStack(stack));
}
try {
String namespace = mapping.getNamespace();
String name = mapping.getName();
String method = mapping.getMethod();
String id = request.getParameter(XWorkContinuationConfig.CONTINUE_PARAM);
if (id != null) {
// remove the continue key from the params - we don't want to bother setting
// on the value stack since we know it won't work. Besides, this breaks devMode!
Map params = (Map) extraContext.get(ActionContext.PARAMETERS);
params.remove(XWorkContinuationConfig.CONTINUE_PARAM);
// and now put the key in the context to be picked up later by XWork
extraContext.put(XWorkContinuationConfig.CONTINUE_KEY, id);
}
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);
proxy.setMethod(method);
request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());
// if the ActionMapping says to go straight to a result, do it!
if (mapping.getResult() != null) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation());
} else {
proxy.execute();
}
// If there was a previous value stack then set it back onto the request
if (stack != null) {
request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack);
}
} catch (ConfigurationException e) {
LOG.error("Could not find action", e);
sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);
} catch (Exception e) {
throw new ServletException(e);
}
}
Loads the action and executes it. This method first creates the action context from the given
parameters then loads an ActionProxy from the given action name and namespace. After that,
the action is executed and output channels throught the response object. Actions not found are
sent back to the user via the DispatcherUtils#sendError method, using the 404 return code.
All other errors are reported by throwing a ServletException. |
public static void setInstance(DispatcherUtils instance) {
DispatcherUtils.instance = instance;
}
|
public static void setPortletSupportActive(boolean portletSupportActive) {
DispatcherUtils.portletSupportActive = portletSupportActive;
}
Set the flag that portlet support is active or not. |
public HttpServletRequest wrapRequest(HttpServletRequest request,
ServletContext servletContext) throws IOException {
// don't wrap more than once
if (request instanceof WebWorkRequestWrapper) {
return request;
}
if (MultiPartRequest.isMultiPart(request)) {
request = new MultiPartRequestWrapper(request, getSaveDir(servletContext), getMaxSize());
} else {
request = new WebWorkRequestWrapper(request);
}
return request;
}
Wraps and returns the given response or returns the original response object. This is used to transparently
handle multipart data as a wrapped class around the given request. Override this method to handle multipart
requests in a special way or to handle other types of requests. Note, com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper is
flexible - you should look to that first before overriding this method to handle multipart data. |