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