    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 javax.servlet.Servlet;
    017
    018    import org.mortbay.jetty.servlet.FilterHolder;
    019    import org.mortbay.jetty.servlet.ServletHolder;
    020    import org.nanocontainer.webcontainer.PicoContext;
    021    import org.nanocontainer.webcontainer.groovy.adapters.NodeBuilderAdapter;
    022    import org.nanocontainer.webcontainer.groovy.adapters.WaffleAdapter;
    023    import org.picocontainer.MutablePicoContainer;
    024
    025    public class ContextBuilder extends NodeBuilder {
    026        private final MutablePicoContainer parentContainer;
    027        private final PicoContext context;
    028
    029        public ContextBuilder(MutablePicoContainer parentContainer, PicoContext context) {
    030            this.parentContainer = parentContainer;
    031            this.context = context;
    032        }
    033
    034        protected Object createNode(Object name, Map map) {
    035            if (name.equals("filter")) {
    036                return makeFilter(map);
    037            } else if (name.equals("servlet")) {
    038                return makeServlet(map);
    039            } else if (name.equals("listener")) {
    040                return makeListener(map);
    041            } else if (name.equals("staticContent")) {
    042                setStaticContent(map);
    043                return null;
    044            } else if (name.equals("adapter")) {
    045                return makeAdapter(map);
    046            } else if (name.equals("waffleApp")) {
    047                return new WaffleAdapter(context, parentContainer, map).getNodeBuilder();
    048            }
    049
    050            return null;
    051        }
    052
    053        private void setStaticContent(Map map) {
    054
    055            if (map.containsKey("welcomePage")) {
    056                context.setStaticContext((String) map.remove("path"), (String) map.remove("welcomePage"));
    057            } else {
    058                context.setStaticContext((String) map.remove("path"));
    059            }
    060
    061        }
    062
    063        private Object makeAdapter(Map map) {
    064            return new NodeBuilderAdapter((String) map.remove("nodeBuilder"), context, parentContainer, map).getNodeBuilder();
    065        }
    066
    067        private Object makeListener(Map map) {
    068            return context.addListener((Class) map.remove("class"));
    069        }
    070
    071        private Object makeServlet(Map map) {
    072
    073            if (map.containsKey("class")) {
    074                ServletHolder servlet = context.addServletWithMapping((Class) map.remove("class"), (String) map
    075                        .remove("path"));
    076                return new ServletHolderBuilder(servlet);
    077            } else {
    078                Servlet servlet = (Servlet) map.remove("instance");
    079                context.addServletWithMapping(servlet, (String) map.remove("path"));
    080                return servlet;
    081            }
    082
    083        }
    084
    085        private Object makeFilter(Map map) {
    086            FilterHolder filter = context.addFilterWithMapping((Class) map.remove("class"), (String) map.remove("path"),
    087                    extractDispatchers(map));
    088            FilterHolderBuilder builder = new FilterHolderBuilder(filter);
    089            return builder;
    090        }
    091
    092        private int extractDispatchers(Map map) {
    093            Object dispatchers = map.remove("dispatchers");
    094            if (dispatchers != null) {
    095                return ((Integer) dispatchers).intValue();
    096            }
    097            // default value
    098            return 0;
    099        }
    100
    101    }