diff options
author | emusic <emina.music@egiz.gv.at> | 2018-03-21 13:06:24 +0100 |
---|---|---|
committer | emusic <emina.music@egiz.gv.at> | 2018-03-21 13:34:05 +0100 |
commit | e49eb5e433767386a09732f5942b6425098b511b (patch) | |
tree | 43fb29542b0ce702a0dbfbed8b4ead25f7872ac2 /pdf-as-lib/src/main/java/at/gv/egiz/sl | |
parent | f60b607e56700bb6b3130c2ad549b0b1c14267a4 (diff) | |
download | pdf-as-4-e49eb5e433767386a09732f5942b6425098b511b.tar.gz pdf-as-4-e49eb5e433767386a09732f5942b6425098b511b.tar.bz2 pdf-as-4-e49eb5e433767386a09732f5942b6425098b511b.zip |
changes regarding protection, bug fix, thread safe
Diffstat (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/sl')
-rw-r--r-- | pdf-as-lib/src/main/java/at/gv/egiz/sl/util/SLMarschaller.java | 78 |
1 files changed, 51 insertions, 27 deletions
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/SLMarschaller.java b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/SLMarschaller.java index 8e84b491..8b9991fd 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/SLMarschaller.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/SLMarschaller.java @@ -3,19 +3,19 @@ * PDF-AS has been contracted 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 @@ -40,20 +40,15 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SLMarschaller { - private static Marshaller marshaller = null; - private static Unmarshaller unmarshaller = null; + + private static JAXBContext context = null; private static final Logger logger = LoggerFactory .getLogger(SLMarschaller.class); - + static { try { - JAXBContext context = JAXBContext.newInstance("at.gv.egiz.sl.schema"); - marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); - - unmarshaller = context.createUnmarshaller(); + context = JAXBContext.newInstance("at.gv.egiz.sl.schema"); } catch (JAXBException e) { logger.error("Error in creating JAXBContext", e); throw new RuntimeException( @@ -61,17 +56,19 @@ public class SLMarschaller { } } - public static void marshal(Object obj, OutputStream os) throws JAXBException { + public static synchronized void marshal(Object obj, OutputStream os) throws JAXBException { + Marshaller marshaller = createMarshaller(); marshaller.marshal(obj, os); } - public static String marshalToString(Object obj) throws JAXBException { + public static synchronized String marshalToString(Object obj) throws JAXBException { + Marshaller marshaller = createMarshaller(); StringWriter sw = new StringWriter(); marshaller.marshal(obj, sw); return sw.toString(); } - - public static Object unmarshal(InputStream is) throws JAXBException { + + public static synchronized Object unmarshal(InputStream is) throws JAXBException { XMLInputFactory xif = null; try { xif = XMLInputFactory.newFactory(); @@ -79,19 +76,20 @@ public class SLMarschaller { // Fallback for old STAX implementations xif = XMLInputFactory.newInstance(); } - xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); - xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); - XMLStreamReader xmlStreamReader; + xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); + xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); + XMLStreamReader xmlStreamReader; try { xmlStreamReader = xif.createXMLStreamReader(is); + Unmarshaller unmarshaller = createUnmarshaller(); return unmarshaller.unmarshal(xmlStreamReader); } catch (XMLStreamException e) { throw new JAXBException(e); } - + } - - public static Object unmarshalFromString(String message) throws JAXBException { + + public static synchronized Object unmarshalFromString(String message) throws JAXBException { StringReader sr = new StringReader(message); XMLInputFactory xif = null; try { @@ -100,15 +98,41 @@ public class SLMarschaller { // Fallback for old STAX implementations xif = XMLInputFactory.newInstance(); } - - xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); - xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); - XMLStreamReader xmlStreamReader; + + xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); + xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); + XMLStreamReader xmlStreamReader; try { xmlStreamReader = xif.createXMLStreamReader(sr); + Unmarshaller unmarshaller = createUnmarshaller(); return unmarshaller.unmarshal(xmlStreamReader); } catch (XMLStreamException e) { throw new JAXBException(e); } } -} + + private static synchronized Marshaller createMarshaller() { + try { + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); + return marshaller; + } + catch (JAXBException e) { + logger.error("Error in creating Marshaller", e); + throw new RuntimeException( + "There was a problem creating a Marshaller object for formatting the object to XML."); + } + } + + private static synchronized Unmarshaller createUnmarshaller() { + try { + return context.createUnmarshaller(); + } + catch (JAXBException e) { + logger.error("Error in creating Unmarshaller", e); + throw new RuntimeException( + "There was a problem creating a Unmarshaller object for formatting the object to XML."); + } + } +}
\ No newline at end of file |