Commit 13156f7b authored by acoburn's avatar acoburn
Browse files

added binding implementation

parent 94cd8e55
......@@ -20,6 +20,7 @@
</cm:property-placeholder>
<reference id="registryService" interface="edu.amherst.acdc.registry.api.RegistryService" filter="(osgi.jndi.service.name=registry)" />
<reference id="bindingService" interface="edu.amherst.acdc.binding.api.BindingService" filter="(osgi.jndi.service.name=binding)" />
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
......
API-X Service Binding API
=========================
This is the Service Binding API for the API-X Framework.
A binding implementation needs only to implement this API.
Building
--------
To build this project use
mvn install
Deploying in OSGi
-----------------
Each of these projects can be deployed in an OSGi container. For example using
[Apache Karaf](http://karaf.apache.org) version 4.x or better, you can run the following
command from its shell:
feature:repo-add mvn:edu.amherst.acdc/acrepo-karaf/LATEST/xml/features
feature:install acrepo-binding-api
Or by copying any of the compiled bundles into `$KARAF_HOME/deploy`.
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<!--
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.
-->
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>repository-services</artifactId>
<groupId>edu.amherst.acdc</groupId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>acrepo-binding-api</artifactId>
<packaging>bundle</packaging>
<name>API-X Binding API</name>
<description>A mechanism to dynamically register services.</description>
<properties>
<osgi.export.packages>edu.amherst.acdc.binding.api;version=${project.version}</osgi.export.packages>
</properties>
<dependencies>
<!-- logging and testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
/**
* Copyright 2015 Amherst College
*
* 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 edu.amherst.acdc.binding.api;
import java.util.List;
/**
* @author acoburn
* @since 1/23/16
*/
public interface BindingService {
/**
* Bind a service instance to a registry
*
* @param identifier the identifying key for the service
* @param endpoint the endpoint
* @return whether the operation was successful
*/
Boolean bind(final String identifier, final String endpoint);
/**
* Remove a service endpoint from the binding registry
*
* @param identifier the identifying key for the service
* @param endpoint the endpoint
* @return whether the operation was successful
*/
Boolean unbind(final String identifier, final String endpoint);
/**
* List all endpoints
*
* @return the complete service catalog
*/
List<String> list(final String identifier);
/**
* Pick a random endpoint
*
* @return an endpoint
*/
String findAny(final String identifier);
}
Repository In-memory Service Registry
=====================================
This service manages the dynamic registration of services within the API-X framework.
Building
--------
To build this project use
mvn install
Deploying in OSGi
-----------------
Each of these projects can be deployed in an OSGi container. For example using
[Apache Karaf](http://karaf.apache.org) version 4.x or better, you can run the following
command from its shell:
feature:repo-add mvn:edu.amherst.acdc/acrepo-karaf/LATEST/xml/features
feature:install acrepo-registry-memory
Or by copying any of the compiled bundles into `$KARAF_HOME/deploy`.
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<!--
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.
-->
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>repository-services</artifactId>
<groupId>edu.amherst.acdc</groupId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>acrepo-binding-memory</artifactId>
<packaging>bundle</packaging>
<name>Service binding service</name>
<description>A mechanism to dynamically bind services.</description>
<properties>
<osgi.export.packages>edu.amherst.acdc.binding.memory;version=${project.version}</osgi.export.packages>
</properties>
<dependencies>
<dependency>
<groupId>edu.amherst.acdc</groupId>
<artifactId>acrepo-binding-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- logging and testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
/**
* Copyright 2015 Amherst College
*
* 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 edu.amherst.acdc.binding.memory;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.slf4j.LoggerFactory.getLogger;
import edu.amherst.acdc.binding.api.BindingService;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
/**
* @author acoburn
* @since 1/25/16
*/
public class BindingServiceImpl implements BindingService {
private static final Logger LOGGER = getLogger(BindingServiceImpl.class);
private Map<String, Set<String>> instances = new HashMap<>();
/**
* Bind a service instance to a registry
*
* @param identifier the identifying key for the service
* @param endpoint the endpoint
* @return whether the operation was successful
*/
public Boolean bind(final String identifier, final String endpoint) {
if (!instances.containsKey(identifier)) {
instances.put(identifier, new HashSet<>());
}
return instances.get(identifier).add(endpoint);
}
/**
* Remove a service endpoint from the binding registry
*
* @param identifier the identifying key for the service
* @param endpoint the endpoint
* @return whether the operation was successful
*/
public Boolean unbind(final String identifier, final String endpoint) {
if (instances.containsKey(identifier)) {
return instances.get(identifier).remove(endpoint);
}
return false;
}
/**
* List all endpoints
*
* @return the complete service catalog
*/
public List<String> list(final String identifier) {
if (instances.containsKey(identifier)) {
return instances.get(identifier).stream().collect(toList());
}
return emptyList();
}
/**
* Pick a random endpoint
*
* @return an endpoint
*/
public String findAny(final String identifier) {
final List<String> endpoints = list(identifier);
if (!endpoints.isEmpty()) {
final Random rand = new Random();
return endpoints.get(rand.nextInt(endpoints.size()));
}
return null;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd"
default-activation="lazy">
<bean id="bindingServiceBean" class="edu.amherst.acdc.binding.memory.BindingServiceImpl"/>
<service ref="bindingServiceBean" interface="edu.amherst.acdc.binding.api.BindingService">
<service-properties>
<entry key="osgi.jndi.service.name" value="binding"/>
</service-properties>
</service>
</blueprint>
/**
* Copyright 2015 Amherst College
*
* 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 edu.amherst.acdc.binding.memory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import edu.amherst.acdc.binding.api.BindingService;
import org.junit.Test;
/**
* @author acoburn
* @since 9/14/15
*/
public class BindingServiceTest {
@Test
public void testBinding() {
final BindingService binder = new BindingServiceImpl();
assertTrue(binder.bind("foo", "http://example.org"));
assertEquals(1, binder.list("foo").size());
assertEquals("http://example.org", binder.list("foo").get(0));
assertEquals("http://example.org", binder.findAny("foo"));
assertNull(binder.findAny("bar"));
assertFalse(binder.bind("foo", "http://example.org"));
assertEquals(1, binder.list("foo").size());
binder.bind("foo", "http://example.com");
assertEquals(2, binder.list("foo").size());
}
}
......@@ -130,10 +130,26 @@
<bundle>mvn:edu.amherst.acdc/acrepo-registry-memory/${project.version}</bundle>
</feature>
<feature name="acrepo-binding-api" version="${project.version}" resolver="(orb)" start-level="50">
<details>Installs the binding API</details>
<bundle>mvn:edu.amherst.acdc/acrepo-binding-api/${project.version}</bundle>
</feature>
<feature name="acrepo-binding-memory" version="${project.version}" resolver="(orb)" start-level="50">
<details>Installs an in-memory implementation of the API-X binding API</details>
<feature version="${project.version}">acrepo-binding-api</feature>
<bundle>mvn:edu.amherst.acdc/acrepo-binding-memory/${project.version}</bundle>
</feature>
<feature name="acrepo-apix" version="${project.version}" resolver="(orb)" start-level="50">
<details>Installs the API-X Framework</details>
<feature version="${project.version}">acrepo-registry-api</feature>
<feature version="${project.version}">acrepo-binding-api</feature>
<bundle>mvn:edu.amherst.acdc/acrepo-apix/${project.version}</bundle>
</feature>
......
......@@ -70,6 +70,7 @@
<modules>
<module>acrepo-registry-api</module>
<module>acrepo-registry-memory</module>
<module>acrepo-binding-api</module>
<module>acrepo-apix</module>
<module>acrepo-idiomatic-pgsql</module>
<module>acrepo-idiomatic</module>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment