@Before public void onSetUp() { smtpPort = smtpPort + (int) (Math.random() * 100); LocalizedTextUtil.addDefaultResourceBundle(Constants.BUNDLE_KEY); // Initialize ActionContext ConfigurationManager configurationManager = new ConfigurationManager(); configurationManager.addContainerProvider(new XWorkConfigurationProvider()); Configuration config = configurationManager.getConfiguration(); Container container = config.getContainer(); ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack(); stack.getContext().put(ActionContext.CONTAINER, container); ActionContext.setContext(new ActionContext(stack.getContext())); ActionContext.getContext().setSession(new HashMap<String, Object>()); // change the port on the mailSender so it doesn't conflict with an // existing SMTP server on localhost JavaMailSenderImpl mailSender = (JavaMailSenderImpl) applicationContext.getBean("mailSender"); mailSender.setPort(getSmtpPort()); mailSender.setHost("localhost"); // populate the request so getRequest().getSession() doesn't fail in BaseAction.java ServletActionContext.setRequest(new MockHttpServletRequest()); }
private void init_FileManager() throws ClassNotFoundException { if (initParams.containsKey(StrutsConstants.STRUTS_FILE_MANAGER)) { final String fileManagerClassName = initParams.get(StrutsConstants.STRUTS_FILE_MANAGER); final Class<FileManager> fileManagerClass = (Class<FileManager>) Class.forName(fileManagerClassName); if (LOG.isInfoEnabled()) { LOG.info("Custom FileManager specified: #0", fileManagerClassName); } configurationManager.addContainerProvider( new FileManagerProvider(fileManagerClass, fileManagerClass.getSimpleName())); } else { // add any other Struts 2 provided implementations of FileManager configurationManager.addContainerProvider( new FileManagerProvider(JBossFileManager.class, "jboss")); } if (initParams.containsKey(StrutsConstants.STRUTS_FILE_MANAGER_FACTORY)) { final String fileManagerFactoryClassName = initParams.get(StrutsConstants.STRUTS_FILE_MANAGER_FACTORY); final Class<FileManagerFactory> fileManagerFactoryClass = (Class<FileManagerFactory>) Class.forName(fileManagerFactoryClassName); if (LOG.isInfoEnabled()) { LOG.info("Custom FileManagerFactory specified: #0", fileManagerFactoryClassName); } configurationManager.addContainerProvider( new FileManagerFactoryProvider(fileManagerFactoryClass)); } }
/** Releases all instances bound to this dispatcher instance. */ public void cleanup() { // clean up ObjectFactory ObjectFactory objectFactory = getContainer().getInstance(ObjectFactory.class); if (objectFactory == null) { if (LOG.isWarnEnabled()) { 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 occurred during destroy() and log it LOG.error( "exception occurred while destroying ObjectFactory [#0]", e, objectFactory.toString()); } } // clean up Dispatcher itself for this thread instance.set(null); // clean up DispatcherListeners if (!dispatcherListeners.isEmpty()) { for (DispatcherListener l : dispatcherListeners) { l.dispatcherDestroyed(this); } } // clean up all interceptors by calling their destroy() method Set<Interceptor> interceptors = new HashSet<Interceptor>(); Collection<PackageConfig> packageConfigs = configurationManager.getConfiguration().getPackageConfigs().values(); for (PackageConfig packageConfig : packageConfigs) { for (Object config : packageConfig.getAllInterceptorConfigs().values()) { if (config instanceof InterceptorStackConfig) { for (InterceptorMapping interceptorMapping : ((InterceptorStackConfig) config).getInterceptors()) { interceptors.add(interceptorMapping.getInterceptor()); } } } } for (Interceptor interceptor : interceptors) { interceptor.destroy(); } // Clear container holder when application is unloaded / server shutdown ContainerHolder.clear(); // cleanup action context ActionContext.setContext(null); // clean up configuration configurationManager.destroyConfiguration(); configurationManager = null; }
/** * Expose the dependency injection container. * * @return Our dependency injection container */ public Container getContainer() { ConfigurationManager mgr = getConfigurationManager(); if (mgr == null) { throw new IllegalStateException("The configuration manager shouldn't be null"); } else { Configuration config = mgr.getConfiguration(); if (config == null) { throw new IllegalStateException("Unable to load configuration"); } else { return config.getContainer(); } } }
/** * Expose the dependency injection container. * * @return Our dependency injection container */ public Container getContainer() { if (ContainerHolder.get() != null) { return ContainerHolder.get(); } ConfigurationManager mgr = getConfigurationManager(); if (mgr == null) { throw new IllegalStateException("The configuration manager shouldn't be null"); } else { Configuration config = mgr.getConfiguration(); if (config == null) { throw new IllegalStateException("Unable to load configuration"); } else { Container container = config.getContainer(); ContainerHolder.store(container); return container; } } }
private void init_TraditionalXmlConfigurations() { String configPaths = initParams.get("config"); if (configPaths == null) { configPaths = DEFAULT_CONFIGURATION_PATHS; } String[] files = configPaths.split("\\s*[,]\\s*"); for (String file : files) { if (file.endsWith(".xml")) { if ("xwork.xml".equals(file)) { configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false)); } else { configurationManager.addContainerProvider( createStrutsXmlConfigurationProvider(file, false, servletContext)); } } else { throw new IllegalArgumentException("Invalid configuration file name"); } } }
private Container init_PreloadConfiguration() { Configuration config = configurationManager.getConfiguration(); Container container = config.getContainer(); boolean reloadi18n = Boolean.valueOf(container.getInstance(String.class, StrutsConstants.STRUTS_I18N_RELOAD)); LocalizedTextUtil.setReloadBundles(reloadi18n); return container; }
@Before public void onSetUp() { smtpPort = (new Random().nextInt(9999 - 1000) + 1000); log.debug("SMTP Port set to: " + smtpPort); LocalizedTextUtil.addDefaultResourceBundle(Constants.BUNDLE_KEY); // Initialize ActionContext ConfigurationManager configurationManager = new ConfigurationManager(); configurationManager.addContainerProvider(new XWorkConfigurationProvider()); Configuration config = configurationManager.getConfiguration(); Container container = config.getContainer(); ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack(); stack.getContext().put(ActionContext.CONTAINER, container); ActionContext.setContext(new ActionContext(stack.getContext())); ActionContext.getContext().setSession(new HashMap<String, Object>()); // populate the request so getRequest().getProject() doesn't fail in BaseAction.java ServletActionContext.setRequest(new MockHttpServletRequest()); }
public void testGlobalResultInheritenceTest() throws Exception { ConfigurationProvider provider = buildConfigurationProvider( "com/opensymphony/xwork2/config/providers/xwork-test-global-result-inheritence.xml"); ConfigurationManager configurationManager = new ConfigurationManager(); configurationManager.addContainerProvider(new XWorkConfigurationProvider()); configurationManager.addContainerProvider(provider); Configuration configuration = configurationManager.getConfiguration(); ActionConfig parentActionConfig = configuration.getRuntimeConfiguration().getActionConfig("/base", "parentAction"); ActionConfig anotherActionConfig = configuration.getRuntimeConfiguration().getActionConfig("/base", "anotherAction"); ActionConfig childActionConfig = configuration.getRuntimeConfiguration().getActionConfig("/base", "childAction"); ResultConfig parentResultConfig1 = parentActionConfig.getResults().get("mockResult1"); ResultConfig parentResultConfig2 = parentActionConfig.getResults().get("mockResult2"); ResultConfig anotherResultConfig1 = anotherActionConfig.getResults().get("mockResult1"); ResultConfig anotherResultConfig2 = anotherActionConfig.getResults().get("mockResult2"); ResultConfig childResultConfig1 = childActionConfig.getResults().get("mockResult1"); ResultConfig childResultConfig2 = childActionConfig.getResults().get("mockResult2"); System.out.println(parentResultConfig1.getParams().get("identity")); System.out.println(parentResultConfig2.getParams().get("identity")); System.out.println(anotherResultConfig1.getParams().get("identity")); System.out.println(anotherResultConfig2.getParams().get("identity")); System.out.println(childResultConfig1.getParams().get("identity")); System.out.println(childResultConfig2.getParams().get("identity")); assertFalse(parentResultConfig1 == anotherResultConfig1); assertFalse(parentResultConfig2 == anotherResultConfig2); assertFalse(parentResultConfig1 == childResultConfig1); assertTrue(parentResultConfig2 == childResultConfig2); }
public void testIfActionMapperIsNullDontServiceAction() throws Exception { MockServletContext servletContext = new MockServletContext(); MockFilterConfig filterConfig = new MockFilterConfig(servletContext); MockHttpServletRequest req = new MockHttpServletRequest(servletContext); MockHttpServletResponse res = new MockHttpServletResponse(); MockFilterChain chain = new MockFilterChain(); final NoOpDispatcher _dispatcher = new NoOpDispatcher(servletContext); ConfigurationManager confManager = new ConfigurationManager(); confManager.setConfiguration(new DefaultConfiguration()); _dispatcher.setConfigurationManager(confManager); Dispatcher.setInstance(_dispatcher); FilterDispatcher filter = new FilterDispatcher() { protected Dispatcher createDispatcher() { return _dispatcher; } }; filter.init(filterConfig); filter.setActionMapper(null); filter.doFilter(req, res, chain); assertFalse(_dispatcher.serviceRequest); }
private void init_FilterInitParameters() { configurationManager.addContainerProvider( new ConfigurationProvider() { public void destroy() {} public void init(Configuration configuration) throws ConfigurationException {} public void loadPackages() throws ConfigurationException {} public boolean needsReload() { return false; } public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException { props.putAll(initParams); } }); }
private void init_CustomConfigurationProviders() { String configProvs = initParams.get("configProviders"); if (configProvs != null) { String[] classes = configProvs.split("\\s*[,]\\s*"); for (String cname : classes) { try { Class cls = ClassLoaderUtil.loadClass(cname, this.getClass()); ConfigurationProvider prov = (ConfigurationProvider) cls.newInstance(); configurationManager.addContainerProvider(prov); } catch (InstantiationException e) { throw new ConfigurationException("Unable to instantiate provider: " + cname, e); } catch (IllegalAccessException e) { throw new ConfigurationException("Unable to access provider: " + cname, e); } catch (ClassNotFoundException e) { throw new ConfigurationException("Unable to locate provider class: " + cname, e); } } } }
/** * Load Action class for mapping and invoke the appropriate Action method, or go directly to the * Result. * * <p>This method first creates the action context from the given parameters, and then loads an * <tt>ActionProxy</tt> from the given action name and namespace. After that, the Action method is * executed and output channels through the response object. Actions not found are sent back to * the user via the {@link Dispatcher#sendError} method, using the 404 return code. All other * errors are reported by throwing a ServletException. * * @param request the HttpServletRequest object * @param response the HttpServletResponse object * @param mapping the action mapping object * @throws ServletException when an unknown error occurs (not a 404, but typically something that * would end up as a 5xx by the servlet container) * @param context Our ServletContext object */ public void serviceAction( HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException { Map<String, Object> 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 ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY); boolean nullStack = stack == null; if (nullStack) { ActionContext ctx = ActionContext.getContext(); if (ctx != null) { stack = ctx.getValueStack(); } } if (stack != null) { extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack)); } String timerKey = "Handling request from Dispatcher"; try { UtilTimerStack.push(timerKey); String namespace = mapping.getNamespace(); String name = mapping.getName(); String method = mapping.getMethod(); Configuration config = configurationManager.getConfiguration(); ActionProxy proxy = config .getContainer() .getInstance(ActionProxyFactory.class) .createActionProxy(namespace, name, method, extraContext, true, false); request.setAttribute( ServletActionContext.STRUTS_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 (!nullStack) { request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack); } } catch (ConfigurationException e) { // WW-2874 Only log error if in devMode if (devMode) { String reqStr = request.getRequestURI(); if (request.getQueryString() != null) { reqStr = reqStr + "?" + request.getQueryString(); } LOG.error("Could not find action or result\n" + reqStr, e); } else { if (LOG.isWarnEnabled()) { LOG.warn("Could not find action or result", e); } } sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e); } catch (Exception e) { if (handleException || devMode) { sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e); } else { throw new ServletException(e); } } finally { UtilTimerStack.pop(timerKey); } }
private void init_AliasStandardObjects() { configurationManager.addContainerProvider(new BeanSelectionProvider()); }
private void init_LegacyStrutsProperties() { configurationManager.addContainerProvider(new LegacyPropertiesConfigurationProvider()); }
private void init_DefaultProperties() { configurationManager.addContainerProvider(new DefaultPropertiesProvider()); }