Search in sources :

Example 1 with BinaryAccessFile

use of com.ramussoft.common.journal.BinaryAccessFile 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);
    }
}
Also used : FileIEngineImpl(com.ramussoft.core.impl.FileIEngineImpl) InternetEngine(com.ramussoft.net.common.internet.InternetEngine) Element(com.ramussoft.common.Element) Connection(java.sql.Connection) InternetSyncJournal(com.ramussoft.net.common.internet.InternetSyncJournal) ZipException(java.util.zip.ZipException) FileNotFoundException(java.io.FileNotFoundException) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) SQLException(java.sql.SQLException) IOException(java.io.IOException) BinaryAccessFile(com.ramussoft.common.journal.BinaryAccessFile) AccessRules(com.ramussoft.common.AccessRules) RedoObject(com.ramussoft.net.common.internet.RedoObject) UndoObject(com.ramussoft.net.common.internet.UndoObject) ZipFile(java.util.zip.ZipFile) BinaryAccessFile(com.ramussoft.common.journal.BinaryAccessFile) File(java.io.File) InternetHookJournal(com.ramussoft.net.common.internet.InternetHookJournal) Engine(com.ramussoft.common.Engine) InternetEngine(com.ramussoft.net.common.internet.InternetEngine)

Aggregations

AccessRules (com.ramussoft.common.AccessRules)1 Element (com.ramussoft.common.Element)1 Engine (com.ramussoft.common.Engine)1 BinaryAccessFile (com.ramussoft.common.journal.BinaryAccessFile)1 FileIEngineImpl (com.ramussoft.core.impl.FileIEngineImpl)1 InternetEngine (com.ramussoft.net.common.internet.InternetEngine)1 InternetHookJournal (com.ramussoft.net.common.internet.InternetHookJournal)1 InternetSyncJournal (com.ramussoft.net.common.internet.InternetSyncJournal)1 RedoObject (com.ramussoft.net.common.internet.RedoObject)1 UndoObject (com.ramussoft.net.common.internet.UndoObject)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 InvalidPropertiesFormatException (java.util.InvalidPropertiesFormatException)1 ZipException (java.util.zip.ZipException)1 ZipFile (java.util.zip.ZipFile)1