Search in sources :

Example 31 with FileInputStream

use of java.io.FileInputStream in project storm by apache.

the class LocalizerTest method testArchives.

// archive passed in must contain symlink named tmptestsymlink if not a zip file
public void testArchives(String archivePath, boolean supportSymlinks, int size) throws Exception {
    if (Utils.isOnWindows()) {
        // Windows should set this to false cause symlink in compressed file doesn't work properly.
        supportSymlinks = false;
    }
    Map conf = new HashMap();
    // set clean time really high so doesn't kick in
    conf.put(Config.SUPERVISOR_LOCALIZER_CACHE_CLEANUP_INTERVAL_MS, 60 * 60 * 1000);
    String key1 = new File(archivePath).getName();
    String topo1 = "topo1";
    Localizer localizer = new TestLocalizer(conf, baseDir.toString());
    // set really small so will do cleanup
    localizer.setTargetCacheSize(1);
    ReadableBlobMeta rbm = new ReadableBlobMeta();
    rbm.set_settable(new SettableBlobMeta(BlobStoreAclHandler.WORLD_EVERYTHING));
    when(mockblobstore.getBlobMeta(key1)).thenReturn(rbm);
    when(mockblobstore.getBlob(key1)).thenReturn(new TestInputStreamWithMeta(new FileInputStream(archivePath)));
    long timeBefore = System.nanoTime();
    File user1Dir = localizer.getLocalUserFileCacheDir(user1);
    assertTrue("failed to create user dir", user1Dir.mkdirs());
    LocalizedResource lrsrc = localizer.getBlob(new LocalResource(key1, true), user1, topo1, user1Dir);
    long timeAfter = System.nanoTime();
    String expectedUserDir = joinPath(baseDir.toString(), Localizer.USERCACHE, user1);
    String expectedFileDir = joinPath(expectedUserDir, Localizer.FILECACHE, Localizer.ARCHIVESDIR);
    assertTrue("user filecache dir not created", new File(expectedFileDir).exists());
    File keyFile = new File(expectedFileDir, key1 + ".0");
    assertTrue("blob not created", keyFile.exists());
    assertTrue("blob is not uncompressed", keyFile.isDirectory());
    File symlinkFile = new File(keyFile, "tmptestsymlink");
    if (supportSymlinks) {
        assertTrue("blob uncompressed doesn't contain symlink", Files.isSymbolicLink(symlinkFile.toPath()));
    } else {
        assertTrue("blob symlink file doesn't exist", symlinkFile.exists());
    }
    LocalizedResourceSet lrsrcSet = localizer.getUserResources().get(user1);
    assertEquals("local resource set size wrong", 1, lrsrcSet.getSize());
    assertEquals("user doesn't match", user1, lrsrcSet.getUser());
    LocalizedResource key1rsrc = lrsrcSet.get(key1, true);
    assertNotNull("Local resource doesn't exist but should", key1rsrc);
    assertEquals("key doesn't match", key1, key1rsrc.getKey());
    assertEquals("refcount doesn't match", 1, key1rsrc.getRefCount());
    assertEquals("file path doesn't match", keyFile.toString(), key1rsrc.getFilePathWithVersion());
    assertEquals("size doesn't match", size, key1rsrc.getSize());
    assertTrue("timestamp not within range", (key1rsrc.getLastAccessTime() >= timeBefore && key1rsrc.getLastAccessTime() <= timeAfter));
    timeBefore = System.nanoTime();
    localizer.removeBlobReference(lrsrc.getKey(), user1, topo1, true);
    timeAfter = System.nanoTime();
    lrsrcSet = localizer.getUserResources().get(user1);
    assertEquals("local resource set size wrong", 1, lrsrcSet.getSize());
    key1rsrc = lrsrcSet.get(key1, true);
    assertNotNull("Local resource doesn't exist but should", key1rsrc);
    assertEquals("refcount doesn't match", 0, key1rsrc.getRefCount());
    assertTrue("timestamp not within range", (key1rsrc.getLastAccessTime() >= timeBefore && key1rsrc.getLastAccessTime() <= timeAfter));
    // should remove the blob since cache size set really small
    localizer.handleCacheCleanup();
    lrsrcSet = localizer.getUserResources().get(user1);
    assertFalse("blob contents not deleted", symlinkFile.exists());
    assertFalse("blob not deleted", keyFile.exists());
    assertFalse("blob file dir not deleted", new File(expectedFileDir).exists());
    assertFalse("blob dir not deleted", new File(expectedUserDir).exists());
    assertNull("user set should be null", lrsrcSet);
}
Also used : ReadableBlobMeta(org.apache.storm.generated.ReadableBlobMeta) FileInputStream(java.io.FileInputStream) File(java.io.File) SettableBlobMeta(org.apache.storm.generated.SettableBlobMeta)

Example 32 with FileInputStream

use of java.io.FileInputStream in project tomcat by apache.

the class SignCode method getApplicationString.

/**
     * Zips the files, base 64 encodes the resulting zip and then returns the
     * string. It would be far more efficient to stream this directly to the
     * signing server but the files that need to be signed are relatively small
     * and this simpler to write.
     *
     * @param fileNames Modified names of files
     * @param files     Files to be signed
     */
private static String getApplicationString(List<String> fileNames, List<File> files) throws IOException {
    // 16 MB should be more than enough for Tomcat
    // TODO: Refactoring this entire class so it uses streaming rather than
    //       buffering the entire set of files in memory would make it more
    //       widely useful.
    ByteArrayOutputStream baos = new ByteArrayOutputStream(16 * 1024 * 1024);
    try (ZipOutputStream zos = new ZipOutputStream(baos)) {
        byte[] buf = new byte[32 * 1024];
        for (int i = 0; i < files.size(); i++) {
            try (FileInputStream fis = new FileInputStream(files.get(i))) {
                ZipEntry zipEntry = new ZipEntry(fileNames.get(i));
                zos.putNextEntry(zipEntry);
                int numRead;
                while ((numRead = fis.read(buf)) >= 0) {
                    zos.write(buf, 0, numRead);
                }
            }
        }
    }
    return Base64.encodeBase64String(baos.toByteArray());
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream)

Example 33 with FileInputStream

use of java.io.FileInputStream in project tomcat by apache.

the class SetParentClassLoaderRule method load.

/**
     * Start a new server instance.
     */
public void load() {
    long t1 = System.nanoTime();
    initDirs();
    // Before digester - it may be needed
    initNaming();
    // Create and execute our Digester
    Digester digester = createStartDigester();
    InputSource inputSource = null;
    InputStream inputStream = null;
    File file = null;
    try {
        try {
            file = configFile();
            inputStream = new FileInputStream(file);
            inputSource = new InputSource(file.toURI().toURL().toString());
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("catalina.configFail", file), e);
            }
        }
        if (inputStream == null) {
            try {
                inputStream = getClass().getClassLoader().getResourceAsStream(getConfigFile());
                inputSource = new InputSource(getClass().getClassLoader().getResource(getConfigFile()).toString());
            } catch (Exception e) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("catalina.configFail", getConfigFile()), e);
                }
            }
        }
        // Alternative: don't bother with xml, just create it manually.
        if (inputStream == null) {
            try {
                inputStream = getClass().getClassLoader().getResourceAsStream("server-embed.xml");
                inputSource = new InputSource(getClass().getClassLoader().getResource("server-embed.xml").toString());
            } catch (Exception e) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("catalina.configFail", "server-embed.xml"), e);
                }
            }
        }
        if (inputStream == null || inputSource == null) {
            if (file == null) {
                log.warn(sm.getString("catalina.configFail", getConfigFile() + "] or [server-embed.xml]"));
            } else {
                log.warn(sm.getString("catalina.configFail", file.getAbsolutePath()));
                if (file.exists() && !file.canRead()) {
                    log.warn("Permissions incorrect, read permission is not allowed on the file.");
                }
            }
            return;
        }
        try {
            inputSource.setByteStream(inputStream);
            digester.push(this);
            digester.parse(inputSource);
        } catch (SAXParseException spe) {
            log.warn("Catalina.start using " + getConfigFile() + ": " + spe.getMessage());
            return;
        } catch (Exception e) {
            log.warn("Catalina.start using " + getConfigFile() + ": ", e);
            return;
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            // Ignore
            }
        }
    }
    getServer().setCatalina(this);
    getServer().setCatalinaHome(Bootstrap.getCatalinaHomeFile());
    getServer().setCatalinaBase(Bootstrap.getCatalinaBaseFile());
    // Stream redirection
    initStreams();
    // Start the new server
    try {
        getServer().init();
    } catch (LifecycleException e) {
        if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) {
            throw new java.lang.Error(e);
        } else {
            log.error("Catalina.start", e);
        }
    }
    long t2 = System.nanoTime();
    if (log.isInfoEnabled()) {
        log.info("Initialization processed in " + ((t2 - t1) / 1000000) + " ms");
    }
}
Also used : InputSource(org.xml.sax.InputSource) LifecycleException(org.apache.catalina.LifecycleException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) Digester(org.apache.tomcat.util.digester.Digester) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ConnectException(java.net.ConnectException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException)

Example 34 with FileInputStream

use of java.io.FileInputStream in project tomcat by apache.

the class ContextConfig method getWebXmlSource.

/**
     * Utility method to create an input source from the specified XML file.
     * @param filename  Name of the file (possibly with one or more leading path
     *                  segments) to read
     * @param path      Location that filename is relative to
     * @return the input source
     */
protected InputSource getWebXmlSource(String filename, String path) {
    File file = new File(filename);
    if (!file.isAbsolute()) {
        file = new File(path, filename);
    }
    InputStream stream = null;
    InputSource source = null;
    try {
        if (!file.exists()) {
            // Use getResource and getResourceAsStream
            stream = getClass().getClassLoader().getResourceAsStream(filename);
            if (stream != null) {
                source = new InputSource(getClass().getClassLoader().getResource(filename).toURI().toString());
            }
        } else {
            source = new InputSource(file.getAbsoluteFile().toURI().toString());
            stream = new FileInputStream(file);
        }
        if (stream != null && source != null) {
            source.setByteStream(stream);
        }
    } catch (Exception e) {
        log.error(sm.getString("contextConfig.defaultError", filename, file), e);
    } finally {
        if (source == null && stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            // Ignore
            }
        }
    }
    return source;
}
Also used : InputSource(org.xml.sax.InputSource) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ClassFormatException(org.apache.tomcat.util.bcel.classfile.ClassFormatException)

Example 35 with FileInputStream

use of java.io.FileInputStream in project tomcat by apache.

the class SetParentClassLoaderRule method stopServer.

public void stopServer(String[] arguments) {
    if (arguments != null) {
        arguments(arguments);
    }
    Server s = getServer();
    if (s == null) {
        // Create and execute our Digester
        Digester digester = createStopDigester();
        File file = configFile();
        try (FileInputStream fis = new FileInputStream(file)) {
            InputSource is = new InputSource(file.toURI().toURL().toString());
            is.setByteStream(fis);
            digester.push(this);
            digester.parse(is);
        } catch (Exception e) {
            log.error("Catalina.stop: ", e);
            System.exit(1);
        }
    } else {
        // Server object already present. Must be running as a service
        try {
            s.stop();
        } catch (LifecycleException e) {
            log.error("Catalina.stop: ", e);
        }
        return;
    }
    // Stop the existing server
    s = getServer();
    if (s.getPort() > 0) {
        try (Socket socket = new Socket(s.getAddress(), s.getPort());
            OutputStream stream = socket.getOutputStream()) {
            String shutdown = s.getShutdown();
            for (int i = 0; i < shutdown.length(); i++) {
                stream.write(shutdown.charAt(i));
            }
            stream.flush();
        } catch (ConnectException ce) {
            log.error(sm.getString("catalina.stopServer.connectException", s.getAddress(), String.valueOf(s.getPort())));
            log.error("Catalina.stop: ", ce);
            System.exit(1);
        } catch (IOException e) {
            log.error("Catalina.stop: ", e);
            System.exit(1);
        }
    } else {
        log.error(sm.getString("catalina.stopServer"));
        System.exit(1);
    }
}
Also used : InputSource(org.xml.sax.InputSource) LifecycleException(org.apache.catalina.LifecycleException) Server(org.apache.catalina.Server) OutputStream(java.io.OutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ConnectException(java.net.ConnectException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) Digester(org.apache.tomcat.util.digester.Digester) File(java.io.File) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

Aggregations

FileInputStream (java.io.FileInputStream)14180 File (java.io.File)6801 IOException (java.io.IOException)6283 InputStream (java.io.InputStream)4121 FileOutputStream (java.io.FileOutputStream)2210 FileNotFoundException (java.io.FileNotFoundException)2002 InputStreamReader (java.io.InputStreamReader)1733 BufferedInputStream (java.io.BufferedInputStream)1593 Test (org.junit.Test)1548 BufferedReader (java.io.BufferedReader)1414 Properties (java.util.Properties)1410 ArrayList (java.util.ArrayList)960 OutputStream (java.io.OutputStream)753 ByteArrayInputStream (java.io.ByteArrayInputStream)599 HashMap (java.util.HashMap)597 ZipEntry (java.util.zip.ZipEntry)575 DataInputStream (java.io.DataInputStream)549 GZIPInputStream (java.util.zip.GZIPInputStream)435 KeyStore (java.security.KeyStore)430 ByteArrayOutputStream (java.io.ByteArrayOutputStream)407