package at.gv.egiz.eaaf.modules.sigverify.moasig.impl;
import java.security.Provider;
import java.security.Security;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.w3c.dom.Document;
import at.gv.egovernment.moa.spss.server.config.ConfigurationException;
import at.gv.egovernment.moa.spss.server.iaik.config.IaikConfigurator;
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.pki.PKIFactory;
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 In case of an error
*/
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(final 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, moaSigConfig.getConfigHolder().getMoaSpssConfig());
txMgr.setTransactionContext(ctx);
}
//set Logging context into MOA-Sig
if (logMgr.getLoggingContext() == null) {
final LoggingContext ctx = new LoggingContext(transactionID);
logMgr.setLoggingContext(ctx);
}
//dump Java Security-Providers
if (log.isTraceEnabled()) {
log.trace("Set-Up verifier Bean: {}", this);
log.trace("ClassLoader: {}", this.getClass().getClassLoader());
dumpSecProviders("MOA-Sig Context-Set-Up");
}
if (!PKIFactory.getInstance().isAlreadyConfigured()) {
log.info("IAIK PKI Module is NOT configurated. Starting configuration again ... ");
IaikConfigurator.configure(moaSigConfig.getConfigHolder().getMoaSpssConfig());
}
}
private static void dumpSecProviders(String message) {
log.trace("Security Providers: {}", message);
for (final Provider provider : Security.getProviders()) {
log.trace(" - {} - {}", provider.getName(), provider.getVersion());
}
}
/**
* Tear down thread-local context information.
*/
protected void tearDownContexts() {
TransactionContextManager.getInstance().setTransactionContext(null);
LoggingContextManager.getInstance().setLoggingContext(null);
log.debug("Closing MOA-Sig transaction context");
}
}