Skip to content

Commit 020a8ef

Browse files
committed
First version, start by including OSGi module
1 parent dda3162 commit 020a8ef

File tree

13 files changed

+562
-2
lines changed

13 files changed

+562
-2
lines changed

README.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1-
# jackson-base-modules
2-
Uber-project for foundational modules of Jackson that build directly on core components but nothing else; not including data format or datatype modules
1+
## Overview
2+
3+
This is a multi-module umbrella project for [Jackson](../../../jackson)
4+
modules that are considered foundational, building on core databind, but
5+
not including datatype or data format modules.
6+
7+
Currently included are:
8+
9+
* [Afterburner](afterburner/)
10+
* [Mr Bean](mrbean/)
11+
* [OSGi](osgi/)
12+
13+
All modules are licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt).
14+
15+
## Status
16+
17+
[![Build Status](https://travis-ci.org/FasterXML/jackson-base-modules.svg)](https://travis-ci.org/FasterXML/jackson-base-modules)
18+
19+
## More
20+
21+
See [Wiki](../../wiki) for more information (javadocs).

osgi/README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
jackson-module-osgi
2+
====================
3+
4+
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.fasterxml.jackson.module/jackson-module-osgi/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.fasterxml.jackson.module/jackson-module-osgi/)
5+
[![Javadoc](https://javadoc-emblem.rhcloud.com/doc/com.fasterxml.jackson.module/jackson-module-osgi/badge.svg)](http://www.javadoc.io/doc/com.fasterxml.jackson.module/jackson-module-osgi)
6+
7+
This module provides a way to inject OSGI services into deserialized objects.
8+
Thanks to the _JacksonInject_ annotations, the _OsgiJacksonModule_ will search for the required service in the OSGI service registry and injects it in the object while deserializing.
9+
10+
## Usage
11+
12+
For example, imagine a drawing software that persists shapes in JSON documents (on file system, mongodb or orientdb). The _Shape_ object needs a _DrawingService_ that is in charge of drawing shapes.
13+
14+
```java
15+
interface Drawable {
16+
void draw();
17+
}
18+
19+
interface DrawingService {
20+
void draw(Drawable drawable);
21+
}
22+
23+
class Shape implements Drawable {
24+
public int x;
25+
public int y;
26+
27+
private DrawingService drawingService;
28+
29+
public Shape(@JacksonInject DrawingService drawingService)
30+
{
31+
this.drawingService = drawingService;
32+
}
33+
34+
@Override
35+
public void draw()
36+
{
37+
drawingService.draw(this);
38+
}
39+
}
40+
```
41+
42+
To deserialize shapes and to inject the drawing service :
43+
44+
```java
45+
ObjectMapper mapper = new ObjectMapper();
46+
mapper.registerModule(new OsgiJacksonModule(bundleContext));
47+
Shape shape = mapper.reader().forType(Shape.class).readValue("{\"x\":13,\"y\":21}");
48+
```
49+
50+
The module supports OSGI filters to select the service more accurately :
51+
52+
```java
53+
public Shape(@JacksonInject(value = "(provider=ACME)") DrawingService drawingService)
54+
{
55+
this.drawingService = drawingService;
56+
}
57+
```
58+
59+
## Limitations
60+
61+
* injecting value in setter is not supported
62+
* dynamicity is not supported. If the service is unregistered, the deserialized object will keep the old service reference.

osgi/pom.xml

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.fasterxml.jackson.module</groupId>
6+
<artifactId>jackson-base-modules-parent</artifactId>
7+
<version>2.7.1-SNAPSHOT</version>
8+
</parent>
9+
<groupId>com.fasterxml.jackson.module</groupId>
10+
<artifactId>jackson-module-osgi</artifactId>
11+
<name>Jackson-module-osgi</name>
12+
<packaging>bundle</packaging>
13+
14+
<description>Jackson module to inject OSGI services in deserialized beans.</description>
15+
<url>https://github.com/FasterXML/jackson-base-modules</url>
16+
17+
<properties>
18+
<version.osgi.core>5.0.0</version.osgi.core>
19+
<version.mockito>1.10.19</version.mockito>
20+
<version.junit>4.12</version.junit>
21+
22+
<!-- Generate PackageVersion.java into this directory. -->
23+
<packageVersion.dir>com/fasterxml/jackson/module/osgi</packageVersion.dir>
24+
<packageVersion.package>${project.groupId}.osgi</packageVersion.package>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.core</groupId>
30+
<artifactId>jackson-databind</artifactId>
31+
<version>${version.jackson.core}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.osgi</groupId>
35+
<artifactId>org.osgi.core</artifactId>
36+
<version>${version.osgi.core}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.mockito</groupId>
40+
<artifactId>mockito-all</artifactId>
41+
<version>${version.mockito}</version>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<!-- Inherited from oss-base. Generate PackageVersion.java.-->
50+
<groupId>com.google.code.maven-replacer-plugin</groupId>
51+
<artifactId>replacer</artifactId>
52+
<executions>
53+
<execution>
54+
<id>process-packageVersion</id>
55+
<phase>generate-sources</phase>
56+
</execution>
57+
</executions>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
62+
</project>

osgi/release-notes/VERSION

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Project: jackson-module-osgi
2+
3+
------------------------------------------------------------------------
4+
=== Releases ===
5+
------------------------------------------------------------------------
6+
7+
2.7.0 (10-Jan-2016)
8+
9+
No changes since 2.6
10+
11+
2.6.3 (12-Oct-2015)
12+
2.6.2 (15-Sep-2015)
13+
2.6.1 (09-Aug-2015)
14+
15+
- Fix version information generation, module version was not being exposed.
16+
17+
2.6.0 (03-Aug-2015)
18+
19+
The first official release!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.fasterxml.jackson.module.osgi;
2+
3+
import org.osgi.framework.BundleContext;
4+
import org.osgi.framework.InvalidSyntaxException;
5+
import org.osgi.framework.ServiceReference;
6+
7+
import com.fasterxml.jackson.databind.BeanProperty;
8+
import com.fasterxml.jackson.databind.DeserializationContext;
9+
import com.fasterxml.jackson.databind.InjectableValues;
10+
11+
/**
12+
* Injects OSGI services in deserialized objects
13+
* <br/>
14+
* Use the {@link com.fasterxml.jackson.annotation.JacksonInject} in the constructor parameters or the class members ask for injecting a matching OSGI services.
15+
* Use the {@link com.fasterxml.jackson.annotation.JacksonInject#value()} to specify an OSGI filter to select more accurately the OSGI services.
16+
* Null is injected when no matching OSGI service is registered.
17+
*/
18+
public class OsgiInjectableValues extends InjectableValues
19+
{
20+
private final BundleContext bundleContext;
21+
22+
public OsgiInjectableValues(BundleContext bundleContext)
23+
{
24+
this.bundleContext = bundleContext;
25+
}
26+
27+
@Override
28+
public Object findInjectableValue(Object valueId,
29+
DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance)
30+
{
31+
return findService(serviceType(forProperty), serviceFilter(valueId));
32+
}
33+
34+
private Object findService(String type, String filter)
35+
{
36+
try
37+
{
38+
ServiceReference<?>[] srs = bundleContext.getServiceReferences(type, filter);
39+
if (srs == null || srs.length == 0) {
40+
return null;
41+
}
42+
return bundleContext.getService(srs[0]);
43+
} catch (InvalidSyntaxException e) {
44+
// this will never happen as the filter was checked before
45+
throw new RuntimeException(e);
46+
}
47+
}
48+
49+
private static String serviceType(BeanProperty forProperty)
50+
{
51+
return forProperty.getType().toCanonical();
52+
}
53+
54+
private String serviceFilter(Object valueId)
55+
{
56+
try {
57+
return bundleContext.createFilter(valueId.toString()).toString();
58+
} catch (InvalidSyntaxException e) {
59+
return null;
60+
}
61+
}
62+
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fasterxml.jackson.module.osgi;
2+
3+
import org.osgi.framework.BundleContext;
4+
5+
import com.fasterxml.jackson.core.Version;
6+
import com.fasterxml.jackson.databind.Module;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
9+
/**
10+
* A Jackson Module to inject OSGI services in deserialized objects.
11+
* Note that registration will replace possibly exsting value injector
12+
* ({@link com.fasterxml.jackson.databind.InjectableValues}) implementation
13+
* by calling {@link ObjectMapper#setInjectableValues}.
14+
*
15+
* @see OsgiInjectableValues
16+
*/
17+
public class OsgiJacksonModule extends Module
18+
{
19+
private final BundleContext bundleContext;
20+
21+
public OsgiJacksonModule(BundleContext bundleContext)
22+
{
23+
this.bundleContext = bundleContext;
24+
}
25+
26+
@Override
27+
public String getModuleName() {
28+
return getClass().getSimpleName();
29+
}
30+
31+
@Override
32+
public Version version() {
33+
return PackageVersion.VERSION;
34+
}
35+
36+
@Override
37+
public void setupModule(SetupContext context)
38+
{
39+
ObjectMapper mapper = context.getOwner();
40+
mapper.setInjectableValues(new OsgiInjectableValues(bundleContext));
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package @package@;
2+
3+
import com.fasterxml.jackson.core.Version;
4+
import com.fasterxml.jackson.core.Versioned;
5+
import com.fasterxml.jackson.core.util.VersionUtil;
6+
7+
/**
8+
* Automatically generated from PackageVersion.java.in during
9+
* packageVersion-generate execution of maven-replacer-plugin in
10+
* pom.xml.
11+
*/
12+
public final class PackageVersion implements Versioned {
13+
public final static Version VERSION = VersionUtil.parseVersion(
14+
"@projectversion@", "@projectgroupid@", "@projectartifactid@");
15+
16+
@Override
17+
public Version version() {
18+
return VERSION;
19+
}
20+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This copy of Jackson JSON processor streaming parser/generator is licensed under the
2+
Apache (Software) License, version 2.0 ("the License").
3+
See the License for details about distribution rights, and the
4+
specific rights regarding derivate works.
5+
6+
You may obtain a copy of the License at:
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Jackson JSON processor
2+
3+
Jackson is a high-performance, Free/Open Source JSON processing library.
4+
It was originally written by Tatu Saloranta (tatu.saloranta@iki.fi), and has
5+
been in development since 2007.
6+
It is currently developed by a community of developers, as well as supported
7+
commercially by FasterXML.com.
8+
9+
## Licensing
10+
11+
Jackson core and extension components may licensed under different licenses.
12+
To find the details that apply to this artifact see the accompanying LICENSE file.
13+
For more information, including possible other licensing options, contact
14+
FasterXML.com (http://fasterxml.com).
15+
16+
## Credits
17+
18+
A list of contributors may be found from CREDITS file, which is included
19+
in some artifacts (usually source distributions); but is always available
20+
from the source code management (SCM) system project uses.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.fasterxml.jackson.module.osgi.OsgiJacksonModule

0 commit comments

Comments
 (0)