Search in sources :

Example 1 with EngineInvocker

use of com.ramussoft.net.common.tcp.EngineInvocker 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 EngineInvocker

use of com.ramussoft.net.common.tcp.EngineInvocker in project ramus by Vitaliy-Yakovchuk.

the class TcpLightClient method getEngine.

@SuppressWarnings("rawtypes")
@Override
protected Engine getEngine(PluginFactory factory, PersistentFactory persistentFactory) {
    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);
    interfaces.add(ILog.class);
    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);
    final Engine cachedEngine = createCachedEngine(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);
    return engine;
}
Also used : EngineInvocker(com.ramussoft.net.common.tcp.EngineInvocker) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Engine(com.ramussoft.common.Engine) CachedEngine(com.ramussoft.common.cached.CachedEngine) StandardAttributesPlugin(com.ramussoft.core.attribute.standard.StandardAttributesPlugin) LogPlugin(com.ramussoft.client.log.LogPlugin) Plugin(com.ramussoft.common.Plugin) UndoRedoPlugin(com.ramussoft.gui.common.UndoRedoPlugin) GUIPlugin(com.ramussoft.gui.common.GUIPlugin)

Aggregations

Engine (com.ramussoft.common.Engine)2 Plugin (com.ramussoft.common.Plugin)2 StandardAttributesPlugin (com.ramussoft.core.attribute.standard.StandardAttributesPlugin)2 EngineInvocker (com.ramussoft.net.common.tcp.EngineInvocker)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Hashtable (java.util.Hashtable)2 TcpClientConnection (com.ramussoft.client.TcpClientConnection)1 TcpClientEngine (com.ramussoft.client.TcpClientEngine)1 LogPlugin (com.ramussoft.client.log.LogPlugin)1 PluginFactory (com.ramussoft.common.PluginFactory)1 PluginProvider (com.ramussoft.common.PluginProvider)1 CachedEngine (com.ramussoft.common.cached.CachedEngine)1 SimpleAttributePluginSuit (com.ramussoft.core.attribute.simple.SimpleAttributePluginSuit)1 GUIPlugin (com.ramussoft.gui.common.GUIPlugin)1 UndoRedoPlugin (com.ramussoft.gui.common.UndoRedoPlugin)1 JDBCTemplate (com.ramussoft.jdbc.JDBCTemplate)1 RowMapper (com.ramussoft.jdbc.RowMapper)1 UserFactoryImpl (com.ramussoft.server.UserFactoryImpl)1