use of cn.hutool.core.lang.Tuple in project hutool by looly.
the class FormatCache method getInstance.
/**
* 使用 pattern, time zone and locale 获得对应的 格式化器
*
* @param pattern 非空日期格式,使用与 {@link java.text.SimpleDateFormat}相同格式
* @param timeZone 时区,默认当前时区
* @param locale 地区,默认使用当前地区
* @return 格式化器
* @throws IllegalArgumentException pattern 无效或{@code null}
*/
public F getInstance(final String pattern, TimeZone timeZone, Locale locale) {
Assert.notBlank(pattern, "pattern must not be blank");
if (timeZone == null) {
timeZone = TimeZone.getDefault();
}
if (locale == null) {
locale = Locale.getDefault();
}
final Tuple key = new Tuple(pattern, timeZone, locale);
F format = cInstanceCache.get(key);
if (format == null) {
format = createInstance(pattern, timeZone, locale);
final F previousValue = cInstanceCache.putIfAbsent(key, format);
if (previousValue != null) {
// another thread snuck in and did the same work
// we should return the instance that is in ConcurrentMap
format = previousValue;
}
}
return format;
}
use of cn.hutool.core.lang.Tuple in project hutool by looly.
the class FormatCache method getPatternForStyle.
/**
* <p>
* Gets a date/time format for the specified styles and locale.
* </p>
*
* @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT, null indicates no date in format
* @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT, null indicates no time in format
* @param locale The non-null locale of the desired format
* @return a localized standard date/time format
* @throws IllegalArgumentException if the Locale has no date/time pattern defined
*/
// package protected, for access from test code; do not make public or protected
static String getPatternForStyle(final Integer dateStyle, final Integer timeStyle, final Locale locale) {
final Tuple key = new Tuple(dateStyle, timeStyle, locale);
String pattern = C_DATE_TIME_INSTANCE_CACHE.get(key);
if (pattern == null) {
try {
DateFormat formatter;
if (dateStyle == null) {
formatter = DateFormat.getTimeInstance(timeStyle, locale);
} else if (timeStyle == null) {
formatter = DateFormat.getDateInstance(dateStyle, locale);
} else {
formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
}
pattern = ((SimpleDateFormat) formatter).toPattern();
final String previous = C_DATE_TIME_INSTANCE_CACHE.putIfAbsent(key, pattern);
if (previous != null) {
// even though it doesn't matter if another thread put the pattern
// it's still good practice to return the String instance that is
// actually in the ConcurrentMap
pattern = previous;
}
} catch (final ClassCastException ex) {
throw new IllegalArgumentException("No date time pattern for locale: " + locale);
}
}
return pattern;
}
use of cn.hutool.core.lang.Tuple in project hutool by dromara.
the class FormatCache method getPatternForStyle.
/**
* <p>
* Gets a date/time format for the specified styles and locale.
* </p>
*
* @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT, null indicates no date in format
* @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT, null indicates no time in format
* @param locale The non-null locale of the desired format
* @return a localized standard date/time format
* @throws IllegalArgumentException if the Locale has no date/time pattern defined
*/
// package protected, for access from test code; do not make public or protected
static String getPatternForStyle(final Integer dateStyle, final Integer timeStyle, final Locale locale) {
final Tuple key = new Tuple(dateStyle, timeStyle, locale);
String pattern = C_DATE_TIME_INSTANCE_CACHE.get(key);
if (pattern == null) {
try {
DateFormat formatter;
if (dateStyle == null) {
formatter = DateFormat.getTimeInstance(timeStyle, locale);
} else if (timeStyle == null) {
formatter = DateFormat.getDateInstance(dateStyle, locale);
} else {
formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
}
pattern = ((SimpleDateFormat) formatter).toPattern();
final String previous = C_DATE_TIME_INSTANCE_CACHE.putIfAbsent(key, pattern);
if (previous != null) {
// even though it doesn't matter if another thread put the pattern
// it's still good practice to return the String instance that is
// actually in the ConcurrentMap
pattern = previous;
}
} catch (final ClassCastException ex) {
throw new IllegalArgumentException("No date time pattern for locale: " + locale);
}
}
return pattern;
}
use of cn.hutool.core.lang.Tuple in project Jpom by dromara.
the class AgentWebSocketUpdateHandle method restart.
/**
* 重启
*
* @param session 回话
* @return 结果
*/
public String restart(Session session) {
String result = Const.UPGRADE_MSG;
try {
UploadFileModel uploadFile = UPLOAD_FILE_INFO.get(session.getId());
String filePath = uploadFile.getFilePath();
JsonMessage<Tuple> error = JpomManifest.checkJpomJar(filePath, Type.Agent);
if (error.getCode() != HttpStatus.HTTP_OK) {
return error.getMsg();
}
JpomManifest.releaseJar(filePath, uploadFile.getVersion());
JpomApplication.restart();
} catch (Exception e) {
result = "重启失败" + e.getMessage();
DefaultSystemLog.getLog().error("重启失败", e);
}
return result;
}
use of cn.hutool.core.lang.Tuple in project Jpom by dromara.
the class JpomManifest method checkJpomJar.
/**
* 检查是否为jpom包
*
* @param path 路径
* @param type 类型
* @param checkRepeat 是否检查版本重复
* @return 结果消息
* @see Type#getApplicationClass()
*/
public static JsonMessage<Tuple> checkJpomJar(String path, Type type, boolean checkRepeat) {
File jarFile = new File(path);
Tuple jarVersion = getJarVersion(jarFile);
if (jarVersion == null) {
return new JsonMessage<>(405, "jar 包文件不合法");
}
try (JarFile jarFile1 = new JarFile(jarFile)) {
// Manifest manifest = jarFile1.getManifest();
// Attributes attributes = manifest.getMainAttributes();
String mainClass = jarVersion.get(2);
if (mainClass == null) {
return new JsonMessage<>(405, "清单文件中没有找到对应的MainClass属性");
}
try (JarClassLoader jarClassLoader = JarClassLoader.load(jarFile)) {
jarClassLoader.loadClass(mainClass);
} catch (ClassNotFoundException notFound) {
return new JsonMessage<>(405, "中没有找到对应的MainClass:" + mainClass);
}
String applicationClass = type.getApplicationClass();
ZipEntry entry = jarFile1.getEntry(StrUtil.format("BOOT-INF/classes/{}.class", StrUtil.replace(applicationClass, ".", StrUtil.SLASH)));
if (entry == null) {
return new JsonMessage<>(405, "此包不是Jpom【" + type.name() + "】包");
}
String version = jarVersion.get(0);
String timeStamp = jarVersion.get(1);
String minVersion = jarVersion.get(4);
if (StrUtil.hasEmpty(version, timeStamp, minVersion)) {
return new JsonMessage<>(405, "此包没有版本号、打包时间、最小兼容版本");
}
if (checkRepeat) {
//
JpomManifest jpomManifest = JpomManifest.getInstance();
if (StrUtil.equals(version, jpomManifest.getVersion()) && StrUtil.equals(timeStamp, jpomManifest.getTimeStamp())) {
return new JsonMessage<>(405, "新包和正在运行的包一致");
}
if (StrUtil.compareVersion(jpomManifest.getVersion(), minVersion) < 0) {
return new JsonMessage<>(405, StrUtil.format("当前程序版本 {} 新版程序最低兼容 {} 不能直接升级", jpomManifest.getVersion(), minVersion));
}
}
} catch (Exception e) {
DefaultSystemLog.getLog().error("解析jar", e);
return new JsonMessage<>(500, " 解析错误:" + e.getMessage());
}
return new JsonMessage<>(200, "", jarVersion);
}
Aggregations