Various fixes.
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 13 Mar 2013 17:42:26 +0000 (17:42 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 13 Mar 2013 17:42:26 +0000 (17:42 +0000)
Start implementing help CLI (not working)

git-svn-id: https://svn.argeo.org/slc/trunk@6128 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

modules/org.argeo.slc.agent.jcr/META-INF/spring/jcr.xml
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultAgent.java
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultAgentCli.java
runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java
runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/execution/SlcAgentDescriptor.java [deleted file]

index 5dd41b79910a1a1ba620face31f979ae4c63644c..5750fc9579493ba794355a98ea1cb79651cba8dd 100644 (file)
@@ -5,6 +5,7 @@
 \r
        <bean id="agent" class="org.argeo.slc.jcr.execution.JcrAgent"\r
                init-method="init" destroy-method="destroy">\r
+               <property name="defaultModulePrefix" value="org.argeo.slc.lib" />\r
                <property name="repository" ref="repository" />\r
                <property name="modulesManager" ref="modulesManager" />\r
        </bean>\r
index 9bfde6edcde2ce006413167e15d33b8dd2430de9..41c66622d01506b951651a1138747028bbd71466 100644 (file)
 package org.argeo.slc.core.execution;
 
 import java.io.UnsupportedEncodingException;
-import java.net.InetAddress;
 import java.net.URI;
 import java.net.URLDecoder;
-import java.net.UnknownHostException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -28,44 +26,45 @@ import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.BasicNameVersion;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.execution.ExecutionModuleDescriptor;
 import org.argeo.slc.execution.ExecutionModulesManager;
 import org.argeo.slc.execution.ExecutionProcess;
 import org.argeo.slc.execution.SlcAgent;
-import org.argeo.slc.execution.SlcAgentDescriptor;
 
 /** Implements the base methods of an SLC agent. */
 public class DefaultAgent implements SlcAgent {
-       private final static Log log = LogFactory.getLog(DefaultAgent.class);
+       // private final static Log log = LogFactory.getLog(DefaultAgent.class);
        /** UTF-8 charset for encoding. */
        private final static String UTF8 = "UTF-8";
 
-       private SlcAgentDescriptor agentDescriptor;
+       private String agentUuid = null;
+       // private SlcAgentDescriptor agentDescriptor;
        private ExecutionModulesManager modulesManager;
 
        private ThreadGroup processesThreadGroup;
        private Map<String, ProcessThread> runningProcesses = Collections
                        .synchronizedMap(new HashMap<String, ProcessThread>());
 
+       private String defaultModulePrefix = null;
+
        /*
         * LIFECYCLE
         */
        /** Initialization */
        public void init() {
-               agentDescriptor = new SlcAgentDescriptor();
-               agentDescriptor.setUuid(initAgentUuid());
-               try {
-                       agentDescriptor.setHost(InetAddress.getLocalHost().getHostName());
-               } catch (UnknownHostException e) {
-                       log.error("Cannot resolve localhost host name: " + e.getMessage());
-                       agentDescriptor.setHost("localhost");
-               }
+               agentUuid = initAgentUuid();
+               // agentDescriptor = new SlcAgentDescriptor();
+               // agentDescriptor.setUuid(initAgentUuid());
+               // try {
+               // agentDescriptor.setHost(InetAddress.getLocalHost().getHostName());
+               // } catch (UnknownHostException e) {
+               // log.error("Cannot resolve localhost host name: " + e.getMessage());
+               // agentDescriptor.setHost("localhost");
+               // }
                processesThreadGroup = new ThreadGroup("SLC Processes of Agent #"
-                               + agentDescriptor.getUuid());
+                               + agentUuid);
                // modulesManager.registerProcessNotifier(this,
                // new HashMap<String, String>());
 
@@ -129,7 +128,9 @@ public class DefaultAgent implements SlcAgent {
                        String[] path = uri.getPath().split("/");
                        if (path.length < 3)
                                throw new SlcException("Badly formatted URI: " + uri);
-                       String module = path[1];
+                       String moduleName = path[1];
+                       // TODO process version
+                       String moduleVersion = null;
                        StringBuilder flow = new StringBuilder();
                        for (int i = 2; i < path.length; i++)
                                flow.append('/').append(path[i]);
@@ -138,9 +139,9 @@ public class DefaultAgent implements SlcAgent {
                        if (uri.getQuery() != null)
                                values = getQueryMap(uri.getQuery());
 
-                       modulesManager.start(new BasicNameVersion(module, null));
+                       // Get execution module descriptor
                        ExecutionModuleDescriptor emd = getExecutionModuleDescriptor(
-                                       module, null);
+                                       moduleName, moduleVersion);
                        process.getRealizedFlows().add(
                                        emd.asRealizedFlow(flow.toString(), values));
                }
@@ -181,8 +182,24 @@ public class DefaultAgent implements SlcAgent {
        }
 
        public ExecutionModuleDescriptor getExecutionModuleDescriptor(
-                       String moduleName, String version) {
-               return modulesManager.getExecutionModuleDescriptor(moduleName, version);
+                       String moduleName, String moduleVersion) {
+               // Get execution module descriptor
+               ExecutionModuleDescriptor emd;
+               try {
+                       modulesManager.start(new BasicNameVersion(moduleName, moduleVersion));
+                       emd = modulesManager.getExecutionModuleDescriptor(moduleName,
+                                       moduleVersion);
+               } catch (SlcException e) {
+                       if (defaultModulePrefix != null) {
+                               moduleName = defaultModulePrefix + "." + moduleName;
+                               modulesManager.start(new BasicNameVersion(moduleName,
+                                               moduleVersion));
+                               emd = modulesManager.getExecutionModuleDescriptor(moduleName,
+                                               moduleVersion);
+                       } else
+                               throw e;
+               }
+               return emd;
        }
 
        public List<ExecutionModuleDescriptor> listExecutionModuleDescriptors() {
@@ -235,16 +252,16 @@ public class DefaultAgent implements SlcAgent {
                this.modulesManager = modulesManager;
        }
 
-       protected SlcAgentDescriptor getAgentDescriptor() {
-               return agentDescriptor;
+       public void setDefaultModulePrefix(String defaultModulePrefix) {
+               this.defaultModulePrefix = defaultModulePrefix;
        }
 
        public String getAgentUuid() {
-               return agentDescriptor.getUuid();
+               return agentUuid;
        }
 
        @Override
        public String toString() {
-               return agentDescriptor.toString();
+               return "Agent #" + getAgentUuid();
        }
 }
index 823207e87193e0c342b85ba6fc081e0d2d2ba752..c902ad3a37c4fef3a0808753571df77d7578e7fa 100644 (file)
@@ -7,15 +7,27 @@ import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.argeo.security.OsAuthenticationToken;
 import org.argeo.slc.SlcException;
+import org.argeo.slc.execution.ExecutionFlowDescriptor;
+import org.argeo.slc.execution.ExecutionModuleDescriptor;
+import org.argeo.slc.execution.ExecutionSpec;
+import org.argeo.slc.execution.ExecutionSpecAttribute;
 import org.argeo.slc.execution.SlcAgent;
 import org.argeo.slc.execution.SlcAgentCli;
 import org.springframework.security.Authentication;
 import org.springframework.security.AuthenticationManager;
 import org.springframework.security.context.SecurityContextHolder;
 
+/**
+ * Authenticates thread and executes synchronously a command line execution.
+ * Reference implementation of args to URIs algorithm.
+ */
 public class DefaultAgentCli implements SlcAgentCli {
+       private final static Log log = LogFactory.getLog(DefaultAgentCli.class);
+
        private final static String UTF8 = "UTF-8";
        private SlcAgent agent;
        private AuthenticationManager authenticationManager;
@@ -23,14 +35,25 @@ public class DefaultAgentCli implements SlcAgentCli {
        private Long timeout = 24 * 60 * 60 * 1000l;
 
        public String process(String[] args) {
-               OsAuthenticationToken oat = new OsAuthenticationToken();
-               Authentication authentication = authenticationManager.authenticate(oat);
-               SecurityContextHolder.getContext().setAuthentication(authentication);
+               if (SecurityContextHolder.getContext().getAuthentication() == null) {
+                       OsAuthenticationToken oat = new OsAuthenticationToken();
+                       Authentication authentication = authenticationManager
+                                       .authenticate(oat);
+                       SecurityContextHolder.getContext()
+                                       .setAuthentication(authentication);
+               }
 
-               List<URI> uris = asURIs(args);
-               String processUuid = agent.process(uris);
-               agent.waitFor(processUuid, timeout);
-               return processUuid;
+               if (args.length > 0 && args[0].equals("help")) {
+                       StringBuilder buf = new StringBuilder();
+                       help(args, buf);
+                       log.info(buf);
+                       return buf.toString();
+               } else {
+                       List<URI> uris = asURIs(args);
+                       String processUuid = agent.process(uris);
+                       agent.waitFor(processUuid, timeout);
+                       return processUuid;
+               }
        }
 
        public static List<URI> asURIs(String[] args) {
@@ -103,6 +126,83 @@ public class DefaultAgentCli implements SlcAgentCli {
                }
        }
 
+       protected void help(String[] rawArgs, StringBuilder buf) {
+               String[] args = Arrays.copyOfRange(rawArgs, 1, rawArgs.length);
+               List<URI> uris = asURIs(args);
+               uris: for (URI uri : uris) {
+                       String[] path = uri.getPath().split("/");
+                       if (path.length < 2) {
+                               for (ExecutionModuleDescriptor emd : agent
+                                               .listExecutionModuleDescriptors()) {
+                                       buf.append(
+                                                       "# Execution Module " + emd.getName() + " v"
+                                                                       + emd.getVersion()).append('\n');
+                                       if (emd.getDescription() != null
+                                                       && !emd.getDescription().trim().equals(""))
+                                               buf.append(emd.getDescription()).append('\n');
+                               }
+                               continue uris;
+                       }
+
+                       String moduleName = path[1];
+                       // TODO process version
+                       String moduleVersion = null;
+
+                       ExecutionModuleDescriptor emd = agent.getExecutionModuleDescriptor(
+                                       moduleName, moduleVersion);
+
+                       if (path.length >= 2) {
+                               StringBuilder flow = new StringBuilder();
+                               for (int i = 2; i < path.length; i++)
+                                       flow.append('/').append(path[i]);
+                               String flowPath = flow.toString();
+                               ExecutionFlowDescriptor flowDescriptor = null;
+                               for (ExecutionFlowDescriptor efd : emd.getExecutionFlows()) {
+                                       if (efd.getName().equals(flowPath)) {
+                                               flowDescriptor = efd;
+                                               break;
+                                       }
+                               }
+                               if (flowDescriptor == null)
+                                       throw new SlcException("Flow " + uri + " not found");
+
+                               buf.append(
+                                               "# Execution Module " + emd.getName() + " v"
+                                                               + emd.getVersion()).append('\n');
+                               buf.append(" Flow ").append(flowDescriptor.getName());
+                               if (flowDescriptor.getDescription() != null
+                                               && !flowDescriptor.getDescription().trim().equals(""))
+                                       buf.append(" ").append(flowDescriptor.getDescription());
+                               buf.append('\n');
+                               ExecutionSpec spec = flowDescriptor.getExecutionSpec();
+                               for (String attrKey : spec.getAttributes().keySet()) {
+                                       ExecutionSpecAttribute esa = spec.getAttributes().get(
+                                                       attrKey);
+                                       buf.append("  --").append(attrKey);
+                                       // TODO check values in query part
+                                       if (esa.getValue() != null)
+                                               buf.append(" ").append(esa.getValue());
+                                       buf.append('\n');
+                               }
+                       } else {
+                               // module only
+                               buf.append(
+                                               "# Execution Module " + emd.getName() + " v"
+                                                               + emd.getVersion()).append('\n');
+                               if (emd.getDescription() != null
+                                               && !emd.getDescription().trim().equals(""))
+                                       buf.append(emd.getDescription()).append('\n');
+                               for (ExecutionFlowDescriptor efd : emd.getExecutionFlows()) {
+                                       buf.append(" ").append(efd.getName());
+                                       if (efd.getDescription() != null
+                                                       && !efd.getDescription().trim().equals(""))
+                                               buf.append(" ").append(efd.getDescription());
+                               }
+                               buf.append('\n');
+                       }
+               }
+       }
+
        public void setAgent(SlcAgent agent) {
                this.agent = agent;
        }
index 606748279a0a57fc398b6ac340a1ca0ade276f5f..3890feefc674b814196a316b9d981fae645934a9 100644 (file)
@@ -17,6 +17,7 @@ package org.argeo.slc.cli;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Method;
 import java.security.AccessController;
@@ -34,6 +35,7 @@ import javax.security.auth.login.LoginContext;
 
 import org.argeo.osgi.boot.OsgiBoot;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.launch.Framework;
 import org.osgi.framework.launch.FrameworkFactory;
@@ -46,6 +48,8 @@ public class SlcMain implements PrivilegedAction<String> {
 
        public final static String os;
        public final static String slcDirName = ".slc";
+       final static File homeDir = new File(System.getProperty("user.home"));
+
        static {
                String osName = System.getProperty("os.name");
                if (osName.startsWith("Win"))
@@ -83,6 +87,7 @@ public class SlcMain implements PrivilegedAction<String> {
        public String run() {
                long begin = System.currentTimeMillis();
 
+               Framework framework = null;
                try {
                        info("## Date : " + new Date());
                        info("## Data : " + dataDir.getCanonicalPath());
@@ -103,7 +108,7 @@ public class SlcMain implements PrivilegedAction<String> {
                        // Spring configs currently require System properties
                        System.getProperties().putAll(configuration);
 
-                       Framework framework = frameworkFactory.newFramework(configuration);
+                       framework = frameworkFactory.newFramework(configuration);
                        framework.start();
                        BundleContext bundleContext = framework.getBundleContext();
 
@@ -121,7 +126,7 @@ public class SlcMain implements PrivilegedAction<String> {
                                                .getProperty(OsgiBoot.PROP_ARGEO_OSGI_BUNDLES)));
                        else
                                osgiBoot.installUrls(osgiBoot.getBundlesUrls(System
-                                               .getProperty("user.home") + "/.slc/modules/**"));
+                                               .getProperty("user.home") + "/.slc/modules/;in=**"));
 
                        // Start runtime
                        osgiBoot.startBundles(bundlesToStart);
@@ -159,7 +164,16 @@ public class SlcMain implements PrivilegedAction<String> {
 
                        return ret.toString();
                } catch (Exception e) {
+                       // Shutdown OSGi runtime
+                       if (framework != null)
+                               try {
+                                       framework.stop();
+                                       framework.waitForStop(15 * 1000);
+                               } catch (Exception silent) {
+                               }
                        throw new RuntimeException("Cannot run SLC command line", e);
+               } finally {
+
                }
        }
 
@@ -182,6 +196,7 @@ public class SlcMain implements PrivilegedAction<String> {
                                if (slcDir == null) {
                                        slcDir = new File(executionDir, slcDirName);
                                        slcDir.mkdirs();
+                                       info("## Creating an SLC node at " + slcDir + " ...");
                                }
                        }
 
@@ -235,6 +250,13 @@ public class SlcMain implements PrivilegedAction<String> {
                File parentDir = currentDir.getParentFile();
                if (parentDir == null)
                        return null;
+               try {
+                       // ~/.slc reserved for agent
+                       if (parentDir.getCanonicalPath().equals(homeDir.getCanonicalPath()))
+                               return null;
+               } catch (IOException e) {
+                       throw new RuntimeException("Cannot check home directory", e);
+               }
                return findSlcDir(parentDir);
        }
 
diff --git a/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/execution/SlcAgentDescriptor.java b/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/execution/SlcAgentDescriptor.java
deleted file mode 100644 (file)
index 2ab20df..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * Licensed 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.argeo.slc.execution;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
-
-
-public class SlcAgentDescriptor implements Cloneable, Serializable {
-       private static final long serialVersionUID = 1L;
-       private String uuid;
-       private String host;
-       private List<ExecutionModuleDescriptor> moduleDescriptors = new ArrayList<ExecutionModuleDescriptor>();
-
-       public SlcAgentDescriptor() {
-
-       }
-
-       public SlcAgentDescriptor(SlcAgentDescriptor template) {
-               uuid = template.uuid;
-               host = template.host;
-               moduleDescriptors.addAll(template.moduleDescriptors);
-       }
-
-       public String getUuid() {
-               return uuid;
-       }
-
-       public void setUuid(String uuid) {
-               this.uuid = uuid;
-       }
-
-       public String getHost() {
-               return host;
-       }
-
-       public void setHost(String host) {
-               this.host = host;
-       }
-
-       public List<ExecutionModuleDescriptor> getModuleDescriptors() {
-               return moduleDescriptors;
-       }
-
-       public void setModuleDescriptors(
-                       List<ExecutionModuleDescriptor> modulesDescriptors) {
-               this.moduleDescriptors = modulesDescriptors;
-       }
-
-       @Override
-       public String toString() {
-               return host + " #" + uuid;
-       }
-}