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