mainservlet.java

来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 409 行 · 第 1/2 页

JAVA
409
字号
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla;

import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.avalon.framework.container.ContainerUtil;
import org.apache.avalon.framework.logger.Logger;
import org.rapla.components.util.IOUtil;
import org.rapla.components.xmlbundle.I18nBundle;
import org.rapla.framework.Container;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaContextException;
import org.rapla.framework.StartupEnvironment;
import org.rapla.plugin.RaplaExtensionPoints;
import org.rapla.server.RemoteSession;
import org.rapla.server.ServerService;
import org.rapla.server.ShutdownListener;
import org.rapla.server.ShutdownService;
import org.rapla.server.internal.RemoteSessionImpl;
import org.rapla.server.internal.ServerServiceImpl;
import org.rapla.servletpages.DefaultHTMLMenuEntry;
import org.rapla.servletpages.DefaultHTMLMenuExtensionPoint;
import org.rapla.servletpages.RaplaAppletPageGenerator;
import org.rapla.servletpages.RaplaIndexPageGenerator;
import org.rapla.servletpages.RaplaJNLPPageGenerator;
import org.rapla.servletpages.RaplaPageGenerator;
import org.rapla.servletpages.RaplaResourcePageGenerator;
import org.rapla.servletpages.RaplaStatusPageGenerator;
import org.rapla.storage.dbrm.RaplaStorePage;


final public class MainServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    /** The default config filename is raplaserver.xconf*/
    Container raplaMainContainer;
    public final static String DEFAULT_CONFIG_NAME = "raplaserver.xconf";
    I18nBundle m_i18n;
    Collection pageList;
    ServerService serverService;
   
    long serverStartTime;
    String serverId = null;
    
    private File getConfigFile(String entryName, String defaultName) throws ServletException,IOException {
        String configName = getServletConfig().getInitParameter(entryName);
        if (configName == null)
            configName = defaultName;
        if (configName == null)
            throw new ServletException("Must specify " + entryName + " entry in web.xml !");

        File configFile = new File(getServletConfig().getServletContext().getRealPath("/WEB-INF/" + configName));
        if (!configFile.exists()) {
            String message = "ERROR: Config file not found " + configName;
            throw new ServletException(message);
        }
        return configFile.getCanonicalFile();
    }

    public String getServerId()
    {
        return serverId;
    }

    public void setServerId( String serverId )
    {
        this.serverId = serverId;
    }

    /**
     * Initializes Servlet and creates a <code>RaplaMainContainer</code> instance
     *
     * @exception ServletException if an error occurs
     */
    public void init()
        throws ServletException
    {
        String realPath = getServletContext().getRealPath("webclient");
 //       if (realPath != null)
        {
            File webclientFolder= new File(realPath );
            webclientFolder.mkdir();
            copy( "WEB-INF/lib/rapla.jar", "webclient/rapla.jar" );
        }    
        startServer();
    }

    private void copy( String sourceLib, String destLib ) throws ServletException
    {
        if (!new File(getServletContext().getRealPath(sourceLib)).exists())
        {
            return;
        }
        try
        {
            IOUtil.copy( getServletContext().getRealPath(sourceLib), getServletContext().getRealPath(destLib), true);
        }
        catch (IOException e)
        {
            throw new ServletException("Can't copy " + sourceLib + " Cause " + e.getMessage());
        }
    }

    ShutdownListener shutdownListener = new ShutdownListener() {
        public void shutdownInitiated() {
        }
        
        public void shutdownComplete( boolean restart) {
            if ( restart ) {
                Thread restartThread = new Thread() {
                     public void run() {
                         try {
                             log( "Stopping  Server");
                             stopServer();
                             //getServletContext()
                             log( "Restarting Server");
                             startServer();
                         } catch (Exception e) {
                            log( "Error while restarting Server", e );
                         }
                     }
                };
                restartThread.setDaemon( false );
                restartThread.start();
            } 
        }
    };

    void startServer()throws ServletException {
        log("Starting Rapla Servlet");
        Logger logger = null;
        serverStartTime = System.currentTimeMillis();
        
        try
        {
        	File configFile = getConfigFile("config-file",DEFAULT_CONFIG_NAME);
            URL configURL = configFile.toURI().toURL();
            URL logConfigURL = getConfigFile("log-config-file","raplaserver.xlog").toURI().toURL();

            RaplaStartupEnvironment env = new RaplaStartupEnvironment();
            env.setStartupMode( StartupEnvironment.SERVLET);
            env.setConfigURL( configURL );
            env.setLogConfigURL( logConfigURL );

            raplaMainContainer = new RaplaMainContainer( env );
            logger = (Logger)raplaMainContainer.getContext().lookup(Logger.class.getName());
            try {
                //lookup shutdownService
                ShutdownService shutdownService = (ShutdownService) raplaMainContainer.getContext().lookup(ShutdownService.ROLE);
                shutdownService.addShutdownListener( shutdownListener );
            } catch (RaplaContextException ex) {
            	log("No shutdown service found. You must stop the server with ctrl-c!");
            }
            // Start the storage service
            RaplaContext sm = raplaMainContainer.getContext();

            // adds 5 basic pages to the webapplication
            raplaMainContainer.addContainerProvidedComponent( RaplaExtensionPoints.SERVLET_PAGE_EXTENSION, RaplaStatusPageGenerator.class.getName(), "server", null);
            raplaMainContainer.addContainerProvidedComponent( RaplaExtensionPoints.SERVLET_PAGE_EXTENSION, RaplaIndexPageGenerator.class.getName(), "index", null);
            raplaMainContainer.addContainerProvidedComponent( RaplaExtensionPoints.SERVLET_PAGE_EXTENSION, RaplaJNLPPageGenerator.class.getName(), "raplaclient", null);
            raplaMainContainer.addContainerProvidedComponent( RaplaExtensionPoints.SERVLET_PAGE_EXTENSION, RaplaAppletPageGenerator.class.getName(), "raplaapplet", null);
            raplaMainContainer.addContainerProvidedComponent( RaplaExtensionPoints.SERVLET_PAGE_EXTENSION, RaplaResourcePageGenerator.class.getName(), "resource", null);
            raplaMainContainer.addContainerProvidedComponent( RaplaExtensionPoints.SERVLET_PAGE_EXTENSION, RaplaStorePage.class.getName(), "store", null);

            // Index page menu 
            DefaultHTMLMenuExtensionPoint indexMenu = new DefaultHTMLMenuExtensionPoint();
            raplaMainContainer.addContainerProvidedComponentInstance( RaplaExtensionPoints.HTML_MAIN_MENU_EXTENSION_POINT, indexMenu);
            m_i18n = (I18nBundle)sm.lookup(I18nBundle.ROLE +  "/org.rapla.RaplaResources");

            indexMenu.insert( new DefaultHTMLMenuEntry(m_i18n.getString( "start_rapla_with_webstart" ),"rapla?page=raplaclient") );
            indexMenu.insert( new DefaultHTMLMenuEntry(m_i18n.getString( "start_rapla_with_applet" ),"rapla?page=raplaapplet") );
            indexMenu.insert( new DefaultHTMLMenuEntry(m_i18n.getString( "server_status" ),"rapla?page=server") );
            
            String lookupName = ServerService.ROLE ;
            if (serverId != null)
            {
                lookupName += "/" + serverId;
             }
            serverService = (ServerService)sm.lookup( lookupName );

            pageList = serverService.getAllServicesFor( RaplaExtensionPoints.SERVLET_PAGE_EXTENSION);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?