-
Notifications
You must be signed in to change notification settings - Fork 477
/
LogEventMongoDao.java
133 lines (113 loc) · 5.1 KB
/
LogEventMongoDao.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kaaproject.kaa.server.appenders.mongo.appender;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.kaaproject.kaa.common.dto.logs.LogEventDto;
import org.kaaproject.kaa.server.appenders.mongo.config.gen.MongoDBCredential;
import org.kaaproject.kaa.server.appenders.mongo.config.gen.MongoDbConfig;
import org.kaaproject.kaa.server.appenders.mongo.config.gen.MongoDbServer;
import org.kaaproject.kaa.server.common.log.shared.appender.data.ProfileInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.UncategorizedMongoDbException;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.WriteResultChecking;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import java.util.ArrayList;
import java.util.List;
public class LogEventMongoDao implements LogEventDao {
private static final Logger LOG = LoggerFactory.getLogger(LogEventMongoDao.class);
private MongoClient mongoClient;
private MongoTemplate mongoTemplate;
@SuppressWarnings("deprecation")
public LogEventMongoDao(MongoDbConfig configuration) throws Exception {
List<ServerAddress> seeds = new ArrayList<>(configuration.getMongoServers().size());
for (MongoDbServer server : configuration.getMongoServers()) {
seeds.add(new ServerAddress(server.getHost(), server.getPort()));
}
List<MongoCredential> credentials = new ArrayList<>();
if (configuration.getMongoCredentials() != null) {
for (MongoDBCredential credential : configuration.getMongoCredentials()) {
credentials.add(MongoCredential.createMongoCRCredential(credential.getUser(),
configuration.getDbName(),
credential.getPassword().toCharArray()));
}
}
MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
if (configuration.getConnectionsPerHost() != null) {
optionsBuilder.connectionsPerHost(configuration.getConnectionsPerHost());
}
if (configuration.getMaxWaitTime() != null) {
optionsBuilder.maxWaitTime(configuration.getMaxWaitTime());
}
if (configuration.getConnectionTimeout() != null) {
optionsBuilder.connectTimeout(configuration.getConnectionTimeout());
}
if (configuration.getSocketTimeout() != null) {
optionsBuilder.socketTimeout(configuration.getSocketTimeout());
}
if (configuration.getSocketKeepalive() != null) {
optionsBuilder.socketKeepAlive(configuration.getSocketKeepalive());
}
MongoClientOptions options = optionsBuilder.build();
mongoClient = new MongoClient(seeds, credentials, options);
MongoDbFactory dbFactory = new SimpleMongoDbFactory(mongoClient, configuration.getDbName());
MappingMongoConverter converter = new MappingMongoConverter(dbFactory,
new MongoMappingContext());
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
mongoTemplate = new MongoTemplate(dbFactory, converter);
mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
}
@Override
public void createCollection(String collectionName) {
try {
if (!mongoTemplate.collectionExists(collectionName)) {
mongoTemplate.createCollection(collectionName);
}
} catch (UncategorizedMongoDbException ex) {
LOG.warn("Failed to create collection {} due to", collectionName, ex);
}
}
@Override
public List<LogEvent> save(List<LogEventDto> logEventDtos, ProfileInfo clientProfile,
ProfileInfo serverProfile, String collectionName) {
List<LogEvent> logEvents = new ArrayList<>(logEventDtos.size());
for (LogEventDto logEventDto : logEventDtos) {
logEvents.add(new LogEvent(logEventDto, clientProfile, serverProfile));
}
LOG.debug("Saving {} log events", logEvents.size());
mongoTemplate.insert(logEvents, collectionName);
return logEvents;
}
@Override
public void removeAll(String collectionName) {
LOG.debug("Remove all documents from [{}] collection.", collectionName);
mongoTemplate.dropCollection(collectionName);
}
@Override
public void close() {
if (mongoClient != null) {
mongoClient.close();
}
}
}