Skip to content

Commit

Permalink
Adapt to SOFAArk (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongZhang authored and straybirdzls committed Jan 18, 2019
1 parent 3fb1315 commit 3876eef
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.alipay.sofa.common.log.env;

import com.alipay.sofa.common.utils.*;
import org.apache.logging.log4j.util.Strings;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -155,7 +154,7 @@ public static Map<String, String> processGlobalSystemLogProperties() {
}

public static String getLogConfEnvSuffix(String spaceName) {
String logEnvConfig = System.getProperty(LOG_ENV_SUFFIX, Strings.EMPTY);
String logEnvConfig = System.getProperty(LOG_ENV_SUFFIX, StringUtil.EMPTY_STRING);
String[] spaceNameToEnvSuffix = logEnvConfig.split("&");
String suffix = null;
for (int i = 0; i < spaceNameToEnvSuffix.length && suffix == null; ++i) {
Expand All @@ -173,7 +172,7 @@ public static String getLogConfEnvSuffix(String spaceName) {
suffix = conf[1];
}

suffix = (suffix == null) ? Strings.EMPTY : suffix;
suffix = (suffix == null) ? StringUtil.EMPTY_STRING : suffix;
if (!suffix.isEmpty()) {
ReportUtil.reportDebug(spaceName + " log configuration: " + LOG_XML_CONFIG_FILE_NAME
+ suffix);
Expand All @@ -200,8 +199,7 @@ public static void keepCompatible(Map<String, String> context, boolean keep) {
}

public static boolean isLogStarterExist() {
return ClassUtil
.isPresent("com.alipay.sofa.common.boot.logging.CommonLoggingApplicationListener");
return ClassUtil.isPresent("com.alipay.sofa.common.boot.logging.Mark");
}

public static boolean filterAllLogConfig(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public static boolean isPresent(String className) {
}
}

public static boolean isPresent(String className, ClassLoader classLoader) {
try {
classLoader.loadClass(className);
return true;
} catch (Throwable throwable) {
return false;
}
}

public static String getClassNameForObject(Object object) {
return object == null ? null : getClassName(object.getClass().getName(), true);
}
Expand Down
96 changes: 96 additions & 0 deletions core/src/main/java/com/alipay/sofa/common/utils/ResourceUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package com.alipay.sofa.common.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
* @author qilong.zql
* @author Juergen Hoeller
* @since 1.0.17
*/
public class ResourceUtil {

/** URL protocol for a file in the file system: "file" */
public static final String URL_PROTOCOL_FILE = "file";

/**
* Resolve the given resource URL to a {@code java.io.File},
* i.e. to a file in the file system.
* @param resourceUrl the resource URL to resolve
* @return a corresponding File object
* @throws FileNotFoundException if the URL cannot be resolved to
* a file in the file system
*/
public static File getFile(URL resourceUrl) throws FileNotFoundException {
return getFile(resourceUrl, "URL");
}

/**
* Resolve the given resource URL to a {@code java.io.File},
* i.e. to a file in the file system.
* @param resourceUrl the resource URL to resolve
* @param description a description of the original resource that
* the URL was created for (for example, a class path location)
* @return a corresponding File object
* @throws FileNotFoundException if the URL cannot be resolved to
* a file in the file system
*/
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
AssertUtil.notNull(resourceUrl, "Resource URL must not be null");
if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
throw new FileNotFoundException(description
+ " cannot be resolved to absolute file path "
+ "because it does not reside in the file system: "
+ resourceUrl);
}
try {
return new File(toURI(resourceUrl).getSchemeSpecificPart());
} catch (URISyntaxException ex) {
// Fallback for URLs that are not valid URIs (should hardly ever happen).
return new File(resourceUrl.getFile());
}
}

/**
* Create a URI instance for the given URL,
* replacing spaces with "%20" URI encoding first.
* @param url the URL to convert into a URI instance
* @return the URI instance
* @throws URISyntaxException if the URL wasn't a valid URI
* @see java.net.URL#toURI()
*/
public static URI toURI(URL url) throws URISyntaxException {
return toURI(url.toString());
}

/**
* Create a URI instance for the given location String,
* replacing spaces with "%20" URI encoding first.
* @param location the location String to convert into a URI instance
* @return the URI instance
* @throws URISyntaxException if the location wasn't a valid URI
*/
public static URI toURI(String location) throws URISyntaxException {
return new URI(StringUtil.replace(location, " ", "%20"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package com.alipay.sofa.common.utils;

import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Properties;

/**
* @author qilong.zql
* @since 1.0.17
*/
public class ResourceUtilTest {

@Test
public void testGetFile() throws Exception {
URL url = this.getClass().getClassLoader().getResource("test-resource-utils.properties");
File file = ResourceUtil.getFile(url);
Properties properties = new Properties();
properties.load(new FileInputStream(file));
Assert.assertEquals(properties.getProperty("keyA"), "A");
Assert.assertEquals(properties.getProperty("keyB"), "B");
}

}
2 changes: 2 additions & 0 deletions core/src/test/resources/test-resource-utils.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
keyA=A
keyB=B
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.alipay.sofa.common.log.factory.AbstractLoggerSpaceFactory;
import com.alipay.sofa.common.log.factory.Log4j2LoggerSpaceFactory;
import com.alipay.sofa.common.log.factory.LogbackLoggerSpaceFactory;
import com.alipay.sofa.common.log.spi.ReInitializeChecker;
import com.alipay.sofa.common.utils.ReportUtil;
import com.alipay.sofa.common.utils.StringUtil;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
Expand All @@ -43,28 +42,21 @@
*/
public class CommonLoggingApplicationListener
implements
ReInitializeChecker,
ApplicationListener<ApplicationEnvironmentPreparedEvent>,
Ordered {

private final static AtomicBoolean isReInitialized = new AtomicBoolean(false);

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
if (isReInitialized.compareAndSet(false, true)) {
if (DefaultReInitializerChecker.isReInitialized.compareAndSet(false, true)) {
reInitializeLog(loadApplicationEnvironment(event.getEnvironment()));
}
}

public boolean isReInitialize() {
return isReInitialized.get();
}

public void setReInitialize(boolean value) {
isReInitialized.set(value);
DefaultReInitializerChecker.isReInitialized.set(value);
}

private void reInitializeLog(Map<String, String> context) {
public void reInitializeLog(Map<String, String> context) {
for (String key : context.keySet()) {
if (key.startsWith(Constants.SOFA_MIDDLEWARE_CONFIG_PREFIX)
&& !key.equals(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package com.alipay.sofa.common.boot.logging;

import com.alipay.sofa.common.log.spi.ReInitializeChecker;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* @author qilong.zql
* @since 1.0.17
*/
public class DefaultReInitializerChecker implements ReInitializeChecker {

public final static AtomicBoolean isReInitialized = new AtomicBoolean(false);

@Override
public boolean isReInitialize() {
return DefaultReInitializerChecker.isReInitialized.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package com.alipay.sofa.common.boot.logging;

/**
* @author qilong.zql
* @since 1.0.17
*/
public class Mark {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alipay.sofa.common.boot.logging.util.SystemPropertiesGetter;
import com.alipay.sofa.common.log.SpaceId;
import com.alipay.sofa.common.log.spi.Log4j2ReInitializer;
import com.alipay.sofa.common.utils.ResourceUtil;
import com.alipay.sofa.common.utils.StringUtil;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
Expand All @@ -33,7 +34,6 @@
import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.message.Message;
import org.springframework.util.ResourceUtils;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -141,7 +141,7 @@ private ConsoleAppender consoleAppender(Properties properties) {
private ConfigurationSource getConfigurationSource(URL url) throws IOException {
InputStream stream = url.openStream();
if (FILE_PROTOCOL.equals(url.getProtocol())) {
return new ConfigurationSource(stream, ResourceUtils.getFile(url));
return new ConfigurationSource(stream, ResourceUtil.getFile(url));
}
return new ConfigurationSource(stream, url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import com.alipay.sofa.common.boot.logging.util.SystemPropertiesGetter;
import com.alipay.sofa.common.log.SpaceId;
import com.alipay.sofa.common.log.spi.LogbackReInitializer;
import com.alipay.sofa.common.utils.ClassUtil;
import com.alipay.sofa.common.utils.StringUtil;
import org.slf4j.Marker;
import org.springframework.util.ClassUtils;

import java.net.URL;
import java.util.Map;
Expand Down Expand Up @@ -138,7 +138,7 @@ private void stopAndReset(LoggerContext loggerContext) {
}

protected final boolean isBridgeHandlerAvailable() {
return ClassUtils.isPresent(BRIDGE_HANDLER, this.getClass().getClassLoader());
return ClassUtil.isPresent(BRIDGE_HANDLER, this.getClass().getClassLoader());
}

private void addLevelChangePropagator(LoggerContext loggerContext) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
com.alipay.sofa.common.boot.logging.CommonLoggingApplicationListener
com.alipay.sofa.common.boot.logging.DefaultReInitializerChecker

0 comments on commit 3876eef

Please sign in to comment.