Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
OValPlugin |
|
| 2.5;2.5 |
1 | /* | |
2 | * Copyright 2004-2009 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.cubby.plugins.oval; | |
17 | ||
18 | import javax.servlet.ServletContext; | |
19 | ||
20 | import net.sf.oval.Validator; | |
21 | import net.sf.oval.localization.context.OValContextRenderer; | |
22 | import net.sf.oval.localization.message.MessageResolver; | |
23 | import net.sf.oval.logging.LoggerFactorySLF4JImpl; | |
24 | ||
25 | import org.seasar.cubby.plugin.AbstractPlugin; | |
26 | import org.seasar.cubby.spi.ContainerProvider; | |
27 | import org.seasar.cubby.spi.ProviderFactory; | |
28 | import org.seasar.cubby.spi.container.Container; | |
29 | import org.seasar.cubby.spi.container.LookupException; | |
30 | ||
31 | /** | |
32 | * <a href="http://oval.sourceforge.net/">OVal</a> | |
33 | * によるアノテーションベースの入力検証を追加するプラグインです。 | |
34 | * | |
35 | * @author baba | |
36 | */ | |
37 | 0 | public class OValPlugin extends AbstractPlugin { |
38 | ||
39 | /** | |
40 | * {@inheritDoc} | |
41 | * <p> | |
42 | * {@link LoggerFactorySLF4JImpl} を引数に {link | |
43 | * {@link Validator#setLoggerFactory(net.sf.oval.logging.LoggerFactory)} | |
44 | * を実行して、ロガーに SLF4J を使用するように設定します。 | |
45 | * </p> | |
46 | */ | |
47 | @Override | |
48 | public void initialize(final ServletContext servletContext) | |
49 | throws Exception { | |
50 | 0 | Validator.setLoggerFactory(new LoggerFactorySLF4JImpl()); |
51 | 0 | } |
52 | ||
53 | /** | |
54 | * {@inheritDoc} | |
55 | */ | |
56 | @Override | |
57 | public void ready() { | |
58 | 0 | final Container container = ProviderFactory |
59 | .get(ContainerProvider.class).getContainer(); | |
60 | ||
61 | 0 | final MessageResolver messageResolver = buildMessageResolver(container); |
62 | 0 | if (messageResolver != null) { |
63 | 0 | Validator.setMessageResolver(messageResolver); |
64 | } | |
65 | ||
66 | 0 | final OValContextRenderer ovalContextRenderer = buildOValContextRenderer(container); |
67 | 0 | if (ovalContextRenderer != null) { |
68 | 0 | Validator.setContextRenderer(ovalContextRenderer); |
69 | } | |
70 | 0 | } |
71 | ||
72 | /** | |
73 | * {@link MessageResolver} を生成します。 | |
74 | * <p> | |
75 | * 指定されたコンテナから {@link MessageResolver} のインスタンスを取得しますが、コンテナに登録されていない場合は | |
76 | * <code>null</code> を返します。その場合は | |
77 | * {@link Validator#setMessageResolver(MessageResolver)} を実行しないので OVal | |
78 | * デフォルトの動作になります。 | |
79 | * </p> | |
80 | * | |
81 | * @param container | |
82 | * コンテナ | |
83 | * @return {@link MessageResolver} | |
84 | */ | |
85 | protected MessageResolver buildMessageResolver(final Container container) { | |
86 | try { | |
87 | 0 | return container.lookup(MessageResolver.class); |
88 | 0 | } catch (final LookupException e) { |
89 | 0 | return null; |
90 | } | |
91 | } | |
92 | ||
93 | /** | |
94 | * {@link OValContextRenderer} を生成します。 | |
95 | * <p> | |
96 | * 指定されたコンテナから {@link OValContextRenderer} のインスタンスを取得しますが、コンテナに登録されていない場合は | |
97 | * <code>null</code> を返します。その場合は | |
98 | * {@link Validator#setContextRenderer(OValContextRenderer)} を実行しないので OVal | |
99 | * デフォルトの動作になります。 | |
100 | * </p> | |
101 | * | |
102 | * @param container | |
103 | * コンテナ | |
104 | * @return {@link MessageResolver} | |
105 | */ | |
106 | protected OValContextRenderer buildOValContextRenderer( | |
107 | final Container container) { | |
108 | try { | |
109 | 0 | return container.lookup(OValContextRenderer.class); |
110 | 0 | } catch (final LookupException e) { |
111 | 0 | return null; |
112 | } | |
113 | } | |
114 | ||
115 | } |