Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 17 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame^] | 18 | import static java.nio.file.Files.delete; |
| 19 | import static java.nio.file.Files.walkFileTree; |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 22 | import java.io.BufferedReader; |
| 23 | import java.io.File; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 24 | import java.io.FileInputStream; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 25 | import java.io.IOException; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 26 | import java.io.InputStreamReader; |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 27 | import java.lang.Thread.UncaughtExceptionHandler; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 28 | import java.nio.charset.StandardCharsets; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame^] | 29 | import java.nio.file.FileVisitResult; |
| 30 | import java.nio.file.Path; |
| 31 | import java.nio.file.Paths; |
| 32 | import java.nio.file.SimpleFileVisitor; |
| 33 | import java.nio.file.attribute.BasicFileAttributes; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 36 | import java.util.concurrent.ThreadFactory; |
| 37 | |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 40 | import com.google.common.base.Strings; |
| 41 | import com.google.common.primitives.UnsignedLongs; |
| 42 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 43 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 44 | public abstract class Tools { |
| 45 | |
| 46 | private Tools() { |
| 47 | } |
| 48 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame^] | 49 | private static final Logger log = getLogger(Tools.class); |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 50 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Returns a thread factory that produces threads named according to the |
| 53 | * supplied name pattern. |
| 54 | * |
| 55 | * @param pattern name pattern |
| 56 | * @return thread factory |
| 57 | */ |
| 58 | public static ThreadFactory namedThreads(String pattern) { |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 59 | return new ThreadFactoryBuilder() |
| 60 | .setNameFormat(pattern) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame^] | 61 | // FIXME remove UncaughtExceptionHandler before release |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 62 | .setUncaughtExceptionHandler(new UncaughtExceptionHandler() { |
| 63 | |
| 64 | @Override |
| 65 | public void uncaughtException(Thread t, Throwable e) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame^] | 66 | log.error("Uncaught exception on {}", t.getName(), e); |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 67 | } |
| 68 | }).build(); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 69 | } |
| 70 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 71 | /** |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 72 | * Returns a thread factory that produces threads with MIN_PRIORITY. |
| 73 | * |
| 74 | * @param factory backing ThreadFactory |
| 75 | * @return thread factory |
| 76 | */ |
| 77 | public static ThreadFactory minPriority(ThreadFactory factory) { |
| 78 | return new ThreadFactoryBuilder() |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame^] | 79 | .setThreadFactory(factory) |
| 80 | .setPriority(Thread.MIN_PRIORITY) |
| 81 | .build(); |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 85 | * Converts a string from hex to long. |
| 86 | * |
| 87 | * @param string hex number in string form; sans 0x |
| 88 | * @return long value |
| 89 | */ |
| 90 | public static long fromHex(String string) { |
| 91 | return UnsignedLongs.parseUnsignedLong(string, 16); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Converts a long value to hex string; 16 wide and sans 0x. |
| 96 | * |
| 97 | * @param value long value |
| 98 | * @return hex string |
| 99 | */ |
| 100 | public static String toHex(long value) { |
| 101 | return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0'); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Converts a long value to hex string; 16 wide and sans 0x. |
| 106 | * |
| 107 | * @param value long value |
| 108 | * @param width string width; zero padded |
| 109 | * @return hex string |
| 110 | */ |
| 111 | public static String toHex(long value, int width) { |
| 112 | return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0'); |
| 113 | } |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Suspends the current thread for a specified number of millis. |
| 117 | * |
| 118 | * @param ms number of millis |
| 119 | */ |
| 120 | public static void delay(int ms) { |
| 121 | try { |
| 122 | Thread.sleep(ms); |
| 123 | } catch (InterruptedException e) { |
| 124 | throw new RuntimeException("Interrupted", e); |
| 125 | } |
| 126 | } |
| 127 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Slurps the contents of a file into a list of strings, one per line. |
| 130 | * |
| 131 | * @param path file path |
| 132 | * @return file contents |
| 133 | */ |
| 134 | public static List<String> slurp(File path) { |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 135 | try { |
| 136 | BufferedReader br = new BufferedReader( |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame^] | 137 | new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8)); |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 138 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 139 | List<String> lines = new ArrayList<>(); |
| 140 | String line; |
| 141 | while ((line = br.readLine()) != null) { |
| 142 | lines.add(line); |
| 143 | } |
| 144 | return lines; |
| 145 | |
| 146 | } catch (IOException e) { |
| 147 | return null; |
| 148 | } |
| 149 | } |
| 150 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame^] | 151 | |
| 152 | /** |
| 153 | * Purges the specified directory path. Use with great caution since |
| 154 | * no attempt is made to check for symbolic links, which could result in |
| 155 | * deletion of unintended files. |
| 156 | * |
| 157 | * @param path directory to be removed |
| 158 | * @throws java.io.IOException if unable to remove contents |
| 159 | */ |
| 160 | public static void removeDirectory(String path) throws IOException { |
| 161 | walkFileTree(Paths.get(path), new DirectoryDeleter()); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Purges the specified directory path. Use with great caution since |
| 166 | * no attempt is made to check for symbolic links, which could result in |
| 167 | * deletion of unintended files. |
| 168 | * |
| 169 | * @param dir directory to be removed |
| 170 | * @throws java.io.IOException if unable to remove contents |
| 171 | */ |
| 172 | public static void removeDirectory(File dir) throws IOException { |
| 173 | walkFileTree(Paths.get(dir.getAbsolutePath()), new DirectoryDeleter()); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | private static class DirectoryDeleter extends SimpleFileVisitor<Path> { |
| 178 | @Override |
| 179 | public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) |
| 180 | throws IOException { |
| 181 | if (attributes.isRegularFile()) { |
| 182 | delete(file); |
| 183 | } |
| 184 | return FileVisitResult.CONTINUE; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public FileVisitResult postVisitDirectory(Path directory, IOException ioe) |
| 189 | throws IOException { |
| 190 | delete(directory); |
| 191 | return FileVisitResult.CONTINUE; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public FileVisitResult visitFileFailed(Path file, IOException ioe) |
| 196 | throws IOException { |
| 197 | log.warn("Unable to delete file {}", file); |
| 198 | log.warn("Boom", ioe); |
| 199 | return FileVisitResult.CONTINUE; |
| 200 | } |
| 201 | } |
| 202 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 203 | } |