Search in sources :

Example 96 with BufferedInputStream

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 97 with BufferedInputStream

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) IOException(java.io.IOException)

Example 98 with BufferedInputStream

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;
}
Also used : Message(org.opensolaris.opengrok.configuration.messages.Message) UnknownHostException(java.net.UnknownHostException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ServerSocket(java.net.ServerSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) XmlEofInputStream(org.opensolaris.opengrok.util.XmlEofInputStream) JSONObject(org.json.simple.JSONObject) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 99 with BufferedInputStream

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());
    }
}
Also used : FileAnalyzer(org.opensolaris.opengrok.analysis.FileAnalyzer) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Document(org.apache.lucene.document.Document) FileInputStream(java.io.FileInputStream) IndexWriter(org.apache.lucene.index.IndexWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) HistoryException(org.opensolaris.opengrok.history.HistoryException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.lucene.queryparser.classic.ParseException) IOException(java.io.IOException)

Example 100 with BufferedInputStream

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) Executor(org.opensolaris.opengrok.util.Executor) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1488 IOException (java.io.IOException)725 FileInputStream (java.io.FileInputStream)724 InputStream (java.io.InputStream)611 File (java.io.File)344 BufferedOutputStream (java.io.BufferedOutputStream)198 FileOutputStream (java.io.FileOutputStream)181 DataInputStream (java.io.DataInputStream)148 ByteArrayInputStream (java.io.ByteArrayInputStream)141 FileNotFoundException (java.io.FileNotFoundException)128 URL (java.net.URL)127 ByteArrayOutputStream (java.io.ByteArrayOutputStream)98 ZipEntry (java.util.zip.ZipEntry)98 OutputStream (java.io.OutputStream)89 HashMap (java.util.HashMap)61 HttpURLConnection (java.net.HttpURLConnection)59 ZipInputStream (java.util.zip.ZipInputStream)59 ArrayList (java.util.ArrayList)52 ObjectInputStream (java.io.ObjectInputStream)49 Test (org.junit.Test)49