Repository: incubator-unomi Updated Branches: refs/heads/master b4b0560b4 -> 11c847c05
UNOMI-200 New Karaf shell commands for events, sessions and profiles Also in this commit, all existing commands have been prefixed using the unomi scope. Signed-off-by: Serge Huber <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/11c847c0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/11c847c0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/11c847c0 Branch: refs/heads/master Commit: 11c847c0594b63d96c550ab57abbad32d3ca109f Parents: b4b0560 Author: Serge Huber <[email protected]> Authored: Fri Sep 21 15:39:22 2018 +0200 Committer: Serge Huber <[email protected]> Committed: Fri Sep 21 15:39:22 2018 +0200 ---------------------------------------------------------------------- .../unomi/shell/commands/ActionListCommand.java | 2 +- .../unomi/shell/commands/ActionViewCommand.java | 2 +- .../unomi/shell/commands/EventTailCommand.java | 126 +++++++++++++++++++ .../unomi/shell/commands/EventViewCommand.java | 63 ++++++++++ .../shell/commands/ProfileListCommand.java | 75 +++++++++++ .../shell/commands/ProfileViewCommand.java | 49 ++++++++ .../unomi/shell/commands/RuleListCommand.java | 2 +- .../shell/commands/RuleResetStatsCommand.java | 2 +- .../unomi/shell/commands/RuleViewcommand.java | 2 +- .../shell/commands/SegmentListCommand.java | 2 +- .../shell/commands/SegmentViewCommand.java | 2 +- .../shell/commands/SessionListCommand.java | 75 +++++++++++ .../shell/commands/SessionViewCommand.java | 48 +++++++ .../resources/OSGI-INF/blueprint/blueprint.xml | 39 +++++- 14 files changed, 476 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ActionListCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ActionListCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ActionListCommand.java index 74a4339..d6525ec 100644 --- a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ActionListCommand.java +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ActionListCommand.java @@ -25,7 +25,7 @@ import org.apache.unomi.common.DataTable; import java.util.ArrayList; import java.util.Collection; -@Command(scope = "action", name = "list", description = "This will list all the actions deployed in the Apache Unomi Context Server") +@Command(scope = "unomi", name = "action-list", description = "This will list all the actions deployed in the Apache Unomi Context Server") public class ActionListCommand extends ListCommandSupport { private DefinitionsService definitionsService; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ActionViewCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ActionViewCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ActionViewCommand.java index 3852fc0..9d1b820 100644 --- a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ActionViewCommand.java +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ActionViewCommand.java @@ -23,7 +23,7 @@ import org.apache.unomi.api.actions.ActionType; import org.apache.unomi.api.services.DefinitionsService; import org.apache.unomi.persistence.spi.CustomObjectMapper; -@Command(scope = "action", name = "view", description = "This will display a single action deployed in the Apache Unomi Context Server") +@Command(scope = "unomi", name = "action-view", description = "This will display a single action deployed in the Apache Unomi Context Server") public class ActionViewCommand extends OsgiCommandSupport { private DefinitionsService definitionsService; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/EventTailCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/EventTailCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/EventTailCommand.java new file mode 100644 index 0000000..4df6931 --- /dev/null +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/EventTailCommand.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.shell.commands; + +import org.apache.commons.lang3.StringUtils; +import org.apache.karaf.shell.commands.Argument; +import org.apache.karaf.shell.commands.Command; +import org.apache.karaf.shell.console.OsgiCommandSupport; +import org.apache.unomi.api.Event; +import org.apache.unomi.api.services.EventListenerService; +import org.apache.unomi.api.services.EventService; +import org.osgi.framework.ServiceRegistration; + +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.List; + +@Command(scope = "unomi", name = "event-tail", description = "This will tail all the events coming into the Apache Unomi Context Server") +public class EventTailCommand extends OsgiCommandSupport { + + @Argument(index = 0, name = "withInternal", description = "The identifier for the event", required = false, multiValued = false) + boolean withInternal = false; + + int[] columnSizes = new int[] { 36, 14, 36, 36, 29, 15, 5 }; + String[] columnHeaders = new String[] { + "ID", + "Type", + "Session", + "Profile", + "Timestamp", + "Scope", + "Persistent" + }; + + @Override + protected Object doExecute() throws Exception { + PrintStream out = session.getConsole(); + out.flush(); + + TailEventListener tailEventListener = new TailEventListener(out); + + StringBuilder headerLine = new StringBuilder(); + for (int i=0; i < columnSizes.length; i++) { + headerLine.append(getColumn(columnSizes[i], columnHeaders[i])); + headerLine.append("|"); + } + System.out.println(headerLine.toString()); + System.out.println(StringUtils.repeat("-", headerLine.length())); + ServiceRegistration<EventListenerService> tailServiceRegistration = bundleContext.registerService(EventListenerService.class, tailEventListener, new Hashtable<>()); + try { + synchronized (this) { + wait(); + } + out.println("Stopping tail as log.core bundle was stopped."); + } catch (InterruptedException e) { + // Ignore as it will happen if the user breaks the tail using Ctrl-C + } finally { + tailServiceRegistration.unregister(); + } + return null; + } + + protected String getColumn(int columnSize, String columnContent) { + if (columnContent == null) { + columnContent = "null"; + } + if (columnContent.length() == columnSize) { + return columnContent; + } + if (columnContent.length() < columnSize) { + return columnContent + StringUtils.repeat(" ", columnSize - columnContent.length()); + } + return columnContent.substring(0, columnSize); + } + + class TailEventListener implements EventListenerService { + + PrintStream out; + + public TailEventListener(PrintStream out) { + this.out = out; + } + + @Override + public boolean canHandle(Event event) { + return true; + } + + @Override + public int onEvent(Event event) { + if (!event.isPersistent() && !withInternal) { + return EventService.NO_CHANGE; + } + List<String> eventInfo = new ArrayList<>(); + eventInfo.add(event.getItemId()); + eventInfo.add(event.getEventType()); + eventInfo.add(event.getSessionId()); + eventInfo.add(event.getProfileId()); + eventInfo.add(event.getTimeStamp().toString()); + eventInfo.add(event.getScope()); + eventInfo.add(Boolean.toString(event.isPersistent())); + StringBuilder eventLine = new StringBuilder(); + for (int i=0; i < columnSizes.length; i++) { + eventLine.append(getColumn(columnSizes[i], eventInfo.get(i))); + eventLine.append("|"); + } + System.out.println(eventLine.toString()); + return EventService.NO_CHANGE; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/EventViewCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/EventViewCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/EventViewCommand.java new file mode 100644 index 0000000..9be10d5 --- /dev/null +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/EventViewCommand.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.shell.commands; + +import org.apache.karaf.shell.commands.Argument; +import org.apache.karaf.shell.commands.Command; +import org.apache.karaf.shell.console.OsgiCommandSupport; +import org.apache.unomi.api.Event; +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.conditions.Condition; +import org.apache.unomi.api.services.DefinitionsService; +import org.apache.unomi.api.services.EventService; +import org.apache.unomi.persistence.spi.CustomObjectMapper; + +@Command(scope = "unomi", name = "event-view", description = "This command will dump an Event as a JSON object") +public class EventViewCommand extends OsgiCommandSupport { + + private EventService eventService; + private DefinitionsService definitionsService; + + public void setEventService(EventService eventService) { + this.eventService = eventService; + } + + public void setDefinitionsService(DefinitionsService definitionsService) { + this.definitionsService = definitionsService; + } + + @Argument(index = 0, name = "event", description = "The identifier for the event", required = true, multiValued = false) + String eventIdentifier; + + @Override + protected Object doExecute() throws Exception { + + Condition eventCondition = new Condition(definitionsService.getConditionType("eventPropertyCondition")); + eventCondition.setParameter("propertyName", "itemId"); + eventCondition.setParameter("comparisonOperator", "equals"); + eventCondition.setParameter("propertyValue", eventIdentifier); + + PartialList<Event> matchingEvents = eventService.searchEvents(eventCondition, 0, 10); + if (matchingEvents == null || matchingEvents.getTotalSize() != 1) { + System.out.println("Couldn't find a single event with id=" + eventIdentifier + ". Maybe it wasn't a persistent event ?"); + return null; + } + String jsonEvent = CustomObjectMapper.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(matchingEvents.get(0)); + System.out.println(jsonEvent); + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ProfileListCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ProfileListCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ProfileListCommand.java new file mode 100644 index 0000000..7af8a49 --- /dev/null +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ProfileListCommand.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.shell.commands; + +import org.apache.commons.lang3.StringUtils; +import org.apache.karaf.shell.commands.Command; +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.Profile; +import org.apache.unomi.api.conditions.Condition; +import org.apache.unomi.api.query.Query; +import org.apache.unomi.api.services.DefinitionsService; +import org.apache.unomi.api.services.ProfileService; +import org.apache.unomi.common.DataTable; + +import java.util.ArrayList; + +@Command(scope = "unomi", name = "profile-list", description = "This commands lists the latest profiles updated in the Apache Unomi Context Server") +public class ProfileListCommand extends ListCommandSupport { + + private ProfileService profileService; + private DefinitionsService definitionsService; + + public void setProfileService(ProfileService profileService) { + this.profileService = profileService; + } + + public void setDefinitionsService(DefinitionsService definitionsService) { + this.definitionsService = definitionsService; + } + + @java.lang.Override + protected String[] getHeaders() { + return new String[] { + "ID", + "Scope", + "Segments", + "Consents", + "Last modification", + }; + } + + @java.lang.Override + protected DataTable buildDataTable() { + Query query = new Query(); + query.setSortby("properties.lastVisit:desc"); + Condition matchAllCondition = new Condition(definitionsService.getConditionType("matchAllCondition")); + query.setCondition(matchAllCondition); + PartialList<Profile> lastModifiedProfiles = profileService.search(query, Profile.class); + DataTable dataTable = new DataTable(); + for (Profile profile : lastModifiedProfiles.getList()) { + ArrayList<Comparable> rowData = new ArrayList<>(); + rowData.add(profile.getItemId()); + rowData.add(profile.getScope()); + rowData.add(StringUtils.join(profile.getSegments(), ",")); + rowData.add(StringUtils.join(profile.getConsents().keySet(), ",")); + rowData.add((String) profile.getProperty("lastVisit")); + dataTable.addRow(rowData.toArray(new Comparable[rowData.size()])); + } + return dataTable; + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ProfileViewCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ProfileViewCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ProfileViewCommand.java new file mode 100644 index 0000000..34b77e3 --- /dev/null +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/ProfileViewCommand.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.shell.commands; + +import org.apache.karaf.shell.commands.Argument; +import org.apache.karaf.shell.commands.Command; +import org.apache.karaf.shell.console.OsgiCommandSupport; +import org.apache.unomi.api.Profile; +import org.apache.unomi.api.services.ProfileService; +import org.apache.unomi.persistence.spi.CustomObjectMapper; + +@Command(scope = "unomi", name = "profile-view", description = "This command will dump a profile as a JSON string") +public class ProfileViewCommand extends OsgiCommandSupport { + + private ProfileService profileService; + + public void setProfileService(ProfileService profileService) { + this.profileService = profileService; + } + + @Argument(index = 0, name = "profile", description = "The identifier for the profile", required = true, multiValued = false) + String profileIdentifier; + + @Override + protected Object doExecute() throws Exception { + Profile profile = profileService.load(profileIdentifier); + if (profile == null) { + System.out.println("Couldn't find a profile with id=" + profileIdentifier); + return null; + } + String jsonProfile = CustomObjectMapper.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(profile); + System.out.println(jsonProfile); + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleListCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleListCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleListCommand.java index 5b0baf0..d7ce5c6 100644 --- a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleListCommand.java +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleListCommand.java @@ -27,7 +27,7 @@ import java.util.ArrayList; import java.util.Map; import java.util.Set; -@Command(scope = "rule", name = "list", description = "This will list all the rules deployed in the Apache Unomi Context Server") +@Command(scope = "unomi", name = "rule-list", description = "This will list all the rules deployed in the Apache Unomi Context Server") public class RuleListCommand extends ListCommandSupport { private RulesService rulesService; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleResetStatsCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleResetStatsCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleResetStatsCommand.java index eb50a15..3ab0b66 100644 --- a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleResetStatsCommand.java +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleResetStatsCommand.java @@ -20,7 +20,7 @@ import org.apache.karaf.shell.commands.Command; import org.apache.karaf.shell.console.OsgiCommandSupport; import org.apache.unomi.api.services.RulesService; -@Command(scope = "rule", name = "reset-stats", description = "This command will reset the rule statistics") +@Command(scope = "unomi", name = "rule-reset-stats", description = "This command will reset the rule statistics") public class RuleResetStatsCommand extends OsgiCommandSupport { private RulesService rulesService; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleViewcommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleViewcommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleViewcommand.java index 4f86f5e..c37a107 100644 --- a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleViewcommand.java +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/RuleViewcommand.java @@ -23,7 +23,7 @@ import org.apache.unomi.api.rules.Rule; import org.apache.unomi.api.services.RulesService; import org.apache.unomi.persistence.spi.CustomObjectMapper; -@Command(scope = "rule", name = "view", description = "This will allows to view a rule in the Apache Unomi Context Server") +@Command(scope = "unomi", name = "rule-view", description = "This will allows to view a rule in the Apache Unomi Context Server") public class RuleViewcommand extends OsgiCommandSupport { private RulesService rulesService; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SegmentListCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SegmentListCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SegmentListCommand.java index 75f3a67..858dc93 100644 --- a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SegmentListCommand.java +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SegmentListCommand.java @@ -25,7 +25,7 @@ import org.apache.unomi.common.DataTable; import java.util.ArrayList; -@Command(scope = "segment", name = "list", description = "This will list all the segments present in the Apache Unomi Context Server") +@Command(scope = "unomi", name = "segment-list", description = "This will list all the segments present in the Apache Unomi Context Server") public class SegmentListCommand extends ListCommandSupport { private SegmentService segmentService; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SegmentViewCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SegmentViewCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SegmentViewCommand.java index ed448e4..3869e60 100644 --- a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SegmentViewCommand.java +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SegmentViewCommand.java @@ -23,7 +23,7 @@ import org.apache.unomi.api.segments.Segment; import org.apache.unomi.api.services.SegmentService; import org.apache.unomi.persistence.spi.CustomObjectMapper; -@Command(scope = "segment", name = "view", description = "This will allows to view a segment in the Apache Unomi Context Server") +@Command(scope = "unomi", name = "segment-view", description = "This will allows to view a segment in the Apache Unomi Context Server") public class SegmentViewCommand extends OsgiCommandSupport { private SegmentService segmentService; http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SessionListCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SessionListCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SessionListCommand.java new file mode 100644 index 0000000..237712a --- /dev/null +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SessionListCommand.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.shell.commands; + +import org.apache.karaf.shell.commands.Command; +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.Session; +import org.apache.unomi.api.conditions.Condition; +import org.apache.unomi.api.query.Query; +import org.apache.unomi.api.services.DefinitionsService; +import org.apache.unomi.api.services.ProfileService; +import org.apache.unomi.common.DataTable; + +import java.util.ArrayList; + +@Command(scope = "unomi", name = "session-list", description = "This commands lists the latest sessions updated in the Apache Unomi Context Server") +public class SessionListCommand extends ListCommandSupport { + private ProfileService profileService; + private DefinitionsService definitionsService; + + public void setProfileService(ProfileService profileService) { + this.profileService = profileService; + } + + public void setDefinitionsService(DefinitionsService definitionsService) { + this.definitionsService = definitionsService; + } + + @java.lang.Override + protected String[] getHeaders() { + return new String[] { + "ID", + "Scope", + "Last event", + "Duration", + "Profile", + "Timestamp" + }; + } + + @java.lang.Override + protected DataTable buildDataTable() { + Query query = new Query(); + query.setSortby("lastEventDate:desc"); + Condition matchAllCondition = new Condition(definitionsService.getConditionType("matchAllCondition")); + query.setCondition(matchAllCondition); + PartialList<Session> lastModifiedProfiles = profileService.searchSessions(query); + DataTable dataTable = new DataTable(); + for (Session session : lastModifiedProfiles.getList()) { + ArrayList<Comparable> rowData = new ArrayList<>(); + rowData.add(session.getItemId()); + rowData.add(session.getScope()); + rowData.add(session.getLastEventDate()); + rowData.add(session.getDuration()); + rowData.add(session.getProfileId()); + rowData.add(session.getTimeStamp()); + dataTable.addRow(rowData.toArray(new Comparable[rowData.size()])); + } + return dataTable; + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SessionViewCommand.java ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SessionViewCommand.java b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SessionViewCommand.java new file mode 100644 index 0000000..53725d0 --- /dev/null +++ b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/commands/SessionViewCommand.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.unomi.shell.commands; + +import org.apache.karaf.shell.commands.Argument; +import org.apache.karaf.shell.commands.Command; +import org.apache.karaf.shell.console.OsgiCommandSupport; +import org.apache.unomi.api.Session; +import org.apache.unomi.api.services.ProfileService; +import org.apache.unomi.persistence.spi.CustomObjectMapper; + +@Command(scope = "unomi", name = "session-view", description = "This command will dump a session as a JSON string") +public class SessionViewCommand extends OsgiCommandSupport { + private ProfileService profileService; + + public void setProfileService(ProfileService profileService) { + this.profileService = profileService; + } + + @Argument(index = 0, name = "session", description = "The identifier for the session", required = true, multiValued = false) + String sessionIdentifier; + + @Override + protected Object doExecute() throws Exception { + Session session = profileService.loadSession(sessionIdentifier, null); + if (session == null) { + System.out.println("Couldn't find a session with id=" + sessionIdentifier); + return null; + } + String jsonSession = CustomObjectMapper.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(session); + System.out.println(jsonSession); + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/11c847c0/tools/shell-dev-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/tools/shell-dev-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/tools/shell-dev-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml index c86d45f..8fb4530 100644 --- a/tools/shell-dev-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/tools/shell-dev-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -17,14 +17,9 @@ --> <blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0" - xmlns:cxf="http://cxf.apache.org/blueprint/core" xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs" 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 - http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd - http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd - http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd"> + xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <reference id="segmentService" interface="org.apache.unomi.api.services.SegmentService"/> <reference id="userListService" interface="org.apache.unomi.api.services.UserListService"/> @@ -81,6 +76,38 @@ <shell:property name="segmentService" ref="segmentService" /> </shell:action> </shell:command> + <shell:command> + <shell:action class="org.apache.unomi.shell.commands.ProfileListCommand"> + <shell:property name="definitionsService" ref="definitionsService" /> + <shell:property name="profileService" ref="profileService" /> + </shell:action> + </shell:command> + <shell:command> + <shell:action class="org.apache.unomi.shell.commands.ProfileViewCommand"> + <shell:property name="profileService" ref="profileService" /> + </shell:action> + </shell:command> + <shell:command> + <shell:action class="org.apache.unomi.shell.commands.EventTailCommand"> + </shell:action> + </shell:command> + <shell:command> + <shell:action class="org.apache.unomi.shell.commands.EventViewCommand"> + <shell:property name="definitionsService" ref="definitionsService" /> + <shell:property name="eventService" ref="eventService" /> + </shell:action> + </shell:command> + <shell:command> + <shell:action class="org.apache.unomi.shell.commands.SessionViewCommand"> + <shell:property name="profileService" ref="profileService" /> + </shell:action> + </shell:command> + <shell:command> + <shell:action class="org.apache.unomi.shell.commands.SessionListCommand"> + <shell:property name="definitionsService" ref="definitionsService" /> + <shell:property name="profileService" ref="profileService" /> + </shell:action> + </shell:command> </shell:command-bundle> </blueprint>
