blob: 498f3b10ab22cc4c6af35ffe0037f1cfabcee4cb [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
tom5f38b3a2014-08-27 23:50:54 -070016package org.onlab.util;
17
Thomas Vachuska02aeb032015-01-06 22:36:30 -080018import static java.nio.file.Files.delete;
19import static java.nio.file.Files.walkFileTree;
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080020import static org.slf4j.LoggerFactory.getLogger;
21
tom53efab52014-10-07 17:43:48 -070022import java.io.BufferedReader;
23import java.io.File;
Ray Milkey705d9bc2014-11-18 08:19:00 -080024import java.io.FileInputStream;
tom53efab52014-10-07 17:43:48 -070025import java.io.IOException;
Ray Milkey705d9bc2014-11-18 08:19:00 -080026import java.io.InputStreamReader;
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080027import java.lang.Thread.UncaughtExceptionHandler;
Ray Milkey705d9bc2014-11-18 08:19:00 -080028import java.nio.charset.StandardCharsets;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080029import java.nio.file.FileVisitResult;
30import java.nio.file.Path;
31import java.nio.file.Paths;
32import java.nio.file.SimpleFileVisitor;
33import java.nio.file.attribute.BasicFileAttributes;
tom53efab52014-10-07 17:43:48 -070034import java.util.ArrayList;
35import java.util.List;
tom5f38b3a2014-08-27 23:50:54 -070036import java.util.concurrent.ThreadFactory;
37
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080038import org.slf4j.Logger;
39
Ray Milkey705d9bc2014-11-18 08:19:00 -080040import com.google.common.base.Strings;
41import com.google.common.primitives.UnsignedLongs;
42import com.google.common.util.concurrent.ThreadFactoryBuilder;
43
tom5f38b3a2014-08-27 23:50:54 -070044public abstract class Tools {
45
46 private Tools() {
47 }
48
Thomas Vachuska02aeb032015-01-06 22:36:30 -080049 private static final Logger log = getLogger(Tools.class);
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080050
tom5f38b3a2014-08-27 23:50:54 -070051 /**
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 HIGUCHI683e9782014-11-25 17:26:36 -080059 return new ThreadFactoryBuilder()
60 .setNameFormat(pattern)
Thomas Vachuska02aeb032015-01-06 22:36:30 -080061 // FIXME remove UncaughtExceptionHandler before release
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080062 .setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
63
64 @Override
65 public void uncaughtException(Thread t, Throwable e) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080066 log.error("Uncaught exception on {}", t.getName(), e);
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080067 }
68 }).build();
tom5f38b3a2014-08-27 23:50:54 -070069 }
70
tom782a7cf2014-09-11 23:58:38 -070071 /**
Yuta HIGUCHI06586272014-11-25 14:27:03 -080072 * 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 Vachuska02aeb032015-01-06 22:36:30 -080079 .setThreadFactory(factory)
80 .setPriority(Thread.MIN_PRIORITY)
81 .build();
Yuta HIGUCHI06586272014-11-25 14:27:03 -080082 }
83
84 /**
tom782a7cf2014-09-11 23:58:38 -070085 * 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 }
tomf110fff2014-09-26 00:38:18 -0700114
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
tom53efab52014-10-07 17:43:48 -0700128 /**
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 Milkey705d9bc2014-11-18 08:19:00 -0800135 try {
136 BufferedReader br = new BufferedReader(
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800137 new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
Ray Milkey705d9bc2014-11-18 08:19:00 -0800138
tom53efab52014-10-07 17:43:48 -0700139 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 Vachuska02aeb032015-01-06 22:36:30 -0800151
152 /**
153 * Purges the specified directory path.&nbsp;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.&nbsp;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
tom5f38b3a2014-08-27 23:50:54 -0700203}