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.DefaultMuleException;
10  import org.mule.api.MuleContext;
11  import org.mule.api.MuleException;
12  import org.mule.api.endpoint.EndpointURI;
13  import org.mule.api.lifecycle.InitialisationException;
14  import org.mule.module.ibeans.config.IBeanHolder;
15  import org.mule.module.ibeans.spi.MuleIBeansPlugin;
16  import org.mule.transport.AbstractConnector;
17  import org.mule.util.ClassUtils;
18  
19  import java.lang.reflect.Method;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.ibeans.annotation.State;
24  
25  /**
26   * <code>IBeansConnector</code> TODO document
27   */
28  public class IBeansConnector extends AbstractConnector
29  {
30      public static final String STATE_PARAMS_PROPERTY = "ibean.state.params";
31      public static final String CALL_PARAMS_PROPERTY = "ibean.call.params";
32  
33      private MuleIBeansPlugin iBeansPlugin;
34  
35      /* This constant defines the main transport protocol identifier */
36      public static final String PROTOCOL = "ibean";
37  
38      public IBeansConnector(MuleContext context)
39      {
40          super(context);
41          this.iBeansPlugin = new MuleIBeansPlugin(context);
42  
43      }
44         
45      @Override
46      public void doInitialise() throws InitialisationException
47      {
48          //nothing to do
49      }
50  
51      @Override
52      public void doConnect() throws Exception
53      {
54          //nothing to do
55      }
56  
57      @Override
58      public void doDisconnect() throws Exception
59      {
60          //nothing to do
61      }
62  
63      @Override
64      public void doStart() throws MuleException
65      {
66          //nothing to do
67      }
68  
69      @Override
70      public void doStop() throws MuleException
71      {
72          //nothing to do
73      }
74  
75      @Override
76      public void doDispose()
77      {
78          //nothing to do
79      }
80  
81      public String getProtocol()
82      {
83          return PROTOCOL;
84      }
85  
86      public MuleIBeansPlugin getiBeansPlugin()
87      {
88          return iBeansPlugin;
89      }
90  
91      public void setiBeansPlugin(MuleIBeansPlugin iBeansPlugin)
92      {
93          this.iBeansPlugin = iBeansPlugin;
94      }
95  
96      Object createIbean(EndpointURI uri, List<?> state) throws MuleException
97      {
98          try {
99              Object ibean;
100             String address = uri.getAddress();
101             int i = address.indexOf(".");
102             String ibeanName = address.substring(0, i);
103             IBeanHolder holder = getMuleContext().getRegistry().lookupObject(ibeanName);
104             if(holder==null)
105             {
106                 throw new IllegalArgumentException();
107             }
108             ibean = holder.create(getMuleContext(), getiBeansPlugin());
109 
110             if(state.size() > 0)
111             {
112                 Class[] types = new Class[state.size()];
113                 Object[] params = new Object[state.size()];
114                 int x = 0;
115                 for (Object o : state)
116                 {
117                     types[x] = o.getClass();
118                     params[x++] = o;
119                 }
120 
121                 List<Method> methods = ClassUtils.getSatisfiableMethods(holder.getIbeanClass(), types,
122                     true, false, Collections.<String>emptyList(), null);
123                 if(methods.size()==0)
124                 {
125                     throw new IllegalArgumentException("no matching methods");
126                 }
127                 else if(methods.size()==1)
128                 {
129                     if(methods.get(0).isAnnotationPresent(State.class))
130                     {
131                         methods.get(0).invoke(ibean, params);
132                     }
133                 }
134                 else
135                 {
136                     boolean match = false;
137                     for (Method method1 : methods)
138                     {
139                         if(method1.isAnnotationPresent(State.class))
140                         {
141                             method1.invoke(ibean, params);
142                             match = true;
143                             break;
144                         }
145                     }
146                     if(!match)
147                     {
148                         throw new IllegalArgumentException("no matching @State method");
149                     }
150                 }
151             }
152             return ibean;
153         }
154         catch (Exception e)
155         {
156             throw new DefaultMuleException(e);
157         }
158     }
159 }