001: package org.vraptor.webapp;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005:
006: import javax.servlet.ServletContext;
007:
008: import org.apache.log4j.Logger;
009: import org.vraptor.component.ComponentContainer;
010: import org.vraptor.component.ComponentManager;
011: import org.vraptor.component.DefaultComponentContainer;
012: import org.vraptor.config.ConfigException;
013: import org.vraptor.config.Configuration;
014: import org.vraptor.converter.ConverterManager;
015: import org.vraptor.converter.SimpleConverterManager;
016: import org.vraptor.introspector.BasicIntrospector;
017: import org.vraptor.introspector.Introspector;
018: import org.vraptor.plugin.DefaultPluginManager;
019: import org.vraptor.scope.ApplicationContext;
020: import org.vraptor.url.LogicLocator;
021: import org.vraptor.view.RegexViewManager;
022: import org.vraptor.view.ViewManager;
023:
024: /**
025: * A simple web application configuration. It uses the default url manager.
026: *
027: * @author Guilherme Silveira
028: * @author Paulo Silveira
029: */
030: public class DefaultWebApplication implements WebApplication {
031:
032: private static final String JSP_REGEX = "/$1/$2.$3.jsp";
033:
034: private static final Logger LOG = Logger
035: .getLogger(DefaultWebApplication.class);
036:
037: private final Introspector introspector;
038:
039: private final ConverterManager converterManager;
040:
041: private final ComponentManager componentManager;
042:
043: private ViewManager viewManager;
044:
045: private final PluginManager pluginManager;
046:
047: private final ApplicationContext applicationContext;
048:
049: private final LogicLocator logicLocator;
050:
051: private final ComponentContainer container;
052:
053: public DefaultWebApplication(ServletContext context) {
054: this .container = new DefaultComponentContainer(context);
055: applicationContext = container.getApplicationContext();
056: componentManager = container.getComponentManager();
057: logicLocator = container.getLogicLocator();
058: viewManager = new RegexViewManager(JSP_REGEX);
059: converterManager = new SimpleConverterManager();
060: pluginManager = new DefaultPluginManager();
061: introspector = new BasicIntrospector();
062: }
063:
064: public ApplicationContext getApplicationContext() {
065: return applicationContext;
066: }
067:
068: /**
069: * Should be accessed through dependency injection.
070: * @deprecated
071: */
072: @Deprecated
073: public LogicLocator getLogicLocator() {
074: return logicLocator;
075: }
076:
077: /**
078: * Reads the vraptor.xml file. If not found, search for component classes
079: * class loading all of them.
080: *
081: * Inits this configuration: overrides the viewmanager with the
082: * views.properties file
083: *
084: * @throws ConfigException
085: *
086: * @see org.vraptor.webapp.WebApplication#init()
087: */
088: public void init() throws ConfigException {
089:
090: Configuration cfg = new Configuration(applicationContext,
091: componentManager);
092: cfg.load(this );
093:
094: // reads overriden views.properties
095: InputStream viewProperties = DefaultWebApplication.class
096: .getResourceAsStream("/views.properties");
097: if (viewProperties != null) {
098: try {
099: // decorating regex view manager with overriden view manager and
100: // then default view manager
101: this .viewManager = new ViewsPropertiesReader()
102: .overrideViews(viewManager, viewProperties);
103: } catch (IOException e) {
104: LOG.warn("Error reading views.properties", e);
105: }
106: }
107: }
108:
109: public ComponentManager getComponentManager() {
110: return this .componentManager;
111: }
112:
113: public ViewManager getDefaultViewManager() {
114: return this .viewManager;
115: }
116:
117: public ConverterManager getConverterManager() {
118: return this .converterManager;
119: }
120:
121: public void setViewManager(ViewManager viewManager) {
122: this .viewManager = viewManager;
123: }
124:
125: public PluginManager getPluginManager() {
126: return this .pluginManager;
127: }
128:
129: public Introspector getIntrospector() {
130: return introspector;
131: }
132:
133: public ComponentContainer getContainer() {
134: return container;
135: }
136:
137: }
|