
org.glowroot.agent.MainEntryPoint Maven / Gradle / Ivy
/*
* Copyright 2011-2015 the original author or authors.
*
* 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 org.glowroot.agent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import javax.annotation.Nullable;
import org.glowroot.agent.shaded.google.common.annotations.VisibleForTesting;
import org.glowroot.agent.shaded.google.common.base.Objects;
import org.glowroot.agent.shaded.google.common.base.Strings;
import org.glowroot.agent.shaded.google.common.collect.ImmutableMap;
import org.glowroot.agent.shaded.google.common.collect.Maps;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.glowroot.agent.shaded.slf4j.Logger;
import org.glowroot.agent.shaded.slf4j.LoggerFactory;
import org.glowroot.agent.init.GlowrootAgentInit;
import org.glowroot.agent.init.GlowrootThinAgentInit;
import org.glowroot.agent.init.LoggingInit;
import org.glowroot.agent.init.fat.DataDirLocking.BaseDirLockedException;
import org.glowroot.agent.init.fat.GlowrootFatAgentInit;
import org.glowroot.common.util.OnlyUsedByTests;
import org.glowroot.common.util.Version;
public class MainEntryPoint {
private static final Logger startupLogger;
@OnlyUsedByTests
private static @MonotonicNonNull GlowrootAgentInit glowrootAgentInit;
static {
org.glowroot.agent.shaded.qos.logback.classic.Logger rootLogger =
(org.glowroot.agent.shaded.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
// FILE appender depends on ${glowroot.base.dir}, so need to remove it temporarily
rootLogger.detachAppender("FILE");
// log startup messages using logger name "org.glowroot"
startupLogger = LoggerFactory.getLogger("org.glowroot");
}
private MainEntryPoint() {}
public static void premain(Instrumentation instrumentation, @Nullable File glowrootJarFile) {
boolean jbossModules = isJBossModules();
if (jbossModules) {
String jbossModulesSystemPkgs = System.getProperty("jboss.modules.system.pkgs");
if (Strings.isNullOrEmpty(jbossModulesSystemPkgs)) {
jbossModulesSystemPkgs = "org.glowroot";
} else {
jbossModulesSystemPkgs += ",org.glowroot";
}
System.setProperty("jboss.modules.system.pkgs", jbossModulesSystemPkgs);
}
String baseDirPath = System.getProperty("glowroot.base.dir");
File baseDir = BaseDir.getBaseDir(baseDirPath, glowrootJarFile);
try {
// init logger as early as possible
LoggingInit.initStaticLoggerState(baseDir);
ImmutableMap properties = getGlowrootProperties(baseDir);
start(baseDir, properties, instrumentation, glowrootJarFile, jbossModules);
} catch (BaseDirLockedException e) {
logBaseDirLockedException(baseDir);
} catch (Throwable t) {
// log error but don't re-throw which would prevent monitored app from starting
startupLogger.error("Glowroot not started: {}", t.getMessage(), t);
}
}
static void runViewer(@Nullable File glowrootJarFile) throws InterruptedException {
String baseDirPath = System.getProperty("glowroot.base.dir");
File baseDir = BaseDir.getBaseDir(baseDirPath, glowrootJarFile);
String version;
try {
// init logger as early as possible
LoggingInit.initStaticLoggerState(baseDir);
version = Version.getVersion();
ImmutableMap properties = getGlowrootProperties(baseDir);
new GlowrootFatAgentInit().init(baseDir, null, properties, null, glowrootJarFile,
version, false, true);
} catch (BaseDirLockedException e) {
logBaseDirLockedException(baseDir);
return;
} catch (Throwable t) {
startupLogger.error("Glowroot cannot start: {}", t.getMessage(), t);
return;
}
startupLogger.info("Glowroot started (version {})", version);
// Glowroot does not create any non-daemon threads, so need to block jvm from exiting when
// running the viewer
Thread.sleep(Long.MAX_VALUE);
}
private static void start(File baseDir, Map properties,
@Nullable Instrumentation instrumentation, @Nullable File glowrootJarFile,
boolean jbossModules) throws Exception {
ManagementFactory.getThreadMXBean().setThreadCpuTimeEnabled(true);
ManagementFactory.getThreadMXBean().setThreadContentionMonitoringEnabled(true);
String version = Version.getVersion();
String collectorHost = properties.get("glowroot.collector.host");
if (Strings.isNullOrEmpty(collectorHost)) {
collectorHost = System.getProperty("glowroot.collector.host");
}
if (Strings.isNullOrEmpty(collectorHost)) {
glowrootAgentInit = new GlowrootFatAgentInit();
} else {
glowrootAgentInit = new GlowrootThinAgentInit();
}
glowrootAgentInit.init(baseDir, collectorHost, properties, instrumentation, glowrootJarFile,
version, false, jbossModules);
startupLogger.info("Glowroot started (version {})", version);
}
private static ImmutableMap getGlowrootProperties(File baseDir)
throws IOException {
Map properties = Maps.newHashMap();
File propFile = new File(baseDir, "glowroot.properties");
if (propFile.exists()) {
Properties props = new Properties();
InputStream in = new FileInputStream(propFile);
props.load(in);
for (String key : props.stringPropertyNames()) {
String value = props.getProperty(key);
if (value != null) {
properties.put(key, value);
}
}
}
for (Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy