View Javadoc

1   package org.codehaus.ivory;
2   
3   import java.lang.reflect.Method;
4   import java.util.Collection;
5   import java.util.Iterator;
6   import java.util.List;
7   
8   import org.apache.axis.description.OperationDesc;
9   import org.apache.axis.description.ParameterDesc;
10  import org.apache.axis.description.JavaServiceDesc;
11  import org.apache.commons.attributes.Attributes;
12  import org.codehaus.ivory.attributes.NonWebMethod;
13  import org.codehaus.ivory.attributes.ParameterType;
14  
15  /***
16   * Adds meta-data capabilities to Axis's ServiceDesc class.
17   * 
18   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
19   * @since May 5, 2003
20   */
21  public class IvoryServiceDesc
22      extends JavaServiceDesc
23  {
24      private boolean loadingServiceDesc = false;
25      
26      /***
27       * Load the meta data for the class being exposed as a service and take
28       * apropriate action.
29       */
30      public void loadMetaData()
31      {      
32          for ( Iterator itr = getOperations().iterator(); itr.hasNext(); )
33          {
34              OperationDesc operation = ( OperationDesc ) itr.next();
35              
36              customizeOperation( operation );
37              
38              if ( !isValidOperation( operation ) ||
39                   hideOperation( operation ) )
40              {
41                  log.debug( "Removing method " + operation.getName() );
42                  itr.remove();
43              }
44          }
45      }
46  
47      /***
48       * Whether or an Operation on the services should not be exposed.
49       * 
50       * @param operation
51       * @return
52       */
53      protected boolean hideOperation(OperationDesc operation)
54      {
55          Method method = operation.getMethod();
56          
57          return Attributes.hasAttributeType( method, NonWebMethod.class );
58      }
59  
60      protected void customizeOperation( OperationDesc operation )
61      {
62          for( Iterator itr = operation.getParameters().iterator(); itr.hasNext(); )
63          {
64              ParameterDesc parameter = (ParameterDesc) itr.next();
65              
66              customizeParameter( operation, parameter ); 
67          }
68          
69          customizeParameter( operation, operation.getReturnParamDesc() );
70      }
71  
72      protected void customizeParameter( OperationDesc operation, 
73                                         ParameterDesc parameter )
74      {
75          Method method = operation.getMethod();
76          
77          String name = parameter.getName();
78          
79          if ( name == null &&
80               Attributes.hasReturnAttributeType( method, ParameterType.class ) )
81          {
82              Collection allAttrs = Attributes.getReturnAttributes(method);
83              
84              for ( Iterator itr = allAttrs.iterator(); itr.hasNext(); )
85              {
86                  Object attr = itr.next();
87                  if ( attr instanceof ParameterType )
88                  {
89                      changeParameterType( parameter, (ParameterType) attr );        
90                  }
91              }
92          }
93          else if (Attributes.hasReturnAttributeType( method, ParameterType.class ))
94          {
95              // parameters are named "in0", "in1" and so on.
96              // get everything after "in"
97              int num = new Integer(name.substring( 2 )).intValue();
98              Collection allAttrs = Attributes.getParameterAttributes(method, num);
99              
100             for ( Iterator itr = allAttrs.iterator(); itr.hasNext(); )
101             {
102                 Object attr = itr.next();
103                 if ( attr instanceof ParameterType )
104                 {
105                     changeParameterType( parameter, (ParameterType) attr );        
106                 }
107             }
108         }
109     }
110 
111     /***
112      * @param parameter
113      * @param type
114      */
115     private void changeParameterType(ParameterDesc parameter, ParameterType type)
116     {            
117         String clazz = type.getParameterType().getName();
118 
119         log.debug( "Changing parameter type to " + clazz );
120             
121         parameter.setJavaType( type.getParameterType() );
122     }
123 
124     protected boolean isValidOperation( OperationDesc operation )
125     {
126         for( Iterator itr = operation.getParameters().iterator(); itr.hasNext(); )
127         {
128             ParameterDesc parameter = (ParameterDesc) itr.next();
129             
130             if ( !isValidParameter( parameter ) )
131                 return false;   
132         }
133         
134         return isValidParameter( operation.getReturnParamDesc() );
135     }
136 
137     /***
138      * Checks whether or not Axis will work with this parameter.  The only
139      * criterion is that it not be a List or inherit from the List type.
140      * 
141      * @param parameter
142      * @return
143      */
144     protected boolean isValidParameter( ParameterDesc parameter )
145     {
146         if ( parameter.getJavaType().equals( java.util.List.class.getClass() )
147              || List.class.isAssignableFrom( parameter.getJavaType() ) )
148         {
149             return false;
150         }
151         
152         return true;
153     }
154 }