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