001: package org.vraptor.component;
002:
003: import org.apache.log4j.Logger;
004: import org.picocontainer.MutablePicoContainer;
005: import org.picocontainer.defaults.DefaultPicoContainer;
006: import org.vraptor.ValidationErrorsFactory;
007: import org.vraptor.scope.ApplicationContext;
008: import org.vraptor.scope.DefaultApplicationContext;
009: import org.vraptor.url.DefaultLogicLocator;
010: import org.vraptor.url.DefaultViewLocator;
011: import org.vraptor.url.LogicLocator;
012: import org.vraptor.url.ViewLocator;
013: import org.vraptor.validator.DefaultValidationErrorsFactory;
014: import org.vraptor.webapp.DefaultComponentManager;
015:
016: import javax.servlet.ServletContext;
017:
018: public class DefaultComponentContainer implements ComponentContainer {
019:
020: private final MutablePicoContainer container;
021:
022: private final static Logger LOG = Logger
023: .getLogger(DefaultComponentContainer.class);
024:
025: private final static Class<?>[] TYPES = new Class<?>[] {
026: ValidationErrorsFactory.class, LogicLocator.class,
027: ComponentManager.class, LogicMethodFactory.class,
028: ParameterInfoProvider.class, ViewLocator.class };
029:
030: public DefaultComponentContainer(ServletContext context) {
031: this ();
032: container.registerComponentInstance(context);
033: for (Class<?> type : TYPES) {
034: String found = context.getInitParameter(type.getName());
035: if (found != null) {
036: try {
037: LOG.debug("Replacing default implementation of "
038: + type.getName() + " with " + found);
039: container.unregisterComponent(type);
040: container.registerComponentImplementation(type,
041: Class.forName(found));
042: } catch (ClassNotFoundException e) {
043: LOG.error(
044: "Unable to load component implementation",
045: e);
046: }
047: }
048: }
049: }
050:
051: public DefaultComponentContainer() {
052: this .container = new DefaultPicoContainer();
053: container.registerComponentImplementation(
054: ValidationErrorsFactory.class,
055: DefaultValidationErrorsFactory.class);
056: container.registerComponentImplementation(
057: ParameterInfoProvider.class,
058: DefaultParameterInfoProvider.class);
059: container.registerComponentImplementation(
060: LogicMethodFactory.class,
061: DefaultLogicMethodFactory.class);
062: container.registerComponentImplementation(LogicLocator.class,
063: DefaultLogicLocator.class);
064: container.registerComponentImplementation(
065: ComponentManager.class, DefaultComponentManager.class);
066: container.registerComponentImplementation(
067: ApplicationContext.class,
068: DefaultApplicationContext.class);
069: container.registerComponentImplementation(ViewLocator.class,
070: DefaultViewLocator.class);
071: }
072:
073: public ValidationErrorsFactory getValidationErrorsFactory() {
074: return (ValidationErrorsFactory) container
075: .getComponentInstance(ValidationErrorsFactory.class);
076: }
077:
078: public LogicLocator getLogicLocator() {
079: return (LogicLocator) container
080: .getComponentInstanceOfType(LogicLocator.class);
081: }
082:
083: public ComponentManager getComponentManager() {
084: return (ComponentManager) container
085: .getComponentInstanceOfType(ComponentManager.class);
086: }
087:
088: public ApplicationContext getApplicationContext() {
089: return (ApplicationContext) container
090: .getComponentInstanceOfType(ApplicationContext.class);
091: }
092:
093: public ViewLocator getViewLocator() {
094: return (ViewLocator) container
095: .getComponentInstanceOfType(ViewLocator.class);
096: }
097:
098: @Deprecated
099: public LogicLocator getURLManager() {
100: return getLogicLocator();
101: }
102:
103: }
|