Repository: incubator-unomi Updated Branches: refs/heads/master e5075636d -> 0c24a0272
UNOMI-215 New rule monitoring commands & other new commands - Refactor commands to use new style for Karaf 4 - Tried to remove Blueprint but was a little too complex because of some of the initialization that is done. Signed-off-by: Serge Huber <shu...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/0c24a027 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/0c24a027 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/0c24a027 Branch: refs/heads/master Commit: 0c24a0272f176ff721fb8bd72473796d219d160b Parents: e507563 Author: Serge Huber <shu...@apache.org> Authored: Tue Dec 4 11:14:10 2018 +0100 Committer: Serge Huber <shu...@apache.org> Committed: Tue Dec 4 11:14:10 2018 +0100 ---------------------------------------------------------------------- tools/shell-commands/pom.xml | 14 ++++++ .../org/apache/unomi/shell/actions/Start.java | 18 ++++---- .../org/apache/unomi/shell/actions/Stop.java | 17 +++---- .../org/apache/unomi/shell/actions/Version.java | 15 ++++-- .../apache/unomi/shell/migration/Migration.java | 6 +-- .../unomi/shell/migration/actions/Migrate.java | 48 ++++++++++++++++---- .../shell/migration/impl/MigrationTo121.java | 8 ++-- .../shell/migration/impl/MigrationTo122.java | 8 ++-- .../shell/migration/utils/ConsoleUtils.java | 20 ++++---- .../unomi/shell/migration/utils/HttpUtils.java | 4 +- .../resources/OSGI-INF/blueprint/blueprint.xml | 30 +----------- 11 files changed, 108 insertions(+), 80 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0c24a027/tools/shell-commands/pom.xml ---------------------------------------------------------------------- diff --git a/tools/shell-commands/pom.xml b/tools/shell-commands/pom.xml index 28eff70..d4e533f 100644 --- a/tools/shell-commands/pom.xml +++ b/tools/shell-commands/pom.xml @@ -75,4 +75,18 @@ </dependency> </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <configuration> + <instructions> + <Karaf-Commands>*</Karaf-Commands> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + </project> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0c24a027/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Start.java ---------------------------------------------------------------------- diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Start.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Start.java index 8f7530e..5909d99 100644 --- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Start.java +++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Start.java @@ -16,22 +16,22 @@ */ package org.apache.unomi.shell.actions; -import org.apache.karaf.shell.commands.Command; -import org.apache.karaf.shell.console.OsgiCommandSupport; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; import org.apache.unomi.shell.services.UnomiManagementService; @Command(scope = "unomi", name = "start", description = "This will start Apache Unomi") -public class Start extends OsgiCommandSupport { +@Service +public class Start implements Action { - private UnomiManagementService unomiManagementService; + @Reference + UnomiManagementService unomiManagementService; - protected Object doExecute() throws Exception { + public Object execute() throws Exception { unomiManagementService.startUnomi(); - return null; } - public void setUnomiManagementService(UnomiManagementService unomiManagementService) { - this.unomiManagementService = unomiManagementService; - } } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0c24a027/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Stop.java ---------------------------------------------------------------------- diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Stop.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Stop.java index d612cdc..8b8cad5 100644 --- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Stop.java +++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Stop.java @@ -16,22 +16,23 @@ */ package org.apache.unomi.shell.actions; -import org.apache.karaf.shell.commands.Command; -import org.apache.karaf.shell.console.OsgiCommandSupport; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; import org.apache.unomi.shell.services.UnomiManagementService; @Command(scope = "unomi", name = "stop", description = "This will stop Apache Unomi") -public class Stop extends OsgiCommandSupport { +@Service +public class Stop implements Action { - private UnomiManagementService unomiManagementService; + @Reference + UnomiManagementService unomiManagementService; - protected Object doExecute() throws Exception { + public Object execute() throws Exception { unomiManagementService.stopUnomi(); return null; } - public void setUnomiManagementService(UnomiManagementService unomiManagementService) { - this.unomiManagementService = unomiManagementService; - } } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0c24a027/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Version.java ---------------------------------------------------------------------- diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Version.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Version.java index 50661e0..de73c96 100644 --- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Version.java +++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/actions/Version.java @@ -16,13 +16,20 @@ */ package org.apache.unomi.shell.actions; -import org.apache.karaf.shell.commands.Command; -import org.apache.karaf.shell.console.OsgiCommandSupport; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.osgi.framework.BundleContext; @Command(scope = "unomi", name = "version", description = "This will print Apache Unomi current version") -public class Version extends OsgiCommandSupport { +@Service +public class Version implements Action { - protected Object doExecute() throws Exception { + @Reference + BundleContext bundleContext; + + public Object execute() throws Exception { System.out.println("Apache Unomi version: " + bundleContext.getBundle().getVersion().toString()); return null; } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0c24a027/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/Migration.java ---------------------------------------------------------------------- diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/Migration.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/Migration.java index 84f3566..b0332eb 100644 --- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/Migration.java +++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/Migration.java @@ -16,8 +16,8 @@ */ package org.apache.unomi.shell.migration; -import org.apache.felix.service.command.CommandSession; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.karaf.shell.api.console.Session; import org.osgi.framework.Version; import java.io.IOException; @@ -48,9 +48,9 @@ public interface Migration { /** * This method is called to execute the migration - * @param session CommandSession + * @param session the shell's session * @param httpClient CloseableHttpClient * @throws IOException */ - void execute(CommandSession session, CloseableHttpClient httpClient, String esAddress) throws IOException; + void execute(Session session, CloseableHttpClient httpClient, String esAddress) throws IOException; } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0c24a027/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/actions/Migrate.java ---------------------------------------------------------------------- diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/actions/Migrate.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/actions/Migrate.java index 4a0d07e..022add8 100644 --- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/actions/Migrate.java +++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/actions/Migrate.java @@ -17,25 +17,36 @@ package org.apache.unomi.shell.migration.actions; import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.karaf.shell.console.OsgiCommandSupport; -import org.apache.karaf.shell.commands.Command; -import org.apache.karaf.shell.commands.Argument; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Argument; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.karaf.shell.api.console.Session; import org.apache.unomi.shell.migration.Migration; import org.apache.unomi.shell.migration.utils.ConsoleUtils; import org.apache.unomi.shell.migration.utils.HttpUtils; +import org.osgi.framework.BundleContext; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; import org.osgi.framework.Version; import java.util.*; @Command(scope = "unomi", name = "migrate", description = "This will Migrate your date in ES to be compliant with current version") -public class Migrate extends OsgiCommandSupport { +@Service +public class Migrate implements Action { - private List<Migration> migrations; + @Reference + Session session; + + @Reference + BundleContext bundleContext; @Argument(name = "fromVersionWithoutSuffix", description = "Origin version without suffix/qualifier (e.g: 1.2.0)", multiValued = false, valueToShowInHelp = "1.2.0") private String fromVersionWithoutSuffix; - protected Object doExecute() throws Exception { + public Object execute() throws Exception { if (fromVersionWithoutSuffix == null) { listMigrations(); return null; @@ -63,7 +74,7 @@ public class Migrate extends OsgiCommandSupport { String esAddress = ConsoleUtils.askUserWithDefaultAnswer(session, "Elasticsearch address (default = http://localhost:9200): ", "http://localhost:9200"); - for (Migration migration : migrations) { + for (Migration migration : getMigrations()) { if (fromVersion.compareTo(migration.getToVersion()) < 0) { String migrateConfirmation = ConsoleUtils.askUserWithAuthorizedAnswer(session,"Starting migration to version " + migration.getToVersion() + ", do you want to proceed? (yes/no): ", Arrays.asList("yes", "no")); if (migrateConfirmation.equalsIgnoreCase("no")) { @@ -90,7 +101,7 @@ public class Migrate extends OsgiCommandSupport { private void listMigrations() { Version previousVersion = new Version("0.0.0"); - for (Migration migration : migrations) { + for (Migration migration : getMigrations()) { if (migration.getToVersion().getMajor() > previousVersion.getMajor() || migration.getToVersion().getMinor() > previousVersion.getMinor()) { System.out.println("From " + migration.getToVersion().getMajor() + "." + migration.getToVersion().getMinor() + ".0:"); } @@ -101,7 +112,24 @@ public class Migrate extends OsgiCommandSupport { } - public void setMigrations(List<Migration> migrations) { - this.migrations = migrations; + private List<Migration> getMigrations() { + Collection<ServiceReference<Migration>> migrationServiceReferences = null; + try { + migrationServiceReferences = bundleContext.getServiceReferences(Migration.class, null); + } catch (InvalidSyntaxException e) { + e.printStackTrace(); + } + SortedSet<Migration> migrations = new TreeSet<>(new Comparator<Migration>() { + @Override + public int compare(Migration o1, Migration o2) { + return o1.getToVersion().compareTo(o2.getToVersion()); + } + }); + for (ServiceReference<Migration> migrationServiceReference : migrationServiceReferences) { + Migration migration = bundleContext.getService(migrationServiceReference); + migrations.add(migration); + } + return new ArrayList<>(migrations); } + } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0c24a027/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo121.java ---------------------------------------------------------------------- diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo121.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo121.java index 4187862..8fd309e 100644 --- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo121.java +++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo121.java @@ -17,14 +17,15 @@ package org.apache.unomi.shell.migration.impl; import org.apache.commons.lang3.StringUtils; -import org.apache.felix.service.command.CommandSession; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.karaf.shell.api.console.Session; import org.apache.unomi.shell.migration.Migration; import org.apache.unomi.shell.migration.utils.ConsoleUtils; import org.apache.unomi.shell.migration.utils.MigrationUtils; import org.json.JSONArray; import org.json.JSONObject; import org.osgi.framework.Version; +import org.osgi.service.component.annotations.Component; import java.io.IOException; import java.util.*; @@ -32,10 +33,11 @@ import java.util.*; /** * @author dgaillard */ +@Component public class MigrationTo121 implements Migration { private CloseableHttpClient httpClient; - private CommandSession session; + private Session session; private String esAddress; private LinkedHashMap<String, List<String>> tagsStructurePriorTo130; private List propsTaggedAsPersonalIdentifier = Arrays.asList("firstName", "lastName", "email", "phoneNumber", "address", "facebookId", "googleId", "linkedInId", "twitterId"); @@ -56,7 +58,7 @@ public class MigrationTo121 implements Migration { } @Override - public void execute(CommandSession session, CloseableHttpClient httpClient, String esAddress) throws IOException { + public void execute(Session session, CloseableHttpClient httpClient, String esAddress) throws IOException { this.httpClient = httpClient; this.session = session; this.esAddress = esAddress; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0c24a027/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo122.java ---------------------------------------------------------------------- diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo122.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo122.java index 6782728..3b6ad07 100644 --- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo122.java +++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo122.java @@ -16,19 +16,21 @@ */ package org.apache.unomi.shell.migration.impl; -import org.apache.felix.service.command.CommandSession; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.karaf.shell.api.console.Session; import org.apache.unomi.shell.migration.Migration; import org.apache.unomi.shell.migration.utils.ConsoleUtils; import org.apache.unomi.shell.migration.utils.HttpRequestException; import org.apache.unomi.shell.migration.utils.HttpUtils; import org.osgi.framework.Version; +import org.osgi.service.component.annotations.Component; import java.io.IOException; +@Component public class MigrationTo122 implements Migration { private CloseableHttpClient httpClient; - private CommandSession session; + private Session session; private String esAddress; @Override @@ -47,7 +49,7 @@ public class MigrationTo122 implements Migration { } @Override - public void execute(CommandSession session, CloseableHttpClient httpClient, String esAddress) throws IOException { + public void execute(Session session, CloseableHttpClient httpClient, String esAddress) throws IOException { this.httpClient = httpClient; this.session = session; this.esAddress = esAddress; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0c24a027/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/ConsoleUtils.java ---------------------------------------------------------------------- diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/ConsoleUtils.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/ConsoleUtils.java index cae0561..2a26979 100644 --- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/ConsoleUtils.java +++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/ConsoleUtils.java @@ -17,7 +17,7 @@ package org.apache.unomi.shell.migration.utils; import org.apache.commons.lang3.StringUtils; -import org.apache.felix.service.command.CommandSession; +import org.apache.karaf.shell.api.console.Session; import org.jline.reader.LineReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,13 +35,13 @@ public class ConsoleUtils { /** * This will ask a question to the user and return the default answer if the user does not answer. * - * @param session CommandSession + * @param session the shell's session * @param msg String message to ask * @param defaultAnswer String default answer * @return the user answer * @throws IOException */ - public static String askUserWithDefaultAnswer(CommandSession session, String msg, String defaultAnswer) throws IOException { + public static String askUserWithDefaultAnswer(Session session, String msg, String defaultAnswer) throws IOException { String answer = promptMessageToUser(session, msg); if (StringUtils.isBlank(answer)) { return defaultAnswer; @@ -53,13 +53,13 @@ public class ConsoleUtils { * This method allow you to ask a question to the user. * The answer is controlled before being return so the question will be ask until the user enter one the authorized answer * - * @param session CommandSession + * @param session the shell's session * @param msg String message to ask * @param authorizedAnswer Array of possible answer, all answer must be in lower case * @return the user answer * @throws IOException */ - public static String askUserWithAuthorizedAnswer(CommandSession session, String msg, List<String> authorizedAnswer) throws IOException { + public static String askUserWithAuthorizedAnswer(Session session, String msg, List<String> authorizedAnswer) throws IOException { String answer; do { answer = promptMessageToUser(session,msg); @@ -71,22 +71,22 @@ public class ConsoleUtils { * This method allow you to prompt a message to the user. * No control is done on the answer provided by the user. * - * @param session CommandSession + * @param session the shell's session * @param msg String message to prompt * @return the user answer * @throws IOException */ - public static String promptMessageToUser(CommandSession session, String msg) throws IOException { + public static String promptMessageToUser(Session session, String msg) throws IOException { LineReader reader = (LineReader) session.get(".jline.reader"); return reader.readLine(msg, null); } /** * Print a message in the console. - * @param session - * @param msg + * @param session the shell's session + * @param msg the message to print out with a newline */ - public static void printMessage(CommandSession session, String msg) { + public static void printMessage(Session session, String msg) { PrintStream writer = session.getConsole(); writer.println(msg); } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0c24a027/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java ---------------------------------------------------------------------- diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java index 18c12f1..fa56465 100644 --- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java +++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java @@ -16,7 +16,6 @@ */ package org.apache.unomi.shell.migration.utils; -import org.apache.felix.service.command.CommandSession; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.*; @@ -32,6 +31,7 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils; +import org.apache.karaf.shell.api.console.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,7 +52,7 @@ import java.util.Map; public class HttpUtils { private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); - public static CloseableHttpClient initHttpClient(CommandSession session) throws IOException { + public static CloseableHttpClient initHttpClient(Session session) throws IOException { String confirmation = ConsoleUtils.askUserWithAuthorizedAnswer(session,"We need to initialize a HttpClient, do we need to trust all certificates? (yes/no): ", Arrays.asList("yes", "no")); boolean trustAllCertificates = confirmation.equalsIgnoreCase("yes"); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0c24a027/tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml index c6828ed..3b27913 100644 --- a/tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -18,38 +18,12 @@ <blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" - xmlns:shell="http://karaf.apache.org/xmlns/shell/v1.1.0" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <bean id="migrateTo121" class="org.apache.unomi.shell.migration.impl.MigrationTo121"/> <bean id="migrateTo122" class="org.apache.unomi.shell.migration.impl.MigrationTo122"/> - - <shell:command-bundle> - <shell:command> - <shell:action class="org.apache.unomi.shell.migration.actions.Migrate"> - <shell:property name="migrations"> - <list> - <!-- Migration beans must be executed in the right order so you must place new bean at the end of this list --> - <ref component-id="migrateTo121"/> - <ref component-id="migrateTo122"/> - </list> - </shell:property> - </shell:action> - </shell:command> - <shell:command> - <shell:action class="org.apache.unomi.shell.actions.Version"/> - </shell:command> - <shell:command> - <shell:action class="org.apache.unomi.shell.actions.Start"> - <shell:property name="unomiManagementService" ref="unomiManagementServiceImpl"/> - </shell:action> - </shell:command> - <shell:command> - <shell:action class="org.apache.unomi.shell.actions.Stop"> - <shell:property name="unomiManagementService" ref="unomiManagementServiceImpl"/> - </shell:action> - </shell:command> - </shell:command-bundle> + <service ref="migrateTo121" interface="org.apache.unomi.shell.migration.Migration" /> + <service ref="migrateTo122" interface="org.apache.unomi.shell.migration.Migration" /> <bean id="unomiManagementServiceImpl" class="org.apache.unomi.shell.services.internal.UnomiManagementServiceImpl" init-method="init"> <property name="bundleSymbolicNames">