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