📄 remotestorageimpl.java
字号:
/*--------------------------------------------------------------------------*
| 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.server.internal;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.rapla.components.util.SerializableDateTimeFormat;
import org.rapla.components.xmlbundle.I18nBundle;
import org.rapla.entities.DependencyException;
import org.rapla.entities.RaplaType;
import org.rapla.entities.User;
import org.rapla.entities.storage.EntityResolver;
import org.rapla.entities.storage.Mementable;
import org.rapla.entities.storage.RefEntity;
import org.rapla.entities.storage.internal.SimpleEntity;
import org.rapla.entities.storage.internal.SimpleIdentifier;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaDefaultContext;
import org.rapla.framework.RaplaException;
import org.rapla.server.RemoteService;
import org.rapla.server.RemoteSession;
import org.rapla.server.RemoteStorage;
import org.rapla.server.ShutdownService;
import org.rapla.storage.AuthenticationStore;
import org.rapla.storage.CachableStorageOperator;
import org.rapla.storage.IOContext;
import org.rapla.storage.IdTable;
import org.rapla.storage.LocalCache;
import org.rapla.storage.RaplaSecurityException;
import org.rapla.storage.UpdateEvent;
import org.rapla.storage.impl.EntityStore;
import org.rapla.storage.xml.RaplaInput;
import org.rapla.storage.xml.RaplaMainReader;
import org.rapla.storage.xml.RaplaMainWriter;
/** Provides an adapter for each client-session to their shared storage operator
* Handles security and synchronizing aspects.
* @author ckohlhaas
*
*/
public class RemoteStorageImpl extends AbstractLogEnabled implements RemoteStorage, RemoteService{
CachableStorageOperator operator;
RemoteSession session;
protected SecurityManager security;
boolean authenticationStore;
ServerServiceImpl server;
RemoteStorageImpl(ServerServiceImpl server, RemoteSession session) throws RaplaException {
this.server = server;
this.session = session;
operator = (CachableStorageOperator)getContext().lookup(CachableStorageOperator.ROLE);
security = (SecurityManager)getContext().lookup( SecurityManager.class.getName());
authenticationStore = getContext().has(AuthenticationStore.ROLE);
enableLogging( session.getLogger());
}
private void checkAuthentified() throws RaplaSecurityException {
if (!session.isAuthentified())
throw new RaplaSecurityException("User was not authentified ");
}
private User getSessionUser() throws RaplaException {
return session.getUser();
}
public List getResources() throws RaplaException
{
checkAuthentified();
User user = null;
this.getLogger().debug ("A RemoteServer wants to get all resource-objects.");
synchronized (operator.getLock())
{
return makeTransactionSafe(operator.getVisibleEntities(user));
}
}
public void remoteMethodCall( String methodName, Map args, OutputStream out ) throws RaplaException, IOException, ParseException
{
if ( !session.isAuthentified())
{
throw new SessionExpiredException("Session on the server not valid anymore. Maybe the server has been restarted");
}
synchronized (operator.getLock())
{
BufferedWriter outWriter = new BufferedWriter( new OutputStreamWriter( out,"utf-8"));
RaplaDefaultContext ioContext = new IOContext().createOutputContext( getContext(), operator.getCache(), true, true);
RaplaMainWriter writer = new RaplaMainWriter(ioContext);
writer.setWriter( outWriter);
if ( RemoteStorage.GET_RESOURCES.is( methodName ))
{
List resources = getResources();
writer.printList( resources, Collections.EMPTY_LIST, getRepositoryVersion() );
}
else if ( RemoteStorage.GET_RESERVATIONS.is( methodName))
{
String startS = RemoteStorage.GET_RESERVATIONS.value(args,0);
String endS = RemoteStorage.GET_RESERVATIONS.value(args,1);
SerializableDateTimeFormat format = new SerializableDateTimeFormat();
Date start = null;
if ( startS != null)
{
start = format.parseDate( startS, false);
}
Date end = null;
if ( endS != null)
{
end = format.parseDate( endS, true);
}
List resources = getReservations(start, end);
writer.printList( resources, Collections.EMPTY_LIST, getRepositoryVersion() );
}
else if ( RemoteStorage.DISPATCH.is ( methodName))
{
String xml = RemoteStorage.DISPATCH.value(args,0);
LocalCache cache = operator.getCache();
UpdateEvent event = createUpdateEvent( getContext(),xml, cache );
dispatch( event);
}
else if ( RemoteStorage.CREATE_IDENTIFIER.is( methodName))
{
String typeName = RemoteStorage.CREATE_IDENTIFIER.value(args,0);
RaplaType raplaType = RaplaType.find( typeName );
SimpleIdentifier id =(SimpleIdentifier) operator.createIdentifier( raplaType);
String idString = raplaType.getLocalName() + "_" + id.getKey();
outWriter.write( idString );
}
else if (RemoteStorage.GET_SERVER_TIME.is( methodName))
{
String timeString = String.valueOf( getServerTime());
outWriter.write( timeString );
}
else if (RemoteStorage.GET_ENTITY_RECURSIVE.is(methodName))
{
String idS = RemoteStorage.GET_ENTITY_RECURSIVE.value(args,0);
int index_ = idS.lastIndexOf( '_');
String typeName = idS.substring(0, index_ );
String keyString = idS.substring( index_ + 1);
RaplaType raplaType = RaplaType.find( typeName );
int key = Integer.parseInt( keyString);
// RaplaType raplaType = (RaplaType);
SimpleIdentifier id = new SimpleIdentifier( raplaType, key);
List resources = getEntityRecursive( id);
writer.printList( resources, Collections.EMPTY_LIST, getRepositoryVersion() );
}
else if ( RemoteStorage.CAN_CHANGE_PASSWORD.is( methodName))
{
String resultString = String.valueOf( canChangePassword());
outWriter.write( resultString );
}
else if ( RemoteStorage.CHANGE_PASSWORD.is( methodName))
{
String username = RemoteStorage.CHANGE_PASSWORD.value( args, 0);
String oldPassword = RemoteStorage.CHANGE_PASSWORD.value( args, 1);
String newPassword = RemoteStorage.CHANGE_PASSWORD.value( args, 2);
changePassword( username, oldPassword.toCharArray(), newPassword.toCharArray());
}
else if ( RemoteStorage.REFRESH.equals( methodName))
{
String time = RemoteStorage.REFRESH.value( args, 0);
String xml = server.createUpdateXML(Long.valueOf( time).longValue());
outWriter.write( xml);
}
else if ( RemoteStorage.RESTART_SERVER.equals( methodName))
{
restartServer();
}
else
{
throw new UnsupportedOperationException("Operation " + methodName+ " not available on server.");
}
outWriter.flush();
}
}
private long getRepositoryVersion()
{
return server.repositoryVersion;
}
public I18nBundle getI18n() throws RaplaException {
return (I18nBundle)getContext().lookup(I18nBundle.ROLE + "/org.rapla.RaplaResources");
}
public List getEntityRecursive(Object id) throws RaplaException {
checkAuthentified();
//synchronized (operator.getLock())
{
RefEntity entity = operator.resolveId(id);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -