package org.jboss.fresh.deployer;
import org.jboss.fresh.io.IOUtils;
import org.jboss.fresh.registry.RegistryContext;
import org.jboss.fresh.shell.ProcessInfo;
import org.jboss.fresh.shell.Shell;
import org.jboss.fresh.shell.ShellOutputStream;
import org.jboss.fresh.shell.SystemShell;
import org.jboss.fresh.shell.ejb.RemoteShell;
import javax.naming.Context;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
/**
* Service creates a file in VFS.
*/
public class VFSFileService extends ServiceModule implements VFSFileServiceMBean {
private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(VFSFileService.class);
static final org.apache.log4j.Level TRACE = new ApacheLevel(5000, "TRACE", 7);
static final org.apache.log4j.Level NOTICE = new ApacheLevel(25000, "NOTICE", 5);
static class ApacheLevel extends org.apache.log4j.Level {
protected ApacheLevel(int level, String levelStr, int syslogEquivalent) {
super(level, levelStr, syslogEquivalent);
}
}
private String file;
private String content;
private String sourceFile;
private String classpathFile;
private URL url;
public String getName() {
return "VFS File Service";
}
public void setFile(String file) {
this.file = file;
}
public String getFile() {
return file;
}
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public void setFilePath(String sourceFile) {
this.sourceFile = sourceFile;
}
public String getFilePath() {
return sourceFile;
}
public void setResourcePath(String classpathFile) {
this.classpathFile = classpathFile;
}
public String getResourcePath() {
return classpathFile;
}
public void setUrl(String url) throws java.net.MalformedURLException {
this.url = new URL(url);
}
public String getUrl() {
return url == null ? null : url.toString();
}
public void doStart() throws Exception {
if (file == null || file.trim().length() == 0) {
throw new Exception("File name not defined. Please define VFS file name.");
}
Context ctx = null;
Shell shell = null;
RemoteShell rshell = null;
ShellOutputStream sos = null;
OutputStreamWriter osw = null;
try {
ctx = new RegistryContext();
try {
SystemShell sysshell = (SystemShell) ctx.lookup("java:/FRESH/SystemShell");
log.log(TRACE, "Retrieved SystemShell from RegistryContext: 'SystemShell'.");
shell = sysshell.startSession(null, false);
} catch (javax.naming.NameNotFoundException nnfe) {
throw nnfe;
}
log.debug("Using shell: " + shell);
String path = "";
String[] parts = file.split("/");
for (int i = 0; i < parts.length - 1; i++) {
path = path + "/" + parts[i];
String cmd = "mkdir " + path;
log.debug(cmd);
if (shell != null) {
shell.executeAsObject(cmd);
} else {
rshell.executeAsObject(cmd);
}
}
log.info("Creating file " + file);
String cmd;
boolean create = true;
if (classpathFile != null && classpathFile.length() > 0) {
url = this.getClass().getResource(classpathFile);
if (url != null) {
log.debug("Located resource using class.getResource(): " + url.toExternalForm());
content = null;
} else {
url = Thread.currentThread().getContextClassLoader().getResource(classpathFile);
if (url != null) {
log.debug("Located using context class loader: " + url.toExternalForm());
content = null;
} else {
log.warn("ClassPath file defined to '" + classpathFile + "' but not found using getResource() mechanism.");
create = false;
}
}
}
// content over matter
if (content != null && content.length() > 0) {
java.io.StringReader sr = null;
java.io.BufferedReader br = null;
try {
sr = new java.io.StringReader(content);
br = new java.io.BufferedReader(sr);
// let's style it a bit
if (br.readLine().trim().length() == 0) {
log.debug("Whitespace removal activated.");
StringBuffer result = new StringBuffer(content.length());
String whitespace = null;
boolean first = true;
String line = br.readLine();
while (line != null) {
if (first) {
int i = 0;
while (i < line.length() && Character.isWhitespace(line.charAt(i))) {
}
if (i == line.length() - 1) {
whitespace = "";
} else {
whitespace = line.substring(0, i);
log.log(TRACE, "Removing " + whitespace.length() + " whitespace characters from lines.");
}
first = false;
}
if (line.startsWith(whitespace)) {
line = line.substring(whitespace.length());
}
result.append(line).append("\r\n");
line = br.readLine();
}
content = result.toString();
}
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
}
}
if (sr != null) {
try {
sr.close();
} catch (Exception e) {
}
}
}
ProcessInfo pinf = null;
cmd = "cat > " + file;
log.debug(cmd);
if (shell != null) {
pinf = shell.execute(cmd);
sos = new ShellOutputStream(shell, pinf.procid);
} else {
pinf = rshell.execute(cmd);
sos = new ShellOutputStream(rshell, pinf.procid);
}
osw = new OutputStreamWriter(sos, "UTF-8");
osw.write(content);
log.log(NOTICE, "Created file " + file + ", processed " + content.length() + " characters.");
} else if (sourceFile != null && sourceFile.length() > 0) {
java.io.File f = new java.io.File(sourceFile);
if (!f.exists()) {
log.error("File " + f.getAbsolutePath() + " does not exist! Will not create VFS file: " + file);
return;
}
if (!f.isFile()) {
log.error("Path " + f.getAbsolutePath() + " is not a file! Will not create VFS file: " + file);
return;
}
cmd = "cat > " + file + " &< " + f.getAbsolutePath();
log.debug(cmd);
if (shell != null) {
shell.execute(cmd);
} else {
rshell.execute(cmd);
}
log.log(NOTICE, "Created file " + file + ", wrote " + f.length() + " bytes.");
} else if (url != null) {
InputStream is = null;
try {
java.net.URLConnection uc = url.openConnection();
uc.setConnectTimeout(30 * 1000);
uc.setReadTimeout(60 * 1000);
is = uc.getInputStream();
ProcessInfo pinf = null;
cmd = "cat > " + file;
log.debug(cmd);
if (shell != null) {
pinf = shell.execute(cmd);
sos = new ShellOutputStream(shell, pinf.procid);
} else {
pinf = rshell.execute(cmd);
sos = new ShellOutputStream(rshell, pinf.procid);
}
long size = IOUtils.copy(is, sos);
log.log(NOTICE, "Created file " + file + ", processed " + size + " bytes.");
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
}
} else if (create) {
cmd = "touch " + file;
log.debug(cmd);
if (shell != null) {
shell.execute(cmd);
} else {
rshell.execute(cmd);
}
log.log(NOTICE, "Created empty file " + file + ".");
}
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
}
}
// should not close shells. if(shell != null) { try { shell.close(); } catch (Exception e) { } }
if (rshell != null) {
try {
rshell.remove();
} catch (Exception e) {
}
}
if (osw != null) {
try {
osw.close();
} catch (Exception e) {
}
}
if (sos != null) {
try {
sos.close();
} catch (Exception e) {
}
}
}
}
}