Skip to content

Commit

Permalink
Catalog OSGi service
Browse files Browse the repository at this point in the history
  • Loading branch information
azzazzel committed Mar 15, 2015
1 parent 1354458 commit 8c9cdf3
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 0 deletions.
1 change: 1 addition & 0 deletions osgi/catalog-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
49 changes: 49 additions & 0 deletions osgi/catalog-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.glassfish.javaeetutorial</groupId>
<artifactId>osgi</artifactId>
<version>7.0.6-SNAPSHOT</version>
</parent>

<artifactId>catalog-service</artifactId>
<name>OSGi Catalog service provider</name>
<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>org.glassfish.javaeetutorial</groupId>
<artifactId>usecases-catalog</artifactId>
<version>7.0.6-SNAPSHOT</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.3</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Export-Package>com.forest.osgi.catalog*</Export-Package>
<Import-Package>*</Import-Package>
<_dsannotations>*</_dsannotations>
</instructions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.forest.osgi.catalog.wiring;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.forest.usecase.catalog.AbstractBaseCategoryManager;
import com.forest.usecase.catalog.CategoryManager;
import com.forest.usecase.catalog.persistence.CategoryPersistence;

@Component(
immediate = true,
service = CategoryManager.class)
public class CategoryService extends AbstractBaseCategoryManager {

private CategoryPersistence categoryPersistence;

@Reference
protected void bindProductPersistence(CategoryPersistence categoryPersistence) {
this.categoryPersistence = categoryPersistence;
}

protected void unbindCategoryPersistence(CategoryPersistence categoryPersistence) {
this.categoryPersistence = null;
}

@Override
protected CategoryPersistence getCategoryPersistence() {
return categoryPersistence;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.forest.osgi.catalog.wiring;

import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;

import org.osgi.service.component.annotations.Component;

import com.forest.model.Category;
import com.forest.usecase.catalog.persistence.CategoryPersistence;

@Component
public class CategoryStorage implements CategoryPersistence {

/*
* In-memory storage (for demo purpose ONLY) !!!
*/
private static final TreeMap<Integer, Category> categoryById = new TreeMap<Integer, Category>();
private static int idGenerator = 1;

@Override
public void createCategory(Category category) {
category.setId(idGenerator++);
categoryById.put(category.getId(), category);
}

@Override
public void updateCategory(Category category) {
if (categoryById.containsKey(category.getId())) {
categoryById.put(category.getId(), category);
}
}

@Override
public void removeCategory(Category category) {
categoryById.remove(category.getId());
}

@Override
public Category getCategory(int id) {
return categoryById.get(id);
}

@Override
public int count() {
return categoryById.size();
}

@Override
public List<Category> findAll() {
return new LinkedList<Category>(categoryById.values());
}

@Override
public List<Category> findRange(int... range) {
fixRange(range, categoryById.size());
return new LinkedList<Category>(categoryById.subMap(range[0], range[1]).values());
}

private void fixRange(int[] range, int size) {
if (range[0] < 0) range[0] = 0;
if (range[0] > size) range[0] = size;
if (range[1] < range[0]) range[1] = range[0];
if (range[1] > size) range[1] = size;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.forest.osgi.catalog.wiring;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.forest.usecase.catalog.AbstractBaseProductManager;
import com.forest.usecase.catalog.ProductManager;
import com.forest.usecase.catalog.persistence.ProductPersistence;

@Component(
immediate = true,
service = ProductManager.class)
public class ProductService extends AbstractBaseProductManager {

private ProductPersistence productPersistence;

@Reference
protected void bindProductPersistence(ProductPersistence productPersistence) {
this.productPersistence = productPersistence;
}

protected void unbindProductPersistence(ProductPersistence productPersistence) {
this.productPersistence = null;
}

@Override
protected ProductPersistence getProductPersistence() {
return productPersistence;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.forest.osgi.catalog.wiring;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;

import org.osgi.service.component.annotations.Component;

import com.forest.model.Product;
import com.forest.usecase.catalog.persistence.ProductPersistence;

@Component
public class ProductStorage implements ProductPersistence {

/*
* In-memory storage (for demo purpose ONLY) !!!
*/
private static final TreeMap<Integer, Product> productsById = new TreeMap<Integer, Product>();
private static final TreeMap<Integer, List<Product>> productsByCategory = new TreeMap<Integer, List<Product>>();
private static int idGenerator = 1;

@Override
public void createProduct(Product product) {
product.setId(idGenerator++);
productsById.put(product.getId(), product);
updateCategory(product);
}

@Override
public void updateProduct(Product product) {
if (productsById.containsKey(product.getId())) {
productsById.put(product.getId(), product);
updateCategory(product);
}
}

@Override
public void removeProduct(Product product) {
productsById.remove(product.getId());
productsByCategory.get(product.getCategory().getId()).remove(product);
}

@Override
public Product getProduct(int id) {
return productsById.get(id);
}

@Override
public int count() {
return productsById.size();
}

@Override
public List<Product> findAll() {
return new LinkedList<Product>(productsById.values());
}

@Override
public List<Product> findRange(int... range) {
fixRange(range, productsById.size());
return new LinkedList<Product>(productsById.subMap(range[0], range[1])
.values());
}

@Override
public List<Product> findByCategory(int[] range, int categoryId) {
List<Product> products = productsByCategory.get(categoryId);
if (products != null) {
fixRange(range, products.size());
return products.subList(range[0], range[1]);
}
return Collections.emptyList();
}

private void fixRange(int[] range, int size) {
if (range[0] < 0) range[0] = 0;
if (range[0] > size) range[0] = size;
if (range[1] < range[0]) range[1] = range[0];
if (range[1] > size) range[1] = size;
}

private void updateCategory(Product product) {
if (product.getCategory() != null) {
Integer categoryId = product.getCategory().getId();
List<Product> products = productsByCategory.get(categoryId);
if (products == null) {
products = new LinkedList<Product>();
productsByCategory.put(categoryId, products);
}
products.add(product);
}
}

}
Binary file not shown.
32 changes: 32 additions & 0 deletions osgi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.glassfish.javaeetutorial</groupId>
<artifactId>dukes-forest</artifactId>
<version>7.0.6-SNAPSHOT</version>
</parent>

<artifactId>osgi</artifactId>
<name>OSGi-services</name>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<modules>
<module>catalog-service</module>
</modules>
</project>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<module>jee-apps</module>
<module>usecases</module>
<module>catalog-microservice</module>
<module>osgi</module>
</modules>


Expand Down

0 comments on commit 8c9cdf3

Please sign in to comment.