-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.java
118 lines (96 loc) · 5.68 KB
/
Application.java
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
package org.and1ss.tinyboot;
import org.and1ss.tinyboot.framework.database.connection.JdbcConnectionFactory;
import org.and1ss.tinyboot.framework.database.connection.JdbcConnectionOptions;
import org.and1ss.tinyboot.framework.database.connection.JdbcFixedConnectionPoolFactoryImpl;
import org.and1ss.tinyboot.framework.database.converters.JdbcLongTypeConverter;
import org.and1ss.tinyboot.framework.database.converters.JdbcStringTypeConverter;
import org.and1ss.tinyboot.framework.database.converters.JdbcTypeConverterRegistry;
import org.and1ss.tinyboot.framework.database.converters.JdbcTypeConverterRegistryImpl;
import org.and1ss.tinyboot.framework.database.mapper.ResultSetMapper;
import org.and1ss.tinyboot.framework.database.repository.RepositoryInvocationHandler;
import org.and1ss.tinyboot.framework.database.statement.JdbcStatementFactory;
import org.and1ss.tinyboot.framework.database.statement.JdbcStatementFactoryImpl;
import org.and1ss.tinyboot.framework.database.transaction.TransactionManager;
import org.and1ss.tinyboot.framework.database.transaction.TransactionManagerImpl;
import org.and1ss.tinyboot.framework.database.transaction.TransactionalUtil;
import org.and1ss.tinyboot.framework.util.PropertiesUtil;
import org.and1ss.tinyboot.framework.web.DispatcherServlet;
import org.and1ss.tinyboot.framework.web.mapper.HandlerMapper;
import org.and1ss.tinyboot.framework.web.mapper.HandlerMapperImpl;
import org.and1ss.tinyboot.framework.web.registry.HandlerRegistryImpl;
import org.and1ss.tinyboot.repository.UserRepository;
import org.and1ss.tinyboot.resource.UserResource;
import org.and1ss.tinyboot.service.UserService;
import org.and1ss.tinyboot.service.impl.UserServiceImpl;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Proxy;
import java.util.Properties;
public class Application {
public static void main(String[] args) throws IOException, LifecycleException {
final Properties applicationProperties = PropertiesUtil.loadProperties("application.properties");
final JdbcConnectionOptions jdbcConnectionOptions = getJdbcConnectionOptions(applicationProperties);
final JdbcConnectionFactory jdbcConnectionFactory =
new JdbcFixedConnectionPoolFactoryImpl(10, jdbcConnectionOptions);
final JdbcStatementFactory jdbcStatementFactory = new JdbcStatementFactoryImpl(jdbcConnectionFactory);
final JdbcTypeConverterRegistry jdbcTypeConverterRegistry = getJdbcConverterRegistry();
final TransactionManager transactionManager = new TransactionManagerImpl(jdbcConnectionFactory);
final ResultSetMapper resultSetMapper = new ResultSetMapper(jdbcTypeConverterRegistry);
final UserService userService = getUserService(
jdbcConnectionFactory,
jdbcStatementFactory,
resultSetMapper,
jdbcTypeConverterRegistry,
transactionManager);
final HandlerMapper handlerMapper = new HandlerMapperImpl(new HandlerRegistryImpl());
handlerMapper.registerHandler(new UserResource(userService));
final Tomcat tomcat = getTomcat(8080, handlerMapper);
tomcat.start();
tomcat.getServer().await();
jdbcConnectionFactory.closeOpenedConnections();
}
private static JdbcTypeConverterRegistry getJdbcConverterRegistry() {
return new JdbcTypeConverterRegistryImpl()
.registerTypeConverter(new JdbcStringTypeConverter())
.registerTypeConverter(new JdbcLongTypeConverter());
}
private static JdbcConnectionOptions getJdbcConnectionOptions(Properties applicationProperties) {
return JdbcConnectionOptions.builder()
.url(String.format("jdbc:postgresql://%s", applicationProperties.getProperty("database.url")))
.user(applicationProperties.getProperty("database.user"))
.password(applicationProperties.getProperty("database.password"))
.build();
}
private static UserService getUserService(JdbcConnectionFactory jdbcConnectionFactory,
JdbcStatementFactory jdbcStatementFactory,
ResultSetMapper resultSetMapper,
JdbcTypeConverterRegistry jdbcTypeConverterRegistry,
TransactionManager transactionManager) {
final UserRepository userRepository = (UserRepository) Proxy.newProxyInstance(
UserRepository.class.getClassLoader(),
new Class[]{UserRepository.class},
new RepositoryInvocationHandler(
jdbcStatementFactory, resultSetMapper, (a, b, c) -> null, jdbcTypeConverterRegistry));
return TransactionalUtil.wrapInTransaction(
new UserServiceImpl(userRepository),
UserService.class,
jdbcConnectionFactory,
transactionManager);
}
private static Tomcat getTomcat(int port, HandlerMapper handlerMapper) {
final DispatcherServlet dispatcherServlet = new DispatcherServlet(handlerMapper);
final Tomcat tomcat = new Tomcat();
tomcat.setPort(port);
String contextPath = "";
String docBase = new File(".").getAbsolutePath();
Context context = tomcat.addContext(contextPath, docBase);
String servletName = "DispatcherServlet";
String urlPattern = "/*";
tomcat.addServlet(contextPath, servletName, dispatcherServlet);
context.addServletMappingDecoded(urlPattern, servletName);
return tomcat;
}
}