Search in sources :

Example 1 with TcpClientEngine

use of com.ramussoft.client.TcpClientEngine 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)

Aggregations

TcpClientConnection (com.ramussoft.client.TcpClientConnection)1 TcpClientEngine (com.ramussoft.client.TcpClientEngine)1 Engine (com.ramussoft.common.Engine)1 Plugin (com.ramussoft.common.Plugin)1 PluginFactory (com.ramussoft.common.PluginFactory)1 PluginProvider (com.ramussoft.common.PluginProvider)1 SimpleAttributePluginSuit (com.ramussoft.core.attribute.simple.SimpleAttributePluginSuit)1 StandardAttributesPlugin (com.ramussoft.core.attribute.standard.StandardAttributesPlugin)1 JDBCTemplate (com.ramussoft.jdbc.JDBCTemplate)1 RowMapper (com.ramussoft.jdbc.RowMapper)1 EngineInvocker (com.ramussoft.net.common.tcp.EngineInvocker)1 UserFactoryImpl (com.ramussoft.server.UserFactoryImpl)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 Properties (java.util.Properties)1