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 --- .../impl/IdentityLinkInfoboxFactory.java | 4 - .../slcommands/impl/IdentityLinkInfoboxImpl.java | 1 - .../slcommands/impl/IdentityLinkTransformer.java | 115 +++++++++++++++++++++ .../egiz/bku/slcommands/testApplicationContext.xml | 5 +- 4 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/IdentityLinkTransformer.java (limited to 'bkucommon/src') 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 + -- cgit v1.2.3