Skip to content

Commit

Permalink
Merge pull request #33 from lodsve/2.7.x
Browse files Browse the repository at this point in the history
2.7.x
  • Loading branch information
sunhao-java authored Jan 1, 2019
2 parents 3c28878 + 550a12a commit a79a91a
Show file tree
Hide file tree
Showing 60 changed files with 509 additions and 228 deletions.
2 changes: 1 addition & 1 deletion lodsve-3rd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.lodsve</groupId>
<artifactId>lodsve-framework</artifactId>
<version>2.7.1-RELEASE</version>
<version>2.7.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion lodsve-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>lodsve-framework</artifactId>
<groupId>com.lodsve</groupId>
<version>2.7.1-RELEASE</version>
<version>2.7.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion lodsve-amqp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.lodsve</groupId>
<artifactId>lodsve-framework</artifactId>
<version>2.7.1-RELEASE</version>
<version>2.7.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion lodsve-cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.lodsve</groupId>
<artifactId>lodsve-framework</artifactId>
<version>2.7.1-RELEASE</version>
<version>2.7.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.opensymphony.oscache.general.GeneralCacheAdministrator;
import lodsve.cache.properties.CacheProperties;
import lodsve.cache.properties.OscahceConfig;
import lodsve.cache.properties.OscacheConfig;
import lodsve.core.properties.relaxedbind.annotations.EnableConfigurationProperties;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cache.CacheManager;
Expand Down Expand Up @@ -52,7 +52,7 @@ public OscacheCacheConfiguration(ObjectProvider<CacheProperties> cacheProperties

@Bean
public CacheManager cacheManager(GeneralCacheAdministrator cacheAdministrator) {
OscahceConfig oscahce = cacheProperties.getOscahce();
OscacheConfig oscahce = cacheProperties.getOscache();

OscacheCacheManager cacheManager = new OscacheCacheManager();
cacheManager.setAdmin(cacheAdministrator);
Expand All @@ -63,7 +63,7 @@ public CacheManager cacheManager(GeneralCacheAdministrator cacheAdministrator) {

@Bean
public GeneralCacheAdministrator cacheAdministrator() throws IOException {
OscahceConfig oscahce = cacheProperties.getOscahce();
OscacheConfig oscahce = cacheProperties.getOscache();
Resource resource = oscahce.getConfiguration();
if (resource == null || !resource.exists()) {
resource = new ClassPathResource("/META-INF/oscache.properties", Thread.currentThread().getContextClassLoader());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ public class CacheProperties {
private EhcacheConfig ehcache = new EhcacheConfig();
private RedisConfig redis = new RedisConfig();
private MemcachedConfig memcached = new MemcachedConfig();
private OscahceConfig oscahce = new OscahceConfig();
private OscacheConfig oscache = new OscacheConfig();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
@Setter
@Getter
public class OscahceConfig {
public class OscacheConfig {
private Resource configuration;
private OscacheMemcachedCache[] cache = new OscacheMemcachedCache[]{new OscacheMemcachedCache()};

Expand Down
2 changes: 1 addition & 1 deletion lodsve-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>com.lodsve</groupId>
<artifactId>lodsve-framework</artifactId>
<version>2.7.1-RELEASE</version>
<version>2.7.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
27 changes: 27 additions & 0 deletions lodsve-core/src/main/java/lodsve/core/LodsveBannerPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package lodsve.core;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import lodsve.core.configuration.ApplicationProperties;
import lodsve.core.configuration.BannerConfig;
import lodsve.core.configuration.BannerMode;
Expand All @@ -38,7 +40,9 @@
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 模仿spring-boot打印出banner.
Expand All @@ -53,11 +57,17 @@ public class LodsveBannerPrinter implements WebApplicationInitializer {
private static final Banner DEFAULT_BANNER = new LodsveBanner();
private static final String[] IMAGE_EXTENSION = {"gif", "jpg", "png"};
private static final ResourceLoader RESOURCE_LOADER = new LodsveResourceLoader();
/**
* 缓存日志级别
*/
private static final Map<String, Level> LEVEL_CACHE = new HashMap<>();

private BannerConfig bannerConfig;

@Override
public void onStartup(@NonNull ServletContext servletContext) throws ServletException {
closeLogback();

ApplicationProperties properties = new RelaxedBindFactory.Builder<>(ApplicationProperties.class).build();
bannerConfig = properties.getBanner();

Expand All @@ -78,6 +88,23 @@ public void onStartup(@NonNull ServletContext servletContext) throws ServletExce
}

printBannerInConsole(banner);

openLogback();
}

private void openLogback() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
List<ch.qos.logback.classic.Logger> loggers = loggerContext.getLoggerList();
loggers.forEach(l -> l.setLevel(LEVEL_CACHE.get(l.getName())));
}

private void closeLogback() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
List<ch.qos.logback.classic.Logger> loggers = loggerContext.getLoggerList();
loggers.forEach(l -> {
LEVEL_CACHE.put(l.getName(), l.getLevel());
l.setLevel(Level.OFF);
});
}

private void printInLogger(Banner banner) {
Expand Down
125 changes: 108 additions & 17 deletions lodsve-core/src/main/java/lodsve/core/ansi/AnsiOutput.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,87 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright (C) 2018 Sun.Hao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* http://www.apache.org/licenses/LICENSE-2.0
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package lodsve.core.ansi;

import org.springframework.util.Assert;

import java.util.Locale;

/**
* Generates ANSI encoded output, automatically attempting to detect if the terminal
* supports ANSI.
*
* @author Phillip Webb
*/
public class AnsiOutput {
public abstract class AnsiOutput {

private static final String ENCODE_JOIN = ";";

private static Enabled enabled = Enabled.DETECT;

private static Boolean consoleAvailable;

private static Boolean ansiCapable;

private static final String OPERATING_SYSTEM_NAME = System.getProperty("os.name")
.toLowerCase(Locale.ENGLISH);

private static final String ENCODE_START = "\033[";

private static final String ENCODE_END = "m";

private static final String RESET = "0;" + AnsiColor.DEFAULT;
private static final String OPERATING_SYSTEM_NAME = System.getProperty("os.name").toLowerCase();

/**
* Sets if ANSI output is enabled.
*
* @param enabled if ANSI is enabled, disabled or detected
*/
public static void setEnabled(Enabled enabled) {
Assert.notNull(enabled, "Enabled must not be null");
AnsiOutput.enabled = enabled;
}

/**
* Sets if the System.console() is known to be available.
*
* @param consoleAvailable if the console is known to be available or {@code null} to
* use standard detection logic.
*/
public static void setConsoleAvailable(Boolean consoleAvailable) {
AnsiOutput.consoleAvailable = consoleAvailable;
}

static Enabled getEnabled() {
return AnsiOutput.enabled;
}

/**
* Encode a single {@link AnsiElement} if output is enabled.
*
* @param element the element to encode
* @return the encoded element or an empty string
*/
public static String encode(AnsiElement element) {
if (isEnabled()) {
return ENCODE_START + element + ENCODE_END;
}
return "";
}

/**
* Create a new ANSI string from the specified elements. Any {@link AnsiElement}s will
Expand Down Expand Up @@ -83,13 +136,51 @@ private static void buildDisabled(StringBuilder sb, Object[] elements) {
}

private static boolean isEnabled() {
return (System.console() == null) && !(OPERATING_SYSTEM_NAME.contains("win"));
if (enabled == Enabled.DETECT) {
if (ansiCapable == null) {
ansiCapable = detectIfAnsiCapable();
}
return ansiCapable;
}
return enabled == Enabled.ALWAYS;
}

public static String encode(AnsiElement element) {
if (isEnabled()) {
return ENCODE_START + element + ENCODE_END;
private static boolean detectIfAnsiCapable() {
try {
if (Boolean.FALSE.equals(consoleAvailable)) {
return false;
}
if ((consoleAvailable == null) && (System.console() == null)) {
return false;
}
return !(OPERATING_SYSTEM_NAME.contains("win"));
} catch (Throwable ex) {
return false;
}
return "";
}

/**
* Possible values to pass to {@link AnsiOutput#setEnabled}. Determines when to output
* ANSI escape sequences for coloring application output.
*/
public enum Enabled {

/**
* Try to detect whether ANSI coloring capabilities are available. The default
* value for {@link AnsiOutput}.
*/
DETECT,

/**
* Enable ANSI-colored output.
*/
ALWAYS,

/**
* Disable ANSI-colored output.
*/
NEVER

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package lodsve.core.configuration;

import lodsve.core.ansi.AnsiOutput;
import lombok.Getter;
import lombok.Setter;

/**
* 控制台打印参数配置.
*
* @author <a href="mailto:sunhao.java@gmail.com">sunhao(sunhao.java@gmail.com)</a>
*/
@Getter
@Setter
public class AnsiConfig {
/**
* 是否启用
*/
private AnsiOutput.Enabled enabled = AnsiOutput.Enabled.ALWAYS;
/**
* 控制台是否支持
*/
private Boolean consoleAvailable = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public class ApplicationProperties {
* 多线程配置
*/
private ThreadConfig thread;
/**
* 控制台打印参数配置
*/
private AnsiConfig ansi;
/**
* 日志配置文件
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class LogbackConfig {
*/
private String logFile;
/**
* 控制打印格式化字符串
* 控制台打印格式化字符串
*/
private String consoleLogPattern;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

package lodsve.core.properties;

import lodsve.core.ansi.AnsiOutput;
import lodsve.core.configuration.ApplicationProperties;
import lodsve.core.properties.env.EnvLoader;
import lodsve.core.properties.ini.IniLoader;
import lodsve.core.properties.profile.ProfileInitializer;
import lodsve.core.properties.relaxedbind.RelaxedBindFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.lang.NonNull;
Expand Down Expand Up @@ -80,6 +83,13 @@ public void onStartup(@NonNull ServletContext servletContext) throws ServletExce
EnvLoader.init();
IniLoader.init();

// 初始化ApplicationProperties
ApplicationProperties properties = new RelaxedBindFactory.Builder<>(ApplicationProperties.class).build();
AnsiOutput.Enabled enabled = properties.getAnsi().getEnabled();
Boolean consoleAvailable = properties.getAnsi().getConsoleAvailable();
AnsiOutput.setEnabled(enabled);
AnsiOutput.setConsoleAvailable(consoleAvailable);

servletContext.setInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, ProfileInitializer.class.getName());
}
}
Loading

0 comments on commit a79a91a

Please sign in to comment.