\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
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;
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>());
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]);
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));
}
}
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() {
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();
}
}
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;
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) {
}
}
+ 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;
}
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;
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;
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"))
public String run() {
long begin = System.currentTimeMillis();
+ Framework framework = null;
try {
info("## Date : " + new Date());
info("## Data : " + dataDir.getCanonicalPath());
// 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();
.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);
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 {
+
}
}
if (slcDir == null) {
slcDir = new File(executionDir, slcDirName);
slcDir.mkdirs();
+ info("## Creating an SLC node at " + slcDir + " ...");
}
}
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);
}
+++ /dev/null
-/*
- * 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;
- }
-}