Project

General

Profile

« Previous | Next » 

Revision 38782

[maven-release-plugin] copy for tag dnet-mongo-logging-1.1.2

View differences:

modules/dnet-mongo-logging/tags/dnet-mongo-logging-1.1.2/deploy.info
1
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/dnet-mongo-logging/trunk/", "deploy_repository": "dnet4-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet4-snapshots", "name": "dnet-mongo-logging"}
modules/dnet-mongo-logging/tags/dnet-mongo-logging-1.1.2/src/main/java/eu/dnetlib/common/logging/dao/DnetLoggerMongoDao.java
1
package eu.dnetlib.common.logging.dao;
2

  
3
import java.util.Date;
4
import java.util.Iterator;
5
import java.util.Map;
6
import java.util.Map.Entry;
7

  
8
import org.apache.commons.lang.StringUtils;
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.springframework.beans.factory.annotation.Required;
12

  
13
import com.google.common.collect.Maps;
14
import com.mongodb.BasicDBObject;
15
import com.mongodb.BasicDBObjectBuilder;
16
import com.mongodb.DB;
17
import com.mongodb.DBCollection;
18
import com.mongodb.DBCursor;
19
import com.mongodb.DBObject;
20
import com.mongodb.QueryBuilder;
21

  
22
import eu.dnetlib.common.logging.LogMessage;
23

  
24
public class DnetLoggerMongoDao implements DnetLoggerDao {
25

  
26
	private static final Log log = LogFactory.getLog(DnetLoggerMongoDao.class);
27

  
28
	private DB db;
29

  
30
	@Override
31
	public void init(final String collection) {
32
		if (!db.collectionExists(collection)) {
33
			log.info(String.format("creating collection %s", collection));
34
			db.createCollection(collection, null);
35
		}
36
	}
37

  
38
	@Override
39
	public void configureIndex(final String collection, final Map<String, IndexConf> conf) {
40
		final DBCollection coll = db.getCollection(collection);
41
		coll.dropIndexes();
42

  
43
		for (String key : conf.keySet()) {
44
			coll.createIndex(new BasicDBObject(key, 1));
45
		}
46
	}
47

  
48
	@Override
49
	public void writeLog(final String collection, final Map<String, Object> map) {
50
		final DBCollection coll = db.getCollection(collection);
51
		DBObject obj = BasicDBObjectBuilder.start(replaceKeyNames(map)).get();
52
		coll.insert(obj);
53
	}
54

  
55
	private Map<String, Object> replaceKeyNames(final Map<String, Object> inputMap) {
56
		final Map<String, Object> outMap = Maps.newHashMap();
57

  
58
		if (inputMap != null) {
59
			for (Entry<String, Object> e : inputMap.entrySet()) {
60
				final String k = e.getKey();
61
				if (!StringUtils.isBlank(k)) {
62
					Object v = e.getValue();
63
					outMap.put(k.replaceAll("\\.", "_"), v != null ? v : "null");
64
				}
65
			}
66
		}
67
		return outMap;
68
	}
69

  
70
	@Override
71
	public Map<String, String> findOne(final String collection, final String key, final String value) {
72
		final DBCollection coll = db.getCollection(collection);
73
		final DBObject obj = coll.findOne(new BasicDBObject(key, value));
74
		return dbObject2Map(obj);
75
	}
76

  
77
	@Override
78
	public Iterator<Map<String, String>> obtainLogIterator(final String collection) {
79
		final DBCollection coll = db.getCollection(collection);
80
		return iter(collection, coll.find());
81
	}
82

  
83
	@Override
84
	public Iterator<Map<String, String>> find(final String collection, final String key, final String value) {
85
		final DBCollection coll = db.getCollection(collection);
86
		return iter(collection, coll.find(new BasicDBObject(key, value)));
87
	}
88

  
89
	@Override
90
	public Iterator<Map<String, String>> find(final String collection, final Map<String, Object> criteria) {
91
		final DBCollection coll = db.getCollection(collection);
92
		return iter(collection, coll.find(new BasicDBObject(criteria)));
93
	}
94

  
95
	@Override
96
	public Iterator<Map<String, String>> findByDateRange(final String collection, final Date startDate, final Date endDate) {
97
		final DBCollection coll = db.getCollection(collection);
98
		DBObject mongoQueryObject = dateRangeQuery(startDate, endDate);
99
		// System.out.println(mongoQueryObject);
100
		return iter(collection, coll.find(mongoQueryObject));
101
	}
102

  
103
	@Override
104
	public Iterator<Map<String, String>> findByDateRange(final String collection, final Date startDate, final Date endDate, final String key, final String value) {
105
		final DBCollection coll = db.getCollection(collection);
106
		final DBObject dateQuery = dateRangeQuery(startDate, endDate);
107
		final BasicDBObject customQuery = new BasicDBObject(key, value);
108

  
109
		return iter(collection, coll.find(QueryBuilder.start().and(dateQuery, customQuery).get()));
110
	}
111

  
112
	@Override
113
	public Iterator<Map<String, String>> findByDateRange(final String collection, final Date startDate, final Date endDate, final Map<String, Object> criteria) {
114
		final DBCollection coll = db.getCollection(collection);
115
		final DBObject dateQuery = dateRangeQuery(startDate, endDate);
116
		final BasicDBObject customQuery = new BasicDBObject(criteria);
117

  
118
		return iter(collection, coll.find(QueryBuilder.start().and(dateQuery, customQuery).get()));
119
	}
120

  
121
	private BasicDBObject dateRangeQuery(final Date startDate, final Date endDate) {
122
		return new BasicDBObject(LogMessage.LOG_DATE_FIELD, BasicDBObjectBuilder.start("$gte", startDate.getTime()).append("$lt", endDate.getTime()).get());
123
	}
124

  
125
	private Iterator<Map<String, String>> iter(final String collection, final DBCursor cursor) {
126
		return new Iterator<Map<String, String>>() {
127

  
128
			@Override
129
			public boolean hasNext() {
130
				return cursor.hasNext();
131
			}
132

  
133
			@Override
134
			public Map<String, String> next() {
135
				return dbObject2Map(cursor.next());
136
			}
137

  
138
			@Override
139
			public void remove() {
140
				throw new RuntimeException("NOT IMPLEMENTED");
141
			}
142
		};
143
	}
144

  
145
	public DB getDb() {
146
		return db;
147
	}
148

  
149
	@Required
150
	public void setDb(final DB db) {
151
		this.db = db;
152
	}
153

  
154
	private Map<String, String> dbObject2Map(final DBObject obj) {
155
		final Map<String, String> res = Maps.newHashMap();
156
		if (obj != null) {
157
			for (String k : obj.keySet()) {
158
				res.put(k, "" + obj.get(k));
159
			}
160
		}
161
		return res;
162
	}
163

  
164
}
modules/dnet-mongo-logging/tags/dnet-mongo-logging-1.1.2/src/main/resources/eu/dnetlib/common/logging/dao/applicationContext-dnet-mongo-logging.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
4
	xmlns:sec="http://cxf.apache.org/configuration/security" xmlns:wsa="http://cxf.apache.org/ws/addressing"
5
	xmlns:p="http://www.springframework.org/schema/p" xmlns:http="http://cxf.apache.org/transports/http/configuration"
6
	xmlns:t="http://dnetlib.eu/springbeans/t" xmlns:template="http://dnetlib.eu/springbeans/template"
7
	xmlns:util="http://www.springframework.org/schema/util"
8
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
9
                                    http://cxf.apache.org/ws/addressing http://cxf.apache.org/schemas/ws-addr-conf.xsd
10
                                    http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd
11
                                    http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
12
                            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
13
                            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
14
                            http://dnetlib.eu/springbeans/template http://dnetlib.eu/springbeans/template.xsd">
15

  
16

  
17
	<bean id="dnetLoggerMongoDao" class="eu.dnetlib.common.logging.dao.DnetLoggerMongoDao"
18
		p:db-ref="loggingMongoDB" />
19

  
20
	<bean id="loggingMongoServer" class="com.mongodb.Mongo">
21
		<constructor-arg index="0" type="com.mongodb.ServerAddress">
22
			<bean class="com.mongodb.ServerAddress">
23
				<constructor-arg index="0"
24
					value="${dnet.logger.mongo.host}" />
25
				<constructor-arg index="1"
26
					value="${dnet.logger.mongo.port}" />
27
			</bean>
28
		</constructor-arg>
29
	</bean>
30

  
31
	<bean id="loggingMongoDB" factory-bean="loggingMongoServer"
32
		factory-method="getDB">
33
		<constructor-arg index="0" value="${dnet.logger.mongo.db}" />
34
	</bean>
35

  
36
</beans>
modules/dnet-mongo-logging/tags/dnet-mongo-logging-1.1.2/src/main/resources/eu/dnetlib/common/logging/dao/applicationContext-dnet-mongo-logging.properties
1
dnet.logger.mongo.host = localhost
2
dnet.logger.mongo.port = 27017
3
dnet.logger.mongo.db   = dnet_logs
modules/dnet-mongo-logging/tags/dnet-mongo-logging-1.1.2/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
	<parent>
4
		<groupId>eu.dnetlib</groupId>
5
		<artifactId>dnet-parent</artifactId>
6
		<version>1.0.0</version>
7
		<relativePath />
8
	</parent>
9
	<modelVersion>4.0.0</modelVersion>
10
	<groupId>eu.dnetlib</groupId>
11
	<artifactId>dnet-mongo-logging</artifactId>
12
	<packaging>jar</packaging>
13
	<version>1.1.2</version>
14
	<scm>
15
	  <developerConnection>scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/dnet-mongo-logging/tags/dnet-mongo-logging-1.1.2</developerConnection>
16
	</scm>
17
	<dependencies>
18
		<dependency>
19
			<groupId>org.mongodb</groupId>
20
			<artifactId>mongo-java-driver</artifactId>
21
			<version>${mongodb.driver.version}</version>
22
		</dependency>
23
		<dependency>
24
			<groupId>eu.dnetlib</groupId>
25
			<artifactId>dnet-modular-logging</artifactId>
26
			<version>[1.0.0,2.0.0)</version>
27
		</dependency>
28
	</dependencies>
29
</project>

Also available in: Unified diff