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