Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / BaseDataFactory.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;\r
23 \r
24 import java.io.File;\r
25 import java.io.FileInputStream;\r
26 import java.io.FileNotFoundException;\r
27 import java.io.IOException;\r
28 \r
29 import javax.xml.XMLConstants;\r
30 import javax.xml.bind.annotation.XmlRootElement;\r
31 import javax.xml.bind.annotation.XmlSchema;\r
32 import javax.xml.namespace.QName;\r
33 import javax.xml.transform.Source;\r
34 import javax.xml.transform.stream.StreamSource;\r
35 import javax.xml.validation.Schema;\r
36 import javax.xml.validation.SchemaFactory;\r
37 \r
38 import org.onap.aaf.misc.env.impl.EnvFactory;\r
39 import org.xml.sax.SAXException;\r
40 \r
41 \r
42 \r
43 /**\r
44  * DataFactory Constructor will create the Stringifiers and Objectifiers necessary \r
45  * by Type and store the Class of the Type for quick creation of Data Objects\r
46  * with reused (and thread safe) components\r
47  * s\r
48  * Native Types are included.\r
49  * Those types covered by Env Implementation are covered dynamically.\r
50  * Types outside of Env mechanism can be added with "add" function\r
51  * \r
52  * @author Jonathan\r
53  *\r
54  * @param <T>\r
55  */\r
56 public class BaseDataFactory {\r
57     private static final Object LOCK = new Object();\r
58     /**\r
59      * Generate a Schema Object for use in validation based on FileNames.\r
60      * \r
61      * WARNING: The java.xml.binding code requires YOU to figure out what order the\r
62      * files go in.  If there is an import from A in B, then you must list A first.\r
63      * \r
64      * @param err\r
65      * @param filenames\r
66      * @return\r
67      * @throws APIException\r
68      */\r
69     public static Schema genSchema(Store env, String ... filenames) throws APIException {\r
70         String schemaDir = env.get(\r
71                 env.staticSlot(EnvFactory.SCHEMA_DIR),\r
72                 EnvFactory.DEFAULT_SCHEMA_DIR);\r
73         File dir = new File(schemaDir);\r
74         if(!dir.exists())throw new APIException("Schema Directory " + schemaDir + " does not exist.  You can set this with " + EnvFactory.SCHEMA_DIR + " property");\r
75         FileInputStream[] fis = new FileInputStream[filenames.length];\r
76         Source[] sources = new Source[filenames.length];\r
77         File f; \r
78         for(int i=0; i<filenames.length; ++i) {\r
79             if(!(f=new File(schemaDir + File.separatorChar + filenames[i])).exists()) {\r
80                 if(!f.exists()) throw new APIException("Cannot find " + f.getName() + " for schema validation");\r
81             }\r
82             try {\r
83                 fis[i]=new FileInputStream(f);\r
84             } catch (FileNotFoundException e) {\r
85                 throw new APIException(e);\r
86             }\r
87             sources[i]= new StreamSource(fis[i]);\r
88         }\r
89         try {\r
90             //Note: SchemaFactory is not reentrant or very thread safe either... see docs\r
91             synchronized(LOCK) { // SchemaFactory is not reentrant\r
92                 return SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)\r
93                      .newSchema(sources);\r
94             }\r
95         } catch (SAXException e) {\r
96             throw new APIException(e);\r
97         } finally {\r
98             for(FileInputStream d : fis) {\r
99                 try {\r
100                     d.close();\r
101                 } catch (IOException e) {\r
102                  // Never mind... we did our best\r
103                 }\r
104             }\r
105         }\r
106 \r
107     }\r
108 \r
109     public static QName getQName(Class<?> clss) throws APIException {\r
110         // Obtain the Necessary info for QName from Requirement\r
111         XmlRootElement xre = clss.getAnnotation(XmlRootElement.class);\r
112         if(xre==null)throw new APIException(clss.getName() + " does not have an XmlRootElement annotation");\r
113         Package pkg = clss.getPackage();\r
114         XmlSchema xs = pkg.getAnnotation(XmlSchema.class);\r
115         if(xs==null) throw new APIException(clss.getName() + " package-info does not have an XmlSchema annotation");\r
116         return new QName(xs.namespace(),xre.name());\r
117     }\r
118 \r
119     /////////////////////////////////////////////\r
120     // Native Type Converters\r
121     /////////////////////////////////////////////\r
122 //    /**\r
123 //     * StringStringifier\r
124 //     * \r
125 //     * Support the Native Type String.. just return it back\r
126 //     * \r
127 //     * @author Jonathan\r
128 //     *\r
129 //     */\r
130 //    public static class StringStringifier extends NullLifeCycle implements Stringifier<String> {\r
131 //        /* (non-Javadoc)\r
132 //         * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
133 //         */\r
134 //        public String stringify(Env env, String input) throws APIException {\r
135 //            return input;\r
136 //        }\r
137 //    };        \r
138 //\r
139 //    /**\r
140 //     * StringObjectifier\r
141 //     * \r
142 //     * Support the Native Type String.. just return it back\r
143 //     * \r
144 //     * @author Jonathan\r
145 //     *\r
146 //     */\r
147 //    public static class StringObjectifier extends NullLifeCycle implements Objectifier<String> {\r
148 //        /* (non-Javadoc)\r
149 //         * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
150 //         */\r
151 //        public String objectify(Env env, String input) throws APIException {\r
152 //            return input;\r
153 //        }\r
154 //\r
155 //        /* (non-Javadoc)\r
156 //         * @see com.att.env.Objectifier#newObject()\r
157 //         */\r
158 //        public String newInstance() throws APIException {\r
159 //            return "";\r
160 //        }\r
161 //    };\r
162 //    \r
163 //    /**\r
164 //     * LongStringifier\r
165 //     * \r
166 //     * Support the Native Type Long.. use Long parse functions\r
167 //     * \r
168 //     * @author Jonathan\r
169 //     *\r
170 //     */\r
171 //    public static class LongStringifier extends NullLifeCycle implements Stringifier<Long> {\r
172 //        public String stringify(Env env, Long input) throws APIException {\r
173 //            return input.toString();\r
174 //        }\r
175 //    }\r
176 //    \r
177 //    /**\r
178 //     * LongObjectifier\r
179 //     * \r
180 //     * Support the Native Type Long.. use Long parse functions\r
181 //     * \r
182 //     * @author Jonathan\r
183 //     *\r
184 //     */\r
185 //    public static class LongObjectifier extends NullLifeCycle implements Objectifier<Long> {\r
186 //        /* (non-Javadoc)\r
187 //         * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
188 //         */\r
189 //        public Long objectify(Env env, String input) throws APIException {\r
190 //            try {\r
191 //                return new Long(input);\r
192 //            } catch (Exception e) {\r
193 //                APIException ae = new APIException("Cannot create a \"Long\" from [" + input + ']');\r
194 //                ae.initCause(e);\r
195 //                throw ae;\r
196 //            }\r
197 //        }\r
198 //\r
199 //        /* (non-Javadoc)\r
200 //         * @see com.att.env.Objectifier#newObject()\r
201 //         */\r
202 //        public Long newInstance() throws APIException {\r
203 //            return 0L;\r
204 //        }\r
205 //    }\r
206 //\r
207 //    /**\r
208 //     * IntegerStringifier\r
209 //     * \r
210 //     * Support the Native Integer.. use Integer parse functions\r
211 //     * \r
212 //     * @author Jonathan\r
213 //     *\r
214 //     */\r
215 //    public static class IntegerStringifier extends NullLifeCycle implements Stringifier<Integer> {\r
216 //        /* (non-Javadoc)\r
217 //         * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
218 //         */\r
219 //        public String stringify(Env env, Integer input) throws APIException {\r
220 //            return input.toString();\r
221 //        }\r
222 //    }\r
223 //    \r
224 //    /**\r
225 //     * IntegerObjectifier\r
226 //     * \r
227 //     * Support the Native Integer.. use Integer parse functions\r
228 //     * \r
229 //     * @author Jonathan\r
230 //     *\r
231 //     */\r
232 //    public static class IntegerObjectifier extends NullLifeCycle implements Objectifier<Integer> {\r
233 //        /* (non-Javadoc)\r
234 //         * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
235 //         */\r
236 //        public Integer objectify(Env env, String input) throws APIException {\r
237 //            try {\r
238 //                return new Integer(input);\r
239 //            } catch (Exception e) {\r
240 //                APIException ae = new APIException("Cannot create a \"Integer\" from [" + input + ']');\r
241 //                ae.initCause(e);\r
242 //                throw ae;\r
243 //            }\r
244 //        }\r
245 //\r
246 //        /* (non-Javadoc)\r
247 //         * @see com.att.env.Objectifier#newObject()\r
248 //         */\r
249 //        public Integer newInstance() throws APIException {\r
250 //            return 0;\r
251 //        }\r
252 //    }\r
253 //\r
254 //    /**\r
255 //     * ShortStringifier\r
256 //     * \r
257 //     * Support the Native Short.. use Short parse functions\r
258 //     * \r
259 //     * @author Jonathan\r
260 //     *\r
261 //     */\r
262 //    public static class ShortStringifier extends NullLifeCycle implements Stringifier<Short> {\r
263 //        public String stringify(Env env, Short input) throws APIException {\r
264 //            return input.toString();\r
265 //        }\r
266 //    }\r
267 //    \r
268 //    /**\r
269 //     * ShortObjectifier\r
270 //     * \r
271 //     * Support the Native Short.. use Short parse functions\r
272 //     * \r
273 //     * @author Jonathan\r
274 //     *\r
275 //     */\r
276 //    public static class ShortObjectifier extends NullLifeCycle implements Objectifier<Short> {\r
277 //        public Short objectify(Env env, String input) throws APIException {\r
278 //            try {\r
279 //                return new Short(input);\r
280 //            } catch (Exception e) {\r
281 //                APIException ae = new APIException("Cannot create a \"Short\" from [" + input + ']');\r
282 //                ae.initCause(e);\r
283 //                throw ae;\r
284 //            }\r
285 //        }\r
286 //\r
287 //        public Short newInstance() throws APIException {\r
288 //            return 0;\r
289 //        }\r
290 //    }\r
291 //    \r
292 //    /**\r
293 //     * ByteStringifier\r
294 //     * \r
295 //     * Support the Native Byte.. use Byte parse functions\r
296 //     * \r
297 //     * @author Jonathan\r
298 //     *\r
299 //     */\r
300 //    public static class ByteStringifier extends NullLifeCycle implements Stringifier<Byte> {\r
301 //        /* (non-Javadoc)\r
302 //         * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
303 //         */\r
304 //        public String stringify(Env env, Byte input) throws APIException {\r
305 //            return input.toString();\r
306 //        }\r
307 //    }\r
308 //    \r
309 //    /**\r
310 //     * ByteObjectifier\r
311 //     * \r
312 //     * Support the Native Byte.. use Byte parse functions\r
313 //     * \r
314 //     * @author Jonathan\r
315 //     *\r
316 //     */\r
317 //    public static class ByteObjectifier extends NullLifeCycle implements Objectifier<Byte> {\r
318 //        /* (non-Javadoc)\r
319 //         * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
320 //         */\r
321 //        public Byte objectify(Env env, String input) throws APIException {\r
322 //            try {\r
323 //                return new Byte(input);\r
324 //            } catch (Exception e) {\r
325 //                APIException ae = new APIException("Cannot create a \"Byte\" from [" + input + ']');\r
326 //                ae.initCause(e);\r
327 //                throw ae;\r
328 //            }\r
329 //        }\r
330 //\r
331 //        /* (non-Javadoc)\r
332 //         * @see com.att.env.Objectifier#newObject()\r
333 //         */\r
334 //        public Byte newInstance() throws APIException {\r
335 //            return 0;\r
336 //        }\r
337 //    }\r
338 //\r
339 //    /**\r
340 //     * CharacterStringifier\r
341 //     * \r
342 //     * Support the Native Character.. use Character parse functions\r
343 //     * \r
344 //     * @author Jonathan\r
345 //     *\r
346 //     */\r
347 //    public static class CharacterStringifier extends NullLifeCycle implements Stringifier<Character> {\r
348 //        /* (non-Javadoc)\r
349 //         * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
350 //         */\r
351 //        public String stringify(Env env, Character input) throws APIException {\r
352 //            return input.toString();\r
353 //        }\r
354 //    }\r
355 //    \r
356 //    /**\r
357 //     * CharacterObjectifier\r
358 //     * \r
359 //     * Support the Native Character.. use Character parse functions\r
360 //     * \r
361 //     * @author Jonathan\r
362 //     *\r
363 //     */\r
364 //    public static class CharacterObjectifier extends NullLifeCycle implements Objectifier<Character> {\r
365 //        /* (non-Javadoc)\r
366 //         * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
367 //         */\r
368 //        public Character objectify(Env env, String input) throws APIException {\r
369 //            int length = input.length();\r
370 //            if(length<1 || length>1) {\r
371 //                throw new APIException("String [" + input + "] does not represent a single Character");\r
372 //            }\r
373 //            return input.charAt(0);\r
374 //        }\r
375 //\r
376 //        /* (non-Javadoc)\r
377 //         * @see com.att.env.Objectifier#newObject()\r
378 //         */\r
379 //        public Character newInstance() throws APIException {\r
380 //            return 0;\r
381 //        }\r
382 //    }\r
383 //\r
384 //    /**\r
385 //     * FloatStringifier\r
386 //     * \r
387 //     * Support the Native Float.. use Float parse functions\r
388 //     * \r
389 //     * @author Jonathan\r
390 //     *\r
391 //     */\r
392 //    public static class FloatStringifier extends NullLifeCycle implements Stringifier<Float> {\r
393 //        /* (non-Javadoc)\r
394 //         * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
395 //         */\r
396 //        public String stringify(Env env, Float input) throws APIException {\r
397 //            return input.toString();\r
398 //        }\r
399 //    }\r
400 //    \r
401 //    /**\r
402 //     * FloatObjectifier\r
403 //     * \r
404 //     * Support the Native Float.. use Float parse functions\r
405 //     * \r
406 //     * @author Jonathan\r
407 //     *\r
408 //     */\r
409 //    public static class FloatObjectifier extends NullLifeCycle implements Objectifier<Float> {\r
410 //        /* (non-Javadoc)\r
411 //         * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
412 //         */\r
413 //        public Float objectify(Env env, String input) throws APIException {\r
414 //            try {\r
415 //                return new Float(input);\r
416 //            } catch (Exception e) {\r
417 //                APIException ae = new APIException("Cannot create a \"Float\" from [" + input + ']');\r
418 //                ae.initCause(e);\r
419 //                throw ae;\r
420 //            }\r
421 //        }\r
422 //\r
423 //        /* (non-Javadoc)\r
424 //         * @see com.att.env.Objectifier#newObject()\r
425 //         */\r
426 //        public Float newInstance() throws APIException {\r
427 //            return 0.0f;\r
428 //        }\r
429 //    }\r
430 //\r
431 //    /**\r
432 //     * DoubleStringifier\r
433 //     * \r
434 //     * Support the Native Double.. use Double parse functions\r
435 //     * \r
436 //     * @author Jonathan\r
437 //     *\r
438 //     */\r
439 //    public static class DoubleStringifier extends NullLifeCycle implements Stringifier<Double> {\r
440 //        /* (non-Javadoc)\r
441 //         * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
442 //         */\r
443 //        public String stringify(Env env, Double input) throws APIException {\r
444 //            return input.toString();\r
445 //        }\r
446 //    }\r
447 //    \r
448 //    /**\r
449 //     * DoubleObjectifier\r
450 //     * \r
451 //     * Support the Native Double.. use Double parse functions\r
452 //     * \r
453 //     * @author Jonathan\r
454 //     *\r
455 //     */\r
456 //    public static class DoubleObjectifier extends NullLifeCycle implements Objectifier<Double> {\r
457 //        /* (non-Javadoc)\r
458 //         * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
459 //         */\r
460 //        public Double objectify(Env env, String input) throws APIException {\r
461 //            try {\r
462 //                return new Double(input);\r
463 //            } catch (Exception e) {\r
464 //                APIException ae = new APIException("Cannot create a \"Double\" from [" + input + ']');\r
465 //                ae.initCause(e);\r
466 //                throw ae;\r
467 //            }\r
468 //        }\r
469 //\r
470 //        /* (non-Javadoc)\r
471 //         * @see com.att.env.Objectifier#newObject()\r
472 //         */\r
473 //        public Double newInstance() throws APIException {\r
474 //            return 0.0;\r
475 //        }\r
476 //    }\r
477 \r
478 }\r