Update Certificate for all FQDNs
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / jaxb / JAXBmar.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.OutputStream;\r
42 import java.io.StringWriter;\r
43 import java.io.Writer;\r
44 import java.util.HashMap;\r
45 import java.util.Map;\r
46 \r
47 import javax.xml.bind.JAXBContext;\r
48 import javax.xml.bind.JAXBElement;\r
49 import javax.xml.bind.JAXBException;\r
50 import javax.xml.bind.Marshaller;\r
51 import javax.xml.namespace.QName;\r
52 \r
53 import org.onap.aaf.misc.env.APIException;\r
54 import org.onap.aaf.misc.env.LogTarget;\r
55 import org.onap.aaf.misc.env.util.Pool;\r
56 import org.onap.aaf.misc.env.util.Pool.Pooled;\r
57 \r
58 /**\r
59  * JAXBmar classes are inexpensive for going in and out of scope\r
60  * and have been made thread safe via Pooling\r
61 \r
62  * @author Jonathan\r
63  *\r
64  */\r
65 public class JAXBmar {\r
66         // Need to store off possible JAXBContexts based on Class, which will be stored in Creator\r
67         private static Map<Class<?>[],Pool<PMarshaller>> pools = new HashMap<Class<?>[], Pool<PMarshaller>>();\r
68 \r
69         // Handle Marshaller class setting of properties only when needed\r
70         private class PMarshaller {\r
71                 private Marshaller m;\r
72                 private boolean p;\r
73                 private boolean f;\r
74                 \r
75                 public PMarshaller(Marshaller marshaller) throws JAXBException {\r
76                         m = marshaller;\r
77                 m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");\r
78                 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, p = false);\r
79                 m.setProperty(Marshaller.JAXB_FRAGMENT, f = false);\r
80                 }\r
81                 \r
82                 public Marshaller get(boolean pretty, boolean fragment) throws JAXBException {\r
83                         if(pretty != p) {\r
84                         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, p = pretty);\r
85                         }\r
86                         if(fragment != f) {\r
87                         m.setProperty(Marshaller.JAXB_FRAGMENT, f = fragment);\r
88                         }\r
89                         return m;\r
90                 }\r
91         }\r
92         \r
93         private class Creator implements Pool.Creator<PMarshaller> {\r
94                 private JAXBContext jc;\r
95                 private String name;\r
96                 public Creator(Class<?>[] classes) throws JAXBException {\r
97                         jc = JAXBContext.newInstance(classes);\r
98                         name = "JAXBmar: " + classes[0].getName();\r
99                 }\r
100                 \r
101                 // @Override\r
102                 public PMarshaller create() throws APIException {\r
103                         try {\r
104                                 return new PMarshaller(jc.createMarshaller());\r
105                         } catch (JAXBException e) {\r
106                                 throw new APIException(e);\r
107                         }\r
108                 }\r
109 \r
110                 public String toString() {\r
111                         return name;\r
112                 }\r
113 \r
114                 // @Override\r
115                 public void reuse(PMarshaller pm) {\r
116                         // Nothing to do\r
117                 }\r
118                 \r
119                 // @Override\r
120                 public void destroy(PMarshaller pm) {\r
121                         // Nothing to do\r
122                 }\r
123 \r
124                 // @Override\r
125                 public boolean isValid(PMarshaller t) {\r
126                         return true; \r
127                 }\r
128         }\r
129 \r
130         //TODO isn't UTF-8 a standard string somewhere for encoding?\r
131         private boolean fragment= false;\r
132         private boolean pretty=false;\r
133         private QName qname;\r
134         \r
135         private Pool<PMarshaller> mpool; // specific Pool associated with constructed Classes\r
136         private Class<?> cls;\r
137         \r
138         private Pool<PMarshaller> getPool(Class<?> ... classes) throws JAXBException {\r
139                 Pool<PMarshaller> mp;\r
140                 synchronized(pools) {\r
141                         mp = pools.get(classes);\r
142                         if(mp==null) {\r
143                                 pools.put(classes,mp = new Pool<PMarshaller>(new Creator(classes)));\r
144                         }\r
145                 }               \r
146                 return mp;\r
147         }\r
148         \r
149         public JAXBmar(Class<?>... classes) throws JAXBException {\r
150                 cls = classes[0];\r
151                 mpool = getPool(classes);\r
152                 qname = null;\r
153         }\r
154 \r
155         public JAXBmar(QName theQname, Class<?>... classes) throws JAXBException {\r
156                 cls = classes[0];\r
157                 mpool = getPool(classes);\r
158                 qname = theQname;\r
159         }\r
160 \r
161         @SuppressWarnings("unchecked")\r
162         public<O> O marshal(LogTarget lt,O o, Writer writer, boolean ... options) throws JAXBException, APIException {\r
163                 boolean pretty, fragment;\r
164                 pretty = options.length>0?options[0]:this.pretty;\r
165                 fragment = options.length>1?options[1]:this.fragment;\r
166                 Pooled<PMarshaller> m = mpool.get(lt);\r
167                 try {\r
168                         if(qname==null) {\r
169                                 m.content.get(pretty,fragment).marshal(o, writer);\r
170                         } else {\r
171                                 m.content.get(pretty,fragment).marshal(\r
172                                         new JAXBElement<O>(qname, (Class<O>)cls, o ),\r
173                                         writer);\r
174                         }\r
175                         return o;\r
176                 } finally {\r
177                         m.done();\r
178                 }\r
179         }\r
180 \r
181         @SuppressWarnings("unchecked")\r
182         public<O> O marshal(LogTarget lt, O o, OutputStream os, boolean ... options) throws JAXBException, APIException {\r
183                 boolean pretty, fragment;\r
184                 pretty = options.length>0?options[0]:this.pretty;\r
185                 fragment = options.length>1?options[1]:this.fragment;\r
186                 Pooled<PMarshaller> m = mpool.get(lt);\r
187                 try {\r
188                         if(qname==null) {\r
189                                 m.content.get(pretty,fragment).marshal(o, os);\r
190                         } else {\r
191                                 m.content.get(pretty,fragment).marshal(\r
192                                         new JAXBElement<O>(qname, (Class<O>)cls, o ),os);\r
193                         }\r
194                         return o;\r
195                 } finally {\r
196                         m.done();\r
197                 }\r
198         }\r
199         \r
200         public<O> O marshal(LogTarget lt, O o, Writer writer, Class<O> clss) throws JAXBException, APIException {\r
201                 Pooled<PMarshaller> m = mpool.get(lt);\r
202                 try {\r
203                         if(qname==null) {\r
204                                 m.content.get(pretty,fragment).marshal(o, writer);\r
205                         } else {\r
206                                 m.content.get(pretty,fragment).marshal(\r
207                                         new JAXBElement<O>(qname, clss, o),writer);\r
208                         }\r
209                         return o;\r
210                 } finally {\r
211                         m.done();\r
212                 }\r
213                         \r
214         }\r
215 \r
216         public<O> O marshal(LogTarget lt, O o, OutputStream os, Class<O> clss) throws JAXBException, APIException {\r
217                 Pooled<PMarshaller> m = mpool.get(lt);\r
218                 try {\r
219                         if(qname==null) { \r
220                                 m.content.get(pretty,fragment).marshal(o, os);\r
221                         } else {\r
222                                 m.content.get(pretty,fragment).marshal(\r
223                                         new JAXBElement<O>(qname, clss, o ),os);\r
224                         }\r
225                         return o;\r
226                 } finally {\r
227                         m.done();\r
228                 }\r
229         }\r
230 \r
231         /**\r
232          * @return\r
233          */\r
234         public Class<?> getMarshalClass() {\r
235                 return cls;\r
236         }\r
237 \r
238         public<O> String stringify(LogTarget lt, O o) throws JAXBException, APIException {\r
239                 StringWriter sw = new StringWriter();\r
240                 marshal(lt,o,sw);\r
241                 return sw.toString();\r
242         }\r
243 \r
244         public JAXBmar pretty(boolean pretty) {\r
245                 this.pretty = pretty;\r
246                 return this;\r
247         }\r
248         \r
249         public JAXBmar asFragment(boolean fragment) {\r
250                 this.fragment = fragment;\r
251                 return this;\r
252         }\r
253 }\r