-
Notifications
You must be signed in to change notification settings - Fork 23
/
SingleFileDeployer.java
114 lines (91 loc) · 3.32 KB
/
SingleFileDeployer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cocoon.maven.deployer.monolithic;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import org.apache.cocoon.maven.deployer.utils.FileUtils;
import org.apache.commons.lang.Validate;
import org.apache.maven.plugin.logging.Log;
/**
* Deploy a single file.
*
* @version $Id$
*/
public class SingleFileDeployer implements FileDeployer {
private File basedir;
private Log logger;
private String outputDir;
private boolean skipRootDirectory;
private Set alreadyDeployedFilesSet;
public SingleFileDeployer(final String outputDir, final boolean skipRootDirectory) {
Validate.notNull(outputDir, "An outputDir has to be set.");
this.outputDir = outputDir;
this.skipRootDirectory = skipRootDirectory;
}
public SingleFileDeployer(final String outputDir) {
this(outputDir, false);
}
public void setBasedir(final File basedir) {
this.basedir = basedir;
}
protected File getBasedir() {
return this.basedir;
}
public void setLogger(final Log logger) {
this.logger = logger;
}
protected Log getLogger() {
return this.logger;
}
public void setAlreadyDeployedFilesSet(Set alreadyDeployedFilesSet) {
this.alreadyDeployedFilesSet = alreadyDeployedFilesSet;
}
protected String getFileName(final String documentName) {
return documentName.substring(documentName.lastIndexOf('/') + 1);
}
protected String getOutputDir() {
return this.outputDir;
}
public OutputStream writeResource(final String documentName) throws IOException {
File outDir = new File(this.getBasedir(), getOutputDir());
if(!outDir.exists()) {
outDir.mkdirs();
}
String outputDocumentName = documentName;
// if the root directory is has to be skipped, the remaining path will be used
if(this.skipRootDirectory) {
outputDocumentName = removeRootDirectory(documentName);
this.logger.debug("Changing output document name from '" + documentName + "', to '" + outputDocumentName + "'.");
}
// only take the filename
else {
outputDocumentName = this.getFileName(documentName);
}
File targetFile = FileUtils.createDirectory(new File(outDir, outputDocumentName));
if(this.alreadyDeployedFilesSet.contains(targetFile.getCanonicalFile())) {
throw new FileAlreadyDeployedException("File '" + targetFile + "' already exists!");
}
this.alreadyDeployedFilesSet.add(targetFile.getCanonicalFile());
this.logger.debug("Deploying block resource to " + getOutputDir() + "/" + outputDocumentName);
return new FileOutputStream(FileUtils.createDirectory(targetFile));
}
protected String removeRootDirectory(final String documentName) {
return documentName.substring(documentName.indexOf('/') + 1);
}
}