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    import org.mortbay.jetty.Connector;
014    import org.nanocontainer.webcontainer.PicoContextHandler;
015    import org.nanocontainer.webcontainer.PicoJettyServer;
016    import org.nanocontainer.webcontainer.PicoWebAppContext;
017    import org.nanocontainer.script.groovy.NodeCreator;
018    import org.picocontainer.MutablePicoContainer;
019    
020    import java.util.Map;
021    
022    public class ServerBuilder extends NodeBuilder {
023        private final PicoJettyServer server;
024        private final MutablePicoContainer parentContainer;
025    
026        public ServerBuilder(PicoJettyServer server, MutablePicoContainer parentContainer) {
027            this.server = server;
028            this.parentContainer = parentContainer;
029        }
030    
031        protected Object createNode(Object name, Map map) {
032            if (name.equals("context")) {
033                return createContext(map);
034            } else if (name.equals("blockingChannelConnector")) {
035                return createBlockingChannelConnector(map);
036            } else if (name.equals("xmlWebApplication")) {
037                return createXmlWebApplication(map);
038            }
039            return null;
040        }
041    
042        protected Object createBlockingChannelConnector(Map map) {
043            int port = ((Integer) map.remove("port")).intValue();
044            return server.createBlockingChannelConnector((String) map.remove("host"), port);
045        }
046    
047        protected Object createContext(Map map) {
048            boolean sessions = false;
049            if (map.containsKey("sessions")) {
050                sessions = Boolean.valueOf((String) map.remove("sessions")).booleanValue();
051            }
052            PicoContextHandler context = server.createContext((String) map.remove("path"), sessions);
053            return new ContextBuilder(parentContainer, context);
054        }
055    
056        protected Object createXmlWebApplication(Map map) {
057            PicoWebAppContext context = server.addWebApplication((String) map.remove("path"), (String) map.remove("warfile"));
058            return new WarFileBuilder(parentContainer, context);
059        }
060    
061    }