-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
33 changes: 33 additions & 0 deletions
33
osgi/catalog-service/src/main/java/com/forest/osgi/catalog/wiring/CategoryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
} |
66 changes: 66 additions & 0 deletions
66
osgi/catalog-service/src/main/java/com/forest/osgi/catalog/wiring/CategoryStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
osgi/catalog-service/src/main/java/com/forest/osgi/catalog/wiring/ProductService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
osgi/catalog-service/src/main/java/com/forest/osgi/catalog/wiring/ProductStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters