Merge pull request #998 from afs/jena2102-cmd-init

JENA-2102: Explicitly initialize map for injected commands
diff --git a/jena-cmds/src/main/java/org/apache/jena/cmd/Cmds.java b/jena-cmds/src/main/java/org/apache/jena/cmd/Cmds.java
index b53f56f..5e206c1 100644
--- a/jena-cmds/src/main/java/org/apache/jena/cmd/Cmds.java
+++ b/jena-cmds/src/main/java/org/apache/jena/cmd/Cmds.java
@@ -32,7 +32,15 @@
 
     static { JenaSystem.init(); }
 
-    private static Map<String, Consumer<String[]>> cmds = new HashMap<>();
+    private static Map<String, Consumer<String[]>> cmds;
+
+    // Initialize via JenaSubsystemLifecycle and not rely on class initialization.
+    static void init() {
+        // Initialization should be minimal, just enough to allow modules to register commands.
+        // We may be inside some other place where JenaSystem.init() was called.
+        if ( cmds != null )
+            cmds = new HashMap<>();
+    }
 
     public static void injectCmd(String name, Consumer<String[]> main) {
         cmds.put(name, main);
diff --git a/jena-cmds/src/main/java/org/apache/jena/cmd/InitCmds.java b/jena-cmds/src/main/java/org/apache/jena/cmd/InitCmds.java
new file mode 100644
index 0000000..d0cf7f8
--- /dev/null
+++ b/jena-cmds/src/main/java/org/apache/jena/cmd/InitCmds.java
@@ -0,0 +1,37 @@
+/*
+ * 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.jena.cmd;
+
+import org.apache.jena.sys.JenaSubsystemLifecycle;
+
+public class InitCmds implements JenaSubsystemLifecycle {
+
+    @Override
+    public void start() {
+        Cmds.init();
+    }
+
+    @Override
+    public void stop() {}
+
+    @Override
+    public int level() {
+        return 15;
+    }
+}
diff --git a/jena-cmds/src/main/resources/META-INF/services/org.apache.jena.sys.JenaSubsystemLifecycle b/jena-cmds/src/main/resources/META-INF/services/org.apache.jena.sys.JenaSubsystemLifecycle
new file mode 100644
index 0000000..f26ab98
--- /dev/null
+++ b/jena-cmds/src/main/resources/META-INF/services/org.apache.jena.sys.JenaSubsystemLifecycle
@@ -0,0 +1 @@
+org.apache.jena.cmd.InitCmds