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