| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| S2ContainerUtil |
|
| 0.0;0 |
| 1 | /* | |
| 2 | * Copyright 2004-2008 the Seasar Foundation and the Others. | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
| 13 | * either express or implied. See the License for the specific language | |
| 14 | * governing permissions and limitations under the License. | |
| 15 | */ | |
| 16 | package org.seasar.uruma.util; | |
| 17 | ||
| 18 | import org.seasar.framework.beans.BeanDesc; | |
| 19 | import org.seasar.framework.beans.PropertyDesc; | |
| 20 | import org.seasar.framework.beans.factory.BeanDescFactory; | |
| 21 | import org.seasar.framework.container.ComponentDef; | |
| 22 | import org.seasar.framework.container.S2Container; | |
| 23 | import org.seasar.framework.container.factory.SingletonS2ContainerFactory; | |
| 24 | ||
| 25 | /** | |
| 26 | * {@link S2Container} を利用するためのユーティリティクラスです。<br /> | |
| 27 | * | |
| 28 | * @author y-komori | |
| 29 | */ | |
| 30 | public class S2ContainerUtil { | |
| 31 | 0 | private S2ContainerUtil() { |
| 32 | ||
| 33 | 0 | } |
| 34 | ||
| 35 | /** | |
| 36 | * 指定された名前に対応するコンポーネントを {@link S2Container} から取得して返します。<br /> | |
| 37 | * <p> | |
| 38 | * コンポーネントが存在しない場合は例外をスローせず、<code>null</code> を返します。 | |
| 39 | * </p> | |
| 40 | * | |
| 41 | * @param componentName | |
| 42 | * コンポーネント名称 | |
| 43 | * @return コンポーネントオブジェクト | |
| 44 | */ | |
| 45 | public static Object getComponentNoException(final String componentName) { | |
| 46 | 0 | S2Container container = SingletonS2ContainerFactory.getContainer(); |
| 47 | 0 | return getComponentNoException(componentName, container); |
| 48 | } | |
| 49 | ||
| 50 | /** | |
| 51 | * 指定された名前に対応するコンポーネントを {@link S2Container} から取得して返します。<br /> | |
| 52 | * <p> | |
| 53 | * コンポーネントが存在しない場合は例外をスローせず、<code>null</code> を返します。 | |
| 54 | * </p> | |
| 55 | * | |
| 56 | * @param componentName | |
| 57 | * コンポーネント名称 | |
| 58 | * @param container | |
| 59 | * 検索対象の {@link S2Container} | |
| 60 | * @return コンポーネントオブジェクト | |
| 61 | */ | |
| 62 | public static Object getComponentNoException(final String componentName, | |
| 63 | final S2Container container) { | |
| 64 | 232 | if (container.hasComponentDef(componentName)) { |
| 65 | 4 | return container.getComponentDef(componentName).getComponent(); |
| 66 | } else { | |
| 67 | 228 | return null; |
| 68 | } | |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * 指定されたクラスに対応するコンポーネントを {@link S2Container} から取得して返します。<br /> | |
| 73 | * コンポーネントが存在しない場合は例外をスローせず、 <code>null</code> を返します。 | |
| 74 | * | |
| 75 | * @param componentClass | |
| 76 | * コンポーネントのクラス | |
| 77 | * @param container | |
| 78 | * {@link S2Container} | |
| 79 | * @return コンポーネントオブジェクト | |
| 80 | */ | |
| 81 | public static Object getComponentNoException(final Class<?> componentClass, | |
| 82 | final S2Container container) { | |
| 83 | 660 | if (container.hasComponentDef(componentClass)) { |
| 84 | 128 | return container.getComponent(componentClass); |
| 85 | } else { | |
| 86 | 532 | return null; |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * 指定されたクラスに対応するコンポーネントを {@link S2Container} から取得して返します。<br /> | |
| 92 | * コンポーネントが存在しない場合は例外をスローせず、 <code>null</code> を返します。 | |
| 93 | * | |
| 94 | * @param componentClass | |
| 95 | * コンポーネントのクラス | |
| 96 | * @return コンポーネントオブジェクト | |
| 97 | */ | |
| 98 | public static Object getComponentNoException(final Class<?> componentClass) { | |
| 99 | 660 | return getComponentNoException(componentClass, |
| 100 | SingletonS2ContainerFactory.getContainer()); | |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * 指定された名前に対応するコンポーネントを {@link S2Container} から取得して返します。<br /> | |
| 105 | * コンポーネントが存在しない場合は例外をスローせず、 <code>null</code> を返します。 | |
| 106 | * | |
| 107 | * @param componentName | |
| 108 | * コンポーネント名称 | |
| 109 | * @return コンポーネントオブジェクト | |
| 110 | */ | |
| 111 | public static ComponentDef getComponentDefNoException( | |
| 112 | final String componentName) { | |
| 113 | 112 | S2Container container = SingletonS2ContainerFactory.getContainer(); |
| 114 | 112 | if (container.hasComponentDef(componentName)) { |
| 115 | 112 | return container.getComponentDef(componentName); |
| 116 | } else { | |
| 117 | 0 | return null; |
| 118 | } | |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * 指定されたクラスに対応するコンポーネントを {@link S2Container} から取得して返します。<br /> | |
| 123 | * コンポーネントが存在しない場合は例外をスローせず、 <code>null</code> を返します。 | |
| 124 | * | |
| 125 | * @param componentClass | |
| 126 | * コンポーネントクラス | |
| 127 | * @return コンポーネントオブジェクト | |
| 128 | */ | |
| 129 | public static ComponentDef getComponentNoExceptionDef( | |
| 130 | final Class<?> componentClass) { | |
| 131 | 0 | S2Container container = SingletonS2ContainerFactory.getContainer(); |
| 132 | 0 | if (container.hasComponentDef(componentClass)) { |
| 133 | 0 | return container.getComponentDef(componentClass); |
| 134 | } else { | |
| 135 | 0 | return null; |
| 136 | } | |
| 137 | } | |
| 138 | ||
| 139 | /** | |
| 140 | * 指定された名前に対応するコンポーネントを {@link S2Container} から取得して返します。<br /> | |
| 141 | * | |
| 142 | * @param componentName | |
| 143 | * コンポーネント名称 | |
| 144 | * @return コンポーネントオブジェクト | |
| 145 | */ | |
| 146 | public static ComponentDef getComponentDef(final String componentName) { | |
| 147 | 0 | S2Container container = SingletonS2ContainerFactory.getContainer(); |
| 148 | 0 | return container.getComponentDef(componentName); |
| 149 | } | |
| 150 | ||
| 151 | /** | |
| 152 | * 指定されたクラスに対応するコンポーネントを {@link S2Container} から取得して返します。<br /> | |
| 153 | * | |
| 154 | * @param componentClass | |
| 155 | * コンポーネントクラス | |
| 156 | * @return コンポーネントオブジェクト | |
| 157 | */ | |
| 158 | public static ComponentDef getComponentDef(final Class<?> componentClass) { | |
| 159 | 0 | S2Container container = SingletonS2ContainerFactory.getContainer(); |
| 160 | 0 | return container.getComponentDef(componentClass); |
| 161 | } | |
| 162 | ||
| 163 | /** | |
| 164 | * 指定されたクラスに対応するコンポーネントを {@link S2Container} から取得して返します。<br /> | |
| 165 | * | |
| 166 | * @param componentClass | |
| 167 | * コンポーネントクラス | |
| 168 | * @return コンポーネントオブジェクト | |
| 169 | */ | |
| 170 | public static Object getComponent(final Class<?> componentClass) { | |
| 171 | 0 | return getComponent(componentClass, SingletonS2ContainerFactory |
| 172 | .getContainer()); | |
| 173 | } | |
| 174 | ||
| 175 | /** | |
| 176 | * 指定されたクラスに対応するコンポーネントを {@link S2Container} から取得して返します。<br /> | |
| 177 | * | |
| 178 | * @param componentClass | |
| 179 | * コンポーネントクラス | |
| 180 | * @param container | |
| 181 | * 検索対象の {@link S2Container} | |
| 182 | * @return コンポーネントオブジェクト | |
| 183 | */ | |
| 184 | public static Object getComponent(final Class<?> componentClass, | |
| 185 | final S2Container container) { | |
| 186 | 0 | return container.getComponent(componentClass); |
| 187 | } | |
| 188 | ||
| 189 | /** | |
| 190 | * 指定されたオブジェクトに対して {@link S2Container} からコンポーネントをインジェクションします。<br /> | |
| 191 | * <p> | |
| 192 | * <code>target</code> で指定されたオブジェクトに対して、<code>container</code> で指定された | |
| 193 | * {@link S2Container} からコンポーネントをインジェクションします。<br /> | |
| 194 | * </p> | |
| 195 | * | |
| 196 | * @param target | |
| 197 | * ターゲットオブジェクト | |
| 198 | * @param container | |
| 199 | * {@link S2Container} オブジェクト | |
| 200 | */ | |
| 201 | public static void injectDependency(final Object target, | |
| 202 | final S2Container container) { | |
| 203 | 112 | AssertionUtil.assertNotNull("target", target); |
| 204 | 112 | AssertionUtil.assertNotNull("container", container); |
| 205 | ||
| 206 | 112 | BeanDesc desc = BeanDescFactory.getBeanDesc(target.getClass()); |
| 207 | ||
| 208 | 112 | int pdSize = desc.getPropertyDescSize(); |
| 209 | 756 | for (int i = 0; i < pdSize; i++) { |
| 210 | 644 | PropertyDesc pd = desc.getPropertyDesc(i); |
| 211 | ||
| 212 | 644 | Class<?> clazz = pd.getPropertyType(); |
| 213 | 644 | Object component = getComponentNoException(clazz); |
| 214 | 644 | if (component != null && pd.isWritable()) { |
| 215 | 0 | pd.setValue(target, component); |
| 216 | } | |
| 217 | } | |
| 218 | 112 | } |
| 219 | } |