1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.jersey; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.component.JavaComponent; |
11 | |
import org.mule.api.object.ObjectFactory; |
12 | |
import org.mule.component.BindingUtils; |
13 | |
|
14 | |
import com.sun.jersey.core.spi.component.ComponentContext; |
15 | |
import com.sun.jersey.core.spi.component.ioc.IoCComponentProvider; |
16 | |
import com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory; |
17 | |
import com.sun.jersey.core.spi.component.ioc.IoCInstantiatedComponentProvider; |
18 | |
|
19 | |
import java.util.List; |
20 | |
|
21 | 0 | public class MuleComponentProviderFactory implements IoCComponentProviderFactory |
22 | |
{ |
23 | |
|
24 | |
private final List<JavaComponent> components; |
25 | |
private final MuleContext muleContext; |
26 | |
|
27 | |
public MuleComponentProviderFactory(MuleContext muleContext, List<JavaComponent> components) |
28 | 0 | { |
29 | 0 | this.muleContext = muleContext; |
30 | 0 | this.components = components; |
31 | 0 | } |
32 | |
|
33 | |
public IoCComponentProvider getComponentProvider(Class<?> cls) |
34 | |
{ |
35 | 0 | for (JavaComponent c : components) |
36 | |
{ |
37 | 0 | if (c.getObjectType().isAssignableFrom(cls)) |
38 | |
{ |
39 | 0 | return getComponentProvider(null, cls); |
40 | |
} |
41 | |
} |
42 | 0 | return null; |
43 | |
} |
44 | |
|
45 | |
public IoCComponentProvider getComponentProvider(ComponentContext ctx, final Class<?> cls) |
46 | |
{ |
47 | 0 | final JavaComponent selected = getSelectedComponent(cls); |
48 | |
|
49 | 0 | if (selected == null) |
50 | |
{ |
51 | 0 | return null; |
52 | |
} |
53 | |
|
54 | 0 | return new IoCInstantiatedComponentProvider() |
55 | 0 | { |
56 | |
public Object getInjectableInstance(Object o) |
57 | |
{ |
58 | 0 | return o; |
59 | |
} |
60 | |
|
61 | |
public Object getInstance() |
62 | |
{ |
63 | |
try |
64 | |
{ |
65 | 0 | ObjectFactory objectFactory = selected.getObjectFactory(); |
66 | 0 | Object instance = objectFactory.getInstance(muleContext); |
67 | 0 | BindingUtils.configureBinding(selected, instance); |
68 | |
|
69 | 0 | return instance; |
70 | |
} |
71 | 0 | catch (Exception e) |
72 | |
{ |
73 | 0 | throw new RuntimeException(e); |
74 | |
} |
75 | |
} |
76 | |
}; |
77 | |
} |
78 | |
|
79 | |
private JavaComponent getSelectedComponent(final Class<?> cls) |
80 | |
{ |
81 | 0 | JavaComponent selected = null; |
82 | 0 | for (JavaComponent c : components) |
83 | |
{ |
84 | 0 | if (c.getObjectType().isAssignableFrom(cls)) |
85 | |
{ |
86 | 0 | selected = c; |
87 | |
} |
88 | |
} |
89 | 0 | return selected; |
90 | |
} |
91 | |
|
92 | |
} |