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