    001    /*****************************************************************************
    002     * Copyright (C) NanoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     *****************************************************************************/
    009
    010    package org.nanocontainer.webcontainer.groovy;
    011
    012    import groovy.util.NodeBuilder;
    013
    014    import java.util.Map;
    015
    016    import org.mortbay.jetty.servlet.FilterHolder;
    017    import org.mortbay.jetty.servlet.ServletHolder;
    018    import org.nanocontainer.webcontainer.PicoContextHandler;
    019    import org.nanocontainer.webcontainer.groovy.adapters.WaffleAdapter;
    020    import org.picocontainer.MutablePicoContainer;
    021
    022    import javax.servlet.Servlet;
    023
    024    public class ContextBuilder extends NodeBuilder {
    025        private final MutablePicoContainer parentContainer;
    026        private final PicoContextHandler context;
    027
    028        public ContextBuilder(MutablePicoContainer parentContainer, PicoContextHandler context) {
    029            this.parentContainer = parentContainer;
    030            this.context = context;
    031        }
    032            protected Object createNode(Object name, Map map) {
    033                if (name.equals("filter")) {
    034                    return makeFilter(map);
    035                } else if (name.equals("servlet")) {
    036                    return makeServlet(map);
    037                } else if (name.equals("listener")) {
    038                    return makeListener(map);
    039                } else if (name.equals("staticContent")) {
    040                    setStaticContent(map);
    041                    return null;
    042                } else if (name.equals("waffleApp")) {
    043                    return new WaffleAdapter(context, parentContainer, map).getNodeBuilder();
    044                }
    045
    046                return null;
    047            }
    048
    049        private void setStaticContent(Map map) {
    050
    051            if (map.containsKey("welcomePage")) {
    052                context.setStaticContext((String) map.remove("path"), (String) map.remove("welcomePage"));
    053            } else {
    054                context.setStaticContext((String) map.remove("path"));
    055
    056            }
    057
    058        }
    059
    060        private Object makeListener(Map map) {
    061            return context.addListener((Class) map.remove("class"));
    062        }
    063
    064        private Object makeServlet(Map map) {
    065
    066
    067            if (map.containsKey("class")) {
    068                ServletHolder servlet = context.addServletWithMapping(
    069                        (Class) map.remove("class"),
    070                        (String) map.remove("path"));
    071                return new ServletHolderBuilder(servlet);
    072            } else {
    073                Servlet servlet = (Servlet) map.remove("instance");
    074                context.addServletWithMapping(
    075                        servlet,
    076                        (String) map.remove("path"));
    077                return servlet;
    078            }
    079
    080        }
    081
    082        private Object makeFilter(Map map) {
    083            FilterHolder filter = context.addFilterWithMapping(
    084                    (Class) map.remove("class"),
    085                    (String) map.remove("path"),
    086                    extractDispatchers(map));
    087            FilterHolderBuilder builder = new FilterHolderBuilder(filter);
    088            return builder;
    089        }
    090
    091        private int extractDispatchers(Map map) {
    092            Object dispatchers = map.remove("dispatchers");
    093            if ( dispatchers != null ){
    094              return ((Integer) dispatchers).intValue();
    095            }
    096            // default value
    097            return 0;
    098        }
    099
    100        }