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  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.util.Map;
23  
24  import org.apache.commons.io.FileUtils;
25  import org.apache.commons.io.IOUtils;
26  import org.argeo.slc.SlcException;
27  import org.springframework.core.io.Resource;
28  
29  public class LocalFilesDeployment implements Runnable {
30  	private String targetBase = "";
31  	private ResourceSet resourceSet;
32  
33  	public LocalFilesDeployment() {
34  	}
35  
36  	public LocalFilesDeployment(ResourceSet resourceSet) {
37  		this.resourceSet = resourceSet;
38  	}
39  
40  	public void run() {
41  		Map<String, Resource> resources = resourceSet.listResources();
42  		for (String relPath : resources.keySet()) {
43  			File targetFile = new File(targetBase + File.separator + relPath);
44  			File parentDir = targetFile.getParentFile();
45  			if (!parentDir.exists())
46  				parentDir.mkdirs();
47  
48  			Resource resource = resources.get(relPath);
49  
50  			InputStream in = null;
51  			OutputStream out = null;
52  			try {
53  				in = resource.getInputStream();
54  				out = FileUtils.openOutputStream(targetFile);
55  				IOUtils.copy(in, out);
56  			} catch (IOException e) {
57  				throw new SlcException("Cannot extract " + resource + " to "
58  						+ targetFile, e);
59  			} finally {
60  				IOUtils.closeQuietly(in);
61  				IOUtils.closeQuietly(out);
62  			}
63  		}
64  	}
65  
66  	public void setTargetBase(String targetBase) {
67  		this.targetBase = targetBase;
68  	}
69  
70  	public void setResourceSet(ResourceSet resourceSet) {
71  		this.resourceSet = resourceSet;
72  	}
73  
74  }