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.deploy;
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.argeo.slc.deploy.VersioningDriver;
26  
27  /**
28   * Synchronizes an URL to a local directory, taking into account versioning
29   * information if possible.
30   */
31  public class VersionedDirSync implements Runnable {
32  	private final static Log log = LogFactory.getLog(VersionedDirSync.class);
33  
34  	private VersioningDriver versioningDriver;
35  	private File dir;
36  	private String url;
37  	private Boolean clean = false;
38  
39  	private Boolean changed = null;
40  
41  	public void run() {
42  		changed = null;
43  		if (clean) {
44  			try {
45  				log.info("Clean " + dir);
46  				FileUtils.deleteDirectory(dir);
47  			} catch (IOException e) {
48  				throw new SlcException("Cannot delete checkout directory "
49  						+ dir, e);
50  			}
51  			dir.mkdirs();
52  		}
53  		log.info("Checkout " + url + " to " + dir);
54  		changed = versioningDriver.checkout(url, dir, true);
55  		if (log.isDebugEnabled())
56  			log.debug("Synchronized " + url + " to " + dir);
57  	}
58  
59  	public void setVersioningDriver(VersioningDriver versioningDriver) {
60  		this.versioningDriver = versioningDriver;
61  	}
62  
63  	public void setDir(File dir) {
64  		this.dir = dir;
65  	}
66  
67  	public void setUrl(String url) {
68  		this.url = url;
69  	}
70  
71  	/** Delete before checkout */
72  	public void setClean(Boolean clean) {
73  		this.clean = clean;
74  	}
75  
76  	/** Whether last call has changed the directory */
77  	public Boolean getChanged() {
78  		if (changed == null)
79  			throw new SlcException("Sync has not run");
80  		return changed;
81  	}
82  
83  }