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