2 * ============LICENSE_START====================================================
\r
4 * ===========================================================================
\r
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
\r
6 * ===========================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END====================================================
\r
25 * Created on: Apr 10, 2009
\r
26 * Created by: Jonathan
\r
28 * Revamped to do away with ThreadLocal 5/27/2011, JonathanGathman
\r
30 * (c) 2009 SBC Knowledge Ventures, L.P. All rights reserved.
\r
31 *******************************************************************
\r
32 * RESTRICTED - PROPRIETARY INFORMATION The Information contained
\r
33 * herein is for use only by authorized employees of AT&T Services,
\r
34 * Inc., and authorized Affiliates of AT&T Services, Inc., and is
\r
35 * not for general distribution within or outside the respective
\r
37 *******************************************************************
\r
39 package org.onap.aaf.misc.env.jaxb;
\r
41 import java.io.File;
\r
42 import java.io.InputStream;
\r
43 import java.io.Reader;
\r
44 import java.io.StringReader;
\r
45 import java.util.HashMap;
\r
46 import java.util.Map;
\r
48 import javax.xml.bind.JAXBContext;
\r
49 import javax.xml.bind.JAXBException;
\r
50 import javax.xml.bind.Unmarshaller;
\r
51 import javax.xml.stream.XMLEventReader;
\r
52 import javax.xml.stream.XMLStreamReader;
\r
53 import javax.xml.transform.stream.StreamSource;
\r
54 import javax.xml.validation.Schema;
\r
56 import org.onap.aaf.misc.env.APIException;
\r
57 import org.onap.aaf.misc.env.LogTarget;
\r
58 import org.onap.aaf.misc.env.util.Pool;
\r
59 import org.onap.aaf.misc.env.util.Pool.Pooled;
\r
60 import org.w3c.dom.Node;
\r
63 * JAXBumar classes are inexpensive for going in and out of scope
\r
64 * and have been made thread safe via Pooling
\r
69 public class JAXBumar {
\r
70 // Need to store off possible JAXBContexts based on Class, which will be stored in Creator
\r
71 private static Map<Class<?>[],Pool<SUnmarshaller>> pools = new HashMap<>();
\r
73 private Class<?> cls;
\r
74 private Schema schema;
\r
75 private Pool<SUnmarshaller> mpool;;
\r
77 // Handle Marshaller class setting of properties only when needed
\r
78 private class SUnmarshaller {
\r
79 private Unmarshaller u;
\r
82 public SUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
\r
87 public Unmarshaller get(Schema schema) throws JAXBException {
\r
89 u.setSchema(s = schema);
\r
95 private class Creator implements Pool.Creator<SUnmarshaller> {
\r
96 private JAXBContext jc;
\r
97 private String name;
\r
99 public Creator(Class<?>[] classes) throws JAXBException {
\r
100 jc = JAXBContext.newInstance(classes);
\r
101 name = "JAXBumar: " + classes[0].getName();
\r
105 public SUnmarshaller create() throws APIException {
\r
107 return new SUnmarshaller(jc.createUnmarshaller());
\r
108 } catch (JAXBException e) {
\r
109 throw new APIException(e);
\r
113 public String toString() {
\r
118 public void destroy(SUnmarshaller sui) {
\r
123 public boolean isValid(SUnmarshaller t) {
\r
128 public void reuse(SUnmarshaller t) {
\r
129 // Nothing to do here
\r
134 private Pool<SUnmarshaller> getPool(Class<?> ... classes) throws JAXBException {
\r
135 Pool<SUnmarshaller> mp;
\r
136 synchronized(pools) {
\r
137 mp = pools.get(classes);
\r
139 pools.put(classes,mp = new Pool<SUnmarshaller>(new Creator(classes)));
\r
145 public JAXBumar(Class<?> ... classes) throws JAXBException {
\r
147 mpool = getPool(classes);
\r
152 * Constructs a new JAXBumar with schema validation enabled.
\r
156 * @throws JAXBException
\r
158 public JAXBumar(Schema schema, Class<?> ... classes) throws JAXBException {
\r
160 mpool = getPool(classes);
\r
161 this.schema = schema;
\r
164 @SuppressWarnings("unchecked")
\r
165 public<O> O unmarshal(LogTarget env, Node node) throws JAXBException, APIException {
\r
166 Pooled<SUnmarshaller> s = mpool.get(env);
\r
168 return s.content.get(schema).unmarshal(node,(Class<O>)cls).getValue();
\r
175 @SuppressWarnings("unchecked")
\r
176 public<O> O unmarshal(LogTarget env, String xml) throws JAXBException, APIException {
\r
177 if(xml==null) throw new JAXBException("Null Input for String unmarshal");
\r
178 Pooled<SUnmarshaller> s = mpool.get(env);
\r
180 return (O)s.content.get(schema).unmarshal(
\r
181 new StreamSource(new StringReader(xml))
\r
182 ,(Class<O>)cls).getValue();
\r
188 @SuppressWarnings("unchecked")
\r
189 public<O> O unmarshal(LogTarget env, File xmlFile) throws JAXBException, APIException {
\r
190 Pooled<SUnmarshaller> s = mpool.get(env);
\r
192 return (O)s.content.get(schema).unmarshal(xmlFile);
\r
199 @SuppressWarnings("unchecked")
\r
200 public<O> O unmarshal(LogTarget env,InputStream is) throws JAXBException, APIException {
\r
201 Pooled<SUnmarshaller> s = mpool.get(env);
\r
203 return (O)s.content.get(schema).unmarshal(is);
\r
209 @SuppressWarnings("unchecked")
\r
210 public<O> O unmarshal(LogTarget env, Reader rdr) throws JAXBException, APIException {
\r
211 Pooled<SUnmarshaller> s = mpool.get(env);
\r
213 return (O)s.content.get(schema).unmarshal(rdr);
\r
219 @SuppressWarnings("unchecked")
\r
220 public<O> O unmarshal(LogTarget env, XMLStreamReader xsr) throws JAXBException, APIException {
\r
221 Pooled<SUnmarshaller> s = mpool.get(env);
\r
223 return (O)s.content.get(schema).unmarshal(xsr,(Class<O>)cls).getValue();
\r
229 @SuppressWarnings("unchecked")
\r
230 public<O> O unmarshal(LogTarget env, XMLEventReader xer) throws JAXBException, APIException {
\r
231 Pooled<SUnmarshaller> s = mpool.get(env);
\r
233 return (O)s.content.get(schema).unmarshal(xer,(Class<O>)cls).getValue();
\r
239 @SuppressWarnings("unchecked")
\r
240 public<O> O newInstance() throws InstantiationException, IllegalAccessException{
\r
241 return ((Class<O>)cls).newInstance();
\r