001    /*
002     * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003     *
004     * This file is part of the Java Data Mining Package (JDMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * JDMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * JDMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with JDMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.jdmp.jetty.coreobject;
025    
026    import org.jdmp.core.CoreObject;
027    import org.jdmp.core.module.Module;
028    import org.jdmp.core.variable.HasVariables;
029    import org.jdmp.jetty.JettyObjectServlet;
030    import org.mortbay.jetty.Server;
031    import org.mortbay.jetty.handler.ContextHandlerCollection;
032    import org.mortbay.jetty.servlet.Context;
033    import org.mortbay.jetty.servlet.ServletHolder;
034    
035    public class JettyCoreObjectServer {
036    
037            private Server server = null;
038    
039            private CoreObject object = null;
040    
041            private int port = 8888;
042    
043            public JettyCoreObjectServer(CoreObject o, int port) {
044                    this.object = o;
045                    this.port = port;
046            }
047    
048            public boolean isConnected() {
049                    return server != null && server.isRunning();
050            }
051    
052            public void start() throws Exception {
053                    server = new Server(port);
054    
055                    ContextHandlerCollection contexts = new ContextHandlerCollection();
056                    server.setHandler(contexts);
057    
058                    Context root = new Context(contexts, "/", Context.SESSIONS);
059                    root.addServlet(new ServletHolder(new JettyCoreObjectServlet(object)), "/*");
060    
061                    Context io = new Context(contexts, "/io", Context.SESSIONS);
062                    io.addServlet(new ServletHolder(new JettyObjectServlet(object)), "/*");
063    
064                    if (object instanceof Module) {
065                            Context console = new Context(contexts, "/console",
066                                            Context.SESSIONS);
067                            console.addServlet(new ServletHolder(new JettyConsoleServlet(
068                                            (Module) object)), "/*");
069                    }
070    
071                    if (object instanceof HasVariables) {
072                            Context list = new Context(contexts, "/variables", Context.SESSIONS);
073                            list.addServlet(new ServletHolder(new JettyCoreObjectListServlet(
074                                            ((HasVariables) object).getVariables())), "/*");
075                            Context listio = new Context(contexts, "/io/variables",
076                                            Context.SESSIONS);
077                            listio.addServlet(new ServletHolder(new JettyObjectServlet(
078                                            ((HasVariables) object).getVariables())), "/*");
079                    }
080    
081                    server.start();
082            }
083    
084            public void stop() throws Exception {
085                    server.stop();
086            }
087    
088    }