use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class ClassUtil method getClassPaths.
/**
* 获得ClassPath
*
* @param packageName 包名称
* @param isDecode 是否解码路径中的特殊字符(例如空格和中文)
* @return ClassPath路径字符串集合
* @since 4.0.11
*/
public static Set<String> getClassPaths(String packageName, boolean isDecode) {
String packagePath = packageName.replace(StrUtil.DOT, StrUtil.SLASH);
Enumeration<URL> resources;
try {
resources = getClassLoader().getResources(packagePath);
} catch (IOException e) {
throw new UtilException(e, "Loading classPath [{}] error!", packagePath);
}
final Set<String> paths = new HashSet<>();
String path;
while (resources.hasMoreElements()) {
path = resources.nextElement().getPath();
paths.add(isDecode ? URLUtil.decode(path, CharsetUtil.systemCharsetName()) : path);
}
return paths;
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class SyncFinisher method start.
/**
* 开始工作<br>
* 执行此方法后如果不再重复使用此对象,需调用{@link #stop()}关闭回收资源。
*
* @param sync 是否阻塞等待
* @since 4.5.8
*/
public void start(boolean sync) {
endLatch = new CountDownLatch(workers.size());
if (null == this.executorService || this.executorService.isShutdown()) {
this.executorService = ThreadUtil.newExecutor(threadSize);
}
for (Worker worker : workers) {
executorService.submit(worker);
}
// 保证所有worker同时开始
this.beginLatch.countDown();
if (sync) {
try {
this.endLatch.await();
} catch (InterruptedException e) {
throw new UtilException(e);
}
}
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class ClassLoaderUtil method loadClass.
/**
* 加载类,通过传入类的字符串,返回其对应的类名<br>
* 此方法支持缓存,第一次被加载的类之后会读取缓存中的类<br>
* 加载失败的原因可能是此类不存在或其关联引用类不存在<br>
* 扩展{@link Class#forName(String, boolean, ClassLoader)}方法,支持以下几类类名的加载:
*
* <pre>
* 1、原始类型,例如:int
* 2、数组类型,例如:int[]、Long[]、String[]
* 3、内部类,例如:java.lang.Thread.State会被转为java.lang.Thread$State加载
* </pre>
*
* @param name 类名
* @param classLoader {@link ClassLoader},{@code null} 则使用系统默认ClassLoader
* @param isInitialized 是否初始化类(调用static模块内容和初始化static属性)
* @return 类名对应的类
* @throws UtilException 包装{@link ClassNotFoundException},没有类名对应的类时抛出此异常
*/
public static Class<?> loadClass(String name, ClassLoader classLoader, boolean isInitialized) throws UtilException {
Assert.notNull(name, "Name must not be null");
// 加载原始类型和缓存中的类
Class<?> clazz = loadPrimitiveClass(name);
if (clazz == null) {
clazz = CLASS_CACHE.get(name);
}
if (clazz != null) {
return clazz;
}
if (name.endsWith(ARRAY_SUFFIX)) {
// 对象数组"java.lang.String[]"风格
final String elementClassName = name.substring(0, name.length() - ARRAY_SUFFIX.length());
final Class<?> elementClass = loadClass(elementClassName, classLoader, isInitialized);
clazz = Array.newInstance(elementClass, 0).getClass();
} else if (name.startsWith(NON_PRIMITIVE_ARRAY_PREFIX) && name.endsWith(";")) {
// "[Ljava.lang.String;" 风格
final String elementName = name.substring(NON_PRIMITIVE_ARRAY_PREFIX.length(), name.length() - 1);
final Class<?> elementClass = loadClass(elementName, classLoader, isInitialized);
clazz = Array.newInstance(elementClass, 0).getClass();
} else if (name.startsWith(INTERNAL_ARRAY_PREFIX)) {
// "[[I" 或 "[[Ljava.lang.String;" 风格
final String elementName = name.substring(INTERNAL_ARRAY_PREFIX.length());
final Class<?> elementClass = loadClass(elementName, classLoader, isInitialized);
clazz = Array.newInstance(elementClass, 0).getClass();
} else {
// 加载普通类
if (null == classLoader) {
classLoader = getClassLoader();
}
try {
clazz = Class.forName(name, isInitialized, classLoader);
} catch (ClassNotFoundException ex) {
// 尝试获取内部类,例如java.lang.Thread.State =》java.lang.Thread$State
clazz = tryLoadInnerClass(name, classLoader, isInitialized);
if (null == clazz) {
throw new UtilException(ex);
}
}
}
// 加入缓存并返回
return CLASS_CACHE.put(name, clazz);
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class JAXBUtil method beanToXml.
/**
* JavaBean转换成xml
*
* @param bean Bean对象
* @param charset 编码 eg: utf-8
* @param format 是否格式化输出eg: true
* @return 输出的XML字符串
*/
public static String beanToXml(Object bean, Charset charset, boolean format) {
StringWriter writer;
try {
JAXBContext context = JAXBContext.newInstance(bean.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
marshaller.setProperty(Marshaller.JAXB_ENCODING, charset.name());
writer = new StringWriter();
marshaller.marshal(bean, writer);
} catch (Exception e) {
throw new UtilException("convertToXml 错误:" + e.getMessage(), e);
}
return writer.toString();
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class Tailer method start.
/**
* 开始监听
*
* @param async 是否异步执行
*/
public void start(boolean async) {
// 初始读取
try {
this.readTail();
} catch (IOException e) {
throw new IORuntimeException(e);
}
final LineReadWatcher lineReadWatcher = new LineReadWatcher(this.randomAccessFile, this.charset, this.lineHandler);
final ScheduledFuture<?> scheduledFuture = //
this.executorService.scheduleAtFixedRate(//
lineReadWatcher, //
0, //
this.period, //
TimeUnit.MILLISECONDS);
if (false == async) {
try {
scheduledFuture.get();
} catch (ExecutionException e) {
throw new UtilException(e);
} catch (InterruptedException e) {
// ignore and exist
}
}
}
Aggregations