Elffuss BraidHub

kikocis/java-driver public

Added OSGi example with Integration Test for testing different configs.

Includes a simple example of creating an OSGi bundle with a service that
uses driver-core.  Includes an Integration test that tests with the
following configs:

  * Default (default classifier with all dependencies)
  * Netty-Shaded (shaded classifier with all dependencies w/o netty)
  * Guava 15 (default classifier with Guava 15.0)
  * Guava 16
  * Guava 17
  * Guava 18

This test will if using the 'short' or 'long' profiles, so will be
pulled into continuous integration.

Uses pax-exam (https://ops4j1.jira.com/wiki/display/PAXEXAM4/Pax+Exam)
to test with different OSGi configurations.
Changeset6df6dd37b0ebb953f7097afc167fabdf591e98531fd5674e56fd3eaf8e0be74a
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-04 22:50 -06:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-05 23:42 -06:00
Parentsfa0faab5e1cb

Browse the files at this changeset

14 files changed

modified driver-core/pom.xml +42 −0
@@ -121,6 +121,19 @@
121121 <showDeprecation>true</showDeprecation>
122122 </configuration>
123123 </plugin>
124+ <plugin>
125+ <groupId>org.apache.maven.plugins</groupId>
126+ <artifactId>maven-jar-plugin</artifactId>
127+ <version>2.2</version>
128+ <executions>
129+ <execution>
130+ <goals>
131+ <goal>test-jar</goal>
132+ </goals>
133+ <phase>test-compile</phase>
134+ </execution>
135+ </executions>
136+ </plugin>
124137 <plugin>
125138 <groupId>org.apache.felix</groupId>
126139 <artifactId>maven-bundle-plugin</artifactId>
@@ -196,6 +209,35 @@
196209 </executions>
197210 </plugin>
198211 </plugins>
212+ <pluginManagement>
213+ <plugins>
214+ <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
215+ <plugin>
216+ <groupId>org.eclipse.m2e</groupId>
217+ <artifactId>lifecycle-mapping</artifactId>
218+ <version>1.0.0</version>
219+ <configuration>
220+ <lifecycleMappingMetadata>
221+ <pluginExecutions>
222+ <pluginExecution>
223+ <pluginExecutionFilter>
224+ <groupId>org.apache.maven.plugins</groupId>
225+ <artifactId>maven-jar-plugin</artifactId>
226+ <versionRange>[2.2,)</versionRange>
227+ <goals>
228+ <goal>test-jar</goal>
229+ </goals>
230+ </pluginExecutionFilter>
231+ <action>
232+ <ignore />
233+ </action>
234+ </pluginExecution>
235+ </pluginExecutions>
236+ </lifecycleMappingMetadata>
237+ </configuration>
238+ </plugin>
239+ </plugins>
240+ </pluginManagement>
199241 </build>
200242
201243 <profiles>
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +0 −1
@@ -76,7 +76,6 @@
7676
7777 @AfterClass(groups = "short", alwaysRun=true)
7878 public void teardown() {
79- System.out.println("Tearing down");
8079 if (cluster != null)
8180 cluster.close();
8281 if (ccm != null)
added driver-examples/osgi/README.rst +32 −0
@@ -0,0 +1,32 @@
1+OSGi Example
2+============
3+
4+A simple example application that demonstrates using the Java Driver in
5+an OSGi service.
6+
7+MailboxService is an activated service that uses Cassandra to
8+persist a Mailbox by email address.
9+
10+Usage
11+-----
12+
13+To build the bundle and run tests execute the following maven task::
14+
15+ mvn test -P short
16+
17+The short profile needs to be activated since the tests run under
18+this group.
19+
20+After which the bundle jar will be present in the target/ directory.
21+
22+The project includes an integration test that verifies the service can
23+be activated and used in an OSGi container. It also verifies that
24+driver-core can be used in an OSGi container in the following
25+configurations:
26+
27+1. Default (default classifier with all dependencies)
28+2. Netty-Shaded (shaded classifier with all depedencies w/o Netty)
29+3. Guava 15 (default classifier with Guava 15.0)
30+4. Guava 16
31+5. Guava 17
32+6. Guava 18
added driver-examples/osgi/pom.xml +261 −0
@@ -0,0 +1,261 @@
1+<!--
2+ Licensed under the Apache License, Version 2.0 (the "License");
3+ you may not use this file except in compliance with the License.
4+ You may obtain a copy of the License at
5+
6+ http://www.apache.org/licenses/LICENSE-2.0
7+
8+ Unless required by applicable law or agreed to in writing, software
9+ distributed under the License is distributed on an "AS IS" BASIS,
10+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ See the License for the specific language governing permissions and
12+ limitations under the License. See accompanying LICENSE file.
13+-->
14+<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">
15+ <modelVersion>4.0.0</modelVersion>
16+ <parent>
17+ <groupId>com.datastax.cassandra</groupId>
18+ <artifactId>cassandra-driver-examples-parent</artifactId>
19+ <version>2.0.10-SNAPSHOT</version>
20+ </parent>
21+ <artifactId>cassandra-driver-examples-osgi</artifactId>
22+ <packaging>bundle</packaging>
23+ <name>DataStax Java Driver for Apache Cassandra Examples - OSGi</name>
24+ <description>An example of using DataStax Java Driver in an OSGi container.</description>
25+ <url>https://github.com/datastax/java-driver</url>
26+
27+ <properties>
28+ <felix.version>4.6.0</felix.version>
29+ <pax-exam.version>4.4.0</pax-exam.version>
30+ </properties>
31+
32+ <dependencies>
33+ <dependency>
34+ <groupId>com.datastax.cassandra</groupId>
35+ <artifactId>cassandra-driver-core</artifactId>
36+ <version>2.0.10-SNAPSHOT</version>
37+ </dependency>
38+
39+ <dependency>
40+ <groupId>org.apache.felix</groupId>
41+ <artifactId>org.apache.felix.framework</artifactId>
42+ <version>${felix.version}</version>
43+ </dependency>
44+
45+ <dependency>
46+ <groupId>com.datastax.cassandra</groupId>
47+ <artifactId>cassandra-driver-core</artifactId>
48+ <version>${project.parent.version}</version>
49+ <type>test-jar</type>
50+ <scope>test</scope>
51+ </dependency>
52+
53+ <dependency>
54+ <groupId>org.ops4j.pax.exam</groupId>
55+ <artifactId>pax-exam</artifactId>
56+ <version>${pax-exam.version}</version>
57+ <scope>test</scope>
58+ </dependency>
59+
60+ <dependency>
61+ <groupId>org.ops4j.pax.exam</groupId>
62+ <artifactId>pax-exam-testng</artifactId>
63+ <version>${pax-exam.version}</version>
64+ <scope>test</scope>
65+ </dependency>
66+
67+ <dependency>
68+ <groupId>org.ops4j.pax.exam</groupId>
69+ <artifactId>pax-exam-container-native</artifactId>
70+ <version>${pax-exam.version}</version>
71+ <scope>test</scope>
72+ </dependency>
73+
74+ <dependency>
75+ <groupId>org.ops4j.pax.exam</groupId>
76+ <artifactId>pax-exam-link-mvn</artifactId>
77+ <version>${pax-exam.version}</version>
78+ <scope>test</scope>
79+ </dependency>
80+
81+ <dependency>
82+ <groupId>javax.transaction</groupId>
83+ <artifactId>jta</artifactId>
84+ <version>1.1</version>
85+ <scope>test</scope>
86+ </dependency>
87+
88+ <dependency>
89+ <groupId>log4j</groupId>
90+ <artifactId>log4j</artifactId>
91+ <version>1.2.17</version>
92+ <scope>test</scope>
93+ </dependency>
94+
95+ <dependency>
96+ <groupId>org.slf4j</groupId>
97+ <artifactId>slf4j-log4j12</artifactId>
98+ <version>1.7.6</version>
99+ <scope>test</scope>
100+ </dependency>
101+
102+ <dependency>
103+ <groupId>com.jcabi</groupId>
104+ <artifactId>jcabi-manifests</artifactId>
105+ <version>1.1</version>
106+ <scope>test</scope>
107+ </dependency>
108+
109+ <dependency>
110+ <groupId>javax.servlet</groupId>
111+ <artifactId>javax.servlet-api</artifactId>
112+ <version>3.1.0</version>
113+ <scope>test</scope>
114+ </dependency>
115+ <dependency>
116+ <groupId>javax.inject</groupId>
117+ <artifactId>javax.inject</artifactId>
118+ <version>1</version>
119+ <scope>test</scope>
120+ </dependency>
121+ </dependencies>
122+
123+ <build>
124+ <plugins>
125+ <plugin>
126+ <groupId>org.apache.felix</groupId>
127+ <artifactId>maven-bundle-plugin</artifactId>
128+ <extensions>true</extensions>
129+ <version>2.4.0</version>
130+ <configuration>
131+ <instructions>
132+ <Bundle-SymbolicName>com.datastax.driver.osgi</Bundle-SymbolicName>
133+ <Bundle-Version>${project.version}</Bundle-Version>
134+ <Export-Package>com.datastax.driver.osgi.api,!com.datastax.driver.osgi.impl</Export-Package>
135+ <Bundle-Activator>com.datastax.driver.osgi.impl.Activator</Bundle-Activator>
136+ <_include>-osgi.bnd</_include>
137+ </instructions>
138+ <supportedProjectTypes>
139+ <supportedProjectType>jar</supportedProjectType>
140+ <supportedProjectType>bundle</supportedProjectType>
141+ <supportedProjectType>pom</supportedProjectType>
142+ </supportedProjectTypes>
143+ </configuration>
144+ <executions>
145+ <execution>
146+ <id>bundle-manifest</id>
147+ <phase>compile</phase>
148+ <goals>
149+ <goal>manifest</goal>
150+ </goals>
151+ </execution>
152+ </executions>
153+ </plugin>
154+ </plugins>
155+ </build>
156+
157+ <profiles>
158+ <profile>
159+ <!-- Skip test by default, short or long is required to run unit tests since pax-exam will throw exception if it encounters a
160+ test with no matching methods. -->
161+ <id>default</id>
162+ <properties>
163+ <env>default</env>
164+ </properties>
165+ <activation>
166+ <activeByDefault>true</activeByDefault>
167+ </activation>
168+ <build>
169+ <plugins>
170+ <plugin>
171+ <groupId>org.apache.maven.plugins</groupId>
172+ <artifactId>maven-surefire-plugin</artifactId>
173+ <version>2.14</version>
174+ <configuration>
175+ <skipTests>true</skipTests>
176+ </configuration>
177+ </plugin>
178+ <plugin>
179+ <artifactId>maven-compiler-plugin</artifactId>
180+ <configuration>
181+ <compilerArgument>-Xlint:all</compilerArgument>
182+ <showWarnings>true</showWarnings>
183+ <showDeprecation>true</showDeprecation>
184+ </configuration>
185+ </plugin>
186+ </plugins>
187+ </build>
188+ </profile>
189+
190+ <profile>
191+ <id>short</id>
192+ <properties>
193+ <env>default</env>
194+ </properties>
195+ <build>
196+ <plugins>
197+ <plugin>
198+ <groupId>org.apache.maven.plugins</groupId>
199+ <artifactId>maven-surefire-plugin</artifactId>
200+ <version>2.14</version>
201+ <configuration>
202+ <groups>unit,short</groups>
203+ <useFile>false</useFile>
204+ <systemPropertyVariables>
205+ <cassandra.version>${cassandra.version}</cassandra.version>
206+ <ipprefix>${ipprefix}</ipprefix>
207+ </systemPropertyVariables>
208+ </configuration>
209+ </plugin>
210+ </plugins>
211+ </build>
212+ </profile>
213+
214+ <profile>
215+ <id>long</id>
216+ <properties>
217+ <env>default</env>
218+ </properties>
219+ <build>
220+ <plugins>
221+ <plugin>
222+ <groupId>org.apache.maven.plugins</groupId>
223+ <artifactId>maven-surefire-plugin</artifactId>
224+ <version>2.16</version>
225+ <configuration>
226+ <groups>unit,short,long</groups>
227+ <useFile>false</useFile>
228+ <systemPropertyVariables>
229+ <cassandra.version>${cassandra.version}</cassandra.version>
230+ <ipprefix>${ipprefix}</ipprefix>
231+ </systemPropertyVariables>
232+ </configuration>
233+ </plugin>
234+ </plugins>
235+ </build>
236+ </profile>
237+ </profiles>
238+
239+ <licenses>
240+ <license>
241+ <name>Apache 2</name>
242+ <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
243+ <distribution>repo</distribution>
244+ <comments>Apache License Version 2.0</comments>
245+ </license>
246+ </licenses>
247+
248+ <scm>
249+ <connection>scm:git:git@github.com:datastax/java-driver.git</connection>
250+ <developerConnection>scm:git:git@github.com:datastax/java-driver.git</developerConnection>
251+ <url>https://github.com/datastax/java-driver</url>
252+ </scm>
253+
254+ <developers>
255+ <developer>
256+ <name>Various</name>
257+ <organization>DataStax</organization>
258+ </developer>
259+ </developers>
260+</project>
261+
added driver-examples/osgi/src/main/java/com/datastax/driver/osgi/api/MailboxException.java +23 −0
@@ -0,0 +1,23 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.osgi.api;
17+
18+public class MailboxException extends Exception {
19+
20+ public MailboxException(Throwable cause) {
21+ super("Failure interacting with Mailbox", cause);
22+ }
23+}
added driver-examples/osgi/src/main/java/com/datastax/driver/osgi/api/MailboxMessage.java +60 −0
@@ -0,0 +1,60 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.osgi.api;
17+
18+import java.util.Date;
19+
20+public class MailboxMessage {
21+ private String recipient;
22+ private Date date;
23+ private String sender;
24+ private String body;
25+
26+ public MailboxMessage(String recipient, Date date, String sender, String body) {
27+ this.recipient = recipient;
28+ this.date = date;
29+ this.sender = sender;
30+ this.body = body;
31+ }
32+
33+ public String getRecipient() {
34+ return recipient;
35+ }
36+
37+ public Date getDate() {
38+ return date;
39+ }
40+
41+ public String getSender() {
42+ return sender;
43+ }
44+
45+ public String getBody() {
46+ return body;
47+ }
48+
49+ @Override public boolean equals(Object that) {
50+ if(that instanceof MailboxMessage) {
51+ MailboxMessage thatM = (MailboxMessage)that;
52+ return recipient.equals(thatM.getRecipient()) &&
53+ date.equals(thatM.getDate()) &&
54+ sender.equals(thatM.getSender()) &&
55+ body.equals(thatM.getBody());
56+ } else {
57+ return false;
58+ }
59+ }
60+}
added driver-examples/osgi/src/main/java/com/datastax/driver/osgi/api/MailboxService.java +42 −0
@@ -0,0 +1,42 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.osgi.api;
17+
18+import java.util.Collection;
19+import java.util.UUID;
20+
21+public interface MailboxService {
22+
23+ /**
24+ * Retrieve all messages for a given recipient.
25+ * @param recipient User whose mailbox is being read.
26+ * @return All messages in the mailbox.
27+ */
28+ public Collection<MailboxMessage> getMessages(String recipient) throws MailboxException;
29+
30+ /**
31+ * Stores the given message in the appropriate mailbox.
32+ * @param message Message to send.
33+ * @return UUID generated for the message.
34+ */
35+ public UUID sendMessage(MailboxMessage message) throws MailboxException;
36+
37+ /**
38+ * Deletes all mail for the given recipient.
39+ * @param recipient User whose mailbox will be cleared.
40+ */
41+ public void clearMailbox(String recipient) throws MailboxException;
42+}
added driver-examples/osgi/src/main/java/com/datastax/driver/osgi/impl/Activator.java +57 −0
@@ -0,0 +1,57 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.osgi.impl;
17+
18+import java.util.Hashtable;
19+
20+import org.osgi.framework.BundleActivator;
21+import org.osgi.framework.BundleContext;
22+
23+import com.datastax.driver.core.Cluster;
24+import com.datastax.driver.core.Session;
25+import com.datastax.driver.osgi.api.MailboxService;
26+
27+public class Activator implements BundleActivator {
28+
29+ private Cluster cluster;
30+
31+ @Override public void start(BundleContext context) throws Exception {
32+ String contactPointsStr = context.getProperty("cassandra.contactpoints");
33+ if(contactPointsStr == null) {
34+ contactPointsStr = "127.0.0.1";
35+ }
36+ String[] contactPoints = contactPointsStr.split(",");
37+
38+ String keyspace = context.getProperty("cassandra.keyspace");
39+ if(keyspace == null) {
40+ keyspace = "mailbox";
41+ }
42+
43+ cluster = Cluster.builder().addContactPoints(contactPoints).build();
44+ Session session = cluster.connect();
45+
46+ MailboxImpl mailbox = new MailboxImpl(session, keyspace);
47+ mailbox.init();
48+
49+ context.registerService(MailboxService.class.getName(), mailbox, new Hashtable<String, String>());
50+ }
51+
52+ @Override public void stop(BundleContext context) throws Exception {
53+ if(cluster != null) {
54+ cluster.close();
55+ }
56+ }
57+}
added driver-examples/osgi/src/main/java/com/datastax/driver/osgi/impl/MailboxImpl.java +138 −0
@@ -0,0 +1,138 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.osgi.impl;
17+
18+import java.util.ArrayList;
19+import java.util.Collection;
20+import java.util.Date;
21+import java.util.UUID;
22+
23+import com.datastax.driver.core.*;
24+import com.datastax.driver.core.exceptions.InvalidQueryException;
25+import com.datastax.driver.core.utils.UUIDs;
26+import com.datastax.driver.osgi.api.MailboxException;
27+import com.datastax.driver.osgi.api.MailboxMessage;
28+import com.datastax.driver.osgi.api.MailboxService;
29+
30+import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker;
31+import static com.datastax.driver.core.querybuilder.QueryBuilder.delete;
32+import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
33+import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto;
34+import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
35+
36+public class MailboxImpl implements MailboxService {
37+
38+ private static final String TABLE = "mailbox";
39+
40+ private final Session session;
41+
42+ private final String keyspace;
43+
44+ private volatile boolean initialized = false;
45+
46+ private PreparedStatement retrieveStatement;
47+
48+ private PreparedStatement insertStatement;
49+
50+ private PreparedStatement deleteStatement;
51+
52+ public MailboxImpl(Session session, String keyspace) {
53+ this.session = session;
54+ this.keyspace = keyspace;
55+ }
56+
57+ public synchronized void init() {
58+ if(initialized)
59+ return;
60+
61+ // Create the schema if it does not exist.
62+ try {
63+ session.execute("USE " + keyspace);
64+ } catch (InvalidQueryException e) {
65+ session.execute("CREATE KEYSPACE " + keyspace +
66+ " with replication = {'class': 'SimpleStrategy', 'replication_factor' : 1}");
67+
68+ session.execute("CREATE TABLE " + keyspace + "." + TABLE + " (" +
69+ "recipient text," +
70+ "time timeuuid," +
71+ "sender text," +
72+ "body text," +
73+ "PRIMARY KEY (recipient, time))");
74+ }
75+
76+ retrieveStatement = session.prepare(select()
77+ .from(keyspace, TABLE)
78+ .where(eq("recipient", bindMarker("recipient"))));
79+
80+ insertStatement = session.prepare(insertInto(keyspace, TABLE)
81+ .value("recipient", bindMarker("recipient"))
82+ .value("time", bindMarker("time"))
83+ .value("sender", bindMarker("sender"))
84+ .value("body", bindMarker("body")));
85+
86+ deleteStatement = session.prepare(delete().from(keyspace, TABLE)
87+ .where(eq("recipient", bindMarker("recipient"))));
88+
89+ initialized = true;
90+ }
91+
92+ @Override public Collection<MailboxMessage> getMessages(String recipient) throws MailboxException {
93+ try {
94+ BoundStatement statement = new BoundStatement(retrieveStatement);
95+ statement.setString("recipient", recipient);
96+ ResultSet result = session.execute(statement);
97+
98+ Collection<MailboxMessage> messages = new ArrayList<MailboxMessage>();
99+ for(Row input : result) {
100+ Date date = new Date(UUIDs.unixTimestamp(input.getUUID("time")));
101+ messages.add(new MailboxMessage(input.getString("recipient"),
102+ date,
103+ input.getString("sender"),
104+ input.getString("body")));
105+ }
106+ return messages;
107+ } catch(Exception e) {
108+ throw new MailboxException(e);
109+ }
110+ }
111+
112+ @Override public UUID sendMessage(MailboxMessage message) throws MailboxException {
113+ try {
114+ UUID time = UUIDs.startOf(message.getDate().getTime());
115+
116+ BoundStatement statement = new BoundStatement(insertStatement);
117+ statement.setString("recipient", message.getRecipient());
118+ statement.setUUID("time", time);
119+ statement.setString("sender", message.getSender());
120+ statement.setString("body", message.getBody());
121+
122+ session.execute(statement);
123+ return time;
124+ } catch(Exception e) {
125+ throw new MailboxException(e);
126+ }
127+ }
128+
129+ @Override public void clearMailbox(String recipient) throws MailboxException {
130+ try {
131+ BoundStatement statement = new BoundStatement(deleteStatement);
132+ statement.setString("recipient", recipient);
133+ session.execute(statement);
134+ } catch(Exception e) {
135+ throw new MailboxException(e);
136+ }
137+ }
138+}
added driver-examples/osgi/src/test/java/com/datastax/driver/osgi/CCMBridgeListener.java +54 −0
@@ -0,0 +1,54 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.osgi;
17+
18+import org.testng.ITestContext;
19+import org.testng.ITestListener;
20+import org.testng.ITestResult;
21+
22+import com.datastax.driver.core.CCMBridge;
23+
24+/**
25+ * A listener that fires up a single node CCM instance on test class start and tears it
26+ * down on test class end.
27+ *
28+ * This is needed for tests that use Pax-Exam since it runs some methods in the OSGi container
29+ * which we do not want.
30+ */
31+public class CCMBridgeListener implements ITestListener {
32+
33+ private CCMBridge ccm;
34+
35+ @Override public void onStart(ITestContext context) {
36+ ccm = CCMBridge.create("test", 1);
37+ }
38+
39+ @Override public void onFinish(ITestContext context) {
40+ if(ccm != null) {
41+ ccm.remove();
42+ }
43+ }
44+
45+ @Override public void onTestStart(ITestResult result) {}
46+
47+ @Override public void onTestSuccess(ITestResult result) {}
48+
49+ @Override public void onTestFailure(ITestResult result) {}
50+
51+ @Override public void onTestSkipped(ITestResult result) {}
52+
53+ @Override public void onTestFailedButWithinSuccessPercentage(ITestResult result) {}
54+}
added driver-examples/osgi/src/test/java/com/datastax/driver/osgi/MailboxServiceTest.java +174 −0
@@ -0,0 +1,174 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.osgi;
17+
18+import java.util.ArrayList;
19+import java.util.Collection;
20+import java.util.GregorianCalendar;
21+
22+import javax.inject.Inject;
23+import org.ops4j.pax.exam.Configuration;
24+import org.ops4j.pax.exam.Option;
25+import org.ops4j.pax.exam.options.CompositeOption;
26+import org.ops4j.pax.exam.options.MavenArtifactProvisionOption;
27+import org.ops4j.pax.exam.testng.listener.PaxExam;
28+import org.testng.annotations.Listeners;
29+import org.testng.annotations.Test;
30+
31+import static org.ops4j.pax.exam.CoreOptions.*;
32+import static org.testng.Assert.assertEquals;
33+
34+import com.datastax.driver.core.CCMBridge;
35+import com.datastax.driver.osgi.api.MailboxException;
36+import com.datastax.driver.osgi.api.MailboxMessage;
37+import com.datastax.driver.osgi.api.MailboxService;
38+
39+import static com.datastax.driver.osgi.VersionProvider.projectVersion;
40+
41+@Listeners({CCMBridgeListener.class, PaxExam.class})
42+@Test(groups="short")
43+public class MailboxServiceTest {
44+ @Inject MailboxService service;
45+
46+ private MavenArtifactProvisionOption driverBundle() {
47+ return mavenBundle("com.datastax.cassandra", "cassandra-driver-core", projectVersion());
48+ }
49+
50+ private MavenArtifactProvisionOption guavaBundle() {
51+ return mavenBundle("com.google.guava", "guava", "14.0.1");
52+ }
53+
54+ private MavenArtifactProvisionOption nettyBundle() {
55+ return mavenBundle("io.netty", "netty", "3.9.0.Final");
56+ }
57+
58+ private CompositeOption defaultOptions() {
59+ return new CompositeOption() {
60+
61+ @Override public Option[] getOptions() {
62+ return options(
63+ systemProperty("cassandra.contactpoints").value(CCMBridge.IP_PREFIX + 1),
64+ bundle("reference:file:target/classes"),
65+ mavenBundle("com.codahale.metrics", "metrics-core", "3.0.2"),
66+ mavenBundle("org.slf4j", "slf4j-api", "1.7.5"),
67+ mavenBundle("org.slf4j", "slf4j-simple", "1.7.5").noStart(),
68+ systemPackages("org.testng", "org.junit", "org.junit.runner", "org.junit.runner.manipulation",
69+ "org.junit.runner.notification", "com.jcabi.manifests")
70+ );
71+ }
72+ };
73+ }
74+
75+ @Configuration
76+ public Option[] shadedConfig() {
77+ return options(
78+ driverBundle().classifier("shaded"),
79+ guavaBundle(),
80+ defaultOptions()
81+ );
82+ }
83+
84+ @Configuration
85+ public Option[] defaultConfig() {
86+ return options(
87+ driverBundle(),
88+ guavaBundle(),
89+ nettyBundle(),
90+ defaultOptions()
91+ );
92+ }
93+
94+ @Configuration
95+ public Option[] guava15Config() {
96+ return options(
97+ driverBundle(),
98+ nettyBundle(),
99+ guavaBundle().version("15.0"),
100+ defaultOptions()
101+ );
102+ }
103+
104+ @Configuration
105+ public Option[] guava16Config() {
106+ return options(
107+ driverBundle(),
108+ nettyBundle(),
109+ guavaBundle().version("16.0.1"),
110+ defaultOptions()
111+ );
112+ }
113+
114+ @Configuration
115+ public Option[] guava17Config() {
116+ return options(
117+ driverBundle(),
118+ nettyBundle(),
119+ guavaBundle().version("17.0"),
120+ defaultOptions()
121+ );
122+ }
123+
124+ @Configuration
125+ public Option[] guava18Config() {
126+ return options(
127+ driverBundle(),
128+ nettyBundle(),
129+ guavaBundle().version("18.0"),
130+ defaultOptions()
131+ );
132+ }
133+
134+ /**
135+ * <p>
136+ * Exercises a 'mailbox' service provided by an OSGi bundle that depends on the driver. Ensures that
137+ * queries can be made through the service with the current given configuration.
138+ * </p>
139+ *
140+ * The following configurations are tried (defined via methods with the @Configuration annotation):
141+ * <ol>
142+ * <li>Default bundle (Driver with all of it's dependencies)</li>
143+ * <li>Shaded bundle (Driver with netty shaded)</li>
144+ * <li>With Guava 15</li>
145+ * <li>With Guava 16</li>
146+ * <li>With Guava 17</li>
147+ * <li>With Guava 18</li>
148+ * </ol>
149+ *
150+ * @test_category packaging
151+ * @expected_result Can create, retrieve and delete data using the mailbox service.
152+ * @jira_ticket JAVA-620
153+ * @since 2.0.10, 2.1.5
154+ */
155+ public void service_api_functional() throws MailboxException {
156+ // Insert some data into mailbox for a particular user.
157+ String recipient = "user@datastax.com";
158+
159+ try {
160+ Collection<MailboxMessage> inMessages = new ArrayList<MailboxMessage>();
161+ for (int i = 0; i < 30; i++) {
162+ MailboxMessage message = new MailboxMessage(recipient, new GregorianCalendar(2015, 1, i).getTime(), recipient, "" + i);
163+ inMessages.add(message);
164+ service.sendMessage(message);
165+ }
166+
167+ Collection<MailboxMessage> messages = service.getMessages(recipient);
168+
169+ assertEquals(messages, inMessages);
170+ } finally {
171+ service.clearMailbox(recipient);
172+ }
173+ }
174+}
added driver-examples/osgi/src/test/java/com/datastax/driver/osgi/VersionProvider.java +41 −0
@@ -0,0 +1,41 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.osgi;
17+
18+import com.jcabi.manifests.Manifests;
19+
20+/**
21+ * Attempts to resolve the project version from the Bundle manifest. If not present, will throw RuntimeException
22+ * on initialization. If this happens, try building with 'mvn compile' to generate the Bundle manifest.
23+ *
24+ * In IntelliJ you can have compile run after make by right clicking on 'compile' in the 'Maven Projects' tool window.
25+ */
26+public class VersionProvider {
27+
28+ private static String PROJECT_VERSION;
29+ static {
30+ String bundleName = Manifests.read("Bundle-SymbolicName");
31+ if (bundleName.equals("com.datastax.driver.osgi")) {
32+ PROJECT_VERSION = Manifests.read("Bundle-Version").replaceAll("\\.SNAPSHOT", "-SNAPSHOT");
33+ } else {
34+ throw new RuntimeException("Couldn't resolve bundle manifest (try building with mvn compile)");
35+ }
36+ }
37+
38+ public static String projectVersion() {
39+ return PROJECT_VERSION;
40+ }
41+}
added driver-examples/osgi/src/test/resources/log4j.properties +12 −0
@@ -0,0 +1,12 @@
1+# Set root logger level to DEBUG and its only appender to A1.
2+log4j.rootLogger=INFO, A1
3+
4+# Scassandra's info log is a bit verbose
5+log4j.logger.org.scassandra=WARN
6+
7+# A1 is set to be a ConsoleAppender.
8+log4j.appender.A1=org.apache.log4j.ConsoleAppender
9+
10+# A1 uses PatternLayout.
11+log4j.appender.A1.layout=org.apache.log4j.PatternLayout
12+log4j.appender.A1.layout.ConversionPattern=\ %-6r [%t] %-5p %c %x - %m%n
modified driver-examples/pom.xml +1 −0
@@ -26,6 +26,7 @@
2626
2727 <modules>
2828 <module>stress</module>
29+ <module>osgi</module>
2930 </modules>
3031
3132 <licenses>