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