use of java.io.BufferedInputStream in project OpenGrok by OpenGrok.
the class ClearCaseRepository method getHistoryGet.
@Override
public InputStream getHistoryGet(String parent, String basename, String rev) {
InputStream ret = null;
File directory = new File(directoryName);
Process process = null;
try {
String filename = (new File(parent, basename)).getCanonicalPath().substring(directoryName.length() + 1);
final File tmp = File.createTempFile("opengrok", "tmp");
String tmpName = tmp.getCanonicalPath();
// cleartool can't get to a previously existing file
if (tmp.exists() && !tmp.delete()) {
LOGGER.log(Level.WARNING, "Failed to remove temporary file used by history cache");
}
String decorated = filename + "@@" + rev;
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
String[] argv = { RepoCommand, "get", "-to", tmpName, decorated };
process = Runtime.getRuntime().exec(argv, null, directory);
drainStream(process.getInputStream());
if (waitFor(process) != 0) {
return null;
}
ret = new BufferedInputStream(new FileInputStream(tmp)) {
@Override
public void close() throws IOException {
super.close();
// delete the temporary file on close
if (!tmp.delete()) {
// failed, lets do the next best thing then ..
// delete it on JVM exit
tmp.deleteOnExit();
}
}
};
} catch (Exception exp) {
LOGGER.log(Level.SEVERE, "Failed to get history: " + exp.getClass().toString(), exp);
} finally {
// Clean up zombie-processes...
if (process != null) {
try {
process.exitValue();
} catch (IllegalThreadStateException exp) {
// the process is still running??? just kill it..
process.destroy();
}
}
}
return ret;
}
use of java.io.BufferedInputStream in project OpenGrok by OpenGrok.
the class MonotoneRepository method getHistoryGet.
@Override
public InputStream getHistoryGet(String parent, String basename, String rev) {
InputStream ret = null;
File directory = new File(directoryName);
Process process = null;
String revision = rev;
try {
String filename = (new File(parent, basename)).getCanonicalPath().substring(directoryName.length() + 1);
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
String[] argv = { RepoCommand, "cat", "-r", revision, filename };
process = Runtime.getRuntime().exec(argv, null, directory);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[32 * 1024];
try (InputStream in = process.getInputStream()) {
int len;
while ((len = in.read(buffer)) != -1) {
if (len > 0) {
out.write(buffer, 0, len);
}
}
}
ret = new BufferedInputStream(new ByteArrayInputStream(out.toByteArray()));
} catch (Exception exp) {
LOGGER.log(Level.SEVERE, "Failed to get history: {0}", exp.getClass().toString());
} finally {
// Clean up zombie-processes...
if (process != null) {
try {
process.exitValue();
} catch (IllegalThreadStateException exp) {
// the process is still running??? just kill it..
process.destroy();
}
}
}
return ret;
}
use of java.io.BufferedInputStream in project OpenGrok by OpenGrok.
the class RuntimeEnvironment method startConfigurationListenerThread.
/**
* Start a thread to listen on a socket to receive new messages.
* The messages can contain various commands for the webapp, including
* upload of new configuration.
*
* @param endpoint The socket address to listen on
* @return true if the endpoint was available (and the thread was started)
*/
public boolean startConfigurationListenerThread(SocketAddress endpoint) {
boolean ret = false;
try {
configServerSocket = new ServerSocket();
configServerSocket.bind(endpoint);
ret = true;
final ServerSocket sock = configServerSocket;
configurationListenerThread = new Thread(new Runnable() {
@Override
public void run() {
ByteArrayOutputStream bos = new ByteArrayOutputStream(1 << 15);
while (!sock.isClosed()) {
try (Socket s = sock.accept();
BufferedInputStream in = new BufferedInputStream(new XmlEofInputStream(s.getInputStream()));
OutputStream output = s.getOutputStream()) {
bos.reset();
LOGGER.log(Level.FINE, "OpenGrok: Got request from {0}", s.getInetAddress().getHostAddress());
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
bos.write(buf, 0, len);
}
buf = bos.toByteArray();
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "new config:{0}", new String(buf));
}
Object obj;
try (XMLDecoder d = new XMLDecoder(new ByteArrayInputStream(buf))) {
obj = d.readObject();
}
if (obj instanceof Message) {
Message m = ((Message) obj);
handleMessage(m, output);
}
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Error reading config file: ", e);
} catch (RuntimeException e) {
LOGGER.log(Level.SEVERE, "Error parsing config file: ", e);
}
}
}
}, "configurationListener");
configurationListenerThread.start();
} catch (UnknownHostException ex) {
LOGGER.log(Level.WARNING, "Problem resolving sender: ", ex);
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "I/O error when waiting for config: ", ex);
}
if (!ret && configServerSocket != null) {
IOUtils.close(configServerSocket);
}
return ret;
}
use of java.io.BufferedInputStream in project OpenGrok by OpenGrok.
the class IndexDatabase method addFile.
/**
* Add a file to the Lucene index (and generate a xref file)
*
* @param file The file to add
* @param path The path to the file (from source root)
* @throws java.io.IOException if an error occurs
*/
private void addFile(File file, String path) throws IOException {
FileAnalyzer fa;
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
fa = AnalyzerGuru.getAnalyzer(in, path);
}
for (IndexChangedListener listener : listeners) {
listener.fileAdd(path, fa.getClass().getSimpleName());
}
fa.setCtags(ctags);
fa.setProject(Project.getProject(path));
fa.setScopesEnabled(RuntimeEnvironment.getInstance().isScopesEnabled());
fa.setFoldingEnabled(RuntimeEnvironment.getInstance().isFoldingEnabled());
Document doc = new Document();
try (Writer xrefOut = getXrefWriter(fa, path)) {
analyzerGuru.populateDocument(doc, file, path, fa, xrefOut);
} catch (Exception e) {
LOGGER.log(Level.INFO, "Skipped file ''{0}'' because the analyzer didn''t " + "understand it.", path);
LOGGER.log(Level.FINE, "Exception from analyzer " + fa.getClass().getName(), e);
cleanupResources(doc);
return;
}
try {
writer.addDocument(doc);
} catch (Throwable t) {
cleanupResources(doc);
throw t;
}
setDirty();
for (IndexChangedListener listener : listeners) {
listener.fileAdded(path, fa.getClass().getSimpleName());
}
}
use of java.io.BufferedInputStream in project OpenGrok by OpenGrok.
the class SSCMRepository method getHistoryGet.
@Override
InputStream getHistoryGet(String parent, final String basename, String rev) {
InputStream ret = null;
File directory = new File(parent);
Process process = null;
try {
final File tmp = File.createTempFile("opengrok", "tmp");
String tmpName = tmp.getCanonicalPath();
// cleartool can't get to a previously existing file
if (tmp.exists() && !tmp.delete()) {
LOGGER.log(Level.WARNING, "Failed to remove temporary file used by history cache");
}
if (!tmp.mkdir()) {
LOGGER.log(Level.WARNING, "Failed to create temporary directory used by history cache");
}
List<String> argv = new ArrayList<>();
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
argv.add(RepoCommand);
argv.add("get");
argv.add(basename);
argv.add("-d" + tmpName);
Properties props = getProperties(directory);
String branch = props.getProperty(BRANCH_PROPERTY);
if (branch != null && !branch.isEmpty()) {
argv.add("-b" + branch);
}
String repo = props.getProperty(REPOSITORY_PROPERTY);
if (repo != null && !repo.isEmpty()) {
argv.add("-p" + repo);
}
if (rev != null) {
argv.add("-v" + rev);
}
argv.add("-q");
argv.add("-tmodify");
argv.add("-wreplace");
Executor exec = new Executor(argv, directory);
int status = exec.exec();
if (status != 0) {
LOGGER.log(Level.WARNING, "Failed get revision {2} for: \"{0}\" Exit code: {1}", new Object[] { new File(parent, basename).getAbsolutePath(), String.valueOf(status), rev });
return null;
}
ret = new BufferedInputStream(new FileInputStream(new File(tmp, basename))) {
@Override
public void close() throws IOException {
super.close();
boolean deleteOnExit = false;
File tmpFile = new File(tmp, basename);
// delete the temporary file on close
if (!tmpFile.delete()) {
// try on JVM exit
deleteOnExit = true;
tmpFile.deleteOnExit();
}
// delete the temporary directory on close
if (deleteOnExit || !tmp.delete()) {
// try on JVM exit
tmp.deleteOnExit();
}
}
};
} catch (IOException exp) {
LOGGER.log(Level.SEVERE, "Failed to get file: " + exp.getClass().toString(), exp);
} finally {
// Clean up zombie-processes...
if (process != null) {
try {
process.exitValue();
} catch (IllegalThreadStateException exp) {
// the process is still running??? just kill it..
process.destroy();
}
}
}
return ret;
}
Aggregations