001    /*******************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.
003     * --------------------------------------------------------------------------
004     * The software in this package is published under the terms of the BSD style
005     * license a copy of which has been included with this distribution in the
006     * LICENSE.txt file.
007     ******************************************************************************/
008    package org.picocontainer.web.struts2;
009    
010    import org.picocontainer.web.PicoServletContainerListener;
011    import org.picocontainer.DefaultPicoContainer;
012    import org.picocontainer.MutablePicoContainer;
013    import org.picocontainer.PicoCompositionException;
014    import org.picocontainer.monitors.NullComponentMonitor;
015    import org.picocontainer.monitors.ConsoleComponentMonitor;
016    import org.picocontainer.lifecycle.NullLifecycleStrategy;
017    import org.picocontainer.behaviors.Caching;
018    import org.picocontainer.behaviors.Storing;
019    
020    public class Struts2PicoServletContainerListener extends PicoServletContainerListener {
021    
022        protected ScopedContainers makeScopedContainers() {
023    
024            NullComponentMonitor cm = makeComponentMonitor();
025    
026            NullLifecycleStrategy ls = new NullLifecycleStrategy();
027    
028            DefaultPicoContainer ac = new DefaultPicoContainer(new Caching(), makeParentContainer());
029            Storing ss = new Storing();
030            DefaultPicoContainer sc = new DefaultPicoContainer(ss, ac);
031            Storing rs = new Storing();
032            DefaultPicoContainer rc = new DefaultPicoContainer(rs, ls, sc, cm);
033            return new ScopedContainers(ac,sc,rc,ss,rs);
034    
035        }
036    
037        /**
038         * Struts2 handles whole value objects in some configurations.
039         * This enables lazy instantiation of them    
040         */
041        protected NullComponentMonitor makeComponentMonitor() {
042            return new NullComponentMonitor() {
043                public Object noComponentFound(MutablePicoContainer mutablePicoContainer, Object o) {
044                    return noComponent(mutablePicoContainer, o);
045                }
046    
047                private Object noComponent(MutablePicoContainer mutablePicoContainer, Object o) {
048                    if (o instanceof Class) {
049                        try {
050                            return ((Class) o).newInstance();
051                        } catch (InstantiationException e) {
052                            throw new PicoCompositionException("can't instantiate " + o);
053                        } catch (IllegalAccessException e) {
054                            throw new PicoCompositionException("illegal access " + o);
055                        }
056                    }
057                    return super.noComponentFound(mutablePicoContainer, o);
058                }
059            };
060        }
061    }