View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006, 2007 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   package it.imolinfo.jbi4ejb.webservice.generator.bcm;
9   
10  import it.imolinfo.jbi4ejb.Logger;
11  import it.imolinfo.jbi4ejb.LoggerFactory;
12  import it.imolinfo.jbi4ejb.jbi.Messages;
13  import it.imolinfo.jbi4ejb.webservice.generator.DynamicEJBWSDLGenerator;
14  
15  import java.lang.reflect.Method;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import org.objectweb.asm.ClassAdapter;
20  import org.objectweb.asm.ClassVisitor;
21  import org.objectweb.asm.MethodVisitor;
22  
23  
24  /**
25   * Adds the correct managed exception to the remote interface.
26   * 
27   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
28   */
29  public class RemoteInterfaceExceptionAdapter extends ClassAdapter {
30  
31      /** The logger. */
32      private static final Logger LOG
33      = LoggerFactory.getLogger(RemoteInterfaceExceptionAdapter.class); 
34      private static final Messages MESSAGES
35      = Messages.getMessages(RemoteInterfaceExceptionAdapter.class);
36  
37      /** The class loader. */
38      private final ClassLoader classLoader;
39  
40      /** the exceptions added to the interface. */
41      private final List<String> exceptionsAdded = new ArrayList<String>();
42  
43      /**
44       * Constructor.
45       * 
46       * @param arg0
47       *            The <code>ClassVisitor</code>
48       * @param classLoader
49       *            The classLoader
50       */
51      public RemoteInterfaceExceptionAdapter(ClassVisitor arg0, ClassLoader classLoader) {
52          super(arg0);    
53          this.classLoader = classLoader;
54      }
55  
56      
57      /**
58       * Look for the Exception wrapped by the
59       * <code>org.codehaus.xfire.fault.FaultInfoException</code> classes
60       * generated by WSDL.
61       * 
62       * @param access
63       *            The access modifier
64       * @param name
65       *            The method name
66       * @param desc
67       *            The description
68       * @param signature
69       *            The method signature
70       * @param exceptions
71       *            The throwed exceptions
72       * 
73       * @return
74       *          The MethodVisitor
75       * @see org.objectweb.asm.ClassAdapter#visitMethod(int, java.lang.String,
76       *      java.lang.String, java.lang.String, java.lang.String[])
77       */
78      @SuppressWarnings("unchecked")
79      public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
80  
81  
82          List<String> newExceptions = new ArrayList<String>();
83          
84          if (exceptions != null) {
85              for (int i = 0; i < exceptions.length; i++) {
86  
87                  String exception = exceptions[i];
88                  String exceptionName = exception.replace('/', '.');
89                  LOG.debug("Exception found in method " + name + ": "+ exceptionName);
90  
91                  Class myExceptionClass = null;
92                  try {
93                      myExceptionClass = classLoader.loadClass(exceptionName);
94                  } catch (ClassNotFoundException e) {
95                  	LOG.warn("EJB001101_Exception_in_rethrowing_exception", new Object[]{e.getMessage()});
96                  }
97  
98                  if (myExceptionClass != null) {              
99                      // Tests if the exception class is a FaultInfoException
100                     if (myExceptionClass.getGenericSuperclass().equals(org.codehaus.xfire.fault.FaultInfoException.class)) {
101                         LOG.debug("The exception: " + exceptionName + " is a FaultInfoException");
102                     }              
103                     Method getFaultInfoMethod = null;
104                     try {
105                         // Gets the getFaultInfom method
106                         getFaultInfoMethod = myExceptionClass.getMethod("getFaultInfo", new Class[]{});
107                     } catch (SecurityException e) {
108                         LOG.warn("EJB001101_Exception_in_rethrowing_exception", new Object[]{e.getMessage()});
109                     } catch (NoSuchMethodException e) {
110                         LOG.warn("EJB001101_Exception_in_rethrowing_exception", new Object[]{e.getMessage()});
111                     }
112                     if (getFaultInfoMethod != null) {
113                         Class returnType = getFaultInfoMethod.getReturnType();                       
114                         String exceptionInternalName = returnType.getName().replace('.', '/');
115                         // LOG.debug("Find the return type (internal name): " + exceptionInternalName);
116                         newExceptions.add(exceptionInternalName);
117                         // Saves all the exceptions names
118                         if (!exceptionsAdded.contains(returnType.getName())) {
119                             exceptionsAdded.add(returnType.getName());
120                         }
121                     }                           
122                 }          
123             }
124         }
125 
126         String[] newExceptionsArray = newExceptions.toArray(new String[0]);
127         for (int i = 0; i < newExceptionsArray.length; i++) {
128             LOG.debug("modifiying the method " + name + " with the exception: " + newExceptionsArray[i]);    
129         }           
130 
131         return super.visitMethod(access, name, desc, signature, newExceptionsArray);
132     }
133 
134 
135     /**
136      * Gets the exceptions added to the interface.
137      * 
138      * @return the exceptions added
139      */
140     public List<String> getExceptionsAdded() {
141         return exceptionsAdded;
142     }
143 
144 
145 }
146 
147