remoteoperator.java
来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 639 行 · 第 1/2 页
JAVA
639 行
} catch (RaplaException rex) {
throw new EntityNotFoundException("Object for id " + id.toString() + " not found due to " + ex.getMessage());
}
return super.resolveId(id);
}
}
public SortedSet getAppointments(User user,Date start,Date end) throws RaplaException {
checkConnected();
updateReservations( user, start, end );
return super.getAppointments( user, start, end );
}
public List getReservations(User user,Date start,Date end) throws RaplaException {
checkConnected();
updateReservations( user, start, end );
return super.getReservations( user, start, end);
}
private String readResultToString( InputStream input) throws IOException
{
InputStreamReader in = new InputStreamReader( input);
char[] buf = new char[4096];
StringBuffer buffer = new StringBuffer();
while ( true )
{
int len = in.read(buf);
if ( len == -1)
{
break;
}
buffer.append( buf, 0,len );
//buf.
}
String result = buffer.toString();
return result;
}
public String call( String serviceName, RemoteMethod method, String[] args ) throws RaplaException
{
return serv.call(serviceName,method, (String[])args);
}
long clientRepositoryVerion = 0;
public class ServerStub implements RemoteStorage {
String call( RemoteMethod method,String[] args) throws RaplaException {
return call( null, method, args);
}
InputStream callInput( RemoteMethod method,String[] args) throws RaplaException {
return callInput( null, method, args);
}
String call( String service, RemoteMethod method,String[] args) throws RaplaException {
try {
InputStream stream = callInput( service,method,args);
String result = readResultToString( stream);
// System.out.println( result );
return result;
} catch (IOException ex) {
throw new RaplaException(ex);
}
}
InputStream callInput( String service,RemoteMethod method,String[] args) throws RaplaException {
try {
String methodName = method.method();
Map argMap = createArgumentMap( method, args);
if ( service != null)
{
methodName = service +"/" + methodName;
}
return connector.call( methodName, argMap );
} catch (SessionExpiredException ex) {
disconnect();
throw ex;
} catch (IOException ex) {
throw new RaplaException(ex);
}
}
private Map createArgumentMap( RemoteMethod method, String[] args ) throws RaplaException
{
Map argMap = new HashMap();
if ( args.length != method.length())
{
throw new RaplaException("Paramter list don't match Expected " + method.length() +" but was " + args.length);
}
for ( int i=0;i<args.length;i++)
{
String argName = method.arg( i );
argMap.put(argName, args[i]);
}
return argMap;
}
public void login(String username,String password) throws RaplaException {
call(LOGIN,new String[] { username,password});
}
public void checkServerVersion(String clientVersion) throws RaplaException {
call(CHECK_SERVER_VERSION, new String[] {clientVersion});
}
public void authenticate(String username,String password) throws RaplaException {
String[] args = new String[] { username,password};
call(AUTHENTICATE,args);
}
public long getServerTime() throws RaplaException {
String timeString = call(GET_SERVER_TIME,new String[] {});
return Long.parseLong( timeString);
}
public boolean canChangePassword() throws RaplaException {
String result = call(CAN_CHANGE_PASSWORD, new String[] {});
return Boolean.valueOf( result).booleanValue();
}
public void changePassword(String username,char[] oldPassword,char[] newPassword) throws RaplaException {
String oldPasswordS = new String(oldPassword);
String newPasswordS = new String(newPassword);
call(CHANGE_PASSWORD,new String[] {username, oldPasswordS, newPasswordS});
}
public List getEntityRecursive(Object id) throws RaplaException {
SimpleIdentifier castedId = (SimpleIdentifier) id;
String idS = castedId.getTypeName() + "_" + String.valueOf(castedId.getKey());
InputStream stream = callInput( GET_ENTITY_RECURSIVE, new String[] {idS});
EntityStore store = new EntityStore( cache, cache.getSuperCategory());
return readIntoStore( stream, store );
}
public List getResources() throws RaplaException {
InputStream stream = callInput( GET_RESOURCES,new String[] {});
EntityStore store = new EntityStore( null, cache.getSuperCategory());
return readIntoStore( stream, store );
}
public List getReservations(Date start,Date end) throws RaplaException {
SerializableDateTimeFormat format = new SerializableDateTimeFormat();
String startS = null;
String endS = null;
if (start!= null)
{
startS = format.formatDate( start);
}
if ( end != null )
{
endS = format.formatDate( end);
}
InputStream stream = callInput( GET_RESERVATIONS,new String[] {startS, endS});
EntityStore store = new EntityStore( cache, cache.getSuperCategory());
return readIntoStore( stream, store );
}
private List readIntoStore( InputStream stream, EntityStore store ) throws RaplaException
{
RaplaContext inputContext = new IOContext().createInputContext(serviceManager,store,idTable);
RaplaInput xmlAdapter = new RaplaInput( getLogger().getChildLogger("reading"));
RaplaMainReader contentHandler = new RaplaMainReader( inputContext);
try
{
xmlAdapter.read(new InputStreamReader( stream,"UTF-8"), contentHandler, false);
}
catch (IOException e)
{
throw new RaplaException( "Error retrieving Data ", e);
}
return new ArrayList(store.getList());
}
public Object createIdentifier(RaplaType raplaType) throws RaplaException {
String raplaTypeS = raplaType.toString();
String id = call(CREATE_IDENTIFIER, new String[] {raplaTypeS});
try
{
return LocalCache.getId( raplaType, id);
}
catch (ParseException e)
{
throw new RaplaException( "Invalid id Object", e);
}
}
public void dispatch(UpdateEvent evt) throws RaplaException {
try
{
String xml = RemoteStorageImpl.createUpdateEvent( serviceManager,cache,evt );
call( DISPATCH, new String[] {xml});
}
catch (IOException e)
{
throw new RaplaException( "Error retrieving Data ", e);
}
}
public void restartServer() throws RaplaException {
call(RESTART_SERVER,new String[] {});
}
public void refresh() throws RaplaException {
String xml = call( REFRESH,new String[] {String.valueOf(clientRepositoryVerion)});
if ( xml.length() < 50 && xml.indexOf( "<uptodate/>")>=0)
{
}
else
{
RemoteOperator.this.refresh( xml);
}
}
public void notifyUpdate() {
if ( isRestarting)
return;
notifyQueue.enqueue(new UpdateCommand());
}
}
private void refresh(String xml) throws RaplaException
{
synchronized (getLock())
{
UpdateEvent evt = RemoteStorageImpl.createUpdateEvent( serviceManager,xml, cache );
Iterator it = evt.getStoreObjects().iterator();
while (it.hasNext()) {
SimpleEntity entity = (SimpleEntity) it.next();
RefEntity cachedVersion = (RefEntity) cache.get(entity.getId());
// Ignore object if its not newer than the one in cache.
if (cachedVersion != null && cachedVersion.getVersion() >= entity.getVersion()) {
//getLogger().debug("already on client " + entity + " version " + cachedVersion.getVersion());
it.remove();
continue;
}
if (getLogger().isDebugEnabled())
getLogger().debug(" storing " + entity.getId()
+ " version: " + entity.getVersion());
}
RemoteOperator.super.resolveEntities
(
evt.getStoreObjects().iterator()
,createEntityResolver(evt.getStoreObjects(),cache)
);
it = evt.getRemoveObjects().iterator();
while (it.hasNext()) {
SimpleEntity entity = (SimpleEntity) it.next();
RefEntity cachedVersion = (RefEntity) cache.get(entity.getId());
// Ignore object, if its not in cache.
if (cachedVersion == null) {
it.remove();
continue;
}
if (getLogger().isDebugEnabled())
getLogger().debug(" removing " + entity.getId()
+ " version: " + entity.getVersion());
}
RemoteOperator.super.resolveEntities
(
evt.getRemoveObjects().iterator()
,createEntityResolver(evt.getStoreObjects(),cache)
);
if ( bSessionActive &&
( evt.getRemoveObjects().size() > 0
|| evt.getStoreObjects().size() > 0 ) ) {
getLogger().info("Objects updated!");
UpdateResult result = update(evt, false);
clientRepositoryVerion = evt.getRepositoryVersion();
// now we can set the cache as updated
fireStorageUpdated(result);
}
clientRepositoryVerion = evt.getRepositoryVersion();
}
}
public void serverDisconnected() {
bSessionActive = false;
}
//******* End ClientInterface *************
class UpdateCommand implements Command {
public void execute() {
if ( !bSessionActive )
return; // We can ignore the update!
try {
serv.refresh();
} catch (Exception ex) {
getLogger().error(ex.getMessage(),ex);
/*
// #TODO. Do we need do disconnect on every notify error?
try {
disconnect();
} catch (RaplaException rex) {
getLogger().error(rex.getMessage(),rex);
}
*/
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?