View Javadoc

1   /*
2    *
3    * The Seasar Software License, Version 1.1
4    *
5    * Copyright (c) 2003-2004 The Seasar Project. All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or
8    * without modification, are permitted provided that the following
9    * conditions are met:
10   *
11   * 1. Redistributions of source code must retain the above
12   *    copyright notice, this list of conditions and the following
13   *    disclaimer.
14   *
15   * 2. Redistributions in binary form must reproduce the above
16   *    copyright notice, this list of conditions and the following
17   *    disclaimer in the documentation and/or other materials provided
18   *    with the distribution.
19   *
20   * 3. The end-user documentation included with the redistribution,
21   *    if any, must include the following acknowledgement:
22   *    "This product includes software developed by the
23   *    Seasar Project (http://www.seasar.org/)."
24   *    Alternately, this acknowledgement may appear in the software
25   *    itself, if and wherever such third-party acknowledgements
26   *    normally appear.
27   *
28   * 4. Neither the name "The Seasar Project" nor the names of its
29   *    contributors may be used to endorse or promote products derived
30   *    from this software without specific prior written permission of
31   *    the Seasar Project.
32   *
33   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR
34   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE SEASAR PROJECT
37   * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38   * INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42   * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING
43   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
44   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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      //instance fields
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>&lt;meta name="s2axis:deploy"&gt;</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>&lt;meta name="s2axis:service"&gt;</code>
144      * または <code>&lt;meta name="s2axis:handler"&gt;</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>&lt;meta&gt;</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 }