AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / jaxb / JAXBumar.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 /**
23  * JAXBumar.java
24  *
25  * Created on: Apr 10, 2009
26  * Created by: Jonathan
27  *
28  * Revamped to do away with ThreadLocal 5/27/2011, JG1555
29  *
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 
36  * companies. 
37  *******************************************************************
38  */
39 package org.onap.aaf.misc.env.jaxb;
40
41 import java.io.File;
42 import java.io.InputStream;
43 import java.io.Reader;
44 import java.io.StringReader;
45 import java.util.HashMap;
46 import java.util.Map;
47
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;
55
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;
61
62 /**
63  * JAXBumar classes are inexpensive for going in and out of scope
64  * and have been made thread safe via Pooling
65  * 
66  * @author Jonathan
67  *
68  */
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>>();
72
73         private Class<?> cls;
74         private Schema schema;
75         private Pool<SUnmarshaller> mpool;;
76
77         // Handle Marshaller class setting of properties only when needed
78         private class SUnmarshaller {
79                 private Unmarshaller u;
80                 private Schema s;
81                 
82                 public SUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
83                         u = unmarshaller;
84                         s = null;
85                 }
86                 
87                 public Unmarshaller get(Schema schema) throws JAXBException {
88                         if(s != schema) {
89                                 u.setSchema(s = schema);
90                         }
91                         return u;
92                 }
93         }
94         
95         private class Creator implements Pool.Creator<SUnmarshaller> {
96                 private JAXBContext jc;
97                 private String name;
98                 
99                 public Creator(Class<?>[] classes) throws JAXBException {
100                         jc = JAXBContext.newInstance(classes);
101                         name = "JAXBumar: " + classes[0].getName();
102                 }
103                 
104                 // @Override
105                 public SUnmarshaller create() throws APIException {
106                         try {
107                                 return new SUnmarshaller(jc.createUnmarshaller());
108                         } catch (JAXBException e) {
109                                 throw new APIException(e);
110                         }
111                 }
112                 
113                 public String toString() {
114                         return name;
115                 }
116
117                 // @Override
118                 public void destroy(SUnmarshaller sui) {
119                         // Nothing to do
120                 }
121                 
122                 // @Override
123                 public boolean isValid(SUnmarshaller t) {
124                         return true; 
125                 }
126
127                 // @Override
128                 public void reuse(SUnmarshaller t) {
129                         // Nothing to do here
130                 }
131
132         }
133
134         private Pool<SUnmarshaller> getPool(Class<?> ... classes) throws JAXBException {
135                 Pool<SUnmarshaller> mp;
136                 synchronized(pools) {
137                         mp = pools.get(classes);
138                         if(mp==null) {
139                                 pools.put(classes,mp = new Pool<SUnmarshaller>(new Creator(classes)));
140                         }
141                 }               
142                 return mp;
143         }
144
145         public JAXBumar(Class<?> ... classes) throws JAXBException {
146                 cls = classes[0];
147                 mpool = getPool(classes);
148                 schema = null;
149         }
150         
151         /**
152          * Constructs a new JAXBumar with schema validation enabled.
153          * 
154          * @param schema
155          * @param theClass
156          * @throws JAXBException
157          */
158         public JAXBumar(Schema schema, Class<?> ... classes) throws JAXBException {
159                 cls = classes[0];
160                 mpool = getPool(classes);
161                 this.schema = schema;
162         }
163         
164         @SuppressWarnings("unchecked")
165         public<O> O unmarshal(LogTarget env, Node node) throws JAXBException, APIException {
166                 Pooled<SUnmarshaller> s = mpool.get(env);
167                 try {
168                         return s.content.get(schema).unmarshal(node,(Class<O>)cls).getValue();
169                 } finally {
170                         s.done();
171                 }
172
173         }
174         
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);
179                 try {
180                                 return (O)s.content.get(schema).unmarshal(
181                                         new StreamSource(new StringReader(xml))
182                                         ,(Class<O>)cls).getValue();
183                 } finally {
184                         s.done();
185                 }
186         }
187         
188         @SuppressWarnings("unchecked")
189         public<O> O unmarshal(LogTarget env, File xmlFile) throws JAXBException, APIException {
190                 Pooled<SUnmarshaller> s = mpool.get(env);
191                 try {
192                         return (O)s.content.get(schema).unmarshal(xmlFile);
193                 } finally {
194                         s.done();
195                 }
196
197         }
198         
199         @SuppressWarnings("unchecked")
200         public<O> O unmarshal(LogTarget env,InputStream is) throws JAXBException, APIException {
201                 Pooled<SUnmarshaller> s = mpool.get(env);
202                 try {
203                         return (O)s.content.get(schema).unmarshal(is);
204                 } finally {
205                         s.done();
206                 }
207         }
208
209         @SuppressWarnings("unchecked")
210         public<O> O unmarshal(LogTarget env, Reader rdr) throws JAXBException, APIException {
211                 Pooled<SUnmarshaller> s = mpool.get(env);
212                 try {
213                         return (O)s.content.get(schema).unmarshal(rdr);
214                 } finally {
215                         s.done();
216                 }
217         }
218
219         @SuppressWarnings("unchecked")
220         public<O> O unmarshal(LogTarget env, XMLStreamReader xsr) throws JAXBException, APIException {
221                 Pooled<SUnmarshaller> s = mpool.get(env);
222                 try {
223                         return (O)s.content.get(schema).unmarshal(xsr,(Class<O>)cls).getValue();
224                 } finally {
225                         s.done();
226                 }
227         }
228
229         @SuppressWarnings("unchecked")
230         public<O> O unmarshal(LogTarget env, XMLEventReader xer) throws JAXBException, APIException {
231                 Pooled<SUnmarshaller> s = mpool.get(env);
232                 try {
233                         return (O)s.content.get(schema).unmarshal(xer,(Class<O>)cls).getValue();
234                 } finally {
235                         s.done();
236                 }
237         }
238
239         @SuppressWarnings("unchecked")
240         public<O> O newInstance() throws InstantiationException, IllegalAccessException{
241                 return ((Class<O>)cls).newInstance();
242         }
243 }