Github user nickwallen commented on a diff in the pull request:
https://github.com/apache/metron/pull/884#discussion_r158654747
--- Diff:
metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/DefaultStellarShellExecutor.java
---
@@ -0,0 +1,398 @@
+/*
+ *
+ * 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.metron.stellar.common.shell;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.google.common.collect.Maps;
+import org.apache.commons.collections.map.UnmodifiableMap;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.metron.stellar.common.StellarProcessor;
+import org.apache.metron.stellar.common.configuration.ConfigurationsUtils;
+import
org.apache.metron.stellar.common.shell.StellarExecutionListeners.FunctionDefinedListener;
+import
org.apache.metron.stellar.common.shell.StellarExecutionListeners.SpecialDefinedListener;
+import
org.apache.metron.stellar.common.shell.StellarExecutionListeners.VariableDefinedListener;
+import org.apache.metron.stellar.common.shell.specials.AssignmentCommand;
+import org.apache.metron.stellar.common.shell.specials.Comment;
+import org.apache.metron.stellar.common.shell.specials.DocCommand;
+import org.apache.metron.stellar.common.shell.specials.MagicDefineGlobal;
+import org.apache.metron.stellar.common.shell.specials.MagicListFunctions;
+import org.apache.metron.stellar.common.shell.specials.MagicListGlobals;
+import org.apache.metron.stellar.common.shell.specials.MagicListVariables;
+import org.apache.metron.stellar.common.shell.specials.MagicUndefineGlobal;
+import org.apache.metron.stellar.common.shell.specials.QuitCommand;
+import org.apache.metron.stellar.common.shell.specials.SpecialCommand;
+import org.apache.metron.stellar.common.utils.JSONUtils;
+import org.apache.metron.stellar.dsl.Context;
+import org.apache.metron.stellar.dsl.MapVariableResolver;
+import org.apache.metron.stellar.dsl.StellarFunctionInfo;
+import org.apache.metron.stellar.dsl.StellarFunctions;
+import org.apache.metron.stellar.dsl.VariableResolver;
+import org.apache.metron.stellar.dsl.functions.resolver.FunctionResolver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+
+import static
org.apache.metron.stellar.common.configuration.ConfigurationsUtils.readGlobalConfigBytesFromZookeeper;
+import static
org.apache.metron.stellar.common.shell.StellarShellResult.noop;
+import static
org.apache.metron.stellar.common.shell.StellarShellResult.error;
+import static
org.apache.metron.stellar.common.shell.StellarShellResult.success;
+import static
org.apache.metron.stellar.dsl.Context.Capabilities.GLOBAL_CONFIG;
+import static
org.apache.metron.stellar.dsl.Context.Capabilities.STELLAR_CONFIG;
+import static
org.apache.metron.stellar.dsl.Context.Capabilities.ZOOKEEPER_CLIENT;
+
+/**
+ * Default implementation of a StellarShellExecutor.
+ */
+public class DefaultStellarShellExecutor implements StellarShellExecutor {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+ public static final String SHELL_VARIABLES = "shellVariables";
+
+ /**
+ * The variables known by Stellar.
+ */
+ private Map<String, VariableResult> variables;
+
+ /**
+ * The function resolver.
+ */
+ private FunctionResolver functionResolver;
+
+ /**
+ * A Zookeeper client. Only defined if given a valid Zookeeper URL.
+ */
+ private Optional<CuratorFramework> zkClient;
+
+ /**
+ * A registry of all special commands; like %magic, ?doc, and quit.
+ *
+ * Maps the special command (like '%globals') to the command
implementing it.
+ */
+ private List<SpecialCommand> commandRegistry;
+
+ /**
+ * The Stellar execution context.
+ */
+ private Context context;
+
+ /**
+ * Listeners that are notified when a function is defined.
+ */
+ private List<FunctionDefinedListener> functionListeners;
+
+ /**
+ * Listeners that are notified when a variable is defined.
+ */
+ private List<VariableDefinedListener> variableListeners;
+
+ /**
+ * Listeners that are notified when a special command is defined.
+ */
+ private List<SpecialDefinedListener> specialListeners;
+
+ public DefaultStellarShellExecutor(
+ FunctionResolver functionResolver,
+ Properties properties,
+ Optional<String> zookeeperUrl) throws Exception {
+
+ this.functionListeners = new ArrayList<>();
+ this.variableListeners = new ArrayList<>();
+ this.specialListeners = new ArrayList<>();
+ this.variables = new HashMap<>();
+ this.zkClient = createZookeeperClient(zookeeperUrl);
+ this.context = createContext(properties, this.zkClient);
+ this.functionResolver = functionResolver;
+ }
+
+ public DefaultStellarShellExecutor(
+ Properties properties,
+ Optional<String> zookeeperUrl) throws Exception {
+
+ this(StellarFunctions.FUNCTION_RESOLVER(), properties, zookeeperUrl);
+ }
+
+ @Override
+ public void init() {
+ StellarFunctions.initialize(this.context);
+ this.commandRegistry = registerSpecialCommands();
+
+ // TODO this wont really work as functions are probably not defined yet
+ // but the auto-complete reaches out to the function resolver
separately
+ for(StellarFunctionInfo fn : functionResolver.getFunctionInfo()) {
+ notifyFunctionListeners(fn);
+ }
+ }
+
+ /**
+ * Add a listener that will be notified when a function is defined.
+ * @param listener The listener to notify.
+ */
+ @Override
+ public void addFunctionListener(FunctionDefinedListener listener) {
+ this.functionListeners.add(listener);
+ }
+
+ /**
+ * Notify function listeners that a function has been defined.
+ * @param functionInfo The function that was defined.
+ */
+ private void notifyFunctionListeners(StellarFunctionInfo functionInfo) {
+ for(FunctionDefinedListener listener : functionListeners) {
+ listener.whenFunctionDefined(functionInfo);
+ }
+ }
+
+ /**
+ * Add a listener that will be notified when a variable is defined.
+ * @param listener The listener to notify.
+ */
+ @Override
+ public void addVariableListener(VariableDefinedListener listener) {
+ this.variableListeners.add(listener);
+ }
+
+ /**
+ * Notify variable listeners that a variable has been (re)defined.
+ * @param variableName The variable name.
+ * @param result The variable result.
+ */
+ private void notifyVariableListeners(String variableName, VariableResult
result) {
+ for(VariableDefinedListener listener : variableListeners) {
+ listener.whenVariableDefined(variableName, result);
+ }
+ }
+
+ /**
+ * Add a listener that will be notified when a magic command is defined.
+ * @param listener The listener to notify.
+ */
+ @Override
+ public void addSpecialListener(SpecialDefinedListener listener) {
+ this.specialListeners.add(listener);
+ }
+
+ /**
+ * Notify listeners that a magic command has been defined.
+ * @param specialCommand The magic command.
+ */
+ private void notifySpecialListeners(SpecialCommand specialCommand) {
+ for(SpecialDefinedListener listener : specialListeners) {
+ listener.whenSpecialDefined(specialCommand);
+ }
+ }
+
+ @Override
+ public StellarShellResult execute(String expression) {
+ expression = expression.trim();
+
+ // if whitespace, there is nothing much to do
+ if(StringUtils.isBlank(expression)) {
+ return noop();
+ }
+
+ // is this a special command?
+ for(SpecialCommand command : commandRegistry) {
+ if(command.getMatcher().apply(expression)) {
+ return command.execute(expression, this);
+ }
--- End diff --
Anything that is not "core" Stellar is defined separately as a
`SpecialCommand`.
---