Skip to content

Commit

Permalink
Manage Hibernate reps, using logback in KRS.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Børlum committed Oct 26, 2011
1 parent 2b0c3f8 commit 6dd9ec7
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 70 deletions.
12 changes: 5 additions & 7 deletions dodi/data-manager/pom.xml
Expand Up @@ -53,24 +53,24 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${jboss6.hibernate.version}</version>
</dependency>

<!-- Logging -->

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${jboss6.slf4j.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${jboss6.slf4j.version}</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>

<!-- Test -->

Expand All @@ -92,8 +92,6 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${jboss6.slf4j.version}</version>
<scope>test</scope>
</dependency>

<!-- Scheduler -->
Expand Down
15 changes: 0 additions & 15 deletions nsp/authorization-lookup-ws/pom.xml
Expand Up @@ -31,15 +31,11 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${jboss6.slf4j.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${jboss6.slf4j.version}</version>
<scope>runtime</scope>
</dependency>

<dependency>
Expand All @@ -57,21 +53,11 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${jboss6.hibernate.version}</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${jboss6.hibernate.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down Expand Up @@ -99,7 +85,6 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${jboss6.slf4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
39 changes: 26 additions & 13 deletions nsp/batch-copy-ws/pom.xml
Expand Up @@ -49,47 +49,65 @@
<artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- Hibernate -->

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${jboss6.hibernate.version}</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${jboss6.hibernate.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${jboss6.hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>dk.sdsd.nsp</groupId>
<artifactId>nsp-util</artifactId>
</dependency>

<!-- Commons -->

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>

<!-- Logging -->

<!--
| This component doesn't use the provided
| SLF4J but a newer version and Logback.
| Other components should be migrated to
| do the same.
-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${war.slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${war.slf4j.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>1.8.3</version>
</dependency>

<!-- Test -->
Expand All @@ -108,11 +126,6 @@
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>

<dependency>
<groupId>dk.nsi.stamdata</groupId>
Expand Down
Expand Up @@ -29,6 +29,8 @@

import java.util.Properties;

import ch.qos.logback.classic.helpers.MDCInsertingServletFilter;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
Expand Down Expand Up @@ -101,9 +103,20 @@ protected void configureServlets()
bind(ComponentMonitor.class).to(ComponentMonitorImpl.class);
install(new MonitoringModule());

// Logging

// The mandatory SLA filter.
//
bind(SLALoggingServletFilter.class).in(Scopes.SINGLETON);
filterRegex(ALL_EXCEPT_STATUS_PAGE).through(SLALoggingServletFilter.class);

// Inserts IP and other goodies into the MDC.
//
bind(MDCInsertingServletFilter.class).in(Scopes.SINGLETON);
filterRegex(ALL_EXCEPT_STATUS_PAGE).through(MDCInsertingServletFilter.class);

// Security

install(new DenGodeWebServiceModule());
}
}
Expand Down
Expand Up @@ -33,6 +33,9 @@
import javax.xml.ws.Holder;

import org.hibernate.ScrollableResults;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.w3c.dom.Document;

import com.google.inject.Inject;
Expand All @@ -50,12 +53,15 @@
import dk.nsi.stamdata.replication.models.Client;
import dk.nsi.stamdata.replication.models.ClientDao;
import dk.nsi.stamdata.views.View;
import dk.nsi.stamdata.views.Views;

@GuiceWebservice
@SchemaValidation
@WebService(serviceName = "StamdataReplication", endpointInterface = "dk.nsi.stamdata.replication.jaxws.StamdataReplication")
public class StamdataReplicationImpl implements StamdataReplication {

private static final Logger logger = LoggerFactory.getLogger(StamdataReplicationImpl.class);

private static final int MAX_RECORD_LIMIT = 2000;

private final String cvr;
Expand All @@ -80,33 +86,73 @@ public class StamdataReplicationImpl implements StamdataReplication {
@Override
public ReplicationResponseType replicate(Holder<Security> wsseHeader, Holder<Header> medcomHeader, ReplicationRequestType parameters) throws ReplicationFault
{
Class<? extends View> requestedView = getViewClass(parameters);

// Validate authentication.
//
Client client = clients.findByCvr(cvr);
if (client == null || !client.isAuthorizedFor(requestedView))
try
{
throw new ReplicationFault("The provided cvr is not authorized to fetch this datatype.", FaultCodes.UNAUTHORIZED);
Class<? extends View> requestedView = getViewClass(parameters);

MDC.put("view", Views.getViewPath(requestedView));
MDC.put("cvr", cvr);

// Validate authentication.
//
Client client = clients.findByCvr(cvr);
if (client == null || !client.isAuthorizedFor(requestedView))
{
throw new ReplicationFault("The provided cvr is not authorized to fetch this datatype.", FaultCodes.UNAUTHORIZED);
}

// Validate the input parameters.
//
HistoryOffset offset = getOffset(parameters);
int limit = getRecordLimit(parameters);

MDC.put("offset", String.valueOf(offset));
MDC.put("limit", String.valueOf(limit));

// Fetch the records from the database and
// fill the output structure.
//
ScrollableResults results = dao.findPage(requestedView, offset.getRecordID(), offset.getModifiedDate(), limit);
Document feedDocument = createFeed(requestedView, results);

// Construct the output container.
//
ReplicationResponseType response = new ObjectFactory().createReplicationResponseType();
response.setAny(feedDocument.getFirstChild());

// Log that the client successfully accessed the data.
// Simply for audit purposes.
//
// The client details are included in the MDC.
//
logger.info("Records fetched, sending response.");

return response;
}
catch (ReplicationFault e)
{
// Log an throw is normally an anti-pattern, but since
// exceptions are part of JAX-WS's flow it is "OK" here.

logger.warn("The request could not be handled. This is likely the clients mistake.", e);

throw e;
}
catch (RuntimeException e)
{
logger.error("An unhandled error occured.", e);

throw e;
}
finally
{
// Clean up the thread's MDC.
// TODO: This might fit better in a filter or intercepter.
MDC.remove("view");
MDC.remove("cvr");
MDC.remove("offset");
MDC.remove("limit");
}

// Validate the input parameters.
//
HistoryOffset offset = getOffset(parameters);
int limit = getRecordLimit(parameters);

// Fetch the records from the database and
// fill the output structure.
//
ScrollableResults results = dao.findPage(requestedView, offset.getRecordID(), offset.getModifiedDate(), limit);
Document feedDocument = createFeed(requestedView, results);

// Construct the output container.
//
ReplicationResponseType response = new ObjectFactory().createReplicationResponseType();
response.setAny(feedDocument.getFirstChild());

return response;
}


Expand Down
25 changes: 25 additions & 0 deletions nsp/batch-copy-ws/src/main/resources/logback.groovy
@@ -0,0 +1,25 @@
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender
import ch.qos.logback.core.status.OnConsoleStatusListener
import ch.qos.logback.core.rolling.RollingFileAppender
import static ch.qos.logback.classic.Level.INFO
import static ch.qos.logback.core.spi.FilterReply.ACCEPT
import static ch.qos.logback.core.spi.FilterReply.DENY
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy

def LOGS_DIR = System.getProperty("jboss.server.log.dir")
def LOG_FILE = "stamdata-batch-copy-ws.log"

appender("ROLLING", RollingFileAppender) {
file = "${LOGS_DIR}/${LOG_FILE}"

encoder(PatternLayoutEncoder) {
pattern = "%d [%-4level] %msg [%thread] [%mdc]%n"
}

rollingPolicy(TimeBasedRollingPolicy) {
FileNamePattern = "${LOGS_DIR}/${LOG_FILE}.%d{yyyy-MM}.zip"
}
}

root(INFO, ['ROLLING'])
Expand Up @@ -31,8 +31,14 @@

public class GuiceTestRunner extends AbstractGuiceTestRunner
{
private static final String LOG_DIR_PROP = "jboss.server.log.dir";

public GuiceTestRunner(Class<?> classToRun) throws InitializationError
{
super(classToRun, new ComponentController.ComponentModule());

// Set the log folder, if not running in JBoss.
//
System.setProperty(LOG_DIR_PROP, "logs");
}
}
9 changes: 0 additions & 9 deletions nsp/cpr-ws/pom.xml
Expand Up @@ -96,21 +96,12 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${jboss6.hibernate.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${jboss6.hibernate.version}</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down

0 comments on commit 6dd9ec7

Please sign in to comment.