1 package org
.argeo
.slc
.ant
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.net
.InetAddress
;
7 import java
.net
.UnknownHostException
;
9 import java
.util
.Properties
;
10 import java
.util
.UUID
;
12 import org
.apache
.commons
.io
.IOUtils
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.slc
.SlcException
;
16 import org
.argeo
.slc
.process
.SlcExecution
;
17 import org
.argeo
.slc
.runtime
.SlcExecutionOutput
;
18 import org
.argeo
.slc
.runtime
.SlcRuntime
;
19 import org
.argeo
.slc
.spring
.SpringUtils
;
20 import org
.springframework
.core
.io
.DefaultResourceLoader
;
21 import org
.springframework
.core
.io
.FileSystemResource
;
22 import org
.springframework
.core
.io
.Resource
;
24 public class AntSlcRuntime
implements SlcRuntime
<AntExecutionContext
> {
25 private final static Log log
= LogFactory
.getLog(AntSlcRuntime
.class);
27 public final static String SLC_ROOT_FILE_NAME
= "slcRoot.properties";
30 * Simplified execution with default runtime, default target, and no
31 * properties/reference arguments.
35 * @param executionOutput
38 * @see #executeScript(String, String, String, Properties, Map,
41 public void executeScript(String script
,
42 SlcExecutionOutput
<AntExecutionContext
> executionOutput
) {
43 executeScript(null, script
, null, null, null, executionOutput
);
47 * Simplified execution with default runtime, and no properties/reference
53 * comma separated list of targets
54 * @param executionOutput
56 * @see #executeScript(String, String, String, Properties, Map,
59 public void executeScript(String script
, String targets
,
60 SlcExecutionOutput
<AntExecutionContext
> executionOutput
) {
61 executeScript(null, script
, targets
, null, null, executionOutput
);
64 public void executeScript(String runtime
, String script
, String targets
,
65 Properties properties
, Map
<String
, Object
> references
,
66 SlcExecutionOutput
<AntExecutionContext
> executionOutput
) {
68 Resource scriptRes
= findScript(script
);
69 Resource slcRootFile
= findSlcRootFile(scriptRes
);
70 if (slcRootFile
== null)
71 throw new SlcException(
72 "Could not find any SLC root file, "
73 + "please configure one at the root of your scripts hierarchy.");
75 // Create SlcExecution from arguments
76 SlcExecution slcExecution
= createSlcExecution(runtime
, slcRootFile
,
80 AntSlcApplication application
= new AntSlcApplication();
81 application
.setSlcRootFile(slcRootFile
);
82 application
.initFromSlcRootFile();
85 application
.execute(slcExecution
, properties
, references
,
89 protected Resource
findScript(String scriptStr
) {
91 if (new File(scriptStr
).exists()) {
92 scriptRes
= new FileSystemResource(scriptStr
);
94 scriptRes
= new DefaultResourceLoader(Thread
.currentThread()
95 .getContextClassLoader()).getResource(scriptStr
);
100 protected SlcExecution
createSlcExecution(String runtimeStr
,
101 Resource slcRootFile
, Resource script
, String targets
) {
102 SlcExecution slcExecution
= new SlcExecution();
103 slcExecution
.setUuid(UUID
.randomUUID().toString());
105 slcExecution
.setHost(InetAddress
.getLocalHost().getHostName());
106 } catch (UnknownHostException e
) {
107 slcExecution
.setHost(SlcExecution
.UNKOWN_HOST
);
110 slcExecution
.setType(AntConstants
.EXECTYPE_SLC_ANT
);
112 slcExecution
.setUser(System
.getProperty("user.name"));
114 if (runtimeStr
!= null)
115 slcExecution
.getAttributes().put(AntConstants
.EXECATTR_RUNTIME
,
117 String scriptRelativePath
= SpringUtils
.extractRelativePath(SpringUtils
118 .getParent(slcRootFile
), script
);
120 slcExecution
.getAttributes().put(AntConstants
.EXECATTR_ANT_FILE
,
123 slcExecution
.getAttributes().put(AntConstants
.EXECATTR_ANT_TARGETS
,
126 slcExecution
.setStatus(SlcExecution
.STATUS_SCHEDULED
);
131 * Recursively scans directories downwards until it find a file name as
132 * defined by {@link #SLC_ROOT_FILE_NAME}.
134 protected Resource
findSlcRootFile(Resource currDir
) {
135 if (log
.isTraceEnabled())
136 log
.trace("Look for SLC root file in " + currDir
);
139 Resource slcRootFile
= currDir
.createRelative(SLC_ROOT_FILE_NAME
);
140 if (slcRootFile
.exists()) {
141 if (log
.isDebugEnabled())
142 log
.debug("Found SLC root file: " + slcRootFile
);
145 String currPath
= currDir
.getURL().getPath();
146 if (currPath
.equals("/") || currPath
.equals("")) {
149 return findSlcRootFile(SpringUtils
.getParent(currDir
));
152 } catch (IOException e
) {
153 throw new SlcException("Problem when looking in SLC root file in "