aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/JuickApplication.java
blob: e546f5a861656ca1c2d88cfd8b75468af4c0d639 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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.rythmengine.Rythm;
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.*;
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<>();
    private String applicationPath;

    public JuickApplication(Properties conf, String applicationPath) throws IOException {
        this.applicationPath = applicationPath;
        executorService = Executors.newWorkStealingPool();
        initDataSources(conf);
        initSockets(conf);
        initComponents(conf);
        initRythm(conf);
    }

    private void initRythm(Properties conf) {
        String rythmMode = conf.getProperty("rythm_mode", "prod");
        Map<String, Object> map = new HashMap<>();
        map.put("home.template", applicationPath+"/WEB-INF/classes/templates");
        map.put("rythm.engine.mode", rythmMode);
        Rythm.init(map);
    }

    private void initComponents(Properties conf) {
        if (!isHttpDevMode()) {
            addComponent(new S2SComponent(this, conf));
            addComponent(new CrosspostComponent(sql, conf));
            addComponent(new PushComponent(sql, conf));
        }
    }

    private void initSockets(final Properties conf) throws IOException {
        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) {
                        if (!isHttpDevMode()) {
                            logger.log(Level.SEVERE, "www router failed", exc);
                        }
                    }
                });
    }

    private void initDataSources(Properties conf) {
        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);
    }

    public static boolean isHttpDevMode() {
        return "san".equals(System.getenv("USER"));
    }

    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;
    }
}