Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mechanism for sending messages to execution log from Java #112

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@
<!-- Modules Versions -->
<daisy.calabash-adapter.version>2.0.4-SNAPSHOT</daisy.calabash-adapter.version>
<daisy.calabash-custom-steps.version>2.0.0</daisy.calabash-custom-steps.version>
<daisy.common-utils.version>2.1.3-SNAPSHOT</daisy.common-utils.version>
<daisy.framework-core.version>3.0.1-SNAPSHOT</daisy.framework-core.version>
<daisy.common-utils.version>3.0.0-SNAPSHOT</daisy.common-utils.version>
<daisy.framework-core.version>3.1.0-SNAPSHOT</daisy.framework-core.version>
<daisy.framework-persistence.version>2.0.4-SNAPSHOT</daisy.framework-persistence.version>
<daisy.framework-volatile.version>2.0.2-SNAPSHOT</daisy.framework-volatile.version>
<daisy.launcher.version>1.0.0</daisy.launcher.version>
<daisy.logging-activator.version>2.0.2-SNAPSHOT</daisy.logging-activator.version>
<daisy.logging-appender.version>1.1.1-SNAPSHOT</daisy.logging-appender.version>
<daisy.modules-registry.version>2.0.0</daisy.modules-registry.version>
<daisy.persistence-derby.version>2.0.0</daisy.persistence-derby.version>
<daisy.persistence-derby.version>2.0.1-SNAPSHOT</daisy.persistence-derby.version>
<daisy.persistence-mysql.version>2.0.0</daisy.persistence-mysql.version>
<daisy.push-notifier.version>2.0.0</daisy.push-notifier.version>
<daisy.push-notifier.version>2.0.1-SNAPSHOT</daisy.push-notifier.version>
<daisy.updater.version>1.0.1-SNAPSHOT</daisy.updater.version>
<daisy.webservice.version>2.1.1-SNAPSHOT</daisy.webservice.version>
<daisy.webservice-utils.version>2.2.1-SNAPSHOT</daisy.webservice-utils.version>
<daisy.woodstox-osgi-adapter.version>2.0.0</daisy.woodstox-osgi-adapter.version>
<daisy.xmlcatalog.version>2.0.2-SNAPSHOT</daisy.xmlcatalog.version>
<daisy.xpath-registry.version>1.0.1</daisy.xpath-registry.version>
<daisy.xproc-api.version>2.0.0</daisy.xproc-api.version>
<daisy.xproc-api.version>2.0.1-SNAPSHOT</daisy.xproc-api.version>

<!-- Dependencies Versions -->
<benchmark.version>0.7.2</benchmark.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class EventBusMessageListener implements XProcMessageListener {
EventBusProvider eventBus;
MessageBuliderFactory messageBuilderFactory;
Properties props;
int sequence = 0;

public EventBusMessageListener(EventBusProvider eventBus,
Properties props) {
Expand All @@ -40,7 +39,6 @@ private void post(MessageBuilder builder) {
if (props != null && props.getProperty("JOB_ID")!=null) {
builder.withJobId(props.getProperty("JOB_ID"));
}
builder.withSequence(sequence++);
builder.withTimeStamp(new Date());
eventBus.get().post(builder.build());
}
Expand Down
2 changes: 1 addition & 1 deletion common-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<artifactId>common-utils</artifactId>
<version>2.1.3-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>

<packaging>bundle</packaging>
<name>DAISY Pipeline 2 :: Common Utilities</name>
Expand Down
20 changes: 13 additions & 7 deletions common-utils/src/main/java/org/daisy/common/messaging/Message.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.daisy.common.messaging;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Message {
/**
Expand Down Expand Up @@ -86,7 +88,6 @@ public static class MessageBuilder {
int line;
int column;
Date timeStamp;
int sequence;
String jobId;
String file;

Expand Down Expand Up @@ -127,12 +128,6 @@ public MessageBuilder withTimeStamp(Date timeStamp) {
}


public MessageBuilder withSequence(int sequence) {
this.sequence = sequence;
return this;
}


public MessageBuilder withJobId(String jobId) {
this.jobId = jobId;
return this;
Expand All @@ -145,11 +140,22 @@ public MessageBuilder withFile(String file) {
}

public Message build() {
Integer sequence; {
if (jobId == null)
sequence = 0;
else {
sequence = messageCounts.get(jobId);
if (sequence == null)
sequence = 0;
messageCounts.put(jobId, sequence + 1);
}
}
return new Message(throwable, text, level, line, column,
timeStamp, sequence, jobId, file);
}

}

private final static Map<String,Integer> messageCounts = new HashMap<String,Integer>();

}
2 changes: 1 addition & 1 deletion framework-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<relativePath>../parent</relativePath>
</parent>
<artifactId>framework-core</artifactId>
<version>3.0.1-SNAPSHOT</version>
<version>3.1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>DAISY Pipeline 2 :: Framework Core</name>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ private final void broadcastError(String text){
.withJobId(this.getId().toString())
.withLevel(Level.ERROR)
.withText(text)
.withSequence(1)
.build();
if (this.eventBus!=null)
this.eventBus.post(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public class VolatileMessageAccessorTest {
public void setUp() {
String id = JobIdFactory.newId().toString();
m1 = new Message.MessageBuilder().withText("message1")
.withLevel(Level.INFO).withSequence(0).withJobId(id).build();
.withLevel(Level.INFO).withJobId(id).build();

m2 = new Message.MessageBuilder().withText("message2")
.withLevel(Level.ERROR).withSequence(1).withJobId(id)
.withLevel(Level.ERROR).withJobId(id)
.build();

m3 = new Message.MessageBuilder().withText("message3")
.withLevel(Level.WARNING).withSequence(2).withJobId(id)
.withLevel(Level.WARNING).withJobId(id)
.build();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.daisy.common.messaging.Message;
import org.daisy.common.messaging.Message.Level;
import org.daisy.pipeline.job.JobIdFactory;
import org.daisy.pipeline.nonpersistent.impl.messaging.VolatileMessageStorage;
import org.junit.After;
import org.junit.Assert;
Expand All @@ -12,6 +13,8 @@

public class VolatileMessageStorageTest {

String jobId;

Message m1;

Message m2;
Expand All @@ -22,15 +25,16 @@ public class VolatileMessageStorageTest {

@Before
public void setUp() {
jobId = JobIdFactory.newId().toString();
m1 = new Message.MessageBuilder().withText("message1")
.withLevel(Level.INFO).withSequence(0).withJobId("id1").build();
.withLevel(Level.INFO).withJobId(jobId).build();

m2 = new Message.MessageBuilder().withText("message2")
.withLevel(Level.ERROR).withSequence(1).withJobId("id1")
.withLevel(Level.ERROR).withJobId(jobId)
.build();

m3 = new Message.MessageBuilder().withText("message3")
.withLevel(Level.DEBUG).withSequence(2).withJobId("id1")
.withLevel(Level.DEBUG).withJobId(jobId)
.build();

}
Expand All @@ -43,7 +47,7 @@ public void tearDown(){
public void add() {
storage.add(m1);
storage.add(m2);
Assert.assertEquals(storage.get("id1").size(), 2);
Assert.assertEquals(storage.get(jobId).size(), 2);
}

@Test
Expand All @@ -61,19 +65,19 @@ public void run() {
};
sleeper.start();
sleeper.join();
Assert.assertEquals(storage.get("id1").size(), 0);
Assert.assertEquals(storage.get(jobId).size(), 0);

}
@Test
public void addDebug() {
storage.add(m3);
Assert.assertEquals(0,storage.get("id1").size());
Assert.assertEquals(0,storage.get(jobId).size());
}
@Test
public void get() {
storage.add(m1);
storage.add(m2);
List<Message> list= storage.get("id1");
List<Message> list= storage.get(jobId);
Assert.assertEquals(0,list.get(0).getSequence());
Assert.assertEquals(1,list.get(1).getSequence());
}
Expand All @@ -88,8 +92,8 @@ public void getEmpty() {
public void remove() {
storage.add(m1);
storage.add(m2);
storage.remove("id1");
Assert.assertEquals(0,storage.get("id1").size());
storage.remove(jobId);
Assert.assertEquals(0,storage.get(jobId).size());
}

}
13 changes: 13 additions & 0 deletions logging-appender/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
<name>DAISY Pipeline 2 :: Logging Appender</name>

<dependencies>
<dependency>
<groupId>org.daisy.pipeline</groupId>
<artifactId>common-utils</artifactId>
</dependency>
<dependency>
<groupId>org.daisy.pipeline</groupId>
<artifactId>framework-core</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.daisy.pipeline.logging;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.sift.MDCBasedDiscriminator;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;

import org.daisy.common.messaging.Message;
import org.daisy.common.messaging.MessageBuliderFactory;
import org.daisy.pipeline.event.EventBusProvider;

import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;

/**
* Configure like this:
*
* &lt;appender name="EVENTBUS" class="org.daisy.pipeline.logging.EventBusAppender"&gt;
* &lt;filter class="org.daisy.pipeline.logging.ThresholdFilter"&gt;
* &lt;rootLevel&gt;INFO&lt;/rootLevel&gt;
* &lt;loggerLevels&gt;
* cz.vutbr.web=WARN
* org.daisy.common.xproc.calabash.steps.Message=WARN
* com.xmlcalabash.runtime.XAtomicStep=WARN
* &lt;/loggerLevels&gt;
* &lt;/filter&gt;
* &lt;/appender&gt;
*/
public class EventBusAppender extends AppenderBase<ILoggingEvent> {

private EventBusProvider eventBusProvider;
private BundleContext bundleContext;
private MessageBuliderFactory messageBuilderFactory;
private List<ILoggingEvent> eventBuffer;
private MDCBasedDiscriminator discriminator;

@Override
public void start() {
bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way of getting an osgi agnostic appender? If the effort is to big just leave it like that

Copy link
Member Author

@bertfrees bertfrees Jun 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from Slack:

@capitancambio Yes, you're right, and this while we are trying to go in the other direction :). I'm not sure there is a way to avoid it. I couldn't use declarative services because it is a fragment bundle. Maybe the only way is to have several artifacts, one for OSGi, and one for each other dependency injection mechanism you want to use?

messageBuilderFactory = new MessageBuliderFactory();
eventBuffer = new ArrayList<ILoggingEvent>();
discriminator = new MDCBasedDiscriminator();
discriminator.setKey("jobid");
discriminator.setDefaultValue("default");
super.start();
}

protected void append(ILoggingEvent event) {
if (!isStarted())
return;
String jobId = discriminator.getDiscriminatingValue(event);
if (!"default".equals(jobId)) {
if (eventBusProvider == null) {
try {
eventBusProvider = bundleContext.getService(
bundleContext.getServiceReferences(EventBusProvider.class, null).iterator().next());
for (ILoggingEvent e : eventBuffer)
append(eventBusProvider, discriminator.getDiscriminatingValue(e), e);
eventBuffer.clear(); }
catch (Exception e) {
eventBuffer.add(event);
return; }}
append(eventBusProvider, jobId, event); }
}

private void append(EventBusProvider eventBus, String jobId, ILoggingEvent event) {
Level level = event.getLevel();
Message m = messageBuilderFactory.newMessageBuilder()
.withTimeStamp(new Date())
.withJobId(jobId)
.withLevel(messageLevelFromLogbackLevel(level))
.withText(event.getFormattedMessage())
.build();
eventBus.get().post(m);
}

private static Message.Level messageLevelFromLogbackLevel(Level level) {
switch(level.toInt()) {
case Level.TRACE_INT:
return Message.Level.TRACE;
case Level.DEBUG_INT:
return Message.Level.DEBUG;
case Level.INFO_INT:
return Message.Level.INFO;
case Level.WARN_INT:
return Message.Level.WARNING;
case Level.ERROR_INT:
return Message.Level.ERROR;
case Level.ALL_INT:
case Level.OFF_INT:
default:
return null; }
}
}
Loading