View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.core.execution.tasks;
17  
18  import java.io.File;
19  import java.io.IOException;
20  
21  import org.apache.commons.io.FileUtils;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.argeo.slc.SlcException;
25  import org.springframework.core.io.Resource;
26  
27  public class Echo implements Runnable {
28  	private final static Log defaultLog = LogFactory.getLog(Echo.class);
29  	private Resource writeTo = null;
30  
31  	private Log log;
32  	private Object message;
33  
34  	public void run() {
35  		log().info(message);
36  
37  		if (writeTo != null) {
38  			try {
39  				File file = writeTo.getFile();
40  				if (log().isDebugEnabled())
41  					log().debug("Write to " + file);
42  				if (message != null)
43  					FileUtils.writeStringToFile(file, message.toString());
44  			} catch (IOException e) {
45  				throw new SlcException("Could not write to " + writeTo, e);
46  			}
47  		}
48  	}
49  
50  	private Log log() {
51  		return log != null ? log : defaultLog;
52  	}
53  
54  	public void setMessage(Object message) {
55  		this.message = message;
56  	}
57  
58  	public void setWriteTo(Resource writeTo) {
59  		this.writeTo = writeTo;
60  	}
61  
62  }