Remove dependency of System.getProperty()

This commit is contained in:
Woonduk Kang
2015-02-10 18:44:22 +09:00
parent c6bc38370c
commit e3bb776376
10 changed files with 134 additions and 33 deletions
@@ -17,6 +17,8 @@
package com.navercorp.pinpoint.bootstrap;
import com.navercorp.pinpoint.common.util.SystemProperty;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
@@ -86,7 +88,7 @@ public class ClassPathResolver {
}
public static String getClassPathFromSystemProperty() {
return System.getProperty("java.class.path");
return SystemProperty.INSTANCE.getProperty("java.class.path");
}
public boolean findAgentJar() {
@@ -19,10 +19,7 @@ package com.navercorp.pinpoint.bootstrap;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.jar.JarFile;
import java.util.logging.Level;
@@ -34,6 +31,8 @@ import com.navercorp.pinpoint.bootstrap.util.IdValidateUtils;
import com.navercorp.pinpoint.common.PinpointConstants;
import com.navercorp.pinpoint.common.ServiceTypeProviderLoader;
import com.navercorp.pinpoint.common.util.BytesUtils;
import com.navercorp.pinpoint.common.util.SimpleProperty;
import com.navercorp.pinpoint.common.util.SystemProperty;
/**
* @author emeroad
@@ -49,6 +48,8 @@ public class PinpointBootStrap {
private static final boolean STATE_STARTED = true;
private static final AtomicBoolean LOAD_STATE = new AtomicBoolean(STATE_NONE);
private static final SimpleProperty SYSTEM_PROPERTY = SystemProperty.INSTANCE;
public static void premain(String agentArgs, Instrumentation instrumentation) {
if (agentArgs != null) {
logger.info(ProductInfo.CAMEL_NAME + " agentArgs:" + agentArgs);
@@ -65,7 +66,7 @@ public class PinpointBootStrap {
final ClassPathResolver classPathResolver = new ClassPathResolver();
boolean agentJarNotFound = classPathResolver.findAgentJar();
if (!agentJarNotFound) {
logger.severe("pinpoint-bootstrap-x.x.x(-SNAPSHOT).jar not found.");
logger.severe("pinpoint-bootstrap-x.x.x(-SNAPSHOT).jar Fnot found.");
logPinpointAgentLoadFail();
return;
}
@@ -186,7 +187,7 @@ public class PinpointBootStrap {
private static boolean isValidId(String propertyName, int maxSize) {
logger.info("check -D" + propertyName);
String value = System.getProperty(propertyName);
String value = SYSTEM_PROPERTY.getProperty(propertyName);
if (value == null){
logger.severe("-D" + propertyName + " is null. value:null");
return false;
@@ -222,12 +223,12 @@ public class PinpointBootStrap {
String agentLogFilePath = classPathResolver.getAgentLogFilePath();
logger.info("logPath:" + agentLogFilePath);
System.setProperty(ProductInfo.NAME + ".log", agentLogFilePath);
SYSTEM_PROPERTY.setProperty(ProductInfo.NAME + ".log", agentLogFilePath);
}
private static String getConfigPath(ClassPathResolver classPathResolver) {
final String configName = ProductInfo.NAME + ".config";
String pinpointConfigFormSystemProperty = System.getProperty(configName);
String pinpointConfigFormSystemProperty = SYSTEM_PROPERTY.getProperty(configName);
if (pinpointConfigFormSystemProperty != null) {
logger.info(configName + " systemProperty found. " + pinpointConfigFormSystemProperty);
return pinpointConfigFormSystemProperty;
@@ -18,6 +18,8 @@ package com.navercorp.pinpoint.collector.config;
import com.navercorp.pinpoint.common.util.PropertyUtils;
import com.navercorp.pinpoint.common.util.SimpleProperty;
import com.navercorp.pinpoint.common.util.SystemProperty;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -42,6 +44,8 @@ public class CollectorConfiguration implements InitializingBean {
private Properties properties;
private SimpleProperty SYSTEM_PROPERTY = SystemProperty.INSTANCE;
public void setProperties(Properties properties) {
this.properties = properties;
}
@@ -158,7 +162,7 @@ public class CollectorConfiguration implements InitializingBean {
public void readConfigFile() {
// may be useful for some kind of standalone like testcase. It should be modified to read a classpath for testcase.
String configFileName = System.getProperty(CONFIG_FILE_NAME);
String configFileName = SYSTEM_PROPERTY.getProperty(CONFIG_FILE_NAME);
if (configFileName == null) {
logger.warn("Property is not set. Using default values. PROPERTY_NAME={}, defaultValue={}", CONFIG_FILE_NAME, this);
return;
@@ -0,0 +1,28 @@
/*
* Copyright 2014 NAVER Corp.
*
* 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
*
* 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.navercorp.pinpoint.common.util;
/**
* @author emeroad
*/
public interface SimpleProperty {
void setProperty(String key, String value);
String getProperty(String key);
String getProperty(String key, String defaultValue);
}
@@ -0,0 +1,45 @@
/*
* Copyright 2014 NAVER Corp.
*
* 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
*
* 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.navercorp.pinpoint.common.util;
/**
* @author emeroad
*/
public class SystemProperty implements SimpleProperty {
public static final SystemProperty INSTANCE = new SystemProperty();
@Override
public void setProperty(String key, String value) {
System.setProperty(key, value);
}
@Override
public String getProperty(String key) {
return System.getProperty(key);
}
@Override
public String getProperty(String key, String defaultValue) {
return System.getProperty(key, defaultValue);
}
public String getEnv(String name) {
return System.getenv(name);
}
}
@@ -21,11 +21,15 @@ import com.navercorp.pinpoint.common.PinpointConstants;
import com.navercorp.pinpoint.common.ServiceType;
import com.navercorp.pinpoint.common.Version;
import com.navercorp.pinpoint.common.util.BytesUtils;
import com.navercorp.pinpoint.common.util.SimpleProperty;
import com.navercorp.pinpoint.common.util.SystemProperty;
import com.navercorp.pinpoint.profiler.util.RuntimeMXBeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Properties;
/**
* @author emeroad
@@ -33,6 +37,7 @@ import org.slf4j.LoggerFactory;
public class AgentInformationFactory {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final SimpleProperty systemProperty = SystemProperty.INSTANCE;
public AgentInformationFactory() {
}
@@ -53,7 +58,7 @@ public class AgentInformationFactory {
}
private String getId(String key, String defaultValue, int maxlen) {
String value = System.getProperty(key, defaultValue);
String value = systemProperty.getProperty(key, defaultValue);
validateId(value, key, maxlen);
return value;
}
@@ -20,6 +20,8 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.navercorp.pinpoint.common.util.SimpleProperty;
import com.navercorp.pinpoint.common.util.SystemProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -41,12 +43,14 @@ public class ApplicationServerTypeResolver {
private final ServiceType defaultType;
private final List<ServerTypeDetector> detectors = new ArrayList<ServerTypeDetector>();
private final SimpleProperty systemProperty = SystemProperty.INSTANCE;
/**
* If we have to invoke startup() during agent initialization.
* Some service types like BLOC or STAND_ALONE don't have an acceptor to do this.
*/
private boolean manuallyStartupRequired = true;
public ApplicationServerTypeResolver(List<DefaultProfilerPluginContext> plugins, ServiceType defaultType) {
this.defaultType = defaultType;
@@ -108,11 +112,12 @@ public class ApplicationServerTypeResolver {
private String applicationHomePath() {
String path = null;
if (System.getProperty("catalina.home") != null) {
path = System.getProperty("catalina.home");
final SimpleProperty system = this.systemProperty;
if (system.getProperty("catalina.home") != null) {
path = system.getProperty("catalina.home");
} else {
path = System.getProperty("user.dir");
path = system.getProperty("user.dir");
}
if (logger.isInfoEnabled()) {
@@ -29,6 +29,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import com.navercorp.pinpoint.common.util.SystemProperty;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
@@ -60,6 +61,8 @@ public class ForkRunner extends BlockJUnit4ClassRunner {
private final String[] jvmArguments;
private final String javaHomeEnvName;
private final SystemProperty systemProperty = SystemProperty.INSTANCE;
public ForkRunner(Class<?> testClass) throws InitializationError {
super(testClass);
@@ -131,7 +134,7 @@ public class ForkRunner extends BlockJUnit4ClassRunner {
builder.command(buildCommand());
builder.redirectErrorStream(true);
System.out.println("Working directory: " + System.getProperty("user.dir"));
System.out.println("Working directory: " + systemProperty.getProperty("user.dir"));
System.out.println("Command: " + builder.command());
Process process;
@@ -260,9 +263,9 @@ public class ForkRunner extends BlockJUnit4ClassRunner {
String javaHome;
if (javaHomeEnvName == null) {
javaHome = System.getProperty("java.home");
javaHome = systemProperty.getProperty("java.home");
} else {
javaHome = System.getenv(javaHomeEnvName);
javaHome = systemProperty.getEnv(javaHomeEnvName);
}
builder.append(javaHome);
@@ -271,7 +274,7 @@ public class ForkRunner extends BlockJUnit4ClassRunner {
builder.append(File.separatorChar);
builder.append("java");
if (System.getProperty("os.name").contains("indows")) {
if (systemProperty.getProperty("os.name").contains("indows")) {
builder.append(".exe");
}
@@ -29,6 +29,8 @@ import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import com.navercorp.pinpoint.common.util.SimpleProperty;
import com.navercorp.pinpoint.common.util.SystemProperty;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
@@ -71,10 +73,13 @@ public class DependencyResolver {
private static final String FOLLOW_PRECEDING = "FOLLOW_PRECEDING";
private static final String DEFAULT_LOCAL_REPOSITORY = "target/local-repo";
private static final Logger logger = Logger.getLogger(PinpointBootStrap.class.getName());
private static final SimpleProperty SYSTEM_PROPERTY = SystemProperty.INSTANCE;
private final List<RemoteRepository> repositories;
private final RepositorySystem system;
private final RepositorySystemSession session;
private static RepositorySystem newRepositorySystem() {
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
@@ -105,7 +110,7 @@ public class DependencyResolver {
}
private static String resolveLocalRepository() {
String userHome = System.getProperty("user.home");
String userHome = SYSTEM_PROPERTY.getProperty("user.home");
if (userHome == null) {
logger.fine("Cannot find user.home property. Use default local repository");
@@ -32,6 +32,7 @@ import java.util.List;
import java.util.Map;
import java.util.Scanner;
import com.navercorp.pinpoint.common.util.SystemProperty;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.DependencyResolutionException;
@@ -73,6 +74,8 @@ public class PinpointPluginTestSuite extends Suite {
private final String[] jvmArguments;
private final String javaHomeEnvName;
private final SystemProperty simpleProperty = SystemProperty.INSTANCE;
@Override
protected List<Runner> getChildren() {
return runners;
@@ -286,25 +289,25 @@ public class PinpointPluginTestSuite extends Suite {
@Override
public void evaluate() throws Throwable {
ProcessBuilder builder = new ProcessBuilder();
builder.command(buildCommand(systemLibs, dependencyLibs));
builder.redirectErrorStream(true);
System.out.println("Working directory: " + System.getProperty("user.dir"));
System.out.println("Working directory: " + simpleProperty.getProperty("user.dir"));
System.out.println("Command: " + builder.command());
Process process = builder.start();
final InputStream inputStream = process.getInputStream();
final Scanner out = new Scanner(inputStream, DEFAULT_ENCODING);
try {
while (out.hasNextLine()) {
String line = out.nextLine();
if (line.startsWith(ForkedPinpointPluginTest.JUNIT_OUTPUT_DELIMETER)) {
String[] tokens = line.split(ForkedPinpointPluginTest.JUNIT_OUTPUT_DELIMETER_REGEXP);
String event = tokens[1];
if ("testRunStarted".equals(event)) {
notifier.fireTestRunStarted(getDescription());
} else if ("testRunFinished".equals(event)) {
@@ -333,7 +336,7 @@ public class PinpointPluginTestSuite extends Suite {
out.close();
close(inputStream);
}
try {
process.waitFor();
} catch (InterruptedException e) {
@@ -409,9 +412,9 @@ public class PinpointPluginTestSuite extends Suite {
String javaHome;
if (javaHomeEnvName == null) {
javaHome = System.getProperty("java.home");
javaHome = simpleProperty.getProperty("java.home");
} else {
javaHome = System.getenv(javaHomeEnvName);
javaHome = simpleProperty.getEnv(javaHomeEnvName);
}
builder.append(javaHome);
@@ -420,7 +423,7 @@ public class PinpointPluginTestSuite extends Suite {
builder.append(File.separatorChar);
builder.append("java");
if (System.getProperty("os.name").contains("indows")) {
if (simpleProperty.getProperty("os.name").contains("indows")) {
builder.append(".exe");
}