⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverserviceimpl.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Praktikum Gruppe2?, 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.server.internal;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.rapla.components.util.DateTools;
import org.rapla.components.xmlbundle.I18nBundle;
import org.rapla.entities.Entity;
import org.rapla.entities.RaplaObject;
import org.rapla.entities.User;
import org.rapla.entities.configuration.Preferences;
import org.rapla.entities.configuration.RaplaConfiguration;
import org.rapla.entities.domain.Permission;
import org.rapla.entities.internal.UserImpl;
import org.rapla.entities.storage.RefEntity;
import org.rapla.facade.ClientFacade;
import org.rapla.facade.UpdateModule;
import org.rapla.facade.internal.FacadeImpl;
import org.rapla.framework.PluginDescriptor;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaContextException;
import org.rapla.framework.RaplaException;
import org.rapla.framework.internal.ContainerImpl;
import org.rapla.plugin.RaplaExtensionPoints;
import org.rapla.server.RaplaRemoteServiceFactory;
import org.rapla.server.RemoteService;
import org.rapla.server.RemoteSession;
import org.rapla.server.RemoteStorage;
import org.rapla.server.ServerService;
import org.rapla.storage.AuthenticationStore;
import org.rapla.storage.CachableStorageOperator;
import org.rapla.storage.StorageOperator;
import org.rapla.storage.StorageUpdateListener;
import org.rapla.storage.UpdateEvent;
import org.rapla.storage.UpdateResult;

/** Default implementation of StorageService.
 * <p>Sample configuration 1:
 <pre>
 &lt;storage id="storage" >
 &lt;store>file&lt;/store>
 &lt;/storage>
 </pre>
 * The store value contains the id of a storage-component.
 * Storage-Components are all components that implement the
 * <code>CachableStorageOperator<code> interface.
 * </p>
 * <p>Sample configuration 2:
 <pre>
 &lt;storage id="storage">
 &lt;store>remote&lt;/store>
 &lt;login>
 &lt;username>homer&lt;/username>
 &lt;password>duffs&lt;/password>
 &lt;/login>
 &lt;/storage>
 </pre>
 * This is configuration where the servers are cascaded. The remote-server
 * normally needs an authentication. The provided account should have
 * admin privileges.
 * </p>

 @see ServerService
 */

public class ServerServiceImpl extends ContainerImpl implements StorageUpdateListener, ServerService
{
    protected CachableStorageOperator operator;
    protected I18nBundle i18n;
    protected AuthenticationStore authenticationStore;
    private Configuration operatorConfig;
    List pluginList;

    ClientFacade facade;

    private Map updateMap = new HashMap();
    private Map removeMap = new HashMap();

    long repositoryVersion = 0;
    long cleanupPointVersion = 0;

    public ServerServiceImpl( RaplaContext parentContext, Configuration config ) throws RaplaException
    {
        super( parentContext, config );
        pluginList = (List) parentContext.lookup( PluginDescriptor.PLUGIN_LIST );
        i18n = (I18nBundle) parentContext.lookup( I18nBundle.ROLE + "/org.rapla.RaplaResources" );
        Configuration login = config.getChild( "login" );
        String username = login.getChild( "username" ).getValue( null );
        String password = login.getChild( "password" ).getValue( null );
        operatorConfig = config.getChild( "store" );

        try
        {
            operator = (CachableStorageOperator) getContext().lookup(
                                                                      CachableStorageOperator.ROLE
                                                                              + "/"
                                                                              + operatorConfig.getValue( "*" ) );
            addContainerProvidedComponentInstance( CachableStorageOperator.ROLE, operator );
            addContainerProvidedComponentInstance( StorageOperator.ROLE, operator );
            facade = new FacadeImpl( getContext(), new DefaultConfiguration( "facade" ), getLogger() );
            addContainerProvidedComponentInstance( ClientFacade.ROLE, facade );
            addContainerProvidedComponent( SecurityManager.class.getName() );
            addContainerProvidedComponentInstance( RaplaRemoteServiceFactory.ROLE, RemoteStorage.ROLE,
                                                   new RaplaRemoteServiceFactory()
                                                   {
                                                       public RemoteService createRemoteService( RemoteSession session )
                                                               throws RaplaException
                                                       {
                                                           return new RemoteStorageImpl( ServerServiceImpl.this,
                                                                                         session );
                                                       }

                                                   } );
        }
        catch ( RaplaContextException ex )
        {
            throw new RaplaContextException( CachableStorageOperator.ROLE, "Store at "
                    + operatorConfig.getLocation()
                    + " is not found (or could not be initialized) ", ex );
        }

        operator.addStorageUpdateListener( this );
        if ( username != null && password != null )
            operator.connect( username, password.toCharArray() );
        else
            operator.connect();

        initializePlugins( pluginList, operator.getPreferences( null ) );

        if ( getContext().has( AuthenticationStore.ROLE ) )
        {
            try 
       	    {
               	authenticationStore = (AuthenticationStore) getContext().lookup( AuthenticationStore.ROLE );
                getLogger().info( " Using AuthenticationStore " + authenticationStore.getName() );
            } 
            catch ( RaplaException ex)
            {
                getLogger().error( " Can't initialize configured authentication store. Using default authentication." , ex);
            }
        }
        initEventCleanup();
    }

    /**
     * @see org.rapla.server.ServerService#getFacade()
     */
    public ClientFacade getFacade()
    {
        return facade;
    }

    protected void initializePlugins( List pluginList, Preferences preferences ) throws RaplaException
    {

        RaplaConfiguration raplaConfig = (RaplaConfiguration) preferences.getEntry( "org.rapla.plugin" );
        // Add plugin configs
        for ( Iterator it = pluginList.iterator(); it.hasNext(); )
        {
            PluginDescriptor pluginDescriptor = (PluginDescriptor) it.next();
            String pluginClassname = pluginDescriptor.getClass().getName();
            Configuration pluginConfig = null;
            if ( raplaConfig != null )
            {
                pluginConfig = raplaConfig.find( "class", pluginClassname );
            }
            if ( pluginConfig == null )
            {
                pluginConfig = new DefaultConfiguration( "plugin" );
            }
            pluginDescriptor.provideServices( this, pluginConfig );
        }

        Collection clientPlugins = getAllServicesFor( RaplaExtensionPoints.SERVER_EXTENSION );
        // start plugins
        for ( Iterator it = clientPlugins.iterator(); it.hasNext(); )
        {
            String hint = (String) it.next();
            try
            {
                getContext().lookup( RaplaExtensionPoints.SERVER_EXTENSION + "/" + hint );
                getLogger().info( "Initialize " + hint );
            }
            catch ( RaplaContextException ex )
            {
                getLogger().error( "Can't initialize " + hint, ex );
            }
        }
    }

    public void start() throws Exception
    {
        getLogger().info( "Storage service started" );
    }

    public void stop() throws Exception
    {
        operator.removeStorageUpdateListener( this );
        try
        {
            operator.disconnect();
        }
        finally
        {
        }
        getLogger().info( "Storage service stopped" );
    }

    public void dispose()
    {
        super.dispose();
    }

    public String login( Map args ) throws RaplaException
    {
        String username = RemoteStorage.LOGIN.value( args, 0 );
        String password = RemoteStorage.LOGIN.value( args, 1 );
        login( username, password );
        return username;
    }

    public byte[] dispatch( RemoteSession session, String methodName, Map args ) throws Exception
    {
        if ( RemoteStorage.CHECK_SERVER_VERSION.is( methodName ) )
        {
            String clientVersion = RemoteStorage.CHECK_SERVER_VERSION.value( args, 0 );
            checkServerVersion( clientVersion );
        }
        else if ( RemoteStorage.LOGIN.is( methodName ) )
        {

⌨️ 快捷键说明

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