diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-08-30 22:13:04 +0600 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-08-30 22:13:04 +0600 |
commit | e69ba4dbb015949c5d84ba7bbb0b53efac28bb23 (patch) | |
tree | 44cbe6d78216fcb3c37c0aca1dc7ed3fc09906fa /org.jacoco.agent.rt | |
parent | a888d873ac20357a4a11029bc84c5c4b48e394a3 (diff) | |
download | jacoco-e69ba4dbb015949c5d84ba7bbb0b53efac28bb23.tar.gz |
Fix EOLs
Diffstat (limited to 'org.jacoco.agent.rt')
7 files changed, 749 insertions, 749 deletions
diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/CoverageTransformer.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/CoverageTransformer.java index 2f679154..b23d6a10 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/CoverageTransformer.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/CoverageTransformer.java @@ -1,138 +1,138 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Marc R. Hoffmann - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt;
-
-import static java.lang.String.format;
-
-import java.lang.instrument.ClassFileTransformer;
-import java.lang.instrument.IllegalClassFormatException;
-import java.security.ProtectionDomain;
-
-import org.jacoco.core.instr.Instrumenter;
-import org.jacoco.core.runtime.AgentOptions;
-import org.jacoco.core.runtime.IRuntime;
-import org.jacoco.core.runtime.WildcardMatcher;
-
-/**
- * Class file transformer to instrument classes for code coverage analysis.
- */
-public class CoverageTransformer implements ClassFileTransformer {
-
- private static final String AGENT_PREFIX;
-
- static {
- final String name = CoverageTransformer.class.getName();
- AGENT_PREFIX = toVMName(name.substring(0, name.lastIndexOf('.')));
- }
-
- private final IRuntime runtime;
-
- private final Instrumenter instrumenter;
-
- private final IExceptionLogger logger;
-
- private final WildcardMatcher includes;
-
- private final WildcardMatcher excludes;
-
- private final WildcardMatcher exclClassloader;
-
- private final ClassFileDumper classFileDumper;
-
- /**
- * New transformer with the given delegates.
- *
- * @param runtime
- * coverage runtime
- * @param options
- * configuration options for the generator
- * @param logger
- * logger for exceptions during instrumentation
- */
- public CoverageTransformer(final IRuntime runtime,
- final AgentOptions options, final IExceptionLogger logger) {
- this.runtime = runtime;
- this.instrumenter = new Instrumenter(runtime);
- this.logger = logger;
- // Class names will be reported in VM notation:
- includes = new WildcardMatcher(
- toWildcard(toVMName(options.getIncludes())));
- excludes = new WildcardMatcher(
- toWildcard(toVMName(options.getExcludes())));
- exclClassloader = new WildcardMatcher(
- toWildcard(options.getExclClassloader()));
- classFileDumper = new ClassFileDumper(options.getClassDumpDir());
- }
-
- public byte[] transform(final ClassLoader loader, final String classname,
- final Class<?> classBeingRedefined,
- final ProtectionDomain protectionDomain,
- final byte[] classfileBuffer) throws IllegalClassFormatException {
-
- if (!filter(loader, classname)) {
- return null;
- }
-
- try {
- classFileDumper.dump(classname, classfileBuffer);
- if (classBeingRedefined != null) {
- // For redefined classes we must clear the execution data
- // reference as probes might have changed.
- runtime.disconnect(classBeingRedefined);
- }
- return instrumenter.instrument(classfileBuffer);
- } catch (final Exception ex) {
- final IllegalClassFormatException wrapper = new IllegalClassFormatException(
- format("Error while instrumenting class %s.", classname));
- // Report this, as the exception is ignored by the JVM:
- logger.logExeption(wrapper);
- throw (IllegalClassFormatException) wrapper.initCause(ex);
- }
- }
-
- /**
- * Checks whether this class should be instrumented.
- *
- * @param loader
- * loader for the class
- * @param classname
- * VM name of the class to check
- * @return <code>true</code> if the class should be instrumented
- */
- protected boolean filter(final ClassLoader loader, final String classname) {
- // Don't instrument classes of the bootstrap loader:
- return loader != null &&
-
- !classname.startsWith(AGENT_PREFIX) &&
-
- !exclClassloader.matches(loader.getClass().getName()) &&
-
- includes.matches(classname) &&
-
- !excludes.matches(classname);
- }
-
- private String toWildcard(final String src) {
- if (src.indexOf('|') != -1) {
- final IllegalArgumentException ex = new IllegalArgumentException(
- "Usage of '|' as a list separator for JaCoCo agent options is deprecated and will not work in future versions - use ':' instead.");
- logger.logExeption(ex);
- return src.replace('|', ':');
- }
- return src;
- }
-
- private static String toVMName(final String srcName) {
- return srcName.replace('.', '/');
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt; + +import static java.lang.String.format; + +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.IllegalClassFormatException; +import java.security.ProtectionDomain; + +import org.jacoco.core.instr.Instrumenter; +import org.jacoco.core.runtime.AgentOptions; +import org.jacoco.core.runtime.IRuntime; +import org.jacoco.core.runtime.WildcardMatcher; + +/** + * Class file transformer to instrument classes for code coverage analysis. + */ +public class CoverageTransformer implements ClassFileTransformer { + + private static final String AGENT_PREFIX; + + static { + final String name = CoverageTransformer.class.getName(); + AGENT_PREFIX = toVMName(name.substring(0, name.lastIndexOf('.'))); + } + + private final IRuntime runtime; + + private final Instrumenter instrumenter; + + private final IExceptionLogger logger; + + private final WildcardMatcher includes; + + private final WildcardMatcher excludes; + + private final WildcardMatcher exclClassloader; + + private final ClassFileDumper classFileDumper; + + /** + * New transformer with the given delegates. + * + * @param runtime + * coverage runtime + * @param options + * configuration options for the generator + * @param logger + * logger for exceptions during instrumentation + */ + public CoverageTransformer(final IRuntime runtime, + final AgentOptions options, final IExceptionLogger logger) { + this.runtime = runtime; + this.instrumenter = new Instrumenter(runtime); + this.logger = logger; + // Class names will be reported in VM notation: + includes = new WildcardMatcher( + toWildcard(toVMName(options.getIncludes()))); + excludes = new WildcardMatcher( + toWildcard(toVMName(options.getExcludes()))); + exclClassloader = new WildcardMatcher( + toWildcard(options.getExclClassloader())); + classFileDumper = new ClassFileDumper(options.getClassDumpDir()); + } + + public byte[] transform(final ClassLoader loader, final String classname, + final Class<?> classBeingRedefined, + final ProtectionDomain protectionDomain, + final byte[] classfileBuffer) throws IllegalClassFormatException { + + if (!filter(loader, classname)) { + return null; + } + + try { + classFileDumper.dump(classname, classfileBuffer); + if (classBeingRedefined != null) { + // For redefined classes we must clear the execution data + // reference as probes might have changed. + runtime.disconnect(classBeingRedefined); + } + return instrumenter.instrument(classfileBuffer); + } catch (final Exception ex) { + final IllegalClassFormatException wrapper = new IllegalClassFormatException( + format("Error while instrumenting class %s.", classname)); + // Report this, as the exception is ignored by the JVM: + logger.logExeption(wrapper); + throw (IllegalClassFormatException) wrapper.initCause(ex); + } + } + + /** + * Checks whether this class should be instrumented. + * + * @param loader + * loader for the class + * @param classname + * VM name of the class to check + * @return <code>true</code> if the class should be instrumented + */ + protected boolean filter(final ClassLoader loader, final String classname) { + // Don't instrument classes of the bootstrap loader: + return loader != null && + + !classname.startsWith(AGENT_PREFIX) && + + !exclClassloader.matches(loader.getClass().getName()) && + + includes.matches(classname) && + + !excludes.matches(classname); + } + + private String toWildcard(final String src) { + if (src.indexOf('|') != -1) { + final IllegalArgumentException ex = new IllegalArgumentException( + "Usage of '|' as a list separator for JaCoCo agent options is deprecated and will not work in future versions - use ':' instead."); + logger.logExeption(ex); + return src.replace('|', ':'); + } + return src; + } + + private static String toVMName(final String srcName) { + return srcName.replace('.', '/'); + } + +} diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/JacocoAgent.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/JacocoAgent.java index c52fc824..a2f9b521 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/JacocoAgent.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/JacocoAgent.java @@ -1,175 +1,175 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Marc R. Hoffmann - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt;
-
-import java.lang.instrument.Instrumentation;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-
-import org.jacoco.agent.rt.controller.IAgentController;
-import org.jacoco.agent.rt.controller.LocalController;
-import org.jacoco.agent.rt.controller.MBeanController;
-import org.jacoco.agent.rt.controller.TcpClientController;
-import org.jacoco.agent.rt.controller.TcpServerController;
-import org.jacoco.core.runtime.AbstractRuntime;
-import org.jacoco.core.runtime.AgentOptions;
-import org.jacoco.core.runtime.AgentOptions.OutputMode;
-import org.jacoco.core.runtime.IRuntime;
-import org.jacoco.core.runtime.ModifiedSystemClassRuntime;
-
-/**
- * The agent which is referred as the <code>Premain-Class</code>.
- */
-public class JacocoAgent {
-
- private final AgentOptions options;
-
- private final IExceptionLogger logger;
-
- private IAgentController controller;
-
- /**
- * Creates a new agent with the given agent options.
- *
- * @param options
- * agent options
- * @param logger
- * logger used by this agent
- */
- public JacocoAgent(final AgentOptions options, final IExceptionLogger logger) {
- this.options = options;
- this.logger = logger;
- }
-
- /**
- * Creates a new agent with the given agent options string.
- *
- * @param options
- * agent options as text string
- * @param logger
- * logger used by this agent
- */
- public JacocoAgent(final String options, final IExceptionLogger logger) {
- this(new AgentOptions(options), logger);
- }
-
- /**
- * Initializes this agent.
- *
- * @param inst
- * instrumentation services
- * @throws Exception
- * internal startup problem
- */
- public void init(final Instrumentation inst) throws Exception {
- final IRuntime runtime = createRuntime(inst);
- String sessionId = options.getSessionId();
- if (sessionId == null) {
- sessionId = createSessionId();
- }
- runtime.setSessionId(sessionId);
- runtime.startup();
- inst.addTransformer(new CoverageTransformer(runtime, options, logger));
- controller = createAgentController();
- controller.startup(options, runtime);
- }
-
- /**
- * Create controller implementation as given by the agent options.
- *
- * @return configured controller implementation
- */
- protected IAgentController createAgentController() {
- final OutputMode controllerType = options.getOutput();
- switch (controllerType) {
- case file:
- return new LocalController();
- case tcpserver:
- return new TcpServerController(logger);
- case tcpclient:
- return new TcpClientController(logger);
- case mbean:
- return new MBeanController();
- default:
- throw new AssertionError(controllerType);
- }
- }
-
- private String createSessionId() {
- String host;
- try {
- host = InetAddress.getLocalHost().getHostName();
- } catch (final UnknownHostException e) {
- host = "unknownhost";
- }
- return host + "-" + AbstractRuntime.createRandomId();
- }
-
- /**
- * Creates the specific coverage runtime implementation.
- *
- * @param inst
- * instrumentation services
- * @return coverage runtime instance
- * @throws Exception
- * creation problem
- */
- protected IRuntime createRuntime(final Instrumentation inst)
- throws Exception {
- return ModifiedSystemClassRuntime.createFor(inst, "java/util/UUID");
- }
-
- /**
- * Shutdown the agent again.
- */
- public void shutdown() {
- try {
- if (options.getDumpOnExit()) {
- controller.writeExecutionData();
- }
- controller.shutdown();
- } catch (final Exception e) {
- logger.logExeption(e);
- }
- }
-
- /**
- * This method is called by the JVM to initialize Java agents.
- *
- * @param options
- * agent options
- * @param inst
- * instrumentation callback provided by the JVM
- * @throws Exception
- * in case initialization fails
- */
- public static void premain(final String options, final Instrumentation inst)
- throws Exception {
-
- final JacocoAgent agent = new JacocoAgent(options,
- new IExceptionLogger() {
- public void logExeption(final Exception ex) {
- ex.printStackTrace();
- }
- });
-
- agent.init(inst);
-
- Runtime.getRuntime().addShutdownHook(new Thread() {
- @Override
- public void run() {
- agent.shutdown();
- }
- });
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt; + +import java.lang.instrument.Instrumentation; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.jacoco.agent.rt.controller.IAgentController; +import org.jacoco.agent.rt.controller.LocalController; +import org.jacoco.agent.rt.controller.MBeanController; +import org.jacoco.agent.rt.controller.TcpClientController; +import org.jacoco.agent.rt.controller.TcpServerController; +import org.jacoco.core.runtime.AbstractRuntime; +import org.jacoco.core.runtime.AgentOptions; +import org.jacoco.core.runtime.AgentOptions.OutputMode; +import org.jacoco.core.runtime.IRuntime; +import org.jacoco.core.runtime.ModifiedSystemClassRuntime; + +/** + * The agent which is referred as the <code>Premain-Class</code>. + */ +public class JacocoAgent { + + private final AgentOptions options; + + private final IExceptionLogger logger; + + private IAgentController controller; + + /** + * Creates a new agent with the given agent options. + * + * @param options + * agent options + * @param logger + * logger used by this agent + */ + public JacocoAgent(final AgentOptions options, final IExceptionLogger logger) { + this.options = options; + this.logger = logger; + } + + /** + * Creates a new agent with the given agent options string. + * + * @param options + * agent options as text string + * @param logger + * logger used by this agent + */ + public JacocoAgent(final String options, final IExceptionLogger logger) { + this(new AgentOptions(options), logger); + } + + /** + * Initializes this agent. + * + * @param inst + * instrumentation services + * @throws Exception + * internal startup problem + */ + public void init(final Instrumentation inst) throws Exception { + final IRuntime runtime = createRuntime(inst); + String sessionId = options.getSessionId(); + if (sessionId == null) { + sessionId = createSessionId(); + } + runtime.setSessionId(sessionId); + runtime.startup(); + inst.addTransformer(new CoverageTransformer(runtime, options, logger)); + controller = createAgentController(); + controller.startup(options, runtime); + } + + /** + * Create controller implementation as given by the agent options. + * + * @return configured controller implementation + */ + protected IAgentController createAgentController() { + final OutputMode controllerType = options.getOutput(); + switch (controllerType) { + case file: + return new LocalController(); + case tcpserver: + return new TcpServerController(logger); + case tcpclient: + return new TcpClientController(logger); + case mbean: + return new MBeanController(); + default: + throw new AssertionError(controllerType); + } + } + + private String createSessionId() { + String host; + try { + host = InetAddress.getLocalHost().getHostName(); + } catch (final UnknownHostException e) { + host = "unknownhost"; + } + return host + "-" + AbstractRuntime.createRandomId(); + } + + /** + * Creates the specific coverage runtime implementation. + * + * @param inst + * instrumentation services + * @return coverage runtime instance + * @throws Exception + * creation problem + */ + protected IRuntime createRuntime(final Instrumentation inst) + throws Exception { + return ModifiedSystemClassRuntime.createFor(inst, "java/util/UUID"); + } + + /** + * Shutdown the agent again. + */ + public void shutdown() { + try { + if (options.getDumpOnExit()) { + controller.writeExecutionData(); + } + controller.shutdown(); + } catch (final Exception e) { + logger.logExeption(e); + } + } + + /** + * This method is called by the JVM to initialize Java agents. + * + * @param options + * agent options + * @param inst + * instrumentation callback provided by the JVM + * @throws Exception + * in case initialization fails + */ + public static void premain(final String options, final Instrumentation inst) + throws Exception { + + final JacocoAgent agent = new JacocoAgent(options, + new IExceptionLogger() { + public void logExeption(final Exception ex) { + ex.printStackTrace(); + } + }); + + agent.init(inst); + + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + agent.shutdown(); + } + }); + } + +} diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/IAgentController.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/IAgentController.java index 77f4df2f..c8dde9fa 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/IAgentController.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/IAgentController.java @@ -1,54 +1,54 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Brock Janiczak - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import org.jacoco.core.runtime.AgentOptions;
-import org.jacoco.core.runtime.IRuntime;
-
-/**
- * Common interface for different implementations that control execution data
- * dumps.
- */
-public interface IAgentController {
-
- /**
- * Configure the agent controller with the supplied options and connect it
- * to the coverage runtime
- *
- * @param options
- * Options used to configure the agent controller
- * @param runtime
- * Coverage runtime this agent controller will be connected to
- * @throws Exception
- * in case startup fails
- */
- public void startup(final AgentOptions options, final IRuntime runtime)
- throws Exception;
-
- /**
- * Shutdown the agent controller and clean up any resources it has created.
- *
- * @throws Exception
- * in case shutdown fails
- */
- public void shutdown() throws Exception;
-
- /**
- * Write all execution data in the runtime to a location determined by the
- * agent controller. This method should only be called by the Agent
- *
- * @throws Exception
- * in case writing fails
- */
- public void writeExecutionData() throws Exception;
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Brock Janiczak - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import org.jacoco.core.runtime.AgentOptions; +import org.jacoco.core.runtime.IRuntime; + +/** + * Common interface for different implementations that control execution data + * dumps. + */ +public interface IAgentController { + + /** + * Configure the agent controller with the supplied options and connect it + * to the coverage runtime + * + * @param options + * Options used to configure the agent controller + * @param runtime + * Coverage runtime this agent controller will be connected to + * @throws Exception + * in case startup fails + */ + public void startup(final AgentOptions options, final IRuntime runtime) + throws Exception; + + /** + * Shutdown the agent controller and clean up any resources it has created. + * + * @throws Exception + * in case shutdown fails + */ + public void shutdown() throws Exception; + + /** + * Write all execution data in the runtime to a location determined by the + * agent controller. This method should only be called by the Agent + * + * @throws Exception + * in case writing fails + */ + public void writeExecutionData() throws Exception; + +} diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/LocalController.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/LocalController.java index 7668bf41..5f7d405d 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/LocalController.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/LocalController.java @@ -1,59 +1,59 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Brock Janiczak - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-
-import org.jacoco.core.data.ExecutionDataWriter;
-import org.jacoco.core.runtime.AgentOptions;
-import org.jacoco.core.runtime.IRuntime;
-
-/**
- * Local only agent controller that will write coverage data to the filesystem.
- * This controller uses the following agent options:
- * <ul>
- * <li>destfile</li>
- * <li>append</li>
- * </ul>
- */
-public class LocalController implements IAgentController {
-
- private IRuntime runtime;
-
- private OutputStream output;
-
- public final void startup(final AgentOptions options, final IRuntime runtime)
- throws IOException {
- this.runtime = runtime;
- final File destFile = new File(options.getDestfile()).getAbsoluteFile();
- final File folder = destFile.getParentFile();
- if (folder != null) {
- folder.mkdirs();
- }
- output = new BufferedOutputStream(new FileOutputStream(destFile,
- options.getAppend()));
- }
-
- public void writeExecutionData() throws IOException {
- final ExecutionDataWriter writer = new ExecutionDataWriter(output);
- runtime.collect(writer, writer, false);
- }
-
- public void shutdown() throws IOException {
- output.close();
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Brock Janiczak - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import org.jacoco.core.data.ExecutionDataWriter; +import org.jacoco.core.runtime.AgentOptions; +import org.jacoco.core.runtime.IRuntime; + +/** + * Local only agent controller that will write coverage data to the filesystem. + * This controller uses the following agent options: + * <ul> + * <li>destfile</li> + * <li>append</li> + * </ul> + */ +public class LocalController implements IAgentController { + + private IRuntime runtime; + + private OutputStream output; + + public final void startup(final AgentOptions options, final IRuntime runtime) + throws IOException { + this.runtime = runtime; + final File destFile = new File(options.getDestfile()).getAbsoluteFile(); + final File folder = destFile.getParentFile(); + if (folder != null) { + folder.mkdirs(); + } + output = new BufferedOutputStream(new FileOutputStream(destFile, + options.getAppend())); + } + + public void writeExecutionData() throws IOException { + final ExecutionDataWriter writer = new ExecutionDataWriter(output); + runtime.collect(writer, writer, false); + } + + public void shutdown() throws IOException { + output.close(); + } + +} diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpClientController.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpClientController.java index 6539904f..e59c4abd 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpClientController.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpClientController.java @@ -1,88 +1,88 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Marc R. Hoffmann - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import java.io.IOException;
-import java.net.Socket;
-
-import org.jacoco.agent.rt.IExceptionLogger;
-import org.jacoco.core.runtime.AgentOptions;
-import org.jacoco.core.runtime.IRuntime;
-
-/**
- * Controller that connects to a TCP port. This controller uses the following
- * agent options:
- * <ul>
- * <li>address</li>
- * <li>port</li>
- * </ul>
- */
-public class TcpClientController implements IAgentController {
-
- private final IExceptionLogger logger;
-
- private TcpConnection connection;
-
- private Thread worker;
-
- /**
- * New controller instance.
- *
- * @param logger
- * logger to use in case of exceptions is spawned threads
- */
- public TcpClientController(final IExceptionLogger logger) {
- this.logger = logger;
- }
-
- public void startup(final AgentOptions options, final IRuntime runtime)
- throws IOException {
- final Socket socket = createSocket(options);
- connection = new TcpConnection(socket, runtime);
- connection.init();
- worker = new Thread(new Runnable() {
- public void run() {
- try {
- connection.run();
- } catch (final IOException e) {
- logger.logExeption(e);
- }
- }
- });
- worker.setName(getClass().getName());
- worker.setDaemon(true);
- worker.start();
- }
-
- public void shutdown() throws Exception {
- connection.close();
- worker.join();
- }
-
- public void writeExecutionData() throws IOException {
- connection.writeExecutionData();
- }
-
- /**
- * Open a socket based on the given configuration.
- *
- * @param options
- * address and port configuration
- * @return opened socket
- * @throws IOException
- */
- protected Socket createSocket(final AgentOptions options)
- throws IOException {
- return new Socket(options.getAddress(), options.getPort());
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import java.io.IOException; +import java.net.Socket; + +import org.jacoco.agent.rt.IExceptionLogger; +import org.jacoco.core.runtime.AgentOptions; +import org.jacoco.core.runtime.IRuntime; + +/** + * Controller that connects to a TCP port. This controller uses the following + * agent options: + * <ul> + * <li>address</li> + * <li>port</li> + * </ul> + */ +public class TcpClientController implements IAgentController { + + private final IExceptionLogger logger; + + private TcpConnection connection; + + private Thread worker; + + /** + * New controller instance. + * + * @param logger + * logger to use in case of exceptions is spawned threads + */ + public TcpClientController(final IExceptionLogger logger) { + this.logger = logger; + } + + public void startup(final AgentOptions options, final IRuntime runtime) + throws IOException { + final Socket socket = createSocket(options); + connection = new TcpConnection(socket, runtime); + connection.init(); + worker = new Thread(new Runnable() { + public void run() { + try { + connection.run(); + } catch (final IOException e) { + logger.logExeption(e); + } + } + }); + worker.setName(getClass().getName()); + worker.setDaemon(true); + worker.start(); + } + + public void shutdown() throws Exception { + connection.close(); + worker.join(); + } + + public void writeExecutionData() throws IOException { + connection.writeExecutionData(); + } + + /** + * Open a socket based on the given configuration. + * + * @param options + * address and port configuration + * @return opened socket + * @throws IOException + */ + protected Socket createSocket(final AgentOptions options) + throws IOException { + return new Socket(options.getAddress(), options.getPort()); + } + +} diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpConnection.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpConnection.java index 2bff22bb..348eeec0 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpConnection.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpConnection.java @@ -1,109 +1,109 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Marc R. Hoffmann - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import java.io.IOException;
-import java.net.Socket;
-import java.net.SocketException;
-
-import org.jacoco.core.runtime.IRemoteCommandVisitor;
-import org.jacoco.core.runtime.IRuntime;
-import org.jacoco.core.runtime.RemoteControlReader;
-import org.jacoco.core.runtime.RemoteControlWriter;
-
-/**
- * Handler for a single socket based remote connection.
- */
-class TcpConnection implements IRemoteCommandVisitor {
-
- private final IRuntime runtime;
-
- private final Socket socket;
-
- private RemoteControlWriter writer;
-
- private RemoteControlReader reader;
-
- private boolean initialized;
-
- public TcpConnection(final Socket socket, final IRuntime runtime) {
- this.socket = socket;
- this.runtime = runtime;
- this.initialized = false;
- }
-
- public void init() throws IOException {
- this.writer = new RemoteControlWriter(socket.getOutputStream());
- this.reader = new RemoteControlReader(socket.getInputStream());
- this.reader.setRemoteCommandVisitor(this);
- this.initialized = true;
- }
-
- /**
- * Processes all requests for this session until the socket is closed.
- *
- * @throws IOException
- * in case of problems whith the connection
- */
- public void run() throws IOException {
- try {
- while (reader.read()) {
- }
- } catch (final SocketException e) {
- // If the local socket is closed while polling for commands the
- // SocketException is expected.
- if (!socket.isClosed()) {
- throw e;
- }
- } finally {
- close();
- }
- }
-
- /**
- * Dumps the current execution data if the connection is already initialized
- * and the underlying socket is still open.
- *
- * @throws IOException
- */
- public void writeExecutionData() throws IOException {
- if (initialized && !socket.isClosed()) {
- visitDumpCommand(true, false);
- }
- }
-
- /**
- * Closes the underlying socket if not closed yet.
- *
- * @throws IOException
- */
- public void close() throws IOException {
- if (!socket.isClosed()) {
- socket.close();
- }
- }
-
- // === IRemoteCommandVisitor ===
-
- public void visitDumpCommand(final boolean dump, final boolean reset)
- throws IOException {
- if (dump) {
- runtime.collect(writer, writer, reset);
- } else {
- if (reset) {
- runtime.reset();
- }
- }
- writer.sendCmdOk();
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import java.io.IOException; +import java.net.Socket; +import java.net.SocketException; + +import org.jacoco.core.runtime.IRemoteCommandVisitor; +import org.jacoco.core.runtime.IRuntime; +import org.jacoco.core.runtime.RemoteControlReader; +import org.jacoco.core.runtime.RemoteControlWriter; + +/** + * Handler for a single socket based remote connection. + */ +class TcpConnection implements IRemoteCommandVisitor { + + private final IRuntime runtime; + + private final Socket socket; + + private RemoteControlWriter writer; + + private RemoteControlReader reader; + + private boolean initialized; + + public TcpConnection(final Socket socket, final IRuntime runtime) { + this.socket = socket; + this.runtime = runtime; + this.initialized = false; + } + + public void init() throws IOException { + this.writer = new RemoteControlWriter(socket.getOutputStream()); + this.reader = new RemoteControlReader(socket.getInputStream()); + this.reader.setRemoteCommandVisitor(this); + this.initialized = true; + } + + /** + * Processes all requests for this session until the socket is closed. + * + * @throws IOException + * in case of problems whith the connection + */ + public void run() throws IOException { + try { + while (reader.read()) { + } + } catch (final SocketException e) { + // If the local socket is closed while polling for commands the + // SocketException is expected. + if (!socket.isClosed()) { + throw e; + } + } finally { + close(); + } + } + + /** + * Dumps the current execution data if the connection is already initialized + * and the underlying socket is still open. + * + * @throws IOException + */ + public void writeExecutionData() throws IOException { + if (initialized && !socket.isClosed()) { + visitDumpCommand(true, false); + } + } + + /** + * Closes the underlying socket if not closed yet. + * + * @throws IOException + */ + public void close() throws IOException { + if (!socket.isClosed()) { + socket.close(); + } + } + + // === IRemoteCommandVisitor === + + public void visitDumpCommand(final boolean dump, final boolean reset) + throws IOException { + if (dump) { + runtime.collect(writer, writer, reset); + } else { + if (reset) { + runtime.reset(); + } + } + writer.sendCmdOk(); + } + +} diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpServerController.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpServerController.java index 5408e3a2..3538b087 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpServerController.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpServerController.java @@ -1,126 +1,126 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Marc R. Hoffmann - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.ServerSocket;
-import java.net.UnknownHostException;
-
-import org.jacoco.agent.rt.IExceptionLogger;
-import org.jacoco.core.runtime.AgentOptions;
-import org.jacoco.core.runtime.IRuntime;
-
-/**
- * Controller that opens TCP server socket. This controller uses the following
- * agent options:
- * <ul>
- * <li>address</li>
- * <li>port</li>
- * </ul>
- */
-public class TcpServerController implements IAgentController {
-
- private TcpConnection connection;
-
- private final IExceptionLogger logger;
-
- private ServerSocket serverSocket;
-
- private Thread worker;
-
- /**
- * New controller instance.
- *
- * @param logger
- * logger to use in case of exceptions is spawned threads
- */
- public TcpServerController(final IExceptionLogger logger) {
- this.logger = logger;
- }
-
- public void startup(final AgentOptions options, final IRuntime runtime)
- throws IOException {
- serverSocket = createServerSocket(options);
- worker = new Thread(new Runnable() {
- public void run() {
- while (!serverSocket.isClosed()) {
- try {
- synchronized (serverSocket) {
- connection = new TcpConnection(
- serverSocket.accept(), runtime);
- }
- connection.init();
- connection.run();
- } catch (final IOException e) {
- // If the serverSocket is closed while accepting
- // connections a SocketException is expected.
- if (!serverSocket.isClosed()) {
- logger.logExeption(e);
- }
- }
- }
- }
- });
- worker.setName(getClass().getName());
- worker.setDaemon(true);
- worker.start();
- }
-
- public void shutdown() throws Exception {
- serverSocket.close();
- synchronized (serverSocket) {
- if (connection != null) {
- connection.close();
- }
- }
- worker.join();
- }
-
- public void writeExecutionData() throws IOException {
- if (connection != null) {
- connection.writeExecutionData();
- }
- }
-
- /**
- * Open a server socket based on the given configuration.
- *
- * @param options
- * address and port configuration
- * @return opened server socket
- * @throws IOException
- */
- protected ServerSocket createServerSocket(final AgentOptions options)
- throws IOException {
- final InetAddress inetAddr = getInetAddress(options.getAddress());
- return new ServerSocket(options.getPort(), 1, inetAddr);
- }
-
- /**
- * Returns the {@link InetAddress} object to open the server socket on.
- *
- * @param address
- * address specified as a string
- * @return address to open the server socket
- * @throws UnknownHostException
- */
- protected InetAddress getInetAddress(final String address)
- throws UnknownHostException {
- if ("*".equals(address)) {
- return null;
- } else {
- return InetAddress.getByName(address);
- }
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.UnknownHostException; + +import org.jacoco.agent.rt.IExceptionLogger; +import org.jacoco.core.runtime.AgentOptions; +import org.jacoco.core.runtime.IRuntime; + +/** + * Controller that opens TCP server socket. This controller uses the following + * agent options: + * <ul> + * <li>address</li> + * <li>port</li> + * </ul> + */ +public class TcpServerController implements IAgentController { + + private TcpConnection connection; + + private final IExceptionLogger logger; + + private ServerSocket serverSocket; + + private Thread worker; + + /** + * New controller instance. + * + * @param logger + * logger to use in case of exceptions is spawned threads + */ + public TcpServerController(final IExceptionLogger logger) { + this.logger = logger; + } + + public void startup(final AgentOptions options, final IRuntime runtime) + throws IOException { + serverSocket = createServerSocket(options); + worker = new Thread(new Runnable() { + public void run() { + while (!serverSocket.isClosed()) { + try { + synchronized (serverSocket) { + connection = new TcpConnection( + serverSocket.accept(), runtime); + } + connection.init(); + connection.run(); + } catch (final IOException e) { + // If the serverSocket is closed while accepting + // connections a SocketException is expected. + if (!serverSocket.isClosed()) { + logger.logExeption(e); + } + } + } + } + }); + worker.setName(getClass().getName()); + worker.setDaemon(true); + worker.start(); + } + + public void shutdown() throws Exception { + serverSocket.close(); + synchronized (serverSocket) { + if (connection != null) { + connection.close(); + } + } + worker.join(); + } + + public void writeExecutionData() throws IOException { + if (connection != null) { + connection.writeExecutionData(); + } + } + + /** + * Open a server socket based on the given configuration. + * + * @param options + * address and port configuration + * @return opened server socket + * @throws IOException + */ + protected ServerSocket createServerSocket(final AgentOptions options) + throws IOException { + final InetAddress inetAddr = getInetAddress(options.getAddress()); + return new ServerSocket(options.getPort(), 1, inetAddr); + } + + /** + * Returns the {@link InetAddress} object to open the server socket on. + * + * @param address + * address specified as a string + * @return address to open the server socket + * @throws UnknownHostException + */ + protected InetAddress getInetAddress(final String address) + throws UnknownHostException { + if ("*".equals(address)) { + return null; + } else { + return InetAddress.getByName(address); + } + } + +} |