JAXBData.java-Extract the assignment out of expression
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / jaxb / JAXBData.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.ByteArrayInputStream;\r
25 import java.io.IOException;\r
26 import java.io.InputStream;\r
27 import java.io.OutputStream;\r
28 import java.io.Reader;\r
29 import java.io.Writer;\r
30 \r
31 import javax.xml.bind.JAXBException;\r
32 \r
33 import org.onap.aaf.misc.env.APIException;\r
34 import org.onap.aaf.misc.env.Data;\r
35 import org.onap.aaf.misc.env.Env;\r
36 import org.onap.aaf.misc.env.EnvJAXB;\r
37 import org.onap.aaf.misc.env.old.IOStringifier;\r
38 import org.onap.aaf.misc.env.old.Objectifier;\r
39 import org.onap.aaf.misc.env.old.Stringifier;\r
40 /**\r
41  * <H1>Data</H1>\r
42  * <i>Data</i> facilitates lazy marshaling of data with a pre-determined\r
43  * marshaling mechanism.<p>\r
44  * \r
45  * It stores either Object (defined by Generic {@literal <T>}) or String.<p>  \r
46  * \r
47  * On asking for Object of type {@literal <T>}, it will respond with the object\r
48  * if it exists, or unmarshal the string and pass the result back.<p>\r
49  * \r
50  * On asking for String, it will respond with the String\r
51  * if it exists, or marshal the String and pass the result back.<p>\r
52  * \r
53  * @author Jonathan\r
54  *\r
55  * @param <T>\r
56  */\r
57 public final class JAXBData<T> implements Data<T>{\r
58     private Stringifier<T> stringifier;\r
59     private Objectifier<T> objectifier;\r
60     private String dataAsString;\r
61     private T dataAsObject;\r
62     private Class<T> tclass;\r
63     private JAXBDF<T> df;\r
64     private Env creatingEnv;\r
65     private boolean options[] = new boolean[] {false, false};\r
66     \r
67     /**\r
68      * Construct a Data Object with an appropriate Stringifier, Objectifier and Class to support\r
69      * \r
70      * @param env\r
71      * @param strfr\r
72      * @param objfr\r
73      * @param text\r
74      * @param typeClass\r
75      */\r
76     JAXBData(Env env, JAXBDF<T> df, Stringifier<T> strfr, Objectifier<T> objfr, String text, Class<T> typeClass) {\r
77         dataAsString = text;\r
78         dataAsObject = null;\r
79         stringifier = strfr;\r
80         objectifier = objfr;\r
81         tclass = typeClass;\r
82         creatingEnv = env;\r
83         this.df = df;\r
84     }\r
85     \r
86     \r
87     /**\r
88      * Construct a Data Object with an appropriate Stringifier, Objectifier and Object (which will\r
89      * yield it's class)\r
90      * \r
91      * @param env\r
92      * @param strfr\r
93      * @param objfr\r
94      * @param object\r
95      */\r
96     @SuppressWarnings("unchecked")\r
97     JAXBData(Env env, JAXBDF<T> df, Stringifier<T> strfr, Objectifier<T> objfr, T object) {\r
98         dataAsString = null;\r
99         dataAsObject = object;\r
100         stringifier = strfr;\r
101         objectifier = objfr;\r
102         tclass = (Class<T>) object.getClass();\r
103         creatingEnv = env;\r
104         this.df = df;\r
105     }\r
106 \r
107     /**\r
108      * Respond with the String if it exists, or marshal the String and pass the result back.<p>\r
109      * \r
110      * Explicitly use a specific Env for logging purposes\r
111      * \r
112      * @param env\r
113      * @return String\r
114      * @throws APIException\r
115      */\r
116     public String asString(EnvJAXB env) throws APIException {\r
117         if (dataAsString!=null) {\r
118             return dataAsString;\r
119         } else {\r
120                 dataAsString = stringifier.stringify(env, dataAsObject);\r
121             return dataAsString;\r
122         }\r
123     }\r
124 \r
125     /**\r
126      * Respond with the String if it exists, or marshal the String and pass the result back.\r
127      * \r
128      * However, use the Env the Data Object was created with.\r
129      * \r
130      * @return String\r
131      * @throws APIException\r
132      */\r
133     // @Override\r
134     public String asString() throws APIException {\r
135         if (dataAsString!=null) {\r
136             return dataAsString;\r
137         } else {\r
138             dataAsString = stringifier.stringify(creatingEnv, dataAsObject,options);\r
139             return dataAsString;\r
140         }\r
141     }\r
142     \r
143     public Data<T> to(OutputStream os) throws APIException, IOException {\r
144         if (dataAsString!=null) {\r
145             os.write(dataAsString.getBytes());\r
146         } else if (stringifier instanceof IOStringifier){\r
147             ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, os, options);\r
148         } else {\r
149             dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);\r
150             os.write(dataAsString.getBytes());\r
151         }\r
152         return this;\r
153     }\r
154 \r
155 \r
156     // @Override\r
157     public JAXBData<T> to(Writer writer) throws APIException, IOException {\r
158         if (dataAsString!=null) {\r
159             writer.write(dataAsString);\r
160         } else if (stringifier instanceof IOStringifier){\r
161             ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, writer, options);\r
162         } else {\r
163             dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);\r
164             writer.write(dataAsString);\r
165         }\r
166         return this;\r
167     }\r
168 \r
169 \r
170     public InputStream getInputStream() throws APIException {\r
171         if (dataAsString==null) {\r
172             dataAsString = stringifier.stringify(creatingEnv,dataAsObject,options);\r
173         }\r
174         return new ByteArrayInputStream(dataAsString.getBytes());\r
175     }\r
176     \r
177     /**\r
178      * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String \r
179      * and pass the result back.<p>\r
180      * \r
181       * Explicitly use a specific Env for logging purposes\r
182      * \r
183      * @param env\r
184      * @return T\r
185      * @throws APIException\r
186      */\r
187 \r
188     public T asObject(EnvJAXB env) throws APIException {\r
189         if (dataAsObject !=null) {\r
190             return dataAsObject;\r
191         } else {\r
192             // Some Java compilers need two statements here\r
193             dataAsObject = objectifier.objectify(env, dataAsString);\r
194             return dataAsObject;\r
195         }\r
196     }\r
197 \r
198     /**\r
199      * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String \r
200      * and pass the result back.<p>\r
201      *\r
202      * However, use the Env the Data Object was created with.\r
203      * \r
204      * @return T\r
205      * @throws APIException\r
206      */\r
207     // @Override\r
208     public T asObject() throws APIException {\r
209         if (dataAsObject !=null) {\r
210             return dataAsObject;\r
211         } else {\r
212             // Some Java compilers need two statements here\r
213             dataAsObject = objectifier.objectify(creatingEnv, dataAsString);\r
214             return dataAsObject;\r
215         }\r
216     }\r
217     \r
218 \r
219     /**\r
220      * Return the Class Type supported by this DataObject\r
221      * \r
222      * @return {@literal Class<T>}\r
223      */\r
224     // @Override\r
225     public Class<T> getTypeClass() {\r
226         return tclass;\r
227     }\r
228     \r
229     \r
230     /**\r
231      * For Debugging Convenience, we marshal to String if possible.\r
232      * \r
233      * Behavior is essentially the same as asString(), except asString() throws\r
234      * an APIException.  <p>\r
235      * Since toString() must not throw exceptions, the function just catches and prints an\r
236      * error, which is probably not the behavior desired.<p>\r
237      *  \r
238      * Therefore, use "asString()" where possible in actual Transactional code. \r
239      * \r
240      * @see java.lang.Object#toString()\r
241      */\r
242     // @Override\r
243     public String toString() {\r
244         if (dataAsString!=null) {\r
245             return dataAsString;\r
246         } else {\r
247             try {\r
248                 dataAsString = stringifier.stringify(creatingEnv, dataAsObject);\r
249                 return dataAsString;\r
250             } catch (APIException e) {\r
251                 return "ERROR - Can't Stringify from Object " + e.getLocalizedMessage();\r
252             }\r
253         }\r
254     }\r
255 \r
256     public Data<T> load(T t) throws APIException {\r
257         dataAsObject = t;\r
258         dataAsString = null;\r
259         return this;\r
260     }\r
261 \r
262 \r
263     public Data<T> load(String str) throws APIException {\r
264         dataAsObject = null;\r
265         dataAsString = str;\r
266         return this;\r
267     }\r
268 \r
269 \r
270     public Data<T> load(InputStream is) throws APIException {\r
271         try {\r
272             dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),is);\r
273             dataAsString = null;\r
274         } catch (JAXBException e) {\r
275             throw new APIException(e);\r
276         }\r
277         return this;\r
278     }\r
279 \r
280 \r
281     public Data<T> load(Reader rdr) throws APIException {\r
282         try {\r
283             dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),rdr);\r
284             dataAsString = null;\r
285         } catch (JAXBException e) {\r
286             throw new APIException(e);\r
287         }\r
288         return this;\r
289     }\r
290 \r
291 \r
292     // @Override\r
293     public void direct(InputStream input, OutputStream output) throws APIException, IOException {\r
294         byte b[] = new byte[128];\r
295         int count;\r
296         do {\r
297             count = input.read(b);\r
298             if (count>0)output.write(b, 0, count);\r
299         } while (count>=0);\r
300     }\r
301 \r
302 \r
303     // @Override\r
304     public Data<T> out(TYPE type) {\r
305         // it's going to be XML regardless...\r
306         return this;\r
307     }\r
308 \r
309 \r
310     // @Override\r
311     public Data<T> in(TYPE type) {\r
312         // Not Supported... will still be XML\r
313         return this;\r
314     }\r
315 \r
316 \r
317     // @Override\r
318     public Data<T> option(int option) {\r
319         options[0] = (option&Data.PRETTY)==Data.PRETTY;\r
320         options[1] = (option&Data.FRAGMENT)==Data.FRAGMENT;\r
321         return this;\r
322     }\r
323     \r
324 }