View Javadoc

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