1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.seasar.remoting.axis.deployer;
47
48 import javax.servlet.ServletContext;
49
50 import org.apache.axis.AxisEngine;
51 import org.apache.axis.WSDDEngineConfiguration;
52 import org.apache.axis.client.Service;
53 import org.apache.axis.deployment.wsdd.WSDDDeployment;
54 import org.seasar.framework.container.ComponentDef;
55 import org.seasar.framework.container.MetaDef;
56 import org.seasar.framework.container.S2Container;
57 import org.seasar.framework.message.MessageFormatter;
58 import org.seasar.remoting.axis.DeployFailedException;
59 import org.seasar.remoting.axis.S2AxisConstants;
60
61 /***
62 * diconファイル中に記述されたコンポーネントをAxisにデプロイします。
63 *
64 * @author koichik
65 */
66 public class Deployer {
67
68 protected S2Container container;
69 protected ServletContext servletContext;
70
71 protected ItemDeployer serviceDeployer = new ServiceDeployer(this);
72 protected ItemDeployer handlerDeployer = new HandlerDeployer(this);
73 protected ItemDeployer wsddDeployer = new WSDDDeployer(this);
74
75 /***
76 * S2コンテナを設定します。
77 *
78 * @param container
79 * S2コンテナ
80 */
81 public void setContainer(final S2Container container) {
82 this.container = container;
83 }
84
85 /***
86 * サーブレットコンテキストを設定します。
87 *
88 * @param servletContext
89 * サーブレットコンテキスト
90 */
91 public void setServletContext(final ServletContext servletContext) {
92 this.servletContext = servletContext;
93 }
94
95 /***
96 * コンテナに登録されているサービスやハンドラをデプロイします。
97 */
98 public void deploy() {
99 forEach(container.getRoot());
100 }
101
102 /***
103 * コンテナの階層をたどって全てのコンテナとコンポーネント定義を走査します。 <br>
104 * 走査する順序は次の通りです。
105 * <ol>
106 * <li>コンテナ自身</li>
107 * <li>子のコンポーネント定義</li>
108 * <li>子のコンテナを再起的に</li>
109 * </ol>
110 *
111 * @param container
112 * 起点となるコンテナ
113 */
114 protected void forEach(final S2Container container) {
115 process(container);
116
117 final int componentDefSize = container.getComponentDefSize();
118 for (int i = 0; i < componentDefSize; ++i) {
119 process(container.getComponentDef(i));
120 }
121
122 final int childContainerSize = container.getChildSize();
123 for (int i = 0; i < childContainerSize; ++i) {
124 forEach(container.getChild(i));
125 }
126 }
127
128 /***
129 * S2コンテナにS2Axisのメタデータ <code><meta name="s2axis:deploy"></code>
130 * が指定されていれば、そのWSDDをAxisにデプロイします。
131 *
132 * @param container
133 * S2コンテナ
134 */
135 protected void process(final S2Container container) {
136 final MetaDef[] metaDefs = container.getMetaDefs(S2AxisConstants.META_S2AXIS_DEPLOY);
137 for (int i = 0; metaDefs != null && i < metaDefs.length; ++i) {
138 wsddDeployer.deploy(null, metaDefs[i]);
139 }
140 }
141
142 /***
143 * コンポーネント定義にS2Axisのメタデータ <code><meta name="s2axis:service"></code>
144 * または <code><meta name="s2axis:handler"></code>
145 * が指定されていれば、そのコンポーネントをサービスまたはハンドラとしてAxisにデプロイします。
146 *
147 * @param componentDef
148 * コンポーネント定義
149 */
150 protected void process(final ComponentDef componentDef) {
151 final MetaDef serviceMetaDef = componentDef.getMetaDef(S2AxisConstants.META_S2AXIS_SERVICE);
152 if (serviceMetaDef != null) {
153 serviceDeployer.deploy(componentDef, serviceMetaDef);
154 }
155
156 final MetaDef handlerMetaDef = componentDef.getMetaDef(S2AxisConstants.META_S2AXIS_HANDLER);
157 if (handlerMetaDef != null) {
158 handlerDeployer.deploy(componentDef, handlerMetaDef);
159 }
160 }
161
162 /***
163 * WSDDデプロイメントを返します。
164 *
165 * @param container
166 * コンテナ
167 * @return WSDDデプロイメント
168 */
169 protected WSDDDeployment getDeployment(final S2Container container) {
170 return ((WSDDEngineConfiguration) getEngine(container).getConfig()).getDeployment();
171 }
172
173 /***
174 * Axisエンジンを返します。 <br>
175 * Axisエンジンは、コンテナに名前 <code>s2-axis:engine</code> を持つ
176 * <code><meta></code> 要素が指定されていれば、その内容文字列から次のように決定されます。
177 * <dl>
178 * <dt>未定義の場合</dt>
179 * <dd><code>"default"</code> が指定されたものとしてエンジンを決定します。</dd>
180 * <dt><code>"default"</code></dt>
181 * <dd>コンテナに <code>javax.servlet.ServletContext</code> が設定されていれば
182 * <code>"default-server"</code> 、そうでなければ <code>"default-client"</code>
183 * が指定されたものとしてエンジンを決定します。</dd>
184 * <dt><code>"default-client"</code></dt>
185 * <dd>コンテナから <code>javax.xml.rpc.Service</code>
186 * を実装したコンポーネントを取得し、そのエンジンを使用します。</dd>
187 * <dt><code>"default-server"</code></dt>
188 * <dd><code>javax.servlet.ServletContext</code> に設定されているエンジンを使用します。
189 * </dd>
190 * <dt>その他</dt>
191 * <dd>内容文字列を名前として持つコンポーネントをエンジンとして使用します。</dd>
192 * </dl>
193 *
194 * @param container
195 * コンテナ
196 * @return Axisエンジン
197 */
198 protected AxisEngine getEngine(final S2Container container) {
199 String engineName = S2AxisConstants.ENGINE_DEFAULT;
200
201 final MetaDef metadata = container.getMetaDef(S2AxisConstants.META_S2AXIS_ENGINE);
202 if (metadata != null) {
203 engineName = (String) metadata.getValue();
204 }
205
206 if (S2AxisConstants.ENGINE_DEFAULT.equals(engineName)) {
207 if (servletContext == null) {
208 engineName = S2AxisConstants.ENGINE_DEFAULT_CLIENT;
209 }
210 else {
211 engineName = S2AxisConstants.ENGINE_DEFAULT_SERVER;
212 }
213 }
214
215 AxisEngine engine = null;
216 if (S2AxisConstants.ENGINE_DEFAULT_CLIENT.equals(engineName)) {
217 final Service service = (Service) container.getComponent(javax.xml.rpc.Service.class);
218 engine = service.getEngine();
219 }
220 else if (S2AxisConstants.ENGINE_DEFAULT_SERVER.equals(engineName)) {
221 engine = (AxisEngine) servletContext.getAttribute(S2AxisConstants.ATTR_AXIS_ENGINE);
222 }
223 else {
224 engine = (AxisEngine) container.getComponent(engineName);
225 }
226
227 if (engine == null) {
228 throw new DeployFailedException(MessageFormatter.getSimpleMessage("EAXS0007", null));
229 }
230 return engine;
231 }
232 }