diff --git a/osgi/catalog-commands/.gitignore b/osgi/catalog-commands/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/osgi/catalog-commands/.gitignore @@ -0,0 +1 @@ +/target diff --git a/osgi/catalog-commands/pom.xml b/osgi/catalog-commands/pom.xml new file mode 100644 index 0000000..0c6a0df --- /dev/null +++ b/osgi/catalog-commands/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + + org.glassfish.javaeetutorial + osgi + 7.0.6-SNAPSHOT + + + catalog-commands + Sample catalog commands for gogo shell + bundle + + + + org.glassfish.javaeetutorial + catalog-service + 7.0.6-SNAPSHOT + + + + + + + org.apache.felix + maven-bundle-plugin + 2.5.3 + true + + + + bundle + + + + ${project.artifactId} + ${project.version} + com.forest.osgi.command* + * + <_dsannotations>* + + + + + + + + + \ No newline at end of file diff --git a/osgi/catalog-commands/src/main/java/com/forest/osgi/command/ProductCommands.java b/osgi/catalog-commands/src/main/java/com/forest/osgi/command/ProductCommands.java new file mode 100644 index 0000000..5c3b5d8 --- /dev/null +++ b/osgi/catalog-commands/src/main/java/com/forest/osgi/command/ProductCommands.java @@ -0,0 +1,53 @@ +package com.forest.osgi.command; + +import java.util.List; + +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; + +import com.forest.model.Product; +import com.forest.usecase.catalog.ProductManager; + +@Component( + immediate=true, + service = Object.class, + property = { + "osgi.command.function=list", + "osgi.command.function=add", + "osgi.command.scope=product" + }) +public class ProductCommands { + + /* + * As this is only simple DEMO we are going to only implement commands for + * listing and adding products! + */ + + ProductManager productManager; + + @Reference + void bindProductManager (ProductManager productManager) { + this.productManager = productManager; + } + + void unbindProductManager (ProductManager productManager) { + this.productManager = null; + } + + public void add(String name, String description, double price) { + Product product = new Product(); + product.setName(name); + product.setPrice(price); + product.setDescription(description); + productManager.createProduct(product); + System.out.println("Product saved!"); + } + + public void list() { + List products = productManager.getAll(); + System.out.format("%-15s | %-10s | %-40s\n", "NAME", "PRICE", "DESCRIPTION"); + for (Product product : products) { + System.out.format("%-15s | %10.2f | %-40s\n", product.getName(), product.getPrice(), product.getDescription()); + } + } +} diff --git a/osgi/pom.xml b/osgi/pom.xml index eac8ade..5ceef95 100644 --- a/osgi/pom.xml +++ b/osgi/pom.xml @@ -28,5 +28,6 @@ catalog-service + catalog-commands