4571 |
14 Sep 17 |
nicklas |
1 |
package net.sf.basedb.relax.plugins; |
4571 |
14 Sep 17 |
nicklas |
2 |
|
4571 |
14 Sep 17 |
nicklas |
3 |
import java.io.Closeable; |
4571 |
14 Sep 17 |
nicklas |
4 |
import java.io.IOException; |
4571 |
14 Sep 17 |
nicklas |
5 |
import java.io.OutputStream; |
4571 |
14 Sep 17 |
nicklas |
6 |
import java.util.HashSet; |
4571 |
14 Sep 17 |
nicklas |
7 |
import java.util.Map; |
4571 |
14 Sep 17 |
nicklas |
8 |
import java.util.Set; |
4571 |
14 Sep 17 |
nicklas |
9 |
import java.util.zip.GZIPOutputStream; |
4571 |
14 Sep 17 |
nicklas |
10 |
|
4571 |
14 Sep 17 |
nicklas |
11 |
import net.sf.basedb.core.FileServer; |
4571 |
14 Sep 17 |
nicklas |
12 |
import net.sf.basedb.core.plugin.ExportOutputStream; |
4571 |
14 Sep 17 |
nicklas |
13 |
import net.sf.basedb.opengrid.CmdResult; |
4571 |
14 Sep 17 |
nicklas |
14 |
import net.sf.basedb.opengrid.OpenGrid; |
4571 |
14 Sep 17 |
nicklas |
15 |
import net.sf.basedb.opengrid.RemoteHost; |
4571 |
14 Sep 17 |
nicklas |
16 |
import net.sf.basedb.opengrid.RemoteSession; |
4571 |
14 Sep 17 |
nicklas |
17 |
import net.sf.basedb.opengrid.config.ConnectionInfo; |
4571 |
14 Sep 17 |
nicklas |
18 |
import net.sf.basedb.opengrid.filetransfer.FilePermission; |
4571 |
14 Sep 17 |
nicklas |
19 |
|
4571 |
14 Sep 17 |
nicklas |
20 |
/** |
4571 |
14 Sep 17 |
nicklas |
An output location that write files to a remote |
4571 |
14 Sep 17 |
nicklas |
FileServer (assuming that the FileServer is reachable |
4571 |
14 Sep 17 |
nicklas |
with SSH). |
4571 |
14 Sep 17 |
nicklas |
24 |
|
4571 |
14 Sep 17 |
nicklas |
@author nicklas |
4571 |
14 Sep 17 |
nicklas |
26 |
*/ |
4571 |
14 Sep 17 |
nicklas |
27 |
public class FileServerOutputLocation |
4571 |
14 Sep 17 |
nicklas |
28 |
implements Closeable |
4571 |
14 Sep 17 |
nicklas |
29 |
{ |
4571 |
14 Sep 17 |
nicklas |
30 |
|
4571 |
14 Sep 17 |
nicklas |
31 |
private boolean compress; |
4571 |
14 Sep 17 |
nicklas |
32 |
private boolean overwrite; |
4571 |
14 Sep 17 |
nicklas |
33 |
|
4571 |
14 Sep 17 |
nicklas |
34 |
private final FileServer server; |
4571 |
14 Sep 17 |
nicklas |
35 |
private final RemoteSession session; |
4571 |
14 Sep 17 |
nicklas |
36 |
private final String releaseVersion; |
4571 |
14 Sep 17 |
nicklas |
37 |
private final String rootPath; |
4571 |
14 Sep 17 |
nicklas |
38 |
private final String releasePath; |
4571 |
14 Sep 17 |
nicklas |
39 |
private final Set<String> mkdirs; |
4571 |
14 Sep 17 |
nicklas |
40 |
private Map<String, String> releasedFiles; |
4571 |
14 Sep 17 |
nicklas |
41 |
|
4571 |
14 Sep 17 |
nicklas |
42 |
/** |
4571 |
14 Sep 17 |
nicklas |
Create a new location on the given file server. |
4571 |
14 Sep 17 |
nicklas |
The release directory is created by taking the |
4571 |
14 Sep 17 |
nicklas |
server root path and adding a subdirectory with the |
4571 |
14 Sep 17 |
nicklas |
release version. |
4571 |
14 Sep 17 |
nicklas |
47 |
*/ |
4571 |
14 Sep 17 |
nicklas |
48 |
public FileServerOutputLocation(FileServer server, String releaseVersion) |
4571 |
14 Sep 17 |
nicklas |
49 |
{ |
4571 |
14 Sep 17 |
nicklas |
50 |
this.server = server; |
4571 |
14 Sep 17 |
nicklas |
51 |
this.releaseVersion = releaseVersion; |
4571 |
14 Sep 17 |
nicklas |
52 |
this.rootPath = server.getRootPath(); |
4571 |
14 Sep 17 |
nicklas |
53 |
this.releasePath = makePath(rootPath, releaseVersion); |
4571 |
14 Sep 17 |
nicklas |
54 |
this.mkdirs = new HashSet<String>(); |
4571 |
14 Sep 17 |
nicklas |
55 |
|
4571 |
14 Sep 17 |
nicklas |
56 |
ConnectionInfo ci = new ConnectionInfo(server); |
4571 |
14 Sep 17 |
nicklas |
57 |
RemoteHost host = new RemoteHost(ci); |
4571 |
14 Sep 17 |
nicklas |
58 |
this.session = host.connect(5); |
4571 |
14 Sep 17 |
nicklas |
59 |
} |
4571 |
14 Sep 17 |
nicklas |
60 |
|
4571 |
14 Sep 17 |
nicklas |
61 |
/** |
4571 |
14 Sep 17 |
nicklas |
TRUE to compress output files (if supported by |
4571 |
14 Sep 17 |
nicklas |
the actual implementation). |
4571 |
14 Sep 17 |
nicklas |
64 |
*/ |
4571 |
14 Sep 17 |
nicklas |
65 |
public boolean getCompress() |
4571 |
14 Sep 17 |
nicklas |
66 |
{ |
4571 |
14 Sep 17 |
nicklas |
67 |
return compress; |
4571 |
14 Sep 17 |
nicklas |
68 |
} |
4571 |
14 Sep 17 |
nicklas |
69 |
public void setCompress(boolean compress) |
4571 |
14 Sep 17 |
nicklas |
70 |
{ |
4571 |
14 Sep 17 |
nicklas |
71 |
this.compress = compress; |
4571 |
14 Sep 17 |
nicklas |
72 |
} |
4571 |
14 Sep 17 |
nicklas |
73 |
|
4571 |
14 Sep 17 |
nicklas |
74 |
/** |
4571 |
14 Sep 17 |
nicklas |
TRUE to overwrite existing files. |
4571 |
14 Sep 17 |
nicklas |
76 |
*/ |
4571 |
14 Sep 17 |
nicklas |
77 |
public boolean getOverwrite() |
4571 |
14 Sep 17 |
nicklas |
78 |
{ |
4571 |
14 Sep 17 |
nicklas |
79 |
return overwrite; |
4571 |
14 Sep 17 |
nicklas |
80 |
} |
4571 |
14 Sep 17 |
nicklas |
81 |
public void setOverwrite(boolean overwrite) |
4571 |
14 Sep 17 |
nicklas |
82 |
{ |
4571 |
14 Sep 17 |
nicklas |
83 |
this.overwrite = overwrite; |
4571 |
14 Sep 17 |
nicklas |
84 |
} |
4571 |
14 Sep 17 |
nicklas |
85 |
|
4571 |
14 Sep 17 |
nicklas |
86 |
public String getRootPath() |
4571 |
14 Sep 17 |
nicklas |
87 |
{ |
4571 |
14 Sep 17 |
nicklas |
88 |
return rootPath; |
4571 |
14 Sep 17 |
nicklas |
89 |
} |
4571 |
14 Sep 17 |
nicklas |
90 |
|
4571 |
14 Sep 17 |
nicklas |
91 |
public String getReleasePath() |
4571 |
14 Sep 17 |
nicklas |
92 |
{ |
4571 |
14 Sep 17 |
nicklas |
93 |
return releasePath; |
4571 |
14 Sep 17 |
nicklas |
94 |
} |
4571 |
14 Sep 17 |
nicklas |
95 |
|
4571 |
14 Sep 17 |
nicklas |
96 |
public ExportOutputStream getOutputStream(String path, boolean executable) |
4571 |
14 Sep 17 |
nicklas |
97 |
{ |
4571 |
14 Sep 17 |
nicklas |
98 |
String fullPath = makePath(releasePath, path); |
4571 |
14 Sep 17 |
nicklas |
99 |
if (getCompress()) fullPath += ".gz"; |
4571 |
14 Sep 17 |
nicklas |
100 |
|
4571 |
14 Sep 17 |
nicklas |
// Check if directory needs to be created |
4571 |
14 Sep 17 |
nicklas |
102 |
String dir = fullPath.substring(0, fullPath.lastIndexOf('/')); |
4571 |
14 Sep 17 |
nicklas |
103 |
if (!mkdirs.contains(dir)) |
4571 |
14 Sep 17 |
nicklas |
104 |
{ |
4571 |
14 Sep 17 |
nicklas |
105 |
CmdResult<String> r = session.mkdirs(FilePermission.USER_RWX, dir); |
4571 |
14 Sep 17 |
nicklas |
106 |
r.throwExceptionIfNonZeroExitStatus(); |
4571 |
14 Sep 17 |
nicklas |
107 |
mkdirs.add(dir); |
4571 |
14 Sep 17 |
nicklas |
108 |
} |
4571 |
14 Sep 17 |
nicklas |
109 |
|
4571 |
14 Sep 17 |
nicklas |
110 |
OutputStream out = session.writeFile(fullPath, getOverwrite(), null, executable ? FilePermission.USER_RWX : FilePermission.USER_RW); |
4571 |
14 Sep 17 |
nicklas |
111 |
if (getCompress()) |
4571 |
14 Sep 17 |
nicklas |
112 |
{ |
4571 |
14 Sep 17 |
nicklas |
113 |
try |
4571 |
14 Sep 17 |
nicklas |
114 |
{ |
4571 |
14 Sep 17 |
nicklas |
115 |
out = new GZIPOutputStream(out); |
4571 |
14 Sep 17 |
nicklas |
116 |
} |
4571 |
14 Sep 17 |
nicklas |
117 |
catch (IOException ex) |
4571 |
14 Sep 17 |
nicklas |
118 |
{ |
4571 |
14 Sep 17 |
nicklas |
119 |
throw new RuntimeException(ex); |
4571 |
14 Sep 17 |
nicklas |
120 |
} |
4571 |
14 Sep 17 |
nicklas |
121 |
} |
4571 |
14 Sep 17 |
nicklas |
122 |
return new ExportOutputStream(out); |
4571 |
14 Sep 17 |
nicklas |
123 |
} |
4571 |
14 Sep 17 |
nicklas |
124 |
|
4571 |
14 Sep 17 |
nicklas |
125 |
@Override |
4571 |
14 Sep 17 |
nicklas |
126 |
public void close() |
4571 |
14 Sep 17 |
nicklas |
127 |
{ |
4571 |
14 Sep 17 |
nicklas |
128 |
OpenGrid.close(session); |
4571 |
14 Sep 17 |
nicklas |
129 |
} |
4571 |
14 Sep 17 |
nicklas |
130 |
|
4571 |
14 Sep 17 |
nicklas |
131 |
/** |
4571 |
14 Sep 17 |
nicklas |
Combine path elements and make sure that a single path |
4571 |
14 Sep 17 |
nicklas |
separator is inserted between each element. Null and emtpy string |
4571 |
14 Sep 17 |
nicklas |
elements are ignored. |
4571 |
14 Sep 17 |
nicklas |
135 |
*/ |
4571 |
14 Sep 17 |
nicklas |
136 |
public static String makePath(String... elements) |
4571 |
14 Sep 17 |
nicklas |
137 |
{ |
4571 |
14 Sep 17 |
nicklas |
138 |
StringBuilder s = new StringBuilder(); |
4571 |
14 Sep 17 |
nicklas |
139 |
for (String p : elements) |
4571 |
14 Sep 17 |
nicklas |
140 |
{ |
4571 |
14 Sep 17 |
nicklas |
141 |
if (p == null || p.equals("")) continue; |
4571 |
14 Sep 17 |
nicklas |
142 |
|
4571 |
14 Sep 17 |
nicklas |
143 |
if (s.length() > 0) |
4571 |
14 Sep 17 |
nicklas |
144 |
{ |
4571 |
14 Sep 17 |
nicklas |
145 |
char last = s.charAt(s.length()-1); |
4571 |
14 Sep 17 |
nicklas |
146 |
char first = p.charAt(0); |
4571 |
14 Sep 17 |
nicklas |
147 |
|
4571 |
14 Sep 17 |
nicklas |
148 |
if (last == '/' && first == '/') |
4571 |
14 Sep 17 |
nicklas |
149 |
{ |
4571 |
14 Sep 17 |
nicklas |
// Remove one '/' to avoid '//' |
4571 |
14 Sep 17 |
nicklas |
151 |
p = p.substring(1); |
4571 |
14 Sep 17 |
nicklas |
152 |
} |
4571 |
14 Sep 17 |
nicklas |
153 |
else if (last != '/' && first != '/') |
4571 |
14 Sep 17 |
nicklas |
154 |
{ |
4571 |
14 Sep 17 |
nicklas |
// Insert one '/' |
4571 |
14 Sep 17 |
nicklas |
156 |
s.append('/'); |
4571 |
14 Sep 17 |
nicklas |
157 |
} |
4571 |
14 Sep 17 |
nicklas |
158 |
} |
4571 |
14 Sep 17 |
nicklas |
159 |
s.append(p); |
4571 |
14 Sep 17 |
nicklas |
160 |
} |
4571 |
14 Sep 17 |
nicklas |
161 |
return s.toString(); |
4571 |
14 Sep 17 |
nicklas |
162 |
} |
4571 |
14 Sep 17 |
nicklas |
163 |
|
4571 |
14 Sep 17 |
nicklas |
164 |
} |