Add Notice of aaf/inno source moving to aaf/authz
[aaf/inno.git] / env / src / main / java / org / onap / aaf / inno / env / BaseDataFactory.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;\r
24 \r
25 import java.io.File;\r
26 import java.io.FileInputStream;\r
27 import java.io.FileNotFoundException;\r
28 import java.io.IOException;\r
29 \r
30 import javax.xml.XMLConstants;\r
31 import javax.xml.bind.annotation.XmlRootElement;\r
32 import javax.xml.bind.annotation.XmlSchema;\r
33 import javax.xml.namespace.QName;\r
34 import javax.xml.transform.Source;\r
35 import javax.xml.transform.stream.StreamSource;\r
36 import javax.xml.validation.Schema;\r
37 import javax.xml.validation.SchemaFactory;\r
38 \r
39 import org.onap.aaf.inno.env.impl.EnvFactory;\r
40 import org.xml.sax.SAXException;\r
41 \r
42 \r
43 \r
44 /**\r
45  * DataFactory Constructor will create the Stringifiers and Objectifiers necessary \r
46  * by Type and store the Class of the Type for quick creation of Data Objects\r
47  * with reused (and thread safe) components\r
48  * s\r
49  * Native Types are included.\r
50  * Those types covered by Env Implementation are covered dynamically.\r
51  * Types outside of Env mechanism can be added with "add" function\r
52  * \r
53  *\r
54  * @param <T>\r
55  */\r
56 public class BaseDataFactory {\r
57 \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(XMLConstants.W3C_XML_SCHEMA_NS_URI) { // 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 //       *\r
128 //       */\r
129 //      public static class StringStringifier extends NullLifeCycle implements Stringifier<String> {\r
130 //              /* (non-Javadoc)\r
131 //               * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
132 //               */\r
133 //              public String stringify(Env env, String input) throws APIException {\r
134 //                      return input;\r
135 //              }\r
136 //      };              \r
137 //\r
138 //      /**\r
139 //       * StringObjectifier\r
140 //       * \r
141 //       * Support the Native Type String.. just return it back\r
142 //       * \r
143 //       *\r
144 //       */\r
145 //      public static class StringObjectifier extends NullLifeCycle implements Objectifier<String> {\r
146 //              /* (non-Javadoc)\r
147 //               * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
148 //               */\r
149 //              public String objectify(Env env, String input) throws APIException {\r
150 //                      return input;\r
151 //              }\r
152 //\r
153 //              /* (non-Javadoc)\r
154 //               * @see com.att.env.Objectifier#newObject()\r
155 //               */\r
156 //              public String newInstance() throws APIException {\r
157 //                      return "";\r
158 //              }\r
159 //      };\r
160 //      \r
161 //      /**\r
162 //       * LongStringifier\r
163 //       * \r
164 //       * Support the Native Type Long.. use Long parse functions\r
165 //       * \r
166 //       *\r
167 //       */\r
168 //      public static class LongStringifier extends NullLifeCycle implements Stringifier<Long> {\r
169 //              public String stringify(Env env, Long input) throws APIException {\r
170 //                      return input.toString();\r
171 //              }\r
172 //      }\r
173 //      \r
174 //      /**\r
175 //       * LongObjectifier\r
176 //       * \r
177 //       * Support the Native Type Long.. use Long parse functions\r
178 //       * \r
179 //       *\r
180 //       */\r
181 //      public static class LongObjectifier extends NullLifeCycle implements Objectifier<Long> {\r
182 //              /* (non-Javadoc)\r
183 //               * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
184 //               */\r
185 //              public Long objectify(Env env, String input) throws APIException {\r
186 //                      try {\r
187 //                              return new Long(input);\r
188 //                      } catch (Exception e) {\r
189 //                              APIException ae = new APIException("Cannot create a \"Long\" from [" + input + ']');\r
190 //                              ae.initCause(e);\r
191 //                              throw ae;\r
192 //                      }\r
193 //              }\r
194 //\r
195 //              /* (non-Javadoc)\r
196 //               * @see com.att.env.Objectifier#newObject()\r
197 //               */\r
198 //              public Long newInstance() throws APIException {\r
199 //                      return 0L;\r
200 //              }\r
201 //      }\r
202 //\r
203 //      /**\r
204 //       * IntegerStringifier\r
205 //       * \r
206 //       * Support the Native Integer.. use Integer parse functions\r
207 //       * \r
208 //       *\r
209 //       */\r
210 //      public static class IntegerStringifier extends NullLifeCycle implements Stringifier<Integer> {\r
211 //              /* (non-Javadoc)\r
212 //               * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
213 //               */\r
214 //              public String stringify(Env env, Integer input) throws APIException {\r
215 //                      return input.toString();\r
216 //              }\r
217 //      }\r
218 //      \r
219 //      /**\r
220 //       * IntegerObjectifier\r
221 //       * \r
222 //       * Support the Native Integer.. use Integer parse functions\r
223 //       * \r
224 //       *\r
225 //       */\r
226 //      public static class IntegerObjectifier extends NullLifeCycle implements Objectifier<Integer> {\r
227 //              /* (non-Javadoc)\r
228 //               * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
229 //               */\r
230 //              public Integer objectify(Env env, String input) throws APIException {\r
231 //                      try {\r
232 //                              return new Integer(input);\r
233 //                      } catch (Exception e) {\r
234 //                              APIException ae = new APIException("Cannot create a \"Integer\" from [" + input + ']');\r
235 //                              ae.initCause(e);\r
236 //                              throw ae;\r
237 //                      }\r
238 //              }\r
239 //\r
240 //              /* (non-Javadoc)\r
241 //               * @see com.att.env.Objectifier#newObject()\r
242 //               */\r
243 //              public Integer newInstance() throws APIException {\r
244 //                      return 0;\r
245 //              }\r
246 //      }\r
247 //\r
248 //      /**\r
249 //       * ShortStringifier\r
250 //       * \r
251 //       * Support the Native Short.. use Short parse functions\r
252 //       * \r
253 //       *\r
254 //       */\r
255 //      public static class ShortStringifier extends NullLifeCycle implements Stringifier<Short> {\r
256 //              public String stringify(Env env, Short input) throws APIException {\r
257 //                      return input.toString();\r
258 //              }\r
259 //      }\r
260 //      \r
261 //      /**\r
262 //       * ShortObjectifier\r
263 //       * \r
264 //       * Support the Native Short.. use Short parse functions\r
265 //       * \r
266 //       *\r
267 //       */\r
268 //      public static class ShortObjectifier extends NullLifeCycle implements Objectifier<Short> {\r
269 //              public Short objectify(Env env, String input) throws APIException {\r
270 //                      try {\r
271 //                              return new Short(input);\r
272 //                      } catch (Exception e) {\r
273 //                              APIException ae = new APIException("Cannot create a \"Short\" from [" + input + ']');\r
274 //                              ae.initCause(e);\r
275 //                              throw ae;\r
276 //                      }\r
277 //              }\r
278 //\r
279 //              public Short newInstance() throws APIException {\r
280 //                      return 0;\r
281 //              }\r
282 //      }\r
283 //      \r
284 //      /**\r
285 //       * ByteStringifier\r
286 //       * \r
287 //       * Support the Native Byte.. use Byte parse functions\r
288 //       * \r
289 //       *\r
290 //       */\r
291 //      public static class ByteStringifier extends NullLifeCycle implements Stringifier<Byte> {\r
292 //              /* (non-Javadoc)\r
293 //               * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
294 //               */\r
295 //              public String stringify(Env env, Byte input) throws APIException {\r
296 //                      return input.toString();\r
297 //              }\r
298 //      }\r
299 //      \r
300 //      /**\r
301 //       * ByteObjectifier\r
302 //       * \r
303 //       * Support the Native Byte.. use Byte parse functions\r
304 //       * \r
305 //       *\r
306 //       */\r
307 //      public static class ByteObjectifier extends NullLifeCycle implements Objectifier<Byte> {\r
308 //              /* (non-Javadoc)\r
309 //               * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
310 //               */\r
311 //              public Byte objectify(Env env, String input) throws APIException {\r
312 //                      try {\r
313 //                              return new Byte(input);\r
314 //                      } catch (Exception e) {\r
315 //                              APIException ae = new APIException("Cannot create a \"Byte\" from [" + input + ']');\r
316 //                              ae.initCause(e);\r
317 //                              throw ae;\r
318 //                      }\r
319 //              }\r
320 //\r
321 //              /* (non-Javadoc)\r
322 //               * @see com.att.env.Objectifier#newObject()\r
323 //               */\r
324 //              public Byte newInstance() throws APIException {\r
325 //                      return 0;\r
326 //              }\r
327 //      }\r
328 //\r
329 //      /**\r
330 //       * CharacterStringifier\r
331 //       * \r
332 //       * Support the Native Character.. use Character parse functions\r
333 //       * \r
334 //       *\r
335 //       */\r
336 //      public static class CharacterStringifier extends NullLifeCycle implements Stringifier<Character> {\r
337 //              /* (non-Javadoc)\r
338 //               * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
339 //               */\r
340 //              public String stringify(Env env, Character input) throws APIException {\r
341 //                      return input.toString();\r
342 //              }\r
343 //      }\r
344 //      \r
345 //      /**\r
346 //       * CharacterObjectifier\r
347 //       * \r
348 //       * Support the Native Character.. use Character parse functions\r
349 //       * \r
350 //       *\r
351 //       */\r
352 //      public static class CharacterObjectifier extends NullLifeCycle implements Objectifier<Character> {\r
353 //              /* (non-Javadoc)\r
354 //               * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
355 //               */\r
356 //              public Character objectify(Env env, String input) throws APIException {\r
357 //                      int length = input.length();\r
358 //                      if(length<1 || length>1) {\r
359 //                              throw new APIException("String [" + input + "] does not represent a single Character");\r
360 //                      }\r
361 //                      return input.charAt(0);\r
362 //              }\r
363 //\r
364 //              /* (non-Javadoc)\r
365 //               * @see com.att.env.Objectifier#newObject()\r
366 //               */\r
367 //              public Character newInstance() throws APIException {\r
368 //                      return 0;\r
369 //              }\r
370 //      }\r
371 //\r
372 //      /**\r
373 //       * FloatStringifier\r
374 //       * \r
375 //       * Support the Native Float.. use Float parse functions\r
376 //       * \r
377 //       *\r
378 //       */\r
379 //      public static class FloatStringifier extends NullLifeCycle implements Stringifier<Float> {\r
380 //              /* (non-Javadoc)\r
381 //               * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
382 //               */\r
383 //              public String stringify(Env env, Float input) throws APIException {\r
384 //                      return input.toString();\r
385 //              }\r
386 //      }\r
387 //      \r
388 //      /**\r
389 //       * FloatObjectifier\r
390 //       * \r
391 //       * Support the Native Float.. use Float parse functions\r
392 //       * \r
393 //       *\r
394 //       */\r
395 //      public static class FloatObjectifier extends NullLifeCycle implements Objectifier<Float> {\r
396 //              /* (non-Javadoc)\r
397 //               * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
398 //               */\r
399 //              public Float objectify(Env env, String input) throws APIException {\r
400 //                      try {\r
401 //                              return new Float(input);\r
402 //                      } catch (Exception e) {\r
403 //                              APIException ae = new APIException("Cannot create a \"Float\" from [" + input + ']');\r
404 //                              ae.initCause(e);\r
405 //                              throw ae;\r
406 //                      }\r
407 //              }\r
408 //\r
409 //              /* (non-Javadoc)\r
410 //               * @see com.att.env.Objectifier#newObject()\r
411 //               */\r
412 //              public Float newInstance() throws APIException {\r
413 //                      return 0.0f;\r
414 //              }\r
415 //      }\r
416 //\r
417 //      /**\r
418 //       * DoubleStringifier\r
419 //       * \r
420 //       * Support the Native Double.. use Double parse functions\r
421 //       *\r
422 //       */\r
423 //      public static class DoubleStringifier extends NullLifeCycle implements Stringifier<Double> {\r
424 //              /* (non-Javadoc)\r
425 //               * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)\r
426 //               */\r
427 //              public String stringify(Env env, Double input) throws APIException {\r
428 //                      return input.toString();\r
429 //              }\r
430 //      }\r
431 //      \r
432 //      /**\r
433 //       * DoubleObjectifier\r
434 //       * \r
435 //       * Support the Native Double.. use Double parse functions\r
436 //       * \r
437 //       *\r
438 //       */\r
439 //      public static class DoubleObjectifier extends NullLifeCycle implements Objectifier<Double> {\r
440 //              /* (non-Javadoc)\r
441 //               * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)\r
442 //               */\r
443 //              public Double objectify(Env env, String input) throws APIException {\r
444 //                      try {\r
445 //                              return new Double(input);\r
446 //                      } catch (Exception e) {\r
447 //                              APIException ae = new APIException("Cannot create a \"Double\" from [" + input + ']');\r
448 //                              ae.initCause(e);\r
449 //                              throw ae;\r
450 //                      }\r
451 //              }\r
452 //\r
453 //              /* (non-Javadoc)\r
454 //               * @see com.att.env.Objectifier#newObject()\r
455 //               */\r
456 //              public Double newInstance() throws APIException {\r
457 //                      return 0.0;\r
458 //              }\r
459 //      }\r
460 \r
461 }\r