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 | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
| 19 | import com.google.common.primitives.UnsignedLongs; |
| 20 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 21 | import org.slf4j.Logger; |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 22 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 23 | import java.io.BufferedReader; |
| 24 | import java.io.File; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 25 | import java.io.FileInputStream; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 26 | import java.io.IOException; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 27 | import java.io.InputStreamReader; |
| 28 | import java.nio.charset.StandardCharsets; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 29 | import java.nio.file.FileVisitResult; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 30 | import java.nio.file.Files; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 31 | import java.nio.file.Path; |
| 32 | import java.nio.file.Paths; |
| 33 | import java.nio.file.SimpleFileVisitor; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 34 | import java.nio.file.StandardCopyOption; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 35 | import java.nio.file.attribute.BasicFileAttributes; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 37 | import java.util.Collection; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 38 | import java.util.Dictionary; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 39 | import java.util.List; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 40 | import java.util.concurrent.ThreadFactory; |
| 41 | |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 42 | import static java.nio.file.Files.delete; |
| 43 | import static java.nio.file.Files.walkFileTree; |
| 44 | import static org.onlab.util.GroupedThreadFactory.groupedThreadFactory; |
| 45 | import static org.slf4j.LoggerFactory.getLogger; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 46 | |
Thomas Vachuska | c13b90a | 2015-02-18 18:19:55 -0800 | [diff] [blame] | 47 | /** |
| 48 | * Miscellaneous utility methods. |
| 49 | */ |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 50 | public abstract class Tools { |
| 51 | |
| 52 | private Tools() { |
| 53 | } |
| 54 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 55 | private static final Logger log = getLogger(Tools.class); |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 56 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Returns a thread factory that produces threads named according to the |
| 59 | * supplied name pattern. |
| 60 | * |
| 61 | * @param pattern name pattern |
| 62 | * @return thread factory |
| 63 | */ |
| 64 | public static ThreadFactory namedThreads(String pattern) { |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 65 | return new ThreadFactoryBuilder() |
| 66 | .setNameFormat(pattern) |
Thomas Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 67 | .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e)) |
| 68 | .build(); |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 69 | } |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 70 | |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 71 | /** |
| 72 | * Returns a thread factory that produces threads named according to the |
| 73 | * supplied name pattern and from the specified thread-group. The thread |
| 74 | * group name is expected to be specified in slash-delimited format, e.g. |
Thomas Vachuska | c13b90a | 2015-02-18 18:19:55 -0800 | [diff] [blame] | 75 | * {@code onos/intent}. The thread names will be produced by converting |
| 76 | * the thread group name into dash-delimited format and pre-pended to the |
| 77 | * specified pattern. |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 78 | * |
| 79 | * @param groupName group name in slash-delimited format to indicate hierarchy |
| 80 | * @param pattern name pattern |
| 81 | * @return thread factory |
| 82 | */ |
| 83 | public static ThreadFactory groupedThreads(String groupName, String pattern) { |
| 84 | return new ThreadFactoryBuilder() |
| 85 | .setThreadFactory(groupedThreadFactory(groupName)) |
Thomas Vachuska | c13b90a | 2015-02-18 18:19:55 -0800 | [diff] [blame] | 86 | .setNameFormat(groupName.replace(GroupedThreadFactory.DELIMITER, "-") + "-" + pattern) |
Thomas Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 87 | .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e)) |
| 88 | .build(); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 89 | } |
| 90 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 91 | /** |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 92 | * Returns a thread factory that produces threads with MIN_PRIORITY. |
| 93 | * |
| 94 | * @param factory backing ThreadFactory |
| 95 | * @return thread factory |
| 96 | */ |
| 97 | public static ThreadFactory minPriority(ThreadFactory factory) { |
| 98 | return new ThreadFactoryBuilder() |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 99 | .setThreadFactory(factory) |
| 100 | .setPriority(Thread.MIN_PRIORITY) |
| 101 | .build(); |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 105 | * Returns true if the collection is null or is empty. |
| 106 | * |
| 107 | * @param collection collection to test |
| 108 | * @return true if null or empty; false otherwise |
| 109 | */ |
| 110 | public static boolean isNullOrEmpty(Collection collection) { |
| 111 | return collection == null || collection.isEmpty(); |
| 112 | } |
| 113 | |
| 114 | /** |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 115 | * Converts a string from hex to long. |
| 116 | * |
| 117 | * @param string hex number in string form; sans 0x |
| 118 | * @return long value |
| 119 | */ |
| 120 | public static long fromHex(String string) { |
| 121 | return UnsignedLongs.parseUnsignedLong(string, 16); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Converts a long value to hex string; 16 wide and sans 0x. |
| 126 | * |
| 127 | * @param value long value |
| 128 | * @return hex string |
| 129 | */ |
| 130 | public static String toHex(long value) { |
| 131 | return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0'); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Converts a long value to hex string; 16 wide and sans 0x. |
| 136 | * |
| 137 | * @param value long value |
| 138 | * @param width string width; zero padded |
| 139 | * @return hex string |
| 140 | */ |
| 141 | public static String toHex(long value, int width) { |
| 142 | return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0'); |
| 143 | } |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 146 | * Get property as a string value. |
| 147 | * |
| 148 | * @param properties properties to be looked up |
| 149 | * @param propertyName the name of the property to look up |
| 150 | * @return value when the propertyName is defined or return null |
| 151 | */ |
| 152 | public static String get(Dictionary<?, ?> properties, String propertyName) { |
| 153 | Object v = properties.get(propertyName); |
| 154 | String s = (v instanceof String) ? (String) v : |
| 155 | v != null ? v.toString() : null; |
| 156 | return Strings.isNullOrEmpty(s) ? null : s.trim(); |
| 157 | } |
| 158 | |
| 159 | /** |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 160 | * Suspends the current thread for a specified number of millis. |
| 161 | * |
| 162 | * @param ms number of millis |
| 163 | */ |
| 164 | public static void delay(int ms) { |
| 165 | try { |
| 166 | Thread.sleep(ms); |
| 167 | } catch (InterruptedException e) { |
| 168 | throw new RuntimeException("Interrupted", e); |
| 169 | } |
| 170 | } |
| 171 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 172 | /** |
| 173 | * Slurps the contents of a file into a list of strings, one per line. |
| 174 | * |
| 175 | * @param path file path |
| 176 | * @return file contents |
| 177 | */ |
| 178 | public static List<String> slurp(File path) { |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 179 | try { |
| 180 | BufferedReader br = new BufferedReader( |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 181 | new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8)); |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 182 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 183 | List<String> lines = new ArrayList<>(); |
| 184 | String line; |
| 185 | while ((line = br.readLine()) != null) { |
| 186 | lines.add(line); |
| 187 | } |
| 188 | return lines; |
| 189 | |
| 190 | } catch (IOException e) { |
| 191 | return null; |
| 192 | } |
| 193 | } |
| 194 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Purges the specified directory path. Use with great caution since |
| 198 | * no attempt is made to check for symbolic links, which could result in |
| 199 | * deletion of unintended files. |
| 200 | * |
| 201 | * @param path directory to be removed |
| 202 | * @throws java.io.IOException if unable to remove contents |
| 203 | */ |
| 204 | public static void removeDirectory(String path) throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 205 | DirectoryDeleter visitor = new DirectoryDeleter(); |
| 206 | walkFileTree(Paths.get(path), visitor); |
| 207 | if (visitor.exception != null) { |
| 208 | throw visitor.exception; |
| 209 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Purges the specified directory path. Use with great caution since |
| 214 | * no attempt is made to check for symbolic links, which could result in |
| 215 | * deletion of unintended files. |
| 216 | * |
| 217 | * @param dir directory to be removed |
| 218 | * @throws java.io.IOException if unable to remove contents |
| 219 | */ |
| 220 | public static void removeDirectory(File dir) throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 221 | DirectoryDeleter visitor = new DirectoryDeleter(); |
| 222 | walkFileTree(Paths.get(dir.getAbsolutePath()), visitor); |
| 223 | if (visitor.exception != null) { |
| 224 | throw visitor.exception; |
| 225 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 228 | // Auxiliary path visitor for recursive directory structure removal. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 229 | private static class DirectoryDeleter extends SimpleFileVisitor<Path> { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 230 | |
| 231 | private IOException exception; |
| 232 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 233 | @Override |
| 234 | public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) |
| 235 | throws IOException { |
| 236 | if (attributes.isRegularFile()) { |
| 237 | delete(file); |
| 238 | } |
| 239 | return FileVisitResult.CONTINUE; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public FileVisitResult postVisitDirectory(Path directory, IOException ioe) |
| 244 | throws IOException { |
| 245 | delete(directory); |
| 246 | return FileVisitResult.CONTINUE; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public FileVisitResult visitFileFailed(Path file, IOException ioe) |
| 251 | throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 252 | this.exception = ioe; |
| 253 | return FileVisitResult.TERMINATE; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 257 | /** |
| 258 | * Returns a human friendly time ago string for a specified system time. |
Thomas Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 259 | * |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 260 | * @param unixTime system time in millis |
| 261 | * @return human friendly time ago |
| 262 | */ |
| 263 | public static String timeAgo(long unixTime) { |
| 264 | long deltaMillis = System.currentTimeMillis() - unixTime; |
| 265 | long secondsSince = (long) (deltaMillis / 1000.0); |
| 266 | long minsSince = (long) (deltaMillis / (1000.0 * 60)); |
| 267 | long hoursSince = (long) (deltaMillis / (1000.0 * 60 * 60)); |
| 268 | long daysSince = (long) (deltaMillis / (1000.0 * 60 * 60 * 24)); |
| 269 | if (daysSince > 0) { |
| 270 | return String.format("%dd ago", daysSince); |
| 271 | } else if (hoursSince > 0) { |
| 272 | return String.format("%dh ago", hoursSince); |
| 273 | } else if (minsSince > 0) { |
| 274 | return String.format("%dm ago", minsSince); |
| 275 | } else if (secondsSince > 0) { |
| 276 | return String.format("%ds ago", secondsSince); |
| 277 | } else { |
| 278 | return "just now"; |
| 279 | } |
| 280 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 281 | |
| 282 | /** |
| 283 | * Copies the specified directory path. Use with great caution since |
| 284 | * no attempt is made to check for symbolic links, which could result in |
| 285 | * copy of unintended files. |
| 286 | * |
| 287 | * @param src directory to be copied |
| 288 | * @param dst destination directory to be removed |
| 289 | * @throws java.io.IOException if unable to remove contents |
| 290 | */ |
| 291 | public static void copyDirectory(String src, String dst) throws IOException { |
| 292 | walkFileTree(Paths.get(src), new DirectoryCopier(src, dst)); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Copies the specified directory path. Use with great caution since |
| 297 | * no attempt is made to check for symbolic links, which could result in |
| 298 | * copy of unintended files. |
| 299 | * |
| 300 | * @param src directory to be copied |
| 301 | * @param dst destination directory to be removed |
| 302 | * @throws java.io.IOException if unable to remove contents |
| 303 | */ |
| 304 | public static void copyDirectory(File src, File dst) throws IOException { |
| 305 | walkFileTree(Paths.get(src.getAbsolutePath()), |
| 306 | new DirectoryCopier(src.getAbsolutePath(), |
| 307 | dst.getAbsolutePath())); |
| 308 | } |
| 309 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 310 | // Auxiliary path visitor for recursive directory structure copying. |
| 311 | private static class DirectoryCopier extends SimpleFileVisitor<Path> { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 312 | private Path src; |
| 313 | private Path dst; |
| 314 | private StandardCopyOption copyOption = StandardCopyOption.REPLACE_EXISTING; |
| 315 | |
| 316 | DirectoryCopier(String src, String dst) { |
| 317 | this.src = Paths.get(src); |
| 318 | this.dst = Paths.get(dst); |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 323 | Path targetPath = dst.resolve(src.relativize(dir)); |
| 324 | if (!Files.exists(targetPath)) { |
| 325 | Files.createDirectory(targetPath); |
| 326 | } |
| 327 | return FileVisitResult.CONTINUE; |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 332 | Files.copy(file, dst.resolve(src.relativize(file)), copyOption); |
| 333 | return FileVisitResult.CONTINUE; |
| 334 | } |
| 335 | } |
| 336 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 337 | } |