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