use of com.ramussoft.net.common.internet.InternetHookJournal in project ramus by Vitaliy-Yakovchuk.
the class InternetClient method getEngine.
@SuppressWarnings("unused")
@Override
protected Engine getEngine(PluginFactory factory, PersistentFactory pf) {
File tmp = new File(System.getProperty("java.io.tmpdir"), "ramus-" + String.valueOf(System.currentTimeMillis()));
String tmpPath = tmp.getAbsolutePath() + Math.round(Math.random() * 1000);
try {
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:h2:" + tmpPath + File.separator + "client-cache" + ";";
Connection conn = DriverManager.getConnection(url, "sa", "");
template = MemoryDatabase.createStaticTemplate(conn);
String dump = tmpPath + File.separator + "dump.rsf";
new File(tmpPath).mkdirs();
load(dump, factory);
impl = new FileIEngineImpl(0, template, factory, tmpPath) {
@Override
protected boolean deleteStreamBytes(String path) {
try {
return (Boolean) connection.invoke("deleteStream", new Object[] { path });
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public byte[] getStream(String path) {
try {
return (byte[]) connection.invoke("getStream", new Object[] { path });
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
protected void writeStream(String path, byte[] bytes) {
try {
connection.invoke("setStream", new Object[] { path, bytes });
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public long nextValue(String sequence) {
try {
return (Long) connection.invoke("nextValue", new Object[] { sequence });
} catch (Exception e) {
return super.nextValue(sequence);
}
}
};
AccessRules accessor = impl.getAccessor();
String jName = getJournalFileName(impl.getTmpPath() + File.separator);
BinaryAccessFile accessFile = null;
if (jName != null) {
accessFile = new BinaryAccessFile(jName, "rw");
}
final InternetHookJournal journal = new InternetHookJournal(accessFile) {
@Override
public void onUndo(byte[] bs) {
try {
connection.invoke("undo", new Object[] { bs });
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onRedo(byte[] bs) {
try {
connection.invoke("redo", new Object[] { bs });
} catch (Exception e) {
e.printStackTrace();
}
}
};
if (true)
throw new RuntimeException("Not implementated");
InternetEngine internetEngine = new InternetEngine(factory, impl, impl.getPersistentFactory().getRows(), null, accessor) {
@Override
public void replaceElements(Element[] oldElements, Element newElement) {
try {
byte[] bs = (byte[]) connection.invoke("replaceElements", new Object[] { oldElements, newElement });
journal.serverCopy(bs, this);
} catch (Exception e) {
e.printStackTrace();
}
}
};
BinaryAccessFile binaryAccessFile = new BinaryAccessFile(new File(impl.getTmpPath() + File.separator + "hook.tmp"), "rw");
syncJournal = new InternetSyncJournal(binaryAccessFile);
syncJournal.registerEngine(internetEngine);
Engine engine = internetEngine;
if (accessFile != null)
journal.setEnable(true);
engine = (Engine) SuperEngineFactory.createTransactionalEngine(engine, journal);
engine.setPluginProperty("Core", "PluginList", factory.getPlugins());
engine.setPluginProperty("Core", "PluginFactory", factory);
synchronized (startLock) {
for (Object object : runCallbacks) asyncCall(object);
runCallbacks = null;
}
return engine;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations