diff --git a/API/pom.xml b/API/pom.xml index aa451d1..0854cd3 100644 --- a/API/pom.xml +++ b/API/pom.xml @@ -19,9 +19,17 @@ asys api - 0.2 + 0.3 bundle + + + org.osgi + org.osgi.core + 6.0.0 + + + ${project.groupId}.${project.artifactId}-${project.version} diff --git a/API/src/main/java/asys/api/ASysUtils.java b/API/src/main/java/asys/api/ASysUtils.java new file mode 100644 index 0000000..40650b4 --- /dev/null +++ b/API/src/main/java/asys/api/ASysUtils.java @@ -0,0 +1,41 @@ +/* + * DmitriyMX + * 2016-08-15 + */ +package asys.api; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +import java.lang.reflect.Method; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.List; +import java.util.Vector; + +public class ASysUtils { + public static ServiceRegistration RegisterCommands(BundleContext bundleContext, Object commnadsObject, String scope) { + return bundleContext.registerService( + commnadsObject.getClass().getName(), + commnadsObject, + getCommandListByAnnotationMethods(scope, commnadsObject.getClass())); + } + + private static Dictionary getCommandListByAnnotationMethods(String scope, Class clazz) { + List listCommand = new Vector<>(); + + Method[] methods = clazz.getMethods(); + for(Method method : methods) { + if (method.isAnnotationPresent(Command.class)) { + listCommand.add(method.getName()); + } + } + + Dictionary props = new Hashtable<>(); + if (listCommand.size() > 0) { + props.put("osgi.command.scope", scope); + props.put("osgi.command.function", listCommand.toArray(new String[listCommand.size()])); + } + return props; + } +} diff --git a/API/src/main/java/asys/api/Command.java b/API/src/main/java/asys/api/Command.java new file mode 100644 index 0000000..f6ff4b9 --- /dev/null +++ b/API/src/main/java/asys/api/Command.java @@ -0,0 +1,15 @@ +/* + * DmitriyMX + * 2016-08-15 + */ +package asys.api; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(value = ElementType.METHOD) +@Retention(value = RetentionPolicy.RUNTIME) +public @interface Command { +}