432a449a3de7e9f76df0d65c80e5e72980493d20
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / jaxb / JAXBObjectifier.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 package org.onap.aaf.misc.env.jaxb;\r
23 \r
24 import java.io.InputStream;\r
25 import java.io.Reader;\r
26 \r
27 import javax.xml.bind.JAXBException;\r
28 import javax.xml.validation.Schema;\r
29 \r
30 import org.onap.aaf.misc.env.APIException;\r
31 import org.onap.aaf.misc.env.Env;\r
32 import org.onap.aaf.misc.env.TimeTaken;\r
33 import org.onap.aaf.misc.env.old.IOObjectifier;\r
34 \r
35 /**\r
36  * Allow Extended IO interface usage without muddying up the Stringifier Interface\r
37  */\r
38 public class JAXBObjectifier<T> implements IOObjectifier<T> {\r
39         private JAXBumar jumar;\r
40 \r
41         public JAXBObjectifier(Schema schema, Class<?>... classes) throws APIException {\r
42                 try {\r
43                         jumar = new JAXBumar(schema, classes);\r
44                 } catch (JAXBException e) {\r
45                         throw new APIException(e);\r
46                 }\r
47         }\r
48 \r
49         public JAXBObjectifier(Class<?>... classes) throws APIException {\r
50                 try {\r
51                         jumar = new JAXBumar(classes);\r
52                 } catch (JAXBException e) {\r
53                         throw new APIException(e);\r
54                 }\r
55         }\r
56         \r
57     // package on purpose\r
58         JAXBObjectifier(JAXBumar jumar) {\r
59                 this.jumar = jumar;\r
60         }\r
61 \r
62         @SuppressWarnings("unchecked")\r
63         // @Override\r
64         public T objectify(Env env, String input) throws APIException {\r
65                 TimeTaken tt = env.start("JAXB Unmarshal", Env.XML);\r
66                 try {\r
67                         tt.size(input.length());\r
68                         return (T)jumar.unmarshal(env.debug(), input);\r
69                 } catch (JAXBException e) {\r
70                         throw new APIException(e);\r
71                 } finally {\r
72                         tt.done();\r
73                 }\r
74         }\r
75 \r
76         @SuppressWarnings("unchecked")\r
77         // @Override\r
78         public T objectify(Env env, Reader rdr) throws APIException {\r
79                 //TODO create a Reader that Counts?\r
80                 TimeTaken tt = env.start("JAXB Unmarshal", Env.XML);\r
81                 try {\r
82                         return (T)jumar.unmarshal(env.debug(), rdr);\r
83                 } catch (JAXBException e) {\r
84                         throw new APIException(e);\r
85                 } finally {\r
86                         tt.done();\r
87                 }\r
88         }\r
89 \r
90 \r
91         @SuppressWarnings("unchecked")\r
92         // @Override\r
93         public T objectify(Env env, InputStream is) throws APIException {\r
94                 //TODO create a Reader that Counts?\r
95                 TimeTaken tt = env.start("JAXB Unmarshal", Env.XML);\r
96                 try {\r
97                         return (T)jumar.unmarshal(env.debug(), is);\r
98                 } catch (JAXBException e) {\r
99                         throw new APIException(e);\r
100                 } finally {\r
101                         tt.done();\r
102                 }\r
103         }\r
104 \r
105 \r
106         public void servicePrestart(Env env) throws APIException {\r
107         }\r
108 \r
109         public void threadPrestart(Env env) throws APIException {\r
110         }\r
111 \r
112         // // @Override\r
113         public void refresh(Env env) throws APIException {\r
114         }\r
115 \r
116         // // @Override\r
117         public void threadDestroy(Env env) throws APIException {\r
118         }\r
119 \r
120         // // @Override\r
121         public void serviceDestroy(Env env) throws APIException {\r
122         }\r
123 \r
124 \r
125         @SuppressWarnings("unchecked")\r
126         public T newInstance() throws APIException {\r
127                 try {\r
128                         return (T)jumar.newInstance();\r
129                 } catch (Exception e) {\r
130                         throw new APIException(e);\r
131                 }\r
132         }\r
133 \r
134 }\r
135 \r