1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.util; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.seasar.framework.container.ComponentDef; |
22 | |
import org.seasar.framework.container.S2Container; |
23 | |
import org.seasar.framework.container.factory.SingletonS2ContainerFactory; |
24 | |
import org.seasar.framework.log.Logger; |
25 | |
|
26 | |
public class DIContainerUtil { |
27 | |
|
28 | 1 | private static final Logger logger_ = Logger |
29 | 1 | .getLogger(DIContainerUtil.class); |
30 | |
|
31 | 0 | private DIContainerUtil() { |
32 | 0 | } |
33 | |
|
34 | |
public static Object getComponentNoException(Object componentKey) { |
35 | 64 | S2Container container = getContainer(); |
36 | 64 | if (container == null) { |
37 | 0 | return null; |
38 | |
} |
39 | 64 | if (container.hasComponentDef(componentKey)) { |
40 | 28 | return container.getComponent(componentKey); |
41 | |
} |
42 | 36 | return null; |
43 | |
} |
44 | |
|
45 | |
public static ComponentDef getComponentDefNoException(Object componentKey) { |
46 | 0 | if (getContainer().hasComponentDef(componentKey)) { |
47 | 0 | return getContainer().getComponentDef(componentKey); |
48 | |
} |
49 | 0 | return null; |
50 | |
} |
51 | |
|
52 | |
public static Object getComponent(Object componentKey) { |
53 | 11 | return getContainer().getComponent(componentKey); |
54 | |
} |
55 | |
|
56 | |
public static synchronized void register(ComponentDef componentDef) { |
57 | 0 | if (logger_.isDebugEnabled()) { |
58 | 0 | logger_.debug("componentDef name = " |
59 | |
+ componentDef.getComponentName()); |
60 | 0 | logger_.debug("componentDef class = " |
61 | |
+ componentDef.getComponentClass()); |
62 | |
} |
63 | 0 | getContainer().register(componentDef); |
64 | 0 | } |
65 | |
|
66 | |
public static Object[] getComponentKeys(Class clazz) { |
67 | 2 | ComponentDef[] componentDefs = getContainer().findComponentDefs(clazz); |
68 | 2 | List list = new ArrayList(); |
69 | 8 | for (int i = 0; i < componentDefs.length; i++) { |
70 | 6 | ComponentDef componentDef = componentDefs[i]; |
71 | 6 | list.add(componentDef.getComponentName()); |
72 | |
} |
73 | 2 | return list.toArray(); |
74 | |
} |
75 | |
|
76 | |
public static boolean hasComponent(Object componentKey) { |
77 | 106 | return getContainer().hasComponentDef(componentKey); |
78 | |
} |
79 | |
|
80 | |
public static boolean hasContainer() { |
81 | 108 | return SingletonS2ContainerFactory.hasContainer(); |
82 | |
} |
83 | |
|
84 | 1 | private static final Object[] EMPTY = new Object[0]; |
85 | |
|
86 | |
public static Object[] findAllComponents(Object componentKey) { |
87 | 0 | if (!hasContainer()) { |
88 | 0 | return EMPTY; |
89 | |
} |
90 | 0 | return getContainer().findAllComponents(componentKey); |
91 | |
} |
92 | |
|
93 | |
public static S2Container getContainer() { |
94 | 183 | return SingletonS2ContainerFactory.getContainer(); |
95 | |
} |
96 | |
|
97 | |
} |