Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
Copyright (c) 2014 zuendorf
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.sdmlib.replication;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import org.sdmlib.StrUtil;
import org.sdmlib.replication.util.ObjectSet;
import org.sdmlib.replication.util.SeppelScopeSet;
import org.sdmlib.replication.util.SeppelSpaceCreator;
import org.sdmlib.serialization.PropertyChangeInterface;
import de.uniks.networkparser.interfaces.SendableEntityCreator;
import de.uniks.networkparser.interfaces.UpdateListener;
import de.uniks.networkparser.json.JsonArray;
import de.uniks.networkparser.IdMap;
import de.uniks.networkparser.SimpleEvent;
import de.uniks.networkparser.json.JsonObject;
import de.uniks.networkparser.json.JsonTokener;
import javafx.application.Platform;
import de.uniks.networkparser.interfaces.SendableEntity;
import org.sdmlib.replication.ChangeEventList;
/**
*
* @see ReplicationModel.java
* @see ReplicationModel.java
*/
public class SeppelSpace extends Thread implements PropertyChangeInterface, UpdateListener, SendableEntity
{
//==========================================================================
private LinkedBlockingQueue msgQueue = new LinkedBlockingQueue();
//==========================================================================
@Override
public void run()
{
while (true)
{
try
{
ChannelMsg msg = msgQueue.take();
handleMessage(msg);
}
catch (Exception e)
{
// just try again
e.printStackTrace();
}
}
}
//============================================================================
public void enqueueMsg(SeppelChannel channel, String msg)
{
try
{
final ChannelMsg channelMsg = new ChannelMsg(channel, msg);
if (isJavaFXApplication())
{
Platform.runLater(new Runnable()
{
@Override
public void run()
{
try
{
handleMessage(channelMsg);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
else
{
msgQueue.put(channelMsg);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
public void handleMessage(ChannelMsg msg)
{
try
{
// reconstruct change
JsonObject jsonObject = new JsonObject().withValue(msg.msg);
if ( msg.channel != null && ! msg.channel.isLoginValidated())
{
// this message should be the login message, validate login
try {
// if there is something wrong, just throw an exception and terminate
String spaceId = jsonObject.getString("spaceId");
String login = jsonObject.getString("login");
String pwd = jsonObject.getString("pwd");
// find the space proxy for that user
SeppelSpaceProxy theSpace = this.getSelfProxy().getPartners().hasLoginName(login).hasPassword(pwd).first();
theSpace.withChannel(msg.channel);
// that worked, set channel to valid
msg.channel.setLoginValidated(true);
}
finally
{
try
{
msg.channel.getMsgQueue().put("check");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
// now send all related changes
sendAllChanges(msg.channel);
}
return;
}
// handle change messages
this.isApplyingChangeMsg = true;
ChangeEvent change = new ChangeEvent(jsonObject);
historyPos = history.addChange(change);
if (historyPos < 0)
{
// change already known, ignore
return;
}
// try to apply change
applyChange(change, msg.channel);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
this.isApplyingChangeMsg = false;
}
}
public class ChannelMsg
{
public ChannelMsg(SeppelChannel channel, String msg)
{
this.channel = channel;
this.msg = msg;
}
public SeppelChannel channel;
public String msg;
}
private void applyChange(ChangeEvent change, SeppelChannel sender)
{
applyChangeLocally(change);
sendNewChange(change);
}
public void applyChangeLocally(ChangeEvent change)
{
// get source object
Object object = map.getObject(change.getObjectId());
String objectType = change.getObjectType();
SendableEntityCreator creator = map.getCreator(objectType, false);
if (object == null)
{
// new object, create it
object = creator.getSendableInstance(false);
this.put(change.getObjectId(), object);
}
if (ChangeEvent.PLAIN.equals(change.getPropertyKind()))
{
// simple attribute just do assignment
creator.setValue(object, change.getProperty(), change.getNewValue(), null);
}
else if (ChangeEvent.TO_ONE.equals(change.getPropertyKind()))
{
String newValueId = change.getNewValue();
if (newValueId == null)
{
// set pointer to null
creator.setValue(object, change.getProperty(), null, null);
}
else
{
// provide target object
Object targetObject = map.getObject(newValueId);
if (targetObject == null)
{
// not yet known target, build it.
SendableEntityCreator targetCreator = map.getCreator(change.getValueType(), false);
targetObject = targetCreator.getSendableInstance(false);
this.put(newValueId, targetObject);
}
// assign value
creator.setValue(object, change.getProperty(), targetObject, null);
}
}
else // toMany
{
String targetId = change.getNewValue();
if (targetId == null)
{
// remove the object from the to_many attribute
targetId = change.getOldValue();
Object targetObject = map.getObject(targetId);
if (targetObject != null)
{
creator.setValue(object, change.getProperty(), targetObject, IdMap.REMOVE);
}
}
else
{
// insertion
Object targetObject = map.getObject(targetId);
if (targetObject == null)
{
// create unknown target
SendableEntityCreator targetCreator = map.getCreator(change.getValueType(), false);
targetObject = targetCreator.getSendableInstance(false);
this.put(targetId, targetObject);
}
// assign value
creator.setValue(object, change.getProperty(), targetObject, null);
// try to adjust position
tryToAdjustPosition(object, change.getProperty(), targetObject, creator);
}
}
writeChange(change);
}
private void tryToAdjustPosition(Object object, String property, Object targetObject, SendableEntityCreator creator)
{
Object value = creator.getValue(object, property);
if (value != null && value instanceof List)
{
List