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 java.io.InputStream;
49  import java.util.ArrayList;
50  import java.util.List;
51  
52  import org.apache.axis.deployment.wsdd.WSDDConstants;
53  import org.apache.axis.deployment.wsdd.WSDDException;
54  import org.apache.axis.utils.XMLUtils;
55  import org.seasar.framework.container.ComponentDef;
56  import org.seasar.framework.container.MetaDef;
57  import org.seasar.framework.log.Logger;
58  import org.seasar.framework.message.MessageFormatter;
59  import org.seasar.framework.util.ResourceUtil;
60  import org.seasar.remoting.axis.DeployFailedException;
61  import org.seasar.remoting.axis.ServiceDef;
62  import org.seasar.remoting.axis.deployment.WSDDS2Service;
63  import org.w3c.dom.Element;
64  import org.w3c.dom.Node;
65  import org.w3c.dom.NodeList;
66  
67  /***
68   * diconファイル中に記述されたコンポーネントをサービスとしてAxisにデプロイします。
69   * 
70   * @author koichik
71   */
72  public class ServiceDeployer implements ItemDeployer {
73      //class fields
74      private final static Logger logger = Logger.getLogger(ServiceDeployer.class);
75  
76      //instance fields
77      protected AxisDeployer deployer;
78  
79      /***
80       * インスタンスを構築します。
81       * 
82       * @param deployer
83       *            デプロイヤー
84       */
85      public ServiceDeployer(final AxisDeployer deployer) {
86          this.deployer = deployer;
87      }
88  
89      /***
90       * コンポーネントをサービスとしてデプロイします。
91       * 
92       * @param componentDef
93       *            コンポーネント定義
94       * @param metaDef
95       *            メタ定義
96       */
97      public void deploy(final ComponentDef componentDef, final MetaDef metaDef) {
98          final WSDDS2Service service = createWSDDS2Service(componentDef, metaDef);
99          deployer.getDeployment(componentDef.getContainer()).deployService(service);
100 
101         if (logger.isDebugEnabled()) {
102             logger.log("DAXS0003", new Object[] { service.getQName() });
103         }
104     }
105 
106     /***
107      * <code>WSDDS2Service</code> をインスタンス化して返します。 <br>
108      * メタデータの指定に従い、 <code>ServiceDef</code> またはWSDDファイルから
109      * <code>WSDDS2Service</code> をインスタンス化します。
110      * 
111      * @param componentDef
112      *            コンポーネント定義
113      * @param metaDef
114      *            メタデータ定義
115      * @return <code>WSDDS2Service</code>
116      */
117     protected WSDDS2Service createWSDDS2Service(final ComponentDef componentDef,
118             final MetaDef metaDef) {
119         try {
120             Object metadata = metaDef.getValue();
121             if (metadata == null) {
122                 return new WSDDS2Service(componentDef);
123             }
124             else if (metadata instanceof ServiceDef) {
125                 return new WSDDS2Service(componentDef, (ServiceDef) metadata);
126             }
127             else if (metadata instanceof String) {
128                 return new WSDDS2Service(componentDef, getServiceElement((String) metadata));
129             }
130             throw new DeployFailedException();
131         }
132         catch (final WSDDException e) {
133             throw new DeployFailedException(e);
134         }
135     }
136 
137     /***
138      * WSDDファイルをファイルシステムまたはクラスパスから読み込み、 <code>&lt;service&gt;</code> 要素を返します。
139      * 
140      * @param wsddFileName
141      *            WSDDファイルのパス名
142      * @return <code>&lt;service&gt;</code> 要素
143      */
144     protected Element getServiceElement(final String wsddFileName) {
145         try {
146             final InputStream is = ResourceUtil.getResourceAsStream(wsddFileName);
147             final Element documentElement = XMLUtils.newDocument(is).getDocumentElement();
148             final Element[] serviceElements = getChildElements(documentElement,
149                     WSDDConstants.ELEM_WSDD_SERVICE);
150             if (serviceElements.length != 1) {
151                 throw new DeployFailedException(MessageFormatter.getSimpleMessage("EAXS0005",
152                         new Object[] { wsddFileName }));
153             }
154             return serviceElements[0];
155         }
156         catch (final DeployFailedException e) {
157             throw e;
158         }
159         catch (final Exception e) {
160             throw new DeployFailedException(e);
161         }
162     }
163 
164     /***
165      * 指定されたローカル名を持つ子要素の配列を返します。
166      * 
167      * @param parent
168      *            親要素
169      * @param name
170      *            子要素のローカル名
171      * @return 指定されたローカル名を持つ子要素の配列。該当する子要素が存在しない場合は空の配列。
172      */
173     protected Element[] getChildElements(final Element parent, final String name) {
174         final List result = new ArrayList();
175         final NodeList children = parent.getChildNodes();
176         for (int i = 0; i < children.getLength(); i++) {
177             final Node thisNode = children.item(i);
178             if (thisNode instanceof Element) {
179                 final Element element = (Element) thisNode;
180                 if (element.getLocalName().equals(name)) {
181                     result.add(element);
182                 }
183             }
184         }
185         return (Element[]) result.toArray(new Element[result.size()]);
186     }
187 }