Search in sources :

Example 1 with PluginProvider

use of com.ramussoft.common.PluginProvider in project ramus by Vitaliy-Yakovchuk.

the class EngineConnection method init.

public void init(String[] args) throws Exception {
    try {
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        java.sql.Connection jdbcConnection = null;
        Properties ps = EngineFactory.getPropeties();
        if (ps != null) {
            try {
                Class.forName(ps.getProperty("driver"));
                jdbcConnection = DriverManager.getConnection(ps.getProperty("url"), ps.getProperty("user"), ps.getProperty("password"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (jdbcConnection == null)
            jdbcConnection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1/ramus_public", "postgres", "postgres");
        template = new JDBCTemplate(jdbcConnection);
        userFactory = new UserFactoryImpl(template);
        JDBCTemplate template = new JDBCTemplate(jdbcConnection);
        String password = template.queryForObjects("SELECT \"password\" FROM users WHERE \"login\"=?", new RowMapper() {

            @Override
            public Object mapRow(ResultSet rs, int index) throws SQLException {
                return rs.getString(1);
            }
        }, new Object[] { "admin" }, true).toString().trim();
        connection = new TcpClientConnection("127.0.0.1", Metadata.TCP_PORT) {

            @Override
            protected void objectReaded(Object object) {
                if (tcpClientEngine != null)
                    tcpClientEngine.call((EvenstHolder) object);
            }
        };
        connection.start();
        connection.invoke("login", new Object[] { "admin", password });
        tcpClientEngine = new TcpClientEngine((EngineInvocker) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { EngineInvocker.class }, new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return connection.invoke(method.getName(), args);
            }
        }), connection);
        List<Class> interfaces = new ArrayList<Class>();
        interfaces.add(Engine.class);
        interfaces.add(Journaled.class);
        List<PluginProvider> suits = new ArrayList<PluginProvider>();
        suits.add(new SimpleAttributePluginSuit());
        initAdditionalPluginSuits(suits);
        PluginFactory factory = createPluginFactory(suits);
        for (Plugin plugin : factory.getPlugins()) if (plugin.getFunctionalInterface() != null)
            interfaces.add(plugin.getFunctionalInterface());
        final Engine engine1 = (Engine) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces.toArray(new Class[interfaces.size()]), tcpClientEngine);
        // new CachedEngine(engine1);
        final Engine cachedEngine = engine1;
        final Hashtable<Method, Object> hashtable = new Hashtable<Method, Object>();
        for (Method m : Engine.class.getMethods()) {
            hashtable.put(m, cachedEngine);
        }
        for (Method m : Cached.class.getMethods()) {
            hashtable.put(m, cachedEngine);
        }
        interfaces.add(Cached.class);
        Engine engine = (Engine) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces.toArray(new Class[interfaces.size()]), new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object o = hashtable.get(method);
                if (o == null)
                    return tcpClientEngine.invoke(proxy, method, args);
                return method.invoke(o, args);
            }
        });
        tcpClientEngine.setEngine(engine);
        for (Plugin plugin : factory.getPlugins()) plugin.init(engine, rules);
        rules = (AccessRules) createDeligate(AccessRules.class);
        this.engine = engine;
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SQLException(java.sql.SQLException) EngineInvocker(com.ramussoft.net.common.tcp.EngineInvocker) ArrayList(java.util.ArrayList) Properties(java.util.Properties) TcpClientEngine(com.ramussoft.client.TcpClientEngine) UserFactoryImpl(com.ramussoft.server.UserFactoryImpl) JDBCTemplate(com.ramussoft.jdbc.JDBCTemplate) ResultSet(java.sql.ResultSet) PluginFactory(com.ramussoft.common.PluginFactory) TcpClientEngine(com.ramussoft.client.TcpClientEngine) Engine(com.ramussoft.common.Engine) RowMapper(com.ramussoft.jdbc.RowMapper) Hashtable(java.util.Hashtable) Method(java.lang.reflect.Method) PluginProvider(com.ramussoft.common.PluginProvider) InvocationHandler(java.lang.reflect.InvocationHandler) SQLException(java.sql.SQLException) SimpleAttributePluginSuit(com.ramussoft.core.attribute.simple.SimpleAttributePluginSuit) TcpClientConnection(com.ramussoft.client.TcpClientConnection) StandardAttributesPlugin(com.ramussoft.core.attribute.standard.StandardAttributesPlugin) Plugin(com.ramussoft.common.Plugin)

Example 2 with PluginProvider

use of com.ramussoft.common.PluginProvider in project ramus by Vitaliy-Yakovchuk.

the class EngineConnection method createPluginFactory.

private PluginFactory createPluginFactory(List<PluginProvider> list) {
    ArrayList<Plugin> plugins = new ArrayList<Plugin>();
    for (PluginProvider suit : list) {
        plugins.addAll(suit.getPlugins());
    }
    PluginFactory factory = new PluginFactory(plugins);
    return factory;
}
Also used : ArrayList(java.util.ArrayList) PluginProvider(com.ramussoft.common.PluginProvider) PluginFactory(com.ramussoft.common.PluginFactory) StandardAttributesPlugin(com.ramussoft.core.attribute.standard.StandardAttributesPlugin) Plugin(com.ramussoft.common.Plugin)

Example 3 with PluginProvider

use of com.ramussoft.common.PluginProvider in project ramus by Vitaliy-Yakovchuk.

the class BaseExporter method export.

/**
 * @param args
 * @throws IOException
 */
public void export(final File file) throws IOException {
    MemoryDatabase database = new MemoryDatabase() {

        @Override
        protected Collection<? extends PluginProvider> getAdditionalSuits() {
            ArrayList<PluginProvider> ps = new ArrayList<PluginProvider>(1);
            ps.add(new IDEF0PluginProvider());
            return ps;
        }

        @Override
        protected File getFile() {
            return null;
        }

        @Override
        public Connection createConnection() throws SQLException {
            return EngineFactory.createNewConnection();
        }

        @Override
        protected String getJournalDirectoryName(String tmp) {
            return null;
        }

        @Override
        protected FileIEngineImpl createFileIEngine(PluginFactory factory) throws ClassNotFoundException, ZipException, IOException {
            return new FileIEngineImpl(0, template, factory, null);
        }
    };
    Engine s = database.getEngine(null);
    FileIEngineImpl impl = (FileIEngineImpl) s.getDeligate();
    EngineFactory factory = new EngineFactory();
    IEngine d = factory.getEngine();
    ZipOutputStream out = impl.saveToFileNotCloseFile(file);
    for (String streamName : d.getStreamNames()) {
        byte[] bs = d.getStream(streamName);
        if (bs == null) {
        } else {
            if (streamName.startsWith("/"))
                streamName = streamName.substring(1);
            out.putNextEntry(new ZipEntry(streamName));
            out.write(bs);
        }
    }
    out.close();
    try {
        factory.getTemplate().getConnection().close();
        impl.getConnection().close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : FileIEngineImpl(com.ramussoft.core.impl.FileIEngineImpl) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) IEngine(com.ramussoft.common.IEngine) IDEF0PluginProvider(com.ramussoft.idef0.IDEF0PluginProvider) PluginProvider(com.ramussoft.common.PluginProvider) IDEF0PluginProvider(com.ramussoft.idef0.IDEF0PluginProvider) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) SQLException(java.sql.SQLException) ZipOutputStream(java.util.zip.ZipOutputStream) MemoryDatabase(com.ramussoft.database.MemoryDatabase) PluginFactory(com.ramussoft.common.PluginFactory) Engine(com.ramussoft.common.Engine) IEngine(com.ramussoft.common.IEngine)

Example 4 with PluginProvider

use of com.ramussoft.common.PluginProvider in project ramus by Vitaliy-Yakovchuk.

the class Runner method recoverySession.

public boolean recoverySession(final String sessionPath, final File sourceFile) {
    Window rFrame = new Window(null);
    String recovering = GlobalResourcesManager.getString("File.Recovering");
    rFrame.add(new Label(MessageFormat.format(recovering, ((sourceFile == null) ? GlobalResourcesManager.getString("Session.NoName") : sourceFile.getName()))));
    rFrame.pack();
    rFrame.setLocationRelativeTo(null);
    rFrame.setVisible(true);
    final String s = sessionPath + File.separator + "source.rms";
    MemoryDatabase database = new MemoryDatabase() {

        @Override
        protected Collection<? extends PluginProvider> getAdditionalSuits() {
            ArrayList<PluginProvider> ps = new ArrayList<PluginProvider>(1);
            initAdditionalPluginSuits(ps);
            return ps;
        }

        @Override
        protected File getFile() {
            File file = new File(s);
            if (file.exists())
                return file;
            return null;
        }

        protected FileIEngineImpl createFileIEngine(PluginFactory factory) throws ClassNotFoundException, ZipException, IOException {
            return new FileIEngineImpl(0, template, factory, sessionPath);
        }
    };
    final Engine engine = database.getEngine(null);
    DirectoryJournalFactory factory = database.getJournalFactory();
    Journal[] journals = factory.loadJournals(database.getJournaledEngine());
    if (journals.length == 0)
        return false;
    boolean exist = false;
    Journal.RedoCallback redoCallback = new Journal.RedoCallback() {

        boolean hadStartUserTransaction;

        @Override
        public boolean execute(Command command) {
            if (command instanceof StartUserTransactionCommand) {
                hadStartUserTransaction = true;
            }
            return hadStartUserTransaction;
        }
    };
    for (Journal journal : journals) {
        try {
            Command command = null;
            while (journal.canRedo()) {
                command = journal.redo(redoCallback);
            }
            if ((journal.getPointer() == 0l) || (command instanceof StopUndoPointCommand)) {
                rFrame.setVisible(false);
                continue;
            } else
                exist = true;
            if (!(command instanceof EndUserTransactionCommand))
                throw new Exception();
        } catch (Exception e) {
            e.printStackTrace();
            while (journal.canUndo()) {
                if (journal.undo() instanceof StartUserTransactionCommand)
                    break;
            }
        }
    }
    if (!exist) {
        try {
            ((FileIEngineImpl) engine.getDeligate()).close();
            for (Journal journal : journals) journal.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    ((FileIEngineImpl) engine.getDeligate()).recoveryStreams();
    engine.setPluginProperty(CORE, "Changed", Boolean.TRUE);
    engine.setActiveBranch(-1l);
    final AccessRules accessor = database.getAccessRules(null);
    rFrame.setVisible(false);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            SplashScreen screen = new SplashScreen() {

                /**
                 */
                private static final long serialVersionUID = -2727237354089088151L;

                @Override
                protected String getImageName() {
                    return Runner.this.getSplashImageName();
                }
            };
            screen.setLocationRelativeTo(null);
            screen.setVisible(true);
            final JFrame frame = openInNewWindows(engine, accessor, sourceFile, true);
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    String recovered = GlobalResourcesManager.getString("File.Recovered");
                    frame.setTitle(frame.getTitle() + " " + recovered);
                }
            });
            screen.setVisible(false);
        }
    };
    recoveredCount++;
    Thread thread = new Thread(runnable);
    thread.start();
    return true;
}
Also used : Label(java.awt.Label) ArrayList(java.util.ArrayList) Journal(com.ramussoft.common.journal.Journal) JFrame(javax.swing.JFrame) SplashScreen(com.ramussoft.gui.common.SplashScreen) SimleGUIPluginFactory(com.ramussoft.gui.core.simple.SimleGUIPluginFactory) GUIPluginFactory(com.ramussoft.gui.core.GUIPluginFactory) AbstractGUIPluginFactory(com.ramussoft.gui.common.AbstractGUIPluginFactory) PluginFactory(com.ramussoft.common.PluginFactory) Engine(com.ramussoft.common.Engine) Window(java.awt.Window) DirectoryJournalFactory(com.ramussoft.common.journal.DirectoryJournalFactory) FileIEngineImpl(com.ramussoft.core.impl.FileIEngineImpl) StopUndoPointCommand(com.ramussoft.common.journal.StopUndoPointCommand) PluginProvider(com.ramussoft.common.PluginProvider) IOException(java.io.IOException) ZipException(java.util.zip.ZipException) FileMinimumVersionException(com.ramussoft.core.impl.FileMinimumVersionException) IOException(java.io.IOException) EndUserTransactionCommand(com.ramussoft.common.journal.command.EndUserTransactionCommand) Command(com.ramussoft.common.journal.command.Command) StartUserTransactionCommand(com.ramussoft.common.journal.command.StartUserTransactionCommand) EndUserTransactionCommand(com.ramussoft.common.journal.command.EndUserTransactionCommand) StopUndoPointCommand(com.ramussoft.common.journal.StopUndoPointCommand) MemoryDatabase(com.ramussoft.database.MemoryDatabase) AccessRules(com.ramussoft.common.AccessRules) StartUserTransactionCommand(com.ramussoft.common.journal.command.StartUserTransactionCommand) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 5 with PluginProvider

use of com.ramussoft.common.PluginProvider in project ramus by Vitaliy-Yakovchuk.

the class Runner method createDatabase.

public MemoryDatabase createDatabase(final File file) {
    MemoryDatabase database = new MemoryDatabase(true) {

        @Override
        protected Collection<? extends PluginProvider> getAdditionalSuits() {
            ArrayList<PluginProvider> ps = new ArrayList<PluginProvider>(1);
            initAdditionalPluginSuits(ps);
            return ps;
        }

        @Override
        protected File getFile() {
            return file;
        }
    };
    return database;
}
Also used : MemoryDatabase(com.ramussoft.database.MemoryDatabase) ArrayList(java.util.ArrayList) PluginProvider(com.ramussoft.common.PluginProvider)

Aggregations

PluginProvider (com.ramussoft.common.PluginProvider)16 ArrayList (java.util.ArrayList)15 PluginFactory (com.ramussoft.common.PluginFactory)13 Engine (com.ramussoft.common.Engine)8 Plugin (com.ramussoft.common.Plugin)7 IDEF0PluginProvider (com.ramussoft.idef0.IDEF0PluginProvider)6 SQLException (java.sql.SQLException)6 MemoryDatabase (com.ramussoft.database.MemoryDatabase)5 IOException (java.io.IOException)5 AccessRules (com.ramussoft.common.AccessRules)4 IEngine (com.ramussoft.common.IEngine)4 SimpleAttributePluginSuit (com.ramussoft.core.attribute.simple.SimpleAttributePluginSuit)4 PersistentFactory (com.ramussoft.core.persistent.PersistentFactory)4 JournaledEngine (com.ramussoft.common.journal.JournaledEngine)3 FileIEngineImpl (com.ramussoft.core.impl.FileIEngineImpl)3 PersistentsPlugin (com.ramussoft.core.persistent.PersistentsPlugin)3 PersistentsPluginProvider (com.ramussoft.core.persistent.PersistentsPluginProvider)3 AbstractGUIPluginFactory (com.ramussoft.gui.common.AbstractGUIPluginFactory)3 GUIPluginFactory (com.ramussoft.gui.core.GUIPluginFactory)3 SimleGUIPluginFactory (com.ramussoft.gui.core.simple.SimleGUIPluginFactory)3