Skip to content

Commit d478294

Browse files
Test shaded driver core in OSGi environement
1 parent 7641f18 commit d478294

File tree

16 files changed

+645
-151
lines changed

16 files changed

+645
-151
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.datastax.oss.driver.api.osgi.service;
19+
20+
import edu.umd.cs.findbugs.annotations.NonNull;
21+
import java.time.Instant;
22+
import java.util.Objects;
23+
24+
public class TweetMessage {
25+
26+
private String sender;
27+
28+
private Instant timestamp;
29+
30+
private String body;
31+
32+
public TweetMessage() {}
33+
34+
public TweetMessage(@NonNull String sender, @NonNull Instant timestamp, @NonNull String body) {
35+
this.sender = sender;
36+
this.timestamp = timestamp;
37+
this.body = body;
38+
}
39+
40+
public Instant getTimestamp() {
41+
return timestamp;
42+
}
43+
44+
public void setTimestamp(Instant timestamp) {
45+
this.timestamp = timestamp;
46+
}
47+
48+
public String getSender() {
49+
return sender;
50+
}
51+
52+
public void setSender(String sender) {
53+
this.sender = sender;
54+
}
55+
56+
public String getBody() {
57+
return body;
58+
}
59+
60+
public void setBody(String body) {
61+
this.body = body;
62+
}
63+
64+
@Override
65+
public boolean equals(Object o) {
66+
if (this == o) {
67+
return true;
68+
}
69+
if (!(o instanceof TweetMessage)) {
70+
return false;
71+
}
72+
TweetMessage that = (TweetMessage) o;
73+
return Objects.equals(sender, that.sender)
74+
&& Objects.equals(timestamp, that.timestamp)
75+
&& Objects.equals(body, that.body);
76+
}
77+
78+
@Override
79+
public int hashCode() {
80+
return Objects.hash(sender, timestamp, body);
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.datastax.oss.driver.api.osgi.service;
19+
20+
public interface TweetService {
21+
22+
/**
23+
* Stores the given tweet message.
24+
*
25+
* @param message Message to store.
26+
*/
27+
void sendMessage(TweetMessage message);
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.datastax.oss.driver.internal.osgi;
19+
20+
import com.datastax.dse.driver.api.core.config.DseDriverOption;
21+
import com.datastax.dse.driver.internal.core.graph.GraphProtocol;
22+
import com.datastax.oss.driver.api.core.CqlIdentifier;
23+
import com.datastax.oss.driver.api.core.CqlSession;
24+
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
25+
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
26+
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
27+
import com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder;
28+
import java.net.InetSocketAddress;
29+
import java.util.List;
30+
import java.util.stream.Collectors;
31+
import java.util.stream.Stream;
32+
import org.osgi.framework.Bundle;
33+
import org.osgi.framework.BundleActivator;
34+
import org.osgi.framework.BundleContext;
35+
import org.osgi.framework.wiring.BundleWiring;
36+
import org.slf4j.Logger;
37+
import org.slf4j.LoggerFactory;
38+
39+
public abstract class BaseActivator implements BundleActivator {
40+
41+
private static final Logger LOGGER = LoggerFactory.getLogger(TweetActivator.class);
42+
43+
protected CqlSession session;
44+
protected CqlIdentifier keyspace;
45+
protected String graphName;
46+
47+
@Override
48+
public void start(BundleContext context) {
49+
buildSession(context);
50+
registerService(context);
51+
}
52+
53+
private void buildSession(BundleContext context) {
54+
Bundle bundle = context.getBundle();
55+
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
56+
ClassLoader classLoader = bundleWiring.getClassLoader();
57+
58+
LOGGER.info("Application class loader: {}", classLoader);
59+
60+
// Use the application bundle class loader to load classes by reflection when
61+
// they are located in the application bundle. This is not strictly required
62+
// as the driver has a "Dynamic-Import:*" directive which makes it capable
63+
// of loading classes outside its bundle.
64+
CqlSessionBuilder builder = CqlSession.builder().withClassLoader(classLoader);
65+
66+
// Use the application bundle class loader to load configuration resources located
67+
// in the application bundle. This is required, otherwise these resources will
68+
// not be found.
69+
ProgrammaticDriverConfigLoaderBuilder configLoaderBuilder =
70+
DriverConfigLoader.programmaticBuilder(classLoader);
71+
72+
String contactPointsStr = context.getProperty("cassandra.contactpoints");
73+
if (contactPointsStr == null) {
74+
contactPointsStr = "127.0.0.1";
75+
}
76+
LOGGER.info("Contact points: {}", contactPointsStr);
77+
78+
String portStr = context.getProperty("cassandra.port");
79+
if (portStr == null) {
80+
portStr = "9042";
81+
}
82+
LOGGER.info("Port: {}", portStr);
83+
int port = Integer.parseInt(portStr);
84+
85+
List<InetSocketAddress> contactPoints =
86+
Stream.of(contactPointsStr.split(","))
87+
.map((String host) -> InetSocketAddress.createUnresolved(host, port))
88+
.collect(Collectors.toList());
89+
builder.addContactPoints(contactPoints);
90+
91+
String keyspaceStr = context.getProperty("cassandra.keyspace");
92+
if (keyspaceStr == null) {
93+
keyspaceStr = "mailbox";
94+
}
95+
LOGGER.info("Keyspace: {}", keyspaceStr);
96+
keyspace = CqlIdentifier.fromCql(keyspaceStr);
97+
98+
String lbp = context.getProperty("cassandra.lbp");
99+
if (lbp != null) {
100+
LOGGER.info("Custom LBP: " + lbp);
101+
configLoaderBuilder.withString(DefaultDriverOption.LOAD_BALANCING_POLICY_CLASS, lbp);
102+
} else {
103+
LOGGER.info("Custom LBP: NO");
104+
}
105+
106+
String datacenter = context.getProperty("cassandra.datacenter");
107+
if (datacenter != null) {
108+
LOGGER.info("Custom datacenter: " + datacenter);
109+
configLoaderBuilder.withString(
110+
DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, datacenter);
111+
} else {
112+
LOGGER.info("Custom datacenter: NO");
113+
}
114+
115+
String compression = context.getProperty("cassandra.compression");
116+
if (compression != null) {
117+
LOGGER.info("Compression: {}", compression);
118+
configLoaderBuilder.withString(DefaultDriverOption.PROTOCOL_COMPRESSION, compression);
119+
} else {
120+
LOGGER.info("Compression: NONE");
121+
}
122+
123+
graphName = context.getProperty("cassandra.graph.name");
124+
if (graphName != null) {
125+
LOGGER.info("Graph name: {}", graphName);
126+
configLoaderBuilder.withString(DseDriverOption.GRAPH_NAME, graphName);
127+
configLoaderBuilder.withString(
128+
DseDriverOption.GRAPH_SUB_PROTOCOL, GraphProtocol.GRAPH_BINARY_1_0.toInternalCode());
129+
} else {
130+
LOGGER.info("Graph: NONE");
131+
}
132+
133+
builder.withConfigLoader(configLoaderBuilder.build());
134+
135+
LOGGER.info("Initializing session");
136+
session = builder.build();
137+
LOGGER.info("Session initialized");
138+
}
139+
140+
@Override
141+
public void stop(BundleContext context) {
142+
if (session != null) {
143+
LOGGER.info("Closing session");
144+
session.close();
145+
session = null;
146+
LOGGER.info("Session closed");
147+
}
148+
}
149+
150+
protected abstract void registerService(BundleContext context);
151+
}

0 commit comments

Comments
 (0)