package at.gv.egiz.eid.authhandler.modules.sigverify.moasig.impl;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import at.gv.egovernment.moa.spss.server.config.ConfigurationException;
import at.gv.egovernment.moa.spss.server.config.ConfigurationProvider;
import at.gv.egovernment.moa.spss.server.iaik.config.IaikConfigurator;
import at.gv.egovernment.moa.spss.server.logging.TransactionId;
import at.gv.egovernment.moa.spss.server.transaction.TransactionContext;
import at.gv.egovernment.moa.spss.server.transaction.TransactionContextManager;
import at.gv.egovernment.moaspss.logging.LoggingContext;
import at.gv.egovernment.moaspss.logging.LoggingContextManager;
import iaik.server.Configurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.w3c.dom.Document;
public abstract class AbstractSignatureService {
private static final Logger log = LoggerFactory.getLogger(AbstractSignatureService.class);
@Autowired(required = true) MoaSigInitializer moaSigConfig;
/**
* Get a new {@link Document} from {@link DocumentBuilder} in synchronized form, because
* {@link DocumentBuilderFactory} and {@link DocumentBuilder} are not thread-safe.
*
* @return {@link Document}
* @throws ParserConfigurationException
*/
protected synchronized Document getNewDocumentBuilder() throws ParserConfigurationException {
final DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return docBuilder.newDocument();
}
/**
* Set up the thread-local context information needed for calling the various
* Invoker
classes.
*
* @throws ConfigurationException An error occurred setting up the
* configuration in the TransactionContext
.
*/
protected final void setUpContexts( String transactionID) throws ConfigurationException {
final TransactionContextManager txMgr = TransactionContextManager.getInstance();
final LoggingContextManager logMgr = LoggingContextManager.getInstance();
if (txMgr.getTransactionContext() == null) {
log.debug("Set not MOA-Sig transaction context");
final TransactionContext ctx = new TransactionContext(transactionID, null, ConfigurationProvider.getInstance());
txMgr.setTransactionContext(ctx);
}
if (logMgr.getLoggingContext() == null) {
final LoggingContext ctx = new LoggingContext(transactionID);
logMgr.setLoggingContext(ctx);
}
new IaikConfigurator().configure(ConfigurationProvider.getInstance());
}
/**
* Tear down thread-local context information.
*/
protected void tearDownContexts() {
TransactionContextManager.getInstance().setTransactionContext(null);
LoggingContextManager.getInstance().setLoggingContext(null);
log.debug("Closing MOA-Sig transaction context");
}
}