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