View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.ibean;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.endpoint.EndpointURI;
11  import org.mule.api.endpoint.MalformedEndpointException;
12  import org.mule.endpoint.ResourceNameEndpointURIBuilder;
13  import org.mule.module.ibeans.config.IBeanHolder;
14  import org.mule.module.ibeans.i18n.IBeansMessages;
15  
16  import java.lang.reflect.Method;
17  import java.net.URI;
18  import java.util.Properties;
19  
20  import org.ibeans.annotation.Call;
21  import org.ibeans.annotation.Template;
22  
23  /**
24   * A Resource name endpoint builder that will check the validity of an iBean endpoint by looking up the
25   * iBean and checking the method exists on the iBean and pointing to a valid method (i.e. with a {@link org.ibeans.annotation.Call} or {@link org.ibeans.annotation.Template} annotation)
26   */
27  public class IBeansEndpointURIBuilder extends ResourceNameEndpointURIBuilder
28  {
29      private MuleContext muleContext;
30  
31      @Override
32      protected void setEndpoint(URI uri, Properties props) throws MalformedEndpointException
33      {
34          super.setEndpoint(uri, props);
35          //Validate the iBean name and method
36          int i = address.indexOf(".");
37          if(i ==-1)
38          {
39              throw new MalformedEndpointException(uri.toString());
40          }
41  
42          String ibean = address.substring(0, i);
43          String method = address. substring(i+1);
44          IBeanHolder holder = muleContext.getRegistry().lookupObject(ibean);
45          if(holder == null)
46          {
47              throw new MalformedEndpointException(IBeansMessages.ibeanNotRegistered(ibean), uri.toString());
48          }
49          boolean match = false;
50          Method[] methods = holder.getIbeanClass().getMethods();
51          for (int j = 0; j < methods.length; j++)
52          {
53              Method m = methods[j];
54              if(m.getName().equals(method))
55              {
56                  if(m.isAnnotationPresent(Call.class) || m.isAnnotationPresent(Template.class))
57                  {
58                      match = true;
59                      break;
60                  }
61                  else
62                  {
63                      throw new MalformedEndpointException(IBeansMessages.ibeanMethodFoundButNotValid(ibean, method), uri.toString());
64                  }
65              }
66          }
67          if(!match)
68          {
69              throw new MalformedEndpointException(IBeansMessages.ibeanMethodNotFound(ibean, method), uri.toString());
70          }
71      }
72  
73      @Override
74      public EndpointURI build(URI uri, MuleContext muleContext) throws MalformedEndpointException
75      {
76          this.muleContext = muleContext;
77          return super.build(uri, muleContext);
78      }
79  }