aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/JuickApplication.java
blob: c4a62c6f0f41d4c0fe3cc1fe25b8f543edcfc238 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.juick;

import com.juick.xmpp.JID;
import com.juick.xmpp.Stream;
import com.juick.xmpp.StreamComponent;
import com.juick.xmpp.s2s.S2SComponent;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.Channels;
import java.nio.channels.CompletionHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by vt on 03/02/16.
 */
public class JuickApplication {

    private static final Logger logger = Logger.getLogger(JuickApplication.class.getName());

    private ExecutorService executorService;
    private Stream router;
    private JdbcTemplate sql;
    private JdbcTemplate sqlSearch;
    private List<JuickComponent> components = new ArrayList<>();

    public JuickApplication(Properties conf) throws IOException {
        executorService = Executors.newWorkStealingPool();
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver"));
        dataSource.setUrl(conf.getProperty("datasource_url"));
        sql = new JdbcTemplate(dataSource);
        DriverManagerDataSource searchDatasource = new DriverManagerDataSource();
        searchDatasource.setDriverClassName("com.mysql.jdbc.Driver");
        searchDatasource.setUrl("jdbc:mysql://127.0.0.1:9306?autoReconnect=true&useUnicode=yes&characterEncoding=utf8&maxAllowedPacket=512000");
        sqlSearch = new JdbcTemplate(searchDatasource);
        AsynchronousSocketChannel socket = AsynchronousSocketChannel.open();
        socket.connect(new InetSocketAddress("localhost", 5347), socket,
                new CompletionHandler<Void, AsynchronousSocketChannel>() {
                    @Override
                    public void completed(Void result, AsynchronousSocketChannel attachment) {
                        router = new StreamComponent(new JID("", "www.juick.com", ""), Channels.newInputStream(socket),
                                Channels.newOutputStream(socket), conf.getProperty("xmpp_password"));
                        router.startParsing();
                    }

                    @Override
                    public void failed(Throwable exc, AsynchronousSocketChannel attachment) {
                        logger.log(Level.SEVERE, "www router failed", exc);
                    }
                });
        addComponent(new S2SComponent(this, conf));
        addComponent(new CrosspostComponent(sql, conf));
        addComponent(new PushComponent(sql, conf));
    }

    public Stream getRouter() {
        return router;
    }

    public JdbcTemplate getSql() {
        return sql;
    }

    public void addComponent(JuickComponent component) {
        components.add(component);
    }

    public void push(com.juick.xmpp.Message msg) {
        for(JuickComponent c : components) {
            c.messageReceived(msg);
        }
    }

    public ExecutorService getExecutorService() {
        return executorService;
    }

    public JdbcTemplate getSqlSearch() {
        return sqlSearch;
    }
}