All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.sync.remote.RemoteSessionImpl Maven / Gradle / Ivy

There is a newer version: 3.7.10
Show newest version
/*  Copyright 1999 Vince Via [email protected]
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
package com.viaoa.sync.remote;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import com.viaoa.object.OACascade;
import com.viaoa.object.OAObject;
import com.viaoa.object.OAObjectCacheDelegate;
import com.viaoa.object.OAObjectDelegate;
import com.viaoa.object.OAObjectKey;
import com.viaoa.object.OAObjectReflectDelegate;
import com.viaoa.object.OAObjectSaveDelegate;
import com.viaoa.sync.model.ClientInfo;

// see: OAClient

public abstract class RemoteSessionImpl implements RemoteSessionInterface {
	private static Logger LOG = Logger.getLogger(RemoteSessionImpl.class.getName());
	protected ConcurrentHashMap hashServerCache = new ConcurrentHashMap();
	protected ConcurrentHashMap hashLock = new ConcurrentHashMap();
	protected int sessionId;

	public RemoteSessionImpl(int sessionId) {
		this.sessionId = sessionId;
	}

	@Override
	public void addToServerCache(OAObject obj) {
		int guid = OAObjectDelegate.getGuid(obj);
		hashServerCache.put(guid, obj);
		int x = hashServerCache.size();
		if (x % 250 == 0) {
			LOG.fine("sessionId=" + sessionId + ", cache size=" + x + ", obj=" + obj + ", guid=" + guid);
		}
	}

	@Override
	public void removeFromServerCache(int[] guids) {
		if (guids == null) {
			return;
		}
		for (int guid : guids) {
			OAObject obj = hashServerCache.remove(guid);
		}
		int x = hashServerCache.size();
		if (x > 0 && x % 100 == 0) {
			LOG.fine("sessionId=" + sessionId + ", cache size=" + x);
		}
	}

	// called by server to save any client cached objects
	public void saveCache(OACascade cascade, int iCascadeRule) {
		LOG.fine("sessionId=" + sessionId + ", cache size=" + hashServerCache.size());
		for (Map.Entry entry : hashServerCache.entrySet()) {
			OAObject obj = entry.getValue();
			if (!obj.wasDeleted()) {
				OAObjectSaveDelegate.save(obj, iCascadeRule, cascade);
			}
		}
	}

	/**
	 * GUIDs of the objects removed from oaObject cache on server.
	 */
	@Override
	public abstract void removeGuids(int[] guids);

	// called by server when client is disconnected
	public void clearCache() {
		hashServerCache.clear();
		LOG.fine("sessionId=" + sessionId + ", cache size=" + hashServerCache.size());
	}

	@Override
	public boolean setLock(Class objectClass, OAObjectKey objectKey, boolean bLock) {
		OAObject obj = (OAObject) OAObjectCacheDelegate.get(objectClass, objectKey);
		if (obj == null) {
			return false;
		}
		setLock(obj, bLock);
		return true;
	}

	public void setLock(OAObject obj, boolean bLock) {
		if (bLock) {
			hashLock.put(obj, obj);
		} else {
			hashLock.remove(obj);
		}
		LOG.fine("sessionId=" + sessionId + ", cache size=" + hashLock.size() + ", obj=" + obj + ", locked=" + bLock);
	}

	// this is used at disconnect
	public void clearLocks() {
		for (Map.Entry entry : hashLock.entrySet()) {
			OAObject obj = entry.getKey();
			setLock(obj, false);
		}
		LOG.fine("sessionId=" + sessionId + ", cache size=" + hashLock.size());
	}

	@Override
	public OAObject createNewObject(Class clazz) {
		OAObject obj = (OAObject) OAObjectReflectDelegate.createNewObject(clazz);
		LOG.fine("sessionId=" + sessionId + ", obj=" + obj);
		addToServerCache(obj);
		return obj;
	}

	@Override
	public boolean isLockedByThisClient(Class objectClass, OAObjectKey objectKey) {
		Object obj = OAObjectCacheDelegate.get(objectClass, objectKey);
		if (obj == null) {
			return false;
		}
		return (hashLock.get(obj) != null);
	}

	@Override
	public String ping(String msg) {
		return msg;
	}

	@Override
	public void ping2(String msg) {
	}

	@Override
	public abstract boolean isLocked(Class objectClass, OAObjectKey objectKey);

	@Override
	public abstract boolean isLockedByAnotherClient(Class objectClass, OAObjectKey objectKey);

	@Override
	public abstract void sendException(String msg, Throwable ex);

	@Override
	public void update(ClientInfo ci) {
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy