1 /*
2 * Copyright 2007-2008 Naoki NOSE.
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, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package jp.liq.container.vocabulary;
17
18 import java.lang.reflect.Modifier;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import jp.liq.container.Component;
23 import jp.liq.container.Container;
24 import jp.liq.container.Injector;
25 import jp.liq.container.Module;
26 import jp.liq.container.ContainerVocabulary;
27 import jp.liq.container.component.ConstructorComponent;
28
29 public class SentinelConfig {
30 public SentinelModule configure(Container container) {
31 SentinelModule module = new SentinelModule();
32 container.include(module);
33 return module;
34 }
35
36 /**
37 * コンストラクタを一つだけ持つ、任意の具象クラスに対応する{@link jp.liq.container.Component}のインスタンスを生成します。
38 * このクラスは通常、一番最後に{@link jp.liq.container.Container} に追加されます。
39 * @author nosen
40 */
41 public class SentinelModule extends Module implements ContainerVocabulary {
42 private List<Injector> injectors;
43
44 /**
45 * このクラスのインスタンスを構築します。
46 */
47 public SentinelModule() {
48 this.injectors = new ArrayList<Injector>();
49 }
50
51 /**
52 * このモジュールが生成する{@link jp.liq.container.Component}
53 * に設定される Injector を追加します。
54 * @param injector
55 */
56 public SentinelModule with(Injector injector) {
57 injectors.add(injector);
58 return this;
59 }
60
61 /**
62 * @see jp.liq.container.Module#createComponent(java.lang.Class)
63 */
64 protected <T> Component<T> createComponent(Class<T> type) {
65 if(Modifier.isAbstract(type.getModifiers()) || type.getConstructors().length != 1) {
66 return null;
67 }
68 ConstructorComponent<T> c = component.from(ctor.of(type));
69 for(Injector i : injectors) {
70 c.with(i);
71 }
72 return c;
73 }
74 }
75 }