Author: stevel Date: Mon May 19 19:08:56 2014 New Revision: 1595993 URL: http://svn.apache.org/r1595993 Log: IDE review of tools package
Modified: incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/ConfigHelper.java incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/CoreFileSystem.java incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java Modified: incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/ConfigHelper.java URL: http://svn.apache.org/viewvc/incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/ConfigHelper.java?rev=1595993&r1=1595992&r2=1595993&view=diff ============================================================================== --- incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/ConfigHelper.java (original) +++ incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/ConfigHelper.java Mon May 19 19:08:56 2014 @@ -77,10 +77,11 @@ public class ConfigHelper { /** * Take a configuration and return a sorted set * @param conf config - * @return + * @return the sorted keyset + */ public static TreeSet<String> sortedConfigKeys(Iterable<Map.Entry<String, String>> conf) { - TreeSet<String> sorted = new TreeSet<String>(); + TreeSet<String> sorted = new TreeSet<>(); for (Map.Entry<String, String> entry : conf) { sorted.add(entry.getKey()); } @@ -183,28 +184,19 @@ public class ConfigHelper { IOException { int len = (int) fs.getLength(path); byte[] data = new byte[len]; - FSDataInputStream in = fs.open(path); - try { + try(FSDataInputStream in = fs.open(path)) { in.readFully(0, data); - } catch (IOException e) { - in.close(); } - ByteArrayInputStream in2; //this is here to track down a parse issue //related to configurations String s = new String(data, 0, len); log.debug("XML resource {} is \"{}\"", path, s); - in2 = new ByteArrayInputStream(data); - try { + try (ByteArrayInputStream in = new ByteArrayInputStream(data)) { Document document = parseConfigXML(in); return document; - } catch (ParserConfigurationException e) { - throw new IOException(e); - } catch (SAXException e) { + } catch (ParserConfigurationException | SAXException e) { throw new IOException(e); - } finally { - in2.close(); } } @@ -222,11 +214,8 @@ public class ConfigHelper { IOException { int len = (int) fs.getLength(path); byte[] data = new byte[len]; - FSDataInputStream in = fs.open(path); - try { + try (FSDataInputStream in = fs.open(path)) { in.readFully(0, data); - } catch (IOException e) { - in.close(); } ByteArrayInputStream in2; @@ -302,11 +291,26 @@ public class ConfigHelper { */ public static Configuration loadConfFromFile(File file) throws IOException { + return loadConfFromFile(file, false); + } + + /** + * + * Load a Hadoop configuration from a local file. + * @param file file to load + * @param loadDefaults flag to indicate if the defaults should be loaded yet + * @return a configuration which hasn't actually had the load triggered + * yet. + * @throws FileNotFoundException file is not there + * @throws IOException any other IO problem + */ + public static Configuration loadConfFromFile(File file, + boolean loadDefaults) throws IOException { if (!file.exists()) { throw new FileNotFoundException("File not found :" + file.getAbsoluteFile()); } - Configuration conf = new Configuration(false); + Configuration conf = new Configuration(loadDefaults); try { conf.addResource(file.toURI().toURL()); } catch (MalformedURLException e) { @@ -357,7 +361,7 @@ public class ConfigHelper { Path templatePath, String fallbackResource) throws IOException { - Configuration conf = null; + Configuration conf; String origin; if (fs.exists(templatePath)) { log.debug("Loading template configuration {}", templatePath); @@ -375,8 +379,8 @@ public class ConfigHelper { } //force a get conf.get(SliderXmlConfKeys.KEY_TEMPLATE_ORIGIN); - conf.set(SliderXmlConfKeys.KEY_TEMPLATE_ORIGIN, origin); //now set the origin + conf.set(SliderXmlConfKeys.KEY_TEMPLATE_ORIGIN, origin); return conf; } @@ -507,7 +511,7 @@ public class ConfigHelper { * @return hash map */ public static Map<String, String> buildMapFromConfiguration(Configuration conf) { - Map<String, String> map = new HashMap<String, String>(); + Map<String, String> map = new HashMap<>(); return SliderUtils.mergeEntries(map, conf); } @@ -526,7 +530,8 @@ public class ConfigHelper { for (Map.Entry<String, String> entry : keysource) { String key = entry.getKey(); String value = valuesource.get(key); - Preconditions.checkState(value!=null, "no reference for \"%s\" in values", key); + Preconditions.checkState(value != null, + "no reference for \"%s\" in values", key); result.set(key, value); } return result; Modified: incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/CoreFileSystem.java URL: http://svn.apache.org/viewvc/incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/CoreFileSystem.java?rev=1595993&r1=1595992&r2=1595993&view=diff ============================================================================== --- incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/CoreFileSystem.java (original) +++ incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/CoreFileSystem.java Mon May 19 19:08:56 2014 @@ -113,9 +113,7 @@ public class CoreFileSystem { * @return the path for persistent data */ public Path buildClusterDirPath(String clustername) { - if (clustername == null) { - throw new NullPointerException(); - } + Preconditions.checkNotNull(clustername); Path path = getBaseApplicationPath(); return new Path(path, SliderKeys.CLUSTER_DIRECTORY + "/" + clustername); } @@ -147,8 +145,7 @@ public class CoreFileSystem { * to ensure that it is there. * * @param instancePaths instance paths - * @return the path to the cluster directory - * @throws java.io.IOException trouble + * @throws IOException trouble * @throws SliderException slider-specific exceptions */ public void createClusterDirectories(InstancePaths instancePaths) throws @@ -238,7 +235,6 @@ public class CoreFileSystem { * Verify that the given directory is not present * * @param clusterDirectory actual directory to look for - * @return the path to the cluster directory * @throws IOException trouble with FS * @throws SliderException If the directory exists */ @@ -269,7 +265,7 @@ public class CoreFileSystem { verifyPathExists(dirPath); Path tempFile = new Path(dirPath, "tmp-file-for-checks"); try { - FSDataOutputStream out = null; + FSDataOutputStream out ; out = fileSystem.create(tempFile, true); IOUtils.closeStream(out); fileSystem.delete(tempFile, false); @@ -411,7 +407,7 @@ public class CoreFileSystem { //copied to the destination FileStatus[] fileset = fileSystem.listStatus(srcDir); Map<String, LocalResource> localResources = - new HashMap<String, LocalResource>(fileset.length); + new HashMap<>(fileset.length); for (FileStatus entry : fileset) { LocalResource resource = createAmResource(entry.getPath(), Modified: incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java URL: http://svn.apache.org/viewvc/incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java?rev=1595993&r1=1595992&r2=1595993&view=diff ============================================================================== --- incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java (original) +++ incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java Mon May 19 19:08:56 2014 @@ -123,6 +123,7 @@ public final class SliderUtils { * @param num * @param msg the message to be shown in exception */ + @SuppressWarnings("ResultOfMethodCallIgnored") private static void validateNumber(String num, String msg) throws BadConfigException { try { Integer.parseInt(num); @@ -188,7 +189,7 @@ public final class SliderUtils { /** * Find a containing JAR - * @param my_class class to find + * @param clazz class to find * @return the file * @throws IOException any IO problem, including the class not having a * classloader @@ -222,8 +223,8 @@ public final class SliderUtils { throw new IOException("Unable to find resources for class " + my_class); } - for (Enumeration itr = urlEnumeration; itr.hasMoreElements(); ) { - URL url = (URL) itr.nextElement(); + for (; urlEnumeration.hasMoreElements(); ) { + URL url = urlEnumeration.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { @@ -429,7 +430,7 @@ public final class SliderUtils { * @return a stringified list */ public static List<String> collectionToStringList(Collection c) { - List<String> l = new ArrayList<String>(c.size()); + List<String> l = new ArrayList<>(c.size()); for (Object o : c) { l.add(o.toString()); } @@ -526,7 +527,7 @@ public final class SliderUtils { public static String appReportToString(ApplicationReport r, String separator) { StringBuilder builder = new StringBuilder(512); builder.append("application ").append( - r.getName()).append("/").append(r.getApplicationType()).append(separator); + r.getName()).append("/").append(r.getApplicationType()).append(separator); Set<String> tags = r.getApplicationTags(); if (!tags.isEmpty()) { for (String tag : tags) { @@ -710,7 +711,7 @@ public final class SliderUtils { /** * probe to see if the address - * @param address + * @param address network address * @return true if the scheduler address is set to * something other than 0.0.0.0 */ @@ -820,7 +821,7 @@ public final class SliderUtils { * @return a possibly empty map of environment variables. */ public static Map<String, String> buildEnvMap(Map<String, String> roleOpts) { - Map<String, String> env = new HashMap<String, String>(); + Map<String, String> env = new HashMap<>(); if (roleOpts != null) { for (Map.Entry<String, String> entry: roleOpts.entrySet()) { String key = entry.getKey(); @@ -847,7 +848,7 @@ public final class SliderUtils { Map<String, String> optionMap = entry.getValue(); Map<String, String> existingMap = clusterRoleMap.get(key); if (existingMap == null) { - existingMap = new HashMap<String, String>(); + existingMap = new HashMap<>(); } log.debug("Overwriting role options with command line values {}", stringifyMap(optionMap)); @@ -1011,8 +1012,7 @@ public final class SliderUtils { } public static Map<String, Map<String, String>> deepClone(Map<String, Map<String, String>> src) { - Map<String, Map<String, String>> dest = - new HashMap<String, Map<String, String>>(); + Map<String, Map<String, String>> dest = new HashMap<>(); for (Map.Entry<String, Map<String, String>> entry : src.entrySet()) { dest.put(entry.getKey(), stringMapClone(entry.getValue())); } @@ -1020,7 +1020,7 @@ public final class SliderUtils { } public static Map<String, String> stringMapClone(Map<String, String> src) { - Map<String, String> dest = new HashMap<String, String>(); + Map<String, String> dest = new HashMap<>(); return mergeEntries(dest, src.entrySet()); } @@ -1249,7 +1249,7 @@ public final class SliderUtils { /** * Add the cluster build information; this will include Hadoop details too - * @param cd cluster + * @param info cluster info * @param prefix prefix for the build info */ public static void addBuildInfo(Map<String, String> info, String prefix) {