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 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
74 private static final Logger logger = Logger.getLogger(ServiceDeployer.class);
75
76
77 protected final 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 final 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><service></code> 要素を返します。
139 *
140 * @param wsddFileName
141 * WSDDファイルのパス名
142 * @return <code><service></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 }