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.util;
12  
13  import com.legstar.coxb.impl.reflect.CComplexReflectBinding;
14  import com.legstar.coxb.util.BindingUtil;
15  import com.legstar.coxb.util.ClassLoadingException;
16  import com.legstar.coxb.util.ClassUtil;
17  import com.legstar.coxb.host.HostException;
18  
19  /**
20   * Utility methods used for JAXB object tree introspection.
21   * 
22   * @author Fady Moussallam
23   * 
24   * 
25   */
26  public final class CoxbRuntimeUtil {
27  
28      /**
29       * Private constructor to stop anyone from instantiating
30       * this class - the static methods should be used
31       * explicitly.
32       */
33      private CoxbRuntimeUtil() {
34  
35      }
36  
37      /**
38       * This function returns a byte length for a complex object as a String.
39       * 
40       * @param jaxbPackage the java package name from which an ObjectFactory
41       *            can be instanciated
42       * @param jaxbTypeName the JAXB type name of the object for which byte
43       *            length must be returned
44       * @return the byte length as a string
45       * @throws HostException if byte length calculation failed
46       */
47      public static String byteLength(
48              final String jaxbPackage,
49              final String jaxbTypeName)
50              throws HostException {
51  
52          try {
53              /* Load the JAXB object factory from the package */
54              Object objectFactory = ClassUtil.newObject(jaxbPackage,
55                      "ObjectFactory");
56  
57              /* Get an instance of the requested JAXB object */
58              Object jaxbObject = BindingUtil.newJaxbObject(objectFactory,
59                      jaxbTypeName);
60  
61              /* Create a complex cobol element representing this jaxb object */
62              CComplexReflectBinding ce =
63                      new CComplexReflectBinding(objectFactory, jaxbObject);
64              return (Integer.toString(ce.calcByteLength()));
65          } catch (ClassLoadingException e) {
66              throw (new HostException(e));
67          }
68      }
69  
70  }