/** * Returns a {@link ContentType} from a given {@link XContentType}. * * @param xContentType the {@link XContentType} * @return the {@link ContentType} */ @SuppressForbidden(reason = "Only allowed place to convert a XContentType to a ContentType") public static ContentType createContentType(final XContentType xContentType) { return ContentType.create(xContentType.mediaTypeWithoutParameters(), (Charset) null); }
@SuppressForbidden(reason = "Version#parseLeniently() used in a central place") private static final class LenientParser { public static Version parse(String toParse, Version defaultValue) { if (Strings.hasLength(toParse)) { try { return Version.parseLeniently(toParse); } catch (ParseException e) { // pass to default } } return defaultValue; } }
/** * Notify all threads waiting on the monitor on this tracker. These threads should be waiting for the local checkpoint on a specific * allocation ID to catch up to the global checkpoint. */ @SuppressForbidden(reason = "Object#notifyAll waiters for local checkpoint advancement") private synchronized void notifyAllWaiters() { this.notifyAll(); }
@SuppressForbidden(reason = "optimalCapacity is non-negative, therefore the difference cannot be < -Integer.MAX_VALUE") private int getChangeAmount(int optimalCapacity) { assert optimalCapacity >= 0 : "optimal capacity should always be positive, got: " + optimalCapacity; return Math.abs(optimalCapacity - this.capacity); }
@SuppressForbidden(reason = "ThreadLocalRandom is okay when not running tests") private static Random getWithoutSeed() { assert currentMethod == null && getRandomMethod == null : "running under tests but tried to create non-reproducible random"; return ThreadLocalRandom.current(); }
/** * Wait for the local checkpoint to advance to the global checkpoint. * * @throws InterruptedException if this thread was interrupted before of during waiting */ @SuppressForbidden(reason = "Object#wait for local checkpoint advancement") private synchronized void waitForLocalCheckpointToAdvance() throws InterruptedException { this.wait(); }
@SuppressForbidden(reason = "System#err") private static void closeSysError() { System.err.close(); }
@SuppressForbidden(reason = "need path to construct environment") private static Path getConfigPath(final String pathConf) { return Paths.get(pathConf); }
/** * Waits for all operations up to the provided sequence number to complete. * * @param seqNo the sequence number that the checkpoint must advance to before this method returns * @throws InterruptedException if the thread was interrupted while blocking on the condition */ @SuppressForbidden(reason = "Object#wait") public synchronized void waitForOpsToComplete(final long seqNo) throws InterruptedException { while (checkpoint < seqNo) { // notified by updateCheckpoint this.wait(); } }
@SuppressForbidden(reason = "System#out") private static void closeSystOut() { System.out.close(); }
/** Simple checks that everything is ok */ @SuppressForbidden(reason = "accesses jvm default tempdir as a self-test") static void selfTest() throws IOException { // check we can manipulate temporary files try { Path p = Files.createTempFile(null, null); try { Files.delete(p); } catch (IOException ignored) { // potentially virus scanner } } catch (SecurityException problem) { throw new SecurityException("Security misconfiguration: cannot access java.io.tmpdir", problem); } } }
@SuppressForbidden(reason="file arg for cli") private Path getPath(String file) { return PathUtils.get(file); } }
@SuppressForbidden(reason = "halt") @Override public Void run() { // we halt to prevent shutdown hooks from running Runtime.getRuntime().halt(status); return null; }
@SuppressForbidden(reason = "only exposing the method as a passthrough") public ZonedDateTime truncatedTo(TemporalUnit unit) { return dt.truncatedTo(unit); }
@SuppressForbidden(reason = "Necessary to use the path passed in") protected Path getPath(String dirValue) { return PathUtils.get(dirValue, "", ""); }
@SuppressForbidden(reason = "read /proc/diskstats") List<String> readProcDiskStats() throws IOException { return Files.readAllLines(PathUtils.get("/proc/diskstats")); }
@SuppressForbidden(reason = "access /proc/sys/vm/max_map_count") private Path getProcSysVmMaxMapCountPath() { return PathUtils.get("/proc/sys/vm/max_map_count"); }
/** * Cancel execution of this future without interrupting a running thread. See {@link Future#cancel(boolean)} for details. * * @param toCancel the future to cancel * @return false if the future could not be cancelled, otherwise true */ @SuppressForbidden(reason = "Future#cancel()") public static boolean cancel(@Nullable final Future<?> toCancel) { if (toCancel != null) { return toCancel.cancel(false); // this method is a forbidden API since it interrupts threads } return false; }
@SuppressForbidden(reason = "access temp directory for building index") protected static synchronized FSDirectory getTempDir() { if (tmpBuildDir == null) { // Lazy init String tempDirPath = System.getProperty("java.io.tmpdir"); if (tempDirPath == null) { throw new RuntimeException("Java has no temporary folder property (java.io.tmpdir)?"); } try { tmpBuildDir = FSDirectory.open(PathUtils.get(tempDirPath)); } catch (IOException ioe) { throw new RuntimeException(ioe); } } return tmpBuildDir; }
/** * Returns an InputStream the given url if the url has a protocol of 'file' or 'jar', no host, and no port. */ @SuppressForbidden(reason = "Will only open url streams for local files") public static InputStream openFileURLStream(URL url) throws IOException { String protocol = url.getProtocol(); if ("file".equals(protocol) == false && "jar".equals(protocol) == false) { throw new IllegalArgumentException("Invalid protocol [" + protocol + "], must be [file] or [jar]"); } if (Strings.isEmpty(url.getHost()) == false) { throw new IllegalArgumentException("URL cannot have host. Found: [" + url.getHost() + ']'); } if (url.getPort() != -1) { throw new IllegalArgumentException("URL cannot have port. Found: [" + url.getPort() + ']'); } return url.openStream(); }