Skip to content

Commit

Permalink
Test shaded driver core in OSGi environement
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-antoniak committed Nov 21, 2024
1 parent 7641f18 commit d478294
Show file tree
Hide file tree
Showing 16 changed files with 645 additions and 151 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.datastax.oss.driver.api.osgi.service;

import edu.umd.cs.findbugs.annotations.NonNull;
import java.time.Instant;
import java.util.Objects;

public class TweetMessage {

private String sender;

private Instant timestamp;

private String body;

public TweetMessage() {}

public TweetMessage(@NonNull String sender, @NonNull Instant timestamp, @NonNull String body) {
this.sender = sender;
this.timestamp = timestamp;
this.body = body;
}

public Instant getTimestamp() {
return timestamp;
}

public void setTimestamp(Instant timestamp) {
this.timestamp = timestamp;
}

public String getSender() {
return sender;
}

public void setSender(String sender) {
this.sender = sender;
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof TweetMessage)) {
return false;
}
TweetMessage that = (TweetMessage) o;
return Objects.equals(sender, that.sender)
&& Objects.equals(timestamp, that.timestamp)
&& Objects.equals(body, that.body);
}

@Override
public int hashCode() {
return Objects.hash(sender, timestamp, body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.datastax.oss.driver.api.osgi.service;

public interface TweetService {

/**
* Stores the given tweet message.
*
* @param message Message to store.
*/
void sendMessage(TweetMessage message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.datastax.oss.driver.internal.osgi;

import com.datastax.dse.driver.api.core.config.DseDriverOption;
import com.datastax.dse.driver.internal.core.graph.GraphProtocol;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
import com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.wiring.BundleWiring;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BaseActivator implements BundleActivator {

private static final Logger LOGGER = LoggerFactory.getLogger(TweetActivator.class);

protected CqlSession session;
protected CqlIdentifier keyspace;
protected String graphName;

@Override
public void start(BundleContext context) {
buildSession(context);
registerService(context);
}

private void buildSession(BundleContext context) {
Bundle bundle = context.getBundle();
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
ClassLoader classLoader = bundleWiring.getClassLoader();

LOGGER.info("Application class loader: {}", classLoader);

// Use the application bundle class loader to load classes by reflection when
// they are located in the application bundle. This is not strictly required
// as the driver has a "Dynamic-Import:*" directive which makes it capable
// of loading classes outside its bundle.
CqlSessionBuilder builder = CqlSession.builder().withClassLoader(classLoader);

// Use the application bundle class loader to load configuration resources located
// in the application bundle. This is required, otherwise these resources will
// not be found.
ProgrammaticDriverConfigLoaderBuilder configLoaderBuilder =
DriverConfigLoader.programmaticBuilder(classLoader);

String contactPointsStr = context.getProperty("cassandra.contactpoints");
if (contactPointsStr == null) {
contactPointsStr = "127.0.0.1";
}
LOGGER.info("Contact points: {}", contactPointsStr);

String portStr = context.getProperty("cassandra.port");
if (portStr == null) {
portStr = "9042";
}
LOGGER.info("Port: {}", portStr);
int port = Integer.parseInt(portStr);

List<InetSocketAddress> contactPoints =
Stream.of(contactPointsStr.split(","))
.map((String host) -> InetSocketAddress.createUnresolved(host, port))
.collect(Collectors.toList());
builder.addContactPoints(contactPoints);

String keyspaceStr = context.getProperty("cassandra.keyspace");
if (keyspaceStr == null) {
keyspaceStr = "mailbox";
}
LOGGER.info("Keyspace: {}", keyspaceStr);
keyspace = CqlIdentifier.fromCql(keyspaceStr);

String lbp = context.getProperty("cassandra.lbp");
if (lbp != null) {
LOGGER.info("Custom LBP: " + lbp);
configLoaderBuilder.withString(DefaultDriverOption.LOAD_BALANCING_POLICY_CLASS, lbp);
} else {
LOGGER.info("Custom LBP: NO");
}

String datacenter = context.getProperty("cassandra.datacenter");
if (datacenter != null) {
LOGGER.info("Custom datacenter: " + datacenter);
configLoaderBuilder.withString(
DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, datacenter);
} else {
LOGGER.info("Custom datacenter: NO");
}

String compression = context.getProperty("cassandra.compression");
if (compression != null) {
LOGGER.info("Compression: {}", compression);
configLoaderBuilder.withString(DefaultDriverOption.PROTOCOL_COMPRESSION, compression);
} else {
LOGGER.info("Compression: NONE");
}

graphName = context.getProperty("cassandra.graph.name");
if (graphName != null) {
LOGGER.info("Graph name: {}", graphName);
configLoaderBuilder.withString(DseDriverOption.GRAPH_NAME, graphName);
configLoaderBuilder.withString(
DseDriverOption.GRAPH_SUB_PROTOCOL, GraphProtocol.GRAPH_BINARY_1_0.toInternalCode());
} else {
LOGGER.info("Graph: NONE");
}

builder.withConfigLoader(configLoaderBuilder.build());

LOGGER.info("Initializing session");
session = builder.build();
LOGGER.info("Session initialized");
}

@Override
public void stop(BundleContext context) {
if (session != null) {
LOGGER.info("Closing session");
session.close();
session = null;
LOGGER.info("Session closed");
}
}

protected abstract void registerService(BundleContext context);
}
Loading

0 comments on commit d478294

Please sign in to comment.