Search in sources :

Example 1 with SuppressForbidden

use of io.crate.common.SuppressForbidden in project crate by crate.

the class JarHell method parseClassPath.

/**
 * Parses the classpath into a set of URLs. For testing.
 * @param classPath classpath to parse (typically the system property {@code java.class.path})
 * @return array of URLs
 * @throws IllegalStateException if the classpath contains empty elements
 */
@SuppressForbidden(reason = "resolves against CWD because that is how classpaths work")
static Set<URL> parseClassPath(String classPath) {
    String pathSeparator = System.getProperty("path.separator");
    String fileSeparator = System.getProperty("file.separator");
    String[] elements = classPath.split(pathSeparator);
    // order is already lost, but some filesystems have it
    Set<URL> urlElements = new LinkedHashSet<>();
    for (String element : elements) {
        // Instead we just throw an exception, and keep it clean.
        if (element.isEmpty()) {
            throw new IllegalStateException("Classpath should not contain empty elements! (outdated shell script from a previous" + " version?) classpath='" + classPath + "'");
        }
        // specification which java seems to allow, explicitly, right here...
        if (element.startsWith("/") && "\\".equals(fileSeparator)) {
            // "correct" the entry to become a normal entry
            // change to correct file separators
            element = element.replace("/", "\\");
            // if there is a drive letter, nuke the leading separator
            if (element.length() >= 3 && element.charAt(2) == ':') {
                element = element.substring(1);
            }
        }
        // now just parse as ordinary file
        try {
            URL url = PathUtils.get(element).toUri().toURL();
            if (urlElements.add(url) == false) {
                if (!url.getPath().endsWith("idea_rt.jar")) {
                    throw new IllegalStateException("jar hell!" + System.lineSeparator() + "duplicate jar [" + element + "] on classpath: " + classPath);
                }
            }
        } catch (MalformedURLException e) {
            // should not happen, as we use the filesystem API
            throw new RuntimeException(e);
        }
    }
    return Collections.unmodifiableSet(urlElements);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) SuppressForbidden(io.crate.common.SuppressForbidden)

Example 2 with SuppressForbidden

use of io.crate.common.SuppressForbidden in project crate by crate.

the class Ec2NameResolver method resolve.

/**
 * @param type the ec2 hostname type to discover.
 * @return the appropriate host resolved from ec2 meta-data, or null if it cannot be obtained.
 * @see CustomNameResolver#resolveIfPossible(String)
 */
@SuppressForbidden(reason = "We call getInputStream in doPrivileged and provide SocketPermission")
public InetAddress[] resolve(Ec2HostnameType type) throws IOException {
    InputStream in = null;
    String metadataUrl = AwsEc2ServiceImpl.EC2_METADATA_URL + type.ec2Name;
    try {
        URL url = new URL(metadataUrl);
        LOGGER.debug("obtaining ec2 hostname from ec2 meta-data url {}", url);
        URLConnection urlConnection = url.openConnection();
        urlConnection.setConnectTimeout(2000);
        in = urlConnection.getInputStream();
        try (BufferedReader urlReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
            String metadataResult = urlReader.readLine();
            if (metadataResult == null || metadataResult.length() == 0) {
                throw new IOException("no gce metadata returned from [" + url + "] for [" + type.configName + "]");
            }
            // only one address: because we explicitly ask for only one via the Ec2HostnameType
            return new InetAddress[] { InetAddress.getByName(metadataResult) };
        }
    } catch (IOException e) {
        throw new IOException("IOException caught when fetching InetAddress from [" + metadataUrl + "]", e);
    } finally {
        IOUtils.closeWhileHandlingException(in);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) InetAddress(java.net.InetAddress) URL(java.net.URL) URLConnection(java.net.URLConnection) SuppressForbidden(io.crate.common.SuppressForbidden)

Example 3 with SuppressForbidden

use of io.crate.common.SuppressForbidden in project crate by crate.

the class ConcurrentSeqNoVersioningIT method runLinearizabilityChecker.

@SuppressForbidden(reason = "system out is ok for a command line tool")
private static void runLinearizabilityChecker(FileInputStream fileInputStream, long primaryTerm, long seqNo) throws IOException {
    StreamInput is = new InputStreamStreamInput(Base64.getDecoder().wrap(fileInputStream));
    is = new NamedWriteableAwareStreamInput(is, createNamedWriteableRegistry());
    LinearizabilityChecker.History history = readHistory(is);
    Version initialVersion = new Version(primaryTerm, seqNo);
    boolean result = new LinearizabilityChecker().isLinearizable(new CASSequentialSpec(initialVersion), history, missingResponseGenerator());
    System.out.println(LinearizabilityChecker.visualize(new CASSequentialSpec(initialVersion), history, missingResponseGenerator()));
    System.out.println("Linearizable?: " + result);
}
Also used : LinearizabilityChecker(org.elasticsearch.cluster.coordination.LinearizabilityChecker) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput) SuppressForbidden(io.crate.common.SuppressForbidden)

Example 4 with SuppressForbidden

use of io.crate.common.SuppressForbidden in project crate by crate.

the class Ec2DiscoveryPlugin method getAvailabilityZoneNodeAttributes.

// pkg private for testing
@SuppressForbidden(reason = "We call getInputStream in doPrivileged and provide SocketPermission")
static Settings getAvailabilityZoneNodeAttributes(Settings settings, String azMetadataUrl) {
    if (AwsEc2Service.AUTO_ATTRIBUTE_SETTING.get(settings) == false) {
        return Settings.EMPTY;
    }
    final Settings.Builder attrs = Settings.builder();
    final URL url;
    final URLConnection urlConnection;
    try {
        url = new URL(azMetadataUrl);
        logger.debug("obtaining ec2 [placement/availability-zone] from ec2 meta-data url {}", url);
        urlConnection = url.openConnection();
        urlConnection.setConnectTimeout(2000);
    } catch (final IOException e) {
        // should not happen, we know the url is not malformed, and openConnection does not actually hit network
        throw new UncheckedIOException(e);
    }
    try (InputStream in = urlConnection.getInputStream();
        BufferedReader urlReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
        final String metadataResult = urlReader.readLine();
        if ((metadataResult == null) || (metadataResult.length() == 0)) {
            throw new IllegalStateException("no ec2 metadata returned from " + url);
        } else {
            attrs.put(Node.NODE_ATTRIBUTES.getKey() + "aws_availability_zone", metadataResult);
        }
    } catch (final IOException e) {
        // this is lenient so the plugin does not fail when installed outside of ec2
        logger.error("failed to get metadata for [placement/availability-zone]", e);
    }
    return attrs.build();
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Settings(org.elasticsearch.common.settings.Settings) URL(java.net.URL) URLConnection(java.net.URLConnection) SuppressForbidden(io.crate.common.SuppressForbidden)

Example 5 with SuppressForbidden

use of io.crate.common.SuppressForbidden in project crate by crate.

the class JarHell method checkJarHell.

/**
 * Checks the set of URLs for duplicate classes
 * @param urls A set of URLs from the classpath to be checked for conflicting jars
 * @param output A {@link String} {@link Consumer} to which debug output will be sent
 * @throws IllegalStateException if jar hell was found
 */
@SuppressForbidden(reason = "needs JarFile for speed, just reading entries")
public static void checkJarHell(Set<URL> urls, Consumer<String> output) throws URISyntaxException, IOException {
    // we don't try to be sneaky and use deprecated/internal/not portable stuff
    // like sun.boot.class.path, and with jigsaw we don't yet have a way to get
    // a "list" at all. So just exclude any elements underneath the java home
    String javaHome = System.getProperty("java.home");
    output.accept("java.home: " + javaHome);
    final Map<String, Path> clazzes = new HashMap<>(32768);
    Set<Path> seenJars = new HashSet<>();
    for (final URL url : urls) {
        final Path path = PathUtils.get(url.toURI());
        // exclude system resources
        if (path.startsWith(javaHome)) {
            output.accept("excluding system resource: " + path);
            continue;
        }
        if (path.toString().endsWith(".jar")) {
            if (!seenJars.add(path)) {
                throw new IllegalStateException("jar hell!" + System.lineSeparator() + "duplicate jar on classpath: " + path);
            }
            output.accept("examining jar: " + path);
            try (JarFile file = new JarFile(path.toString())) {
                Manifest manifest = file.getManifest();
                if (manifest != null) {
                    checkManifest(manifest, path);
                }
                // inspect entries
                Enumeration<JarEntry> elements = file.entries();
                while (elements.hasMoreElements()) {
                    String entry = elements.nextElement().getName();
                    if (entry.endsWith(".class")) {
                        // for jar format, the separator is defined as /
                        entry = entry.replace('/', '.').substring(0, entry.length() - 6);
                        checkClass(clazzes, entry, path);
                    }
                }
            }
        } else {
            output.accept("examining directory: " + path);
            // case for tests: where we have class files in the classpath
            final Path root = PathUtils.get(url.toURI());
            final String sep = root.getFileSystem().getSeparator();
            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    String entry = root.relativize(file).toString();
                    if (entry.endsWith(".class")) {
                        // normalize with the os separator, remove '.class'
                        entry = entry.replace(sep, ".").substring(0, entry.length() - ".class".length());
                        checkClass(clazzes, entry, path);
                    }
                    return super.visitFile(file, attrs);
                }
            });
        }
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) SuppressForbidden(io.crate.common.SuppressForbidden)

Aggregations

SuppressForbidden (io.crate.common.SuppressForbidden)5 URL (java.net.URL)4 IOException (java.io.IOException)3 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 URLConnection (java.net.URLConnection)2 LinkedHashSet (java.util.LinkedHashSet)2 UncheckedIOException (java.io.UncheckedIOException)1 InetAddress (java.net.InetAddress)1 MalformedURLException (java.net.MalformedURLException)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 Manifest (java.util.jar.Manifest)1 LinearizabilityChecker (org.elasticsearch.cluster.coordination.LinearizabilityChecker)1