From 54d3297738cac7c693bdf53094194054782de890 Mon Sep 17 00:00:00 2001 From: tkellner Date: Mon, 3 Mar 2014 20:16:40 +0000 Subject: Allow to initialize IssuerTemplate cache via config git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@1302 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4 --- .../src/main/webapp/WEB-INF/applicationContext.xml | 3 +- .../src/main/webapp/WEB-INF/applicationContext.xml | 3 +- .../impl/IdentityLinkInfoboxFactory.java | 4 - .../slcommands/impl/IdentityLinkInfoboxImpl.java | 1 - .../slcommands/impl/IdentityLinkTransformer.java | 115 +++++++++++++++++++++ .../egiz/bku/slcommands/testApplicationContext.xml | 5 +- .../at/gv/egiz/idlink/IdentityLinkTransformer.java | 43 ++++---- 7 files changed, 142 insertions(+), 32 deletions(-) create mode 100644 bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkTransformer.java diff --git a/BKULocal/src/main/webapp/WEB-INF/applicationContext.xml b/BKULocal/src/main/webapp/WEB-INF/applicationContext.xml index b75db261..49d4f57f 100644 --- a/BKULocal/src/main/webapp/WEB-INF/applicationContext.xml +++ b/BKULocal/src/main/webapp/WEB-INF/applicationContext.xml @@ -85,8 +85,9 @@ - + + diff --git a/BKUOnline/src/main/webapp/WEB-INF/applicationContext.xml b/BKUOnline/src/main/webapp/WEB-INF/applicationContext.xml index f85875c9..7534a9c9 100644 --- a/BKUOnline/src/main/webapp/WEB-INF/applicationContext.xml +++ b/BKUOnline/src/main/webapp/WEB-INF/applicationContext.xml @@ -78,8 +78,9 @@ - + + diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkInfoboxFactory.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkInfoboxFactory.java index 0e7a7c0d..cc0ad0d3 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkInfoboxFactory.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkInfoboxFactory.java @@ -21,12 +21,8 @@ * that you distribute must include a readable copy of the "NOTICE" text file. */ - - package at.gv.egiz.bku.slcommands.impl; -import at.gv.egiz.idlink.IdentityLinkTransformer; - public class IdentityLinkInfoboxFactory extends AbstractInfoboxFactory { private IdentityLinkTransformer identityLinkTransformer; diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkInfoboxImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkInfoboxImpl.java index add7a3e1..b2643d8e 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkInfoboxImpl.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkInfoboxImpl.java @@ -60,7 +60,6 @@ import at.gv.egiz.bku.slexceptions.SLCommandException; import at.gv.egiz.bku.slexceptions.SLExceptionMessages; import at.gv.egiz.bku.slexceptions.SLRuntimeException; import at.gv.egiz.idlink.CompressedIdentityLinkFactory; -import at.gv.egiz.idlink.IdentityLinkTransformer; import at.gv.egiz.idlink.ans1.IdentityLink; import at.gv.egiz.stal.InfoboxReadRequest; import at.gv.egiz.stal.STALRequest; diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkTransformer.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkTransformer.java new file mode 100644 index 00000000..e24e2f83 --- /dev/null +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkTransformer.java @@ -0,0 +1,115 @@ +/* + * Copyright 2011 by Graz University of Technology, Austria + * MOCCA has been developed by the E-Government Innovation Center EGIZ, a joint + * initiative of the Federal Chancellery Austria and Graz University of Technology. + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://www.osor.eu/eupl/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + * + * This product combines work with different licenses. See the "NOTICE" text + * file for details on the various modules and licenses. + * The "NOTICE" text file is part of the distribution. Any derivative works + * that you distribute must include a readable copy of the "NOTICE" text file. + */ + + +package at.gv.egiz.bku.slcommands.impl; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.transform.Templates; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamSource; + +import org.apache.commons.configuration.Configuration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ResourceLoaderAware; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; + +import at.gv.egiz.bku.conf.MoccaConfigurationFacade; + +public class IdentityLinkTransformer extends at.gv.egiz.idlink.IdentityLinkTransformer implements ResourceLoaderAware { + + private final Logger log = LoggerFactory.getLogger(IdentityLinkTransformer.class); + + /** + * The configuration facade. + */ + protected final ConfigurationFacade configurationFacade = new ConfigurationFacade(); + + private class ConfigurationFacade implements MoccaConfigurationFacade { + + private Configuration configuration; + private ResourceLoader resLoader; + + private final String ISSUER_TEMPLATE_CACHE = + "IssuerTemplateCache"; + private final String ISSUER_TEMPLATE_CACHE_ENTRY = + ISSUER_TEMPLATE_CACHE + ".entry"; + private final String ISSUER_TEMPLATE_CACHE_URLS = + ISSUER_TEMPLATE_CACHE_ENTRY + ".url"; + private final String ISSUER_TEMPLATE_CACHE_RESOURCES = + ISSUER_TEMPLATE_CACHE_ENTRY + ".resource"; + + public Map getIssuerTemplateCacheMap( + TransformerFactory factory) { + Map templates = + Collections.synchronizedMap(new HashMap()); + List urls = configuration.getList(ISSUER_TEMPLATE_CACHE_URLS); + List resources = configuration.getList(ISSUER_TEMPLATE_CACHE_RESOURCES); + if (!urls.isEmpty() && (urls.size() == resources.size())) { + for (int i = 0; i < urls.size(); ++i) { + if ((urls.get(i) == null) || (resources.get(i) == null)) + continue; + String url = urls.get(i).toString(); + String resource = resources.get(i).toString(); + Resource templRes = resLoader.getResource(resource); + try { + Templates template = factory.newTemplates(new StreamSource(templRes.getInputStream())); + templates.put(url, template); + } catch (Exception e) { + log.error("Error initializing issuer template cache", e); + } + } + } + return templates; + } + + public void setResourceLoader(ResourceLoader resLoader) { + this.resLoader = resLoader; + } + } + + @Override + protected Map getInitialTemplateMap() { + Map templateMap = configurationFacade.getIssuerTemplateCacheMap(factory); + log.debug("IssuerTemplate cache initialized with {} elements", templateMap.size()); + return templateMap; + } + + /** + * @param configuration the configuration to set + */ + public void setConfiguration(Configuration configuration) { + configurationFacade.configuration = configuration; + } + + @Override + public void setResourceLoader(ResourceLoader resLoader) { + configurationFacade.setResourceLoader(resLoader); + } +} diff --git a/bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/testApplicationContext.xml b/bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/testApplicationContext.xml index 7d7c967f..fffabb47 100644 --- a/bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/testApplicationContext.xml +++ b/bkucommon/src/test/resources/at/gv/egiz/bku/slcommands/testApplicationContext.xml @@ -53,8 +53,9 @@ - + + @@ -165,4 +166,4 @@ - \ No newline at end of file + diff --git a/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkTransformer.java b/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkTransformer.java index 153c6d53..af60500a 100644 --- a/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkTransformer.java +++ b/utils/src/main/java/at/gv/egiz/idlink/IdentityLinkTransformer.java @@ -27,8 +27,6 @@ package at.gv.egiz.idlink; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; -import java.util.Collections; -import java.util.HashMap; import java.util.Map; import javax.xml.transform.Result; @@ -52,20 +50,27 @@ import org.w3c.dom.Text; import at.gv.egiz.bku.utils.urldereferencer.StreamData; import at.gv.egiz.bku.utils.urldereferencer.URLDereferencer; -public class IdentityLinkTransformer { - +public abstract class IdentityLinkTransformer { + private final Logger log = LoggerFactory.getLogger(IdentityLinkTransformer.class); /** * The transformer factory. */ - private TransformerFactory factory = SAXTransformerFactory.newInstance(); + protected TransformerFactory factory = SAXTransformerFactory.newInstance(); /** * The URLDereferencer used to dereference style-sheet URLs. */ private URLDereferencer urlDereferencer; - + + /** + * Mapping of issuer template URIs to transformation templates. + */ + private Map templates = null; + + protected abstract Map getInitialTemplateMap(); + /** * @return the urlDereferencer */ @@ -102,7 +107,6 @@ public class IdentityLinkTransformer { * @throws NullPointerException if idLink is null. */ public static void setDomainIdentifier(Node idLink, String domainIdentifier) { - Element element; if (idLink instanceof Element) { element = (Element) idLink; @@ -114,7 +118,7 @@ public class IdentityLinkTransformer { } else { throw new NullPointerException("Parameter 'idLink' must no be null."); } - + NodeList nodeList = element.getElementsByTagNameNS( "http://reference.e-government.gv.at/namespace/persondata/20020228#", "Type"); @@ -132,12 +136,7 @@ public class IdentityLinkTransformer { } } - - /** - * Mapping of issuer template URIs to transformation templates. - */ - private Map templates = Collections.synchronizedMap(new HashMap()); - + /** * Transforms an identity link source to result with * the given issuer template from the stylesheetURL. @@ -161,32 +160,30 @@ public class IdentityLinkTransformer { * if transforming the identity link fails. */ public void transformIdLink(String stylesheetURL, Source source, Result result) throws IOException, TransformerException { + if (templates == null) + templates = getInitialTemplateMap(); Templates templ = templates.get(stylesheetURL); - - if (templ == null) { + if (templ == null) { // TODO: implement stylesheet cache URL url = new URL(stylesheetURL); if (!"http".equalsIgnoreCase(url.getProtocol()) && !"https".equalsIgnoreCase(url.getProtocol())) { throw new MalformedURLException("Protocol " + url.getProtocol() + " not supported for IssuerTemplate URL."); } - + StreamData data = urlDereferencer.dereference(url.toExternalForm()); - + log.trace("Trying to create issuer template."); templ = factory.newTemplates(new StreamSource(data.getStream())); log.trace("Successfully created issuer template"); templates.put(stylesheetURL, templ); - } - + Transformer transformer = templ.newTransformer(); - transformer.transform(source, result); - + } - } -- cgit v1.2.3