Github user revans2 commented on a diff in the pull request: https://github.com/apache/storm/pull/1043#discussion_r50901067 --- Diff: storm-core/src/jvm/org/apache/storm/utils/Utils.java --- @@ -1382,5 +1422,438 @@ public static TopologyInfo getTopologyInfo(String name, String asUser, Map storm public static int toPositive(int number) { return number & Integer.MAX_VALUE; } -} + + + //Everything from here on is translated from the old util.clj (storm-core/src/clj/backtype.storm/util.clj) + + //Wraps an exception in a RuntimeException if needed + public static Exception wrapInRuntime (Exception e) { + if (e instanceof RuntimeException) { + return e; + } else { + return (new RuntimeException(e)); + } + } + + public static final boolean isOnWindows = "Windows_NT".equals(System.getenv("OS")); + + public static final String filePathSeparator = System.getProperty("file.separator"); + + public static final String classPathSeparator = System.getProperty("path.separator"); + + + /* + Returns the first item of coll for which (pred item) returns logical true. + Consumes sequences up to the first match, will consume the entire sequence + and return nil if no match is found. + */ + public static Object findFirst (IPredicate pred, Collection coll) { + if (coll == null || pred == null) { + return null; + } else { + Iterator<Object> iter = coll.iterator(); + if (iter==null || !iter.hasNext()) { + return null; + } else { + do { + Object obj = iter.next(); + if (pred.test(obj)) { + return obj; + } + } while (iter.hasNext()); + return null; + } + } + } + + public static Object findFirst (IPredicate pred, Map map) { + if (map == null || pred == null) { + return null; + } else { + Iterator<Object> iter = map.entrySet().iterator(); + if (iter==null || !iter.hasNext()) { + return null; + } else { + do { + Object obj = iter.next(); + if (pred.test(obj)) { + return obj; + } + } while (iter.hasNext()); + return null; + } + } + } + /* + Note: since the following functions are nowhere used in Storm, they were not translated: + dissoc-in + indexed + positions + assoc-conj + set-delta + + clojurify-structure because it wouldn't make sense without clojure + */ + + + public static String localHostname () throws UnknownHostException { + return _instance.localHostnameImpl(); + } + + protected String localHostnameImpl () throws UnknownHostException { + return InetAddress.getLocalHost().getCanonicalHostName(); + } + + private static String memoizedLocalHostnameString = null; + + public static String memoizedLocalHostname () throws UnknownHostException { + if (memoizedLocalHostnameString == null) { + memoizedLocalHostnameString = localHostname(); + } + return memoizedLocalHostnameString; + } + + /* + checks conf for STORM_LOCAL_HOSTNAME. + when unconfigured, falls back to (memoized) guess by `local-hostname`. + */ + public static String hostname (Map<String, Object> conf) throws UnknownHostException { + if (conf == null) { + return memoizedLocalHostname(); + } + Object hostnameString = conf.get(Config.STORM_LOCAL_HOSTNAME); + if (hostnameString == null ) { + return memoizedLocalHostname(); + } + if (hostnameString.equals("")) { + return memoizedLocalHostname(); + } + return hostnameString.toString(); + } + + public static String uuid() { + return UUID.randomUUID().toString(); + } + + public static int currentTimeSecs() { + return Time.currentTimeSecs(); + } + + public static long currentTimeMillis() { + return Time.currentTimeMillis(); + } + + public static long secsToMillisLong(double secs) { + return (long) (1000 * secs); + } + + public static Vector<String> tokenizePath (String path) { + String[] tokens = path.split("/"); + Vector<String> outputs = new Vector<String>(); + if (tokens == null || tokens.length == 0) { + return null; + } + for (String tok: tokens) { + if (!tok.isEmpty()) { + outputs.add(tok); + } + } + return outputs; + } + + public static String parentPath(String path) { + if (path == null) { + return "/"; + } + Vector<String> tokens = tokenizePath(path); + int length = tokens.size(); + if (length == 0) { + return "/"; + } + String output = ""; + for (int i = 0; i < length - 1; i++) { //length - 1 to mimic "butlast" from the old clojure code + output = output + "/" + tokens.get(i); + } + return output; + } + + public static String toksToPath (Vector<String> toks) { + if (toks == null) { + return "/"; + } + int length = toks.size(); + if (length == 0) { + return "/"; + } + String output = ""; + for (int i = 0; i < length; i++) { + output = output + "/" + toks.get(i); + } + return output; + } + public static String normalizePath (String path) { + return toksToPath(tokenizePath(path)); + } + + /* TODO: This function was originally written to replace map-val in util.clj. But we decided to change the coding + style in the caller functions to a more Java style for loop. This is mentioned in TODOs across the clojure + files. + */ + public static Map mapVal (IFn aFn, Map amap) { + Map newMap = new HashMap(); + if (amap == null) { + return newMap; + } + if (amap.keySet()==null) { + return newMap; + } + for (Object key: amap.keySet()) { + Object value = amap.get(key); + if (value == null) { + newMap.put(key, null); + } else { + Object newValue = aFn.eval(value); + newMap.put(key, newValue); + } + } + return newMap; + } + + /* TODO: This function was originally written to replace map-val in util.clj. But we decided to change the coding + style in the caller functions to a more Java style for loop. This is mentioned in TODOs across the clojure + files. + */ +// public static Map filterVal(IPredicate aFn, Map amap) { +// Map newMap = new HashMap(); +// if (amap == null) { +// return newMap; +// } +// for (Object key: amap.keySet()) { +// Object value = amap.get(key); +// if(aFn.test(value)) { +// newMap.put(key, value); +// } +// } +// return newMap; +// } + + /* TODO: This function was originally written to replace filter-key in util.clj. But we decided to change the coding + style in the caller functions to a more Java style for loop + if conditionals. This is mentioned in TODOs + across the clojure files. + */ +// public static Map filterKey(IPredicate aFn, Map amap) { +// Map newMap = new HashMap(); +// if (amap == null) { +// return newMap; +// } +// for (Object key: amap.keySet()) { +// Object value = amap.get(key); +// if(aFn.test(key)) { +// newMap.put(key, value); +// } +// } +// return newMap; +// } + + /* TODO: This function was originally written to replace map-key in util.clj. But we decided to change the coding + style in the caller functions to a more Java style for loop. This is mentioned in TODOs across the clojure + files. + */ +// public static Map mapKey (IFn aFn, Map amap) { +// Map newMap = new HashMap(); +// if (amap == null) { +// return newMap; +// } +// for (Object key: amap.keySet()) { +// Object value = amap.get(key); +// Object newKey = aFn.eval(key); +// newMap.put(newKey, value); +// } +// return newMap; +// } + + /* TODO: This function was originally written to replace separate in util.clj. But since it was only used in + transactional_test.clj, the separate function was moved there for now. + */ + public static Vector<Collection> separate (IPredicate pred, Collection aseq) { + Vector<Collection> outputVector = new Vector<Collection>(); + Collection pass = new HashSet(); + Collection notPass = new HashSet(); + for (Object obj: aseq) { + if (pred.test(obj)) { + pass.add(obj); + } else { + notPass.add(obj); + } + } + outputVector.add(pass); + outputVector.add(notPass); + return outputVector; + } + + public static void exitProcess (int val, Object... msg) { + String combinedErrorMessage = ""; + for (Object oneMessage: msg) { + combinedErrorMessage = combinedErrorMessage + oneMessage.toString(); + } + LOG.error("halting process: " + combinedErrorMessage, new RuntimeException(combinedErrorMessage)); + Runtime.getRuntime().exit(val); + } + + public static Double sum(Collection<Number> vals) { + double sum = 0.0; + double dVal; + if (vals == null) { + return 0.0; + } + for (Number val: vals) { + dVal = val.doubleValue(); + sum = sum + dVal; + } + return new Double(sum); + } + + + public static Object defaulted(Object val, Object defaultObj) { + if (val != null) { + return val; + } else { + return defaultObj; + } + } + + /** + * Deletes a file or directory and its contents if it exists. Does not + * complain if the input is null or does not exist. + * @param path the path to the file or directory + */ + public static void forceDelete(String path) { + _instance.forceDeleteImpl(path); + } + + protected void forceDeleteImpl(String path) { + LOG.debug("Deleting path {}", path); + if (checkFileExists(path)) { + try { + FileUtils.forceDelete(new File(path)); + } catch (IOException ignored) {} + } + } + + /** + * Creates a symbolic link to the target + * @param dir the parent directory of the link + * @param targetDir the parent directory of the link's target + * @param targetFilename the file name of the links target + * @param filename the file name of the link + * @return the path of the link if it did not exist, otherwise null + * @throws IOException + */ + public static Path createSymlink(String dir, String targetDir, + String targetFilename, String filename) throws IOException { + Path path = Paths.get(dir, filename).toAbsolutePath(); + Path target = Paths.get(targetDir, targetFilename).toAbsolutePath(); + LOG.debug("Creating symlink [{}] to [{}]", path, target); + if (!path.toFile().exists()) { + return Files.createSymbolicLink(path, target); + } + return null; + } + + /** + * Convenience method for the case when the link's file name should be the + * same as the file name of the target + */ + public static Path createSymlink(String dir, String targetDir, + String targetFilename) throws IOException { + return Utils.createSymlink(dir, targetDir, targetFilename, + targetFilename); + } + + /** + * Returns a Collection of file names found under the given directory. + * @param dir a directory + * @return the Collection of file names + */ + public static Collection<String> readDirContents(String dir) { + Collection<String> ret = new HashSet<>(); + File[] files = new File(dir).listFiles(); + if (files != null) { + for (File f: files) { + ret.add(f.getName()); + } + } + return ret; + } + + /** + * Returns the value of java.class.path System property. Kept separate for + * testing. + * @return the classpath + */ + public static String currentClasspath() { + return _instance.currentClasspathImpl(); + } + + public String currentClasspathImpl() { + return System.getProperty("java.class.path"); + } + + /** + * Returns a collection of jar file names found under the given directory. + * @param dir the directory to search + * @return the jar file names + */ + private static Collection<String> getFullJars(String dir) { + File[] files = new File(dir).listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.endsWith(".jar"); + } + }); + Collection<String> ret = new HashSet<String>(files.length); + for (File f : files) { + ret.add(Paths.get(dir, f.getName()).toString()); + } + return ret; + } + + public static String workerClasspath() { + String stormDir = System.getProperty("storm.home"); + String stormLibDir = Paths.get(stormDir, "lib").toString(); + String stormConfDir = + System.getenv("STORM_CONF_DIR") != null ? + System.getenv("STORM_CONF_DIR") : + Paths.get(stormDir, "conf").toString(); + String stormExtlibDir = Paths.get(stormDir, "extlib").toString(); + String extcp = System.getenv("STORM_EXT_CLASSPATH"); + if (stormDir == null) { + return Utils.currentClasspath(); + } + List<String> pathElements = new LinkedList<>(); + pathElements.addAll(Utils.getFullJars(stormLibDir)); + pathElements.addAll(Utils.getFullJars(stormExtlibDir)); + pathElements.add(extcp); + pathElements.add(stormConfDir); + + return StringUtils.join(pathElements, + System.getProperty("path.separator")); + } + + public static String addToClasspath(String classpath, + Collection<String> paths) { + return _instance.addToClasspathImpl(classpath, paths); + } + + public String addToClasspathImpl(String classpath, + Collection<String> paths) { + if (paths == null || paths.isEmpty()) { + return classpath; + } + List<String> l = new LinkedList<>(); + l.add(classpath); --- End diff -- In the previous code it would return "nil:a:b:c" now it will return "null:a:b" In this case I am not sure it makes a difference.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---