View Javadoc

1   /*
2    *  Copyright (c) 2005, 2006 Imola Informatica.
3    *  All rights reserved. This program and the accompanying materials
4    *  are made available under the terms of the LGPL License v2.1
5    *  which accompanies this distribution, and is available at
6    *  http://www.gnu.org/licenses/lgpl.html
7    */
8   
9   
10  package it.imolinfo.jbi4cics.webservices.utils.generators;
11  
12  import static org.objectweb.asm.Opcodes.ACC_ABSTRACT;
13  import static org.objectweb.asm.Opcodes.ACC_INTERFACE;
14  import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
15  import static org.objectweb.asm.Opcodes.V1_1;
16  import it.imolinfo.jbi4cics.Logger;
17  import it.imolinfo.jbi4cics.LoggerFactory;
18  import it.imolinfo.jbi4cics.exception.ClassGenerationException;
19  import it.imolinfo.jbi4cics.jbi.BCELClassLoader;
20  import it.imolinfo.jbi4cics.webservices.descriptor.ServiceDescriptor;
21  import org.objectweb.asm.ClassWriter;
22  import org.objectweb.asm.MethodVisitor;
23  import org.objectweb.asm.Type;
24  
25  public final class ServiceInterfaceGenerator {
26  
27      /**
28       * The logger for this class and its instances.
29       */
30      private static final Logger LOG
31              = LoggerFactory.getLogger(ServiceInterfaceGenerator.class);
32  
33      /**
34       * The name of the java class that will be generated. This field contains
35       * values like <i>foo.bar.MyClass</i> and so on.
36       */
37      private final String completeClassName;
38  
39      /**
40       * The service descriptor.
41       */
42      private final ServiceDescriptor serviceDescriptor;
43  
44      public ServiceInterfaceGenerator(
45              final ServiceDescriptor serviceDescriptor) {
46          completeClassName = serviceDescriptor.getServiceInterfacePackageName()
47                              + "." + serviceDescriptor.getServiceInterfaceName();
48          this.serviceDescriptor = serviceDescriptor;
49  
50          if (LOG.isDebugEnabled()) {
51              LOG.debug("Complete interface name: " + completeClassName);
52          }
53      }
54  
55      public Class generateServiceInterface(final BCELClassLoader loader)
56              throws ClassGenerationException {
57  
58          // 'true' so ClassWriter calculate computeMaxs fields in the bytecode
59          ClassWriter cw = new ClassWriter(true);
60          String internalClassName = completeClassName.replace('.', '/');
61          String returnType
62                  = Type.getDescriptor(serviceDescriptor.getOutputBean());
63          String paramType = Type.getDescriptor(serviceDescriptor.getInputBean());
64          MethodVisitor mv;
65          Class clazz;
66  
67          cw.visit(V1_1, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE,
68                   internalClassName, null, "java/lang/Object", null);
69          mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT,
70                              serviceDescriptor.getOperationName(),
71                              "(" + paramType + ")" + returnType, null, null);
72          mv.visitEnd();
73          loader.addClass(completeClassName, cw.toByteArray());
74          try {
75              clazz = loader.loadClass(completeClassName);
76          } catch (ClassNotFoundException e) {
77              Object[] args = new Object[] { completeClassName };
78  
79              LOG.error("CIC002400_Class_generation_error", args, e);
80              throw new ClassGenerationException(
81                      "CIC002400_Class_generation_error", args, e);
82          }
83          serviceDescriptor.setServiceInterface(clazz);
84          return clazz;
85      }
86  }