View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2015 LegSem.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the GNU Lesser Public License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7    * 
8    * Contributors:
9    *     LegSem - initial API and implementation
10   ******************************************************************************/
11  package com.legstar.coxb.impl.reflect;
12  
13  import com.legstar.coxb.CobolBindingException;
14  import com.legstar.coxb.ICobolComplexBinding;
15  import com.legstar.coxb.transform.AbstractHostToJavaTransformer;
16  import com.legstar.coxb.util.JAXBAnnotationException;
17  import com.legstar.coxb.util.JAXBElementDescriptor;
18  
19  /**
20   * This implementation of a host to java transformer dynamically binds to
21   * a complex object using reflection.
22   * The complex object must be a JAXB type with COBOL annotations.
23   * 
24   */
25  public class ReflectHostToJavaTransformer extends AbstractHostToJavaTransformer {
26  
27      /**
28       * The JAXB element descriptor.
29       */
30      private JAXBElementDescriptor _jaxbElementDescriptor;
31  
32      /**
33       * Construct a transformer for a particular JAXB type.
34       * 
35       * @param jaxbPackageName the JAXB type package name
36       * @param jaxbType the JAXB type
37       * @throws ReflectBindingException if JAXB type has no annotations or cannot
38       *             be
39       *             located from the classpath
40       */
41      public ReflectHostToJavaTransformer(
42              final String jaxbPackageName,
43              final String jaxbType) throws ReflectBindingException {
44          try {
45              _jaxbElementDescriptor = new JAXBElementDescriptor(jaxbPackageName,
46                      jaxbType);
47          } catch (JAXBAnnotationException e) {
48              throw new ReflectBindingException(e);
49          }
50      }
51  
52      /** {@inheritDoc} */
53      public ICobolComplexBinding getBinding() throws CobolBindingException {
54          try {
55              CComplexReflectBinding ccem = new CComplexReflectBinding(
56                      getObjectFactory(), getJaxbClass());
57              return ccem;
58          } catch (ReflectBindingException e) {
59              throw new CobolBindingException(e);
60          }
61      }
62  
63      /**
64       * @return the JAXB type factory to use for the JAXB object type
65       */
66      public Object getObjectFactory() {
67          return _jaxbElementDescriptor.getObjectFactory();
68      }
69  
70      /**
71       * @return the JAXB type class
72       */
73      public Class < ? > getJaxbClass() {
74          return getJaxbElementDescriptor().getJaxbClass();
75      }
76  
77      /**
78       * @return the JAXB element descriptor
79       */
80      public JAXBElementDescriptor getJaxbElementDescriptor() {
81          return _jaxbElementDescriptor;
82      }
83  
84  }