Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
Start a logging system for bootstrappy bits.
Browse files Browse the repository at this point in the history
Enable with

  -Dswarm.bootstrap.log.NAME

Where NAME is a simple single word, such as:

  runtime

Use via

  BootstrapLogger.logger( "runtime" )

Currently all levels are enabled.

If you want to use it, you'll need a dependency on org.wildfly.swarm.bootstrap,
with optional="true" in your module.xml.
  • Loading branch information
bobmcwhirter committed Dec 23, 2015
1 parent 926ce39 commit 3e3e070
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 15 deletions.
@@ -0,0 +1,124 @@
package org.wildfly.swarm.bootstrap;

import java.util.Date;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/**
* @author Bob McWhirter
*/
public class BootstrapLogger {

private static class Manager {
static final Manager INSTANCE = new Manager();

private static final String PREFIX = "swarm.bootstrap.log.";

private final Set<String> enabled = new HashSet<>();

private Manager() {
Properties props = System.getProperties();
Set<String> names = props.stringPropertyNames();
for (String name : names) {
if ( name.startsWith( PREFIX ) ) {
String bit = name.substring( PREFIX.length() );
this.enabled.add( bit );
}
}
}

public synchronized void log(String name, String level, String message) {
if ( ! this.enabled.contains(name) ) {
return;
}

Date now = new Date();
String[] lines = message.split("\n");

for (String line : lines) {
System.err.println(String.format("%s %s [%s] (%s) %s",
now,
level,
name,
Thread.currentThread().getName(),
line));
}
}


public synchronized void log(String name, String level, Throwable t) {
if ( ! this.enabled.contains(name) ) {
return;
}
System.err.println(String.format("%s %s [%s] (%s) %s",
new Date().toString(),
level,
name,
Thread.currentThread().getName(),
t.getMessage()));
for (StackTraceElement stackTraceElement : t.getStackTrace()) {
System.err.println(" " + stackTraceElement.toString());
}
}

public void error(String name, String message) {
log(name, "ERROR", message);
}

public void error(String name, Throwable t) {
log(name, "ERROR", t);
}

public void error(String name, String message, Throwable t) {
log(name, "ERROR", message);
log(name, "ERROR", t);
}

public void debug(String name, Object message) {
log(name, "DEBUG", message.toString());
}

public void warn(String name, Object message) {
log(name, "WARN", message.toString());
}

public void info(String name, Object message) {
log(name, "INFO", message.toString());
}
}

private final String name;

private BootstrapLogger(String name) {
this.name = name;
}

public static BootstrapLogger logger(String name) {
return new BootstrapLogger(name);
}

public void error(String message) {
Manager.INSTANCE.error(this.name, message);
}

public void error(Throwable t) {
Manager.INSTANCE.error(this.name, t);
}

public void error(String message, Throwable t) {
Manager.INSTANCE.error(this.name, message, t);
}

public void debug(String message) {
Manager.INSTANCE.debug(this.name, message);
}

public void warn(String message) {
Manager.INSTANCE.warn(this.name, message);
}

public void info(String message) {
Manager.INSTANCE.info(this.name, message);
}
}
Expand Up @@ -50,6 +50,7 @@ public void buildModule(ModuleSpec.Builder builder, ModuleLoader delegateLoader)
builder.addDependency(DependencySpec.createModuleDependencySpec(ModuleIdentifier.create("org.jboss.shrinkwrap")));
builder.addDependency(DependencySpec.createModuleDependencySpec(ModuleIdentifier.create("javax.api")));
HashSet<String> paths = new HashSet<String>();
paths.add( "org/wildfly/swarm/bootstrap");
paths.add( "org/wildfly/swarm/bootstrap/util");
builder.addDependency(DependencySpec.createSystemDependencySpec( paths, true ) );
} catch (IOException e) {
Expand Down
Expand Up @@ -155,6 +155,7 @@ private void createServer(boolean debugBootstrap) throws Exception {
Class<?> serverClass = module.getClassLoader().loadClass("org.wildfly.swarm.container.runtime.RuntimeServer");
try {
this.server = (Server) serverClass.newInstance();
this.server.debug( debugBootstrap );
} catch (Throwable t) {
t.printStackTrace();
}
Expand Down
Expand Up @@ -25,6 +25,8 @@ public interface Server {
Deployer start(Container config) throws Exception;
void stop() throws Exception;

void debug(boolean debug);

Set<Class<? extends Fraction>> getFractionTypes();
Fraction createDefaultFor(Class<? extends Fraction> fractionClazz);
}
Expand Up @@ -12,6 +12,7 @@

<dependencies>
<module name="org.wildfly.swarm.container"/>
<module name="org.wildfly.swarm.bootstrap" optional="true"/>

<module name="org.jboss.modules"/>
<module name="org.jboss.vfs"/>
Expand Down
Expand Up @@ -21,10 +21,13 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.dmr.ModelNode;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.vfs.TempFileProvider;
import org.jboss.vfs.VFS;
Expand Down Expand Up @@ -56,13 +59,17 @@ public class RuntimeDeployer implements Deployer {

private final List<Closeable> mountPoints = new ArrayList<>();

private boolean debug = false;

public RuntimeDeployer(List<ServerConfiguration<Fraction>> configurations, ModelControllerClient client, SimpleContentProvider contentProvider, TempFileProvider tempFileProvider) throws IOException {
this.configurations = configurations;
this.client = client;
this.contentProvider = contentProvider;
this.tempFileProvider = tempFileProvider;
//this.executor = Executors.newSingleThreadScheduledExecutor();
//this.tempFileProvider = TempFileProvider.create("wildfly-swarm", this.executor);
}

public void debug(boolean debug) {
this.debug = debug;
}

@Override
Expand All @@ -72,12 +79,12 @@ public void deploy(Archive<?> deployment) throws DeploymentException {
each.prepareArchive(deployment);
}

/*
Map<ArchivePath, Node> c = deployment.getContent();
for (Map.Entry<ArchivePath, Node> each : c.entrySet()) {
System.err.println(each.getKey() + " // " + each.getValue());
if (this.debug) {
Map<ArchivePath, Node> c = deployment.getContent();
for (Map.Entry<ArchivePath, Node> each : c.entrySet()) {
System.err.println(each.getKey() + " // " + each.getValue());
}
}
*/

String dump = System.getProperty("swarm.export.deployment");
if (dump != null &&
Expand All @@ -92,9 +99,8 @@ public void deploy(Archive<?> deployment) throws DeploymentException {
try (InputStream in = deployment.as(ZipExporter.class).exportAsInputStream()) {
Closeable closeable = VFS.mountZipExpanded(in, deployment.getName(), mountPoint, tempFileProvider);
this.mountPoints.add(closeable);
//System.err.println( "mount: " + mountPoint + " // " + mountPoint.getPhysicalFile() );
} catch (IOException e) {
throw new DeploymentException( deployment, e);
throw new DeploymentException(deployment, e);
}

byte[] hash = this.contentProvider.addContent(mountPoint);
Expand All @@ -118,13 +124,11 @@ public void deploy(Archive<?> deployment) throws DeploymentException {
return;
}

ModelNode description = result.get("failure-description" );
throw new DeploymentException( deployment, description.asString() );
ModelNode description = result.get("failure-description");
throw new DeploymentException(deployment, description.asString());
} catch (IOException e) {
throw new DeploymentException( deployment, e );
throw new DeploymentException(deployment, e);
}

// TODO handle a non-success result
}

void stop() {
Expand Down
Expand Up @@ -51,6 +51,7 @@
import org.jboss.msc.value.ImmediateValue;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.vfs.TempFileProvider;
import org.wildfly.swarm.bootstrap.BootstrapLogger;
import org.wildfly.swarm.container.Container;
import org.wildfly.swarm.container.Deployer;
import org.wildfly.swarm.container.Fraction;
Expand Down Expand Up @@ -92,6 +93,8 @@ public class RuntimeServer implements Server {

private List<ServerConfiguration<Fraction>> configList = new ArrayList<>();

private boolean debug = false;

@SuppressWarnings("unused")
public RuntimeServer() {
try {
Expand All @@ -111,6 +114,11 @@ public RuntimeServer() {
}
}

@Override
public void debug(boolean debug) {
this.debug = debug;
}

@Override
public Deployer start(Container config) throws Exception {

Expand All @@ -130,7 +138,8 @@ public Deployer start(Container config) throws Exception {
// float all <extension> up to the head of the list
list.sort(new ExtensionOpPriorityComparator());

//System.err.println( list );
//System.err.println( list );
BootstrapLogger.logger( "runtime" ).info( list.toString() );

Thread.currentThread().setContextClassLoader(RuntimeServer.class.getClassLoader());

Expand Down Expand Up @@ -179,6 +188,7 @@ public Deployer start(Container config) throws Exception {

this.client = controller.createClient(executor);
this.deployer = new RuntimeDeployer(this.configList, this.client, this.contentProvider, tempFileProvider);
this.deployer.debug( this.debug );

List<Archive> implicitDeployments = new ArrayList<>();

Expand Down

0 comments on commit 3e3e070

Please sign in to comment.