001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport.http.server;
038:
039: import com.sun.istack.Nullable;
040: import com.sun.xml.stream.buffer.XMLStreamBufferResult;
041: import com.sun.xml.ws.api.WSBinding;
042: import com.sun.xml.ws.api.BindingID;
043: import com.sun.xml.ws.api.server.WSEndpoint;
044: import com.sun.xml.ws.binding.BindingImpl;
045: import com.sun.xml.ws.api.server.InstanceResolver;
046: import com.sun.xml.ws.api.server.SDDocumentSource;
047: import com.sun.xml.ws.server.EndpointFactory;
048: import com.sun.xml.ws.server.ServerRtException;
049: import com.sun.xml.ws.util.xml.XmlUtil;
050: import com.sun.istack.NotNull;
051:
052: import java.net.MalformedURLException;
053:
054: import javax.xml.namespace.QName;
055: import javax.xml.transform.Source;
056: import javax.xml.transform.Transformer;
057: import javax.xml.transform.TransformerException;
058: import javax.xml.ws.*;
059: import javax.xml.ws.wsaddressing.W3CEndpointReference;
060:
061: import java.io.IOException;
062: import java.net.URL;
063: import java.util.ArrayList;
064: import java.util.Collections;
065: import java.util.HashMap;
066: import java.util.List;
067: import java.util.Map;
068: import java.util.concurrent.Executor;
069:
070: import org.xml.sax.EntityResolver;
071: import org.w3c.dom.Element;
072:
073: /**
074: * Implements {@link Endpoint}.
075: * <p/>
076: * <p/>
077: * This class accumulates the information necessary to create
078: * {@link WSEndpoint}, and then when {@link #publish} method
079: * is called it will be created.
080: * <p/>
081: * <p/>
082: * This object also allows accumulated information to be retrieved.
083: *
084: * @author Jitendra Kotamraju
085: */
086: public class EndpointImpl extends Endpoint {
087:
088: private static final WebServicePermission ENDPOINT_PUBLISH_PERMISSION = new WebServicePermission(
089: "publishEndpoint");
090:
091: /**
092: * Once the service is published, this field will
093: * be set to the {@link HttpEndpoint} instance.
094: * <p/>
095: * But don't declare the type as {@link HttpEndpoint}
096: * to avoid static type dependency that cause the class loading to
097: * fail if the LW HTTP server doesn't exist.
098: */
099: private Object actualEndpoint;
100:
101: // information accumulated for creating WSEndpoint
102: private final WSBinding binding;
103: private final Object implementor;
104: private List<Source> metadata;
105: private Executor executor;
106: private Map<String, Object> properties = Collections.emptyMap(); // always non-null
107: private boolean stopped;
108:
109: public EndpointImpl(@NotNull
110: BindingID bindingId, @NotNull
111: Object impl) {
112: binding = BindingImpl.create(bindingId);
113: implementor = impl;
114: }
115:
116: /**
117: * Wraps an already created {@link WSEndpoint} into an {@link EndpointImpl},
118: * and immediately publishes it with the given context.
119: *
120: * @deprecated This is a backdoor method. Don't use it unless you know what you are doing.
121: */
122: public EndpointImpl(WSEndpoint wse, Object serverContext) {
123: actualEndpoint = new HttpEndpoint(wse, executor);
124: ((HttpEndpoint) actualEndpoint).publish(serverContext);
125: binding = wse.getBinding();
126: implementor = null; // this violates the semantics, but hey, this is a backdoor.
127: }
128:
129: public Binding getBinding() {
130: return binding;
131: }
132:
133: public Object getImplementor() {
134: return implementor;
135: }
136:
137: public void publish(String address) {
138: canPublish();
139: URL url;
140: try {
141: url = new URL(address);
142: } catch (MalformedURLException ex) {
143: throw new IllegalArgumentException(
144: "Cannot create URL for this address " + address);
145: }
146: if (!url.getProtocol().equals("http")) {
147: throw new IllegalArgumentException(url.getProtocol()
148: + " protocol based address is not supported");
149: }
150: if (!url.getPath().startsWith("/")) {
151: throw new IllegalArgumentException(
152: "Incorrect WebService address="
153: + address
154: + ". The address's path should start with /");
155: }
156: createEndpoint();
157: ((HttpEndpoint) actualEndpoint).publish(address);
158: }
159:
160: public void publish(Object serverContext) {
161: canPublish();
162: if (!com.sun.net.httpserver.HttpContext.class
163: .isAssignableFrom(serverContext.getClass())) {
164: throw new IllegalArgumentException(serverContext.getClass()
165: + " is not a supported context.");
166: }
167: createEndpoint();
168: ((HttpEndpoint) actualEndpoint).publish(serverContext);
169: }
170:
171: public void stop() {
172: if (isPublished()) {
173: ((HttpEndpoint) actualEndpoint).stop();
174: actualEndpoint = null;
175: stopped = true;
176: }
177: }
178:
179: public boolean isPublished() {
180: return actualEndpoint != null;
181: }
182:
183: public List<Source> getMetadata() {
184: return metadata;
185: }
186:
187: public void setMetadata(java.util.List<Source> metadata) {
188: if (isPublished()) {
189: throw new IllegalStateException(
190: "Cannot set Metadata. Endpoint is already published");
191: }
192: this .metadata = metadata;
193: }
194:
195: public Executor getExecutor() {
196: return executor;
197: }
198:
199: public void setExecutor(Executor executor) {
200: this .executor = executor;
201: }
202:
203: public Map<String, Object> getProperties() {
204: return new HashMap<String, Object>(properties);
205: }
206:
207: public void setProperties(Map<String, Object> map) {
208: this .properties = new HashMap<String, Object>(map);
209: }
210:
211: /*
212: * Checks the permission of "publishEndpoint" before accessing HTTP classes.
213: * Also it checks if there is an available HTTP server implementation.
214: */
215: private void createEndpoint() {
216: // Checks permission for "publishEndpoint"
217: SecurityManager sm = System.getSecurityManager();
218: if (sm != null) {
219: sm.checkPermission(ENDPOINT_PUBLISH_PERMISSION);
220: }
221:
222: // See if HttpServer implementation is available
223: try {
224: Class.forName("com.sun.net.httpserver.HttpServer");
225: } catch (Exception e) {
226: throw new UnsupportedOperationException("NOT SUPPORTED");
227: }
228:
229: WSEndpoint wse = WSEndpoint
230: .create((Class<?>) implementor.getClass(), true,
231: InstanceResolver.createSingleton(implementor)
232: .createInvoker(), getProperty(
233: QName.class, Endpoint.WSDL_SERVICE),
234: getProperty(QName.class, Endpoint.WSDL_PORT),
235: null /* no container */, binding,
236: getPrimaryWsdl(), buildDocList(),
237: (EntityResolver) null);
238: // Don't load HttpEndpoint class before as it may load HttpServer classes
239: actualEndpoint = new HttpEndpoint(wse, executor);
240: }
241:
242: private <T> T getProperty(Class<T> type, String key) {
243: Object o = properties.get(key);
244: if (o == null)
245: return null;
246: if (type.isInstance(o))
247: return type.cast(o);
248: else
249: throw new IllegalArgumentException("Property " + key
250: + " has to be of type " + type); // i18n
251: }
252:
253: /**
254: * Convert metadata sources using identity transform. So that we can
255: * reuse the Source object multiple times.
256: */
257: private List<SDDocumentSource> buildDocList() {
258: List<SDDocumentSource> r = new ArrayList<SDDocumentSource>();
259:
260: if (metadata != null) {
261: Transformer transformer = XmlUtil.newTransformer();
262: for (Source source : metadata) {
263: try {
264: XMLStreamBufferResult xsbr = new XMLStreamBufferResult();
265: transformer.transform(source, xsbr);
266: String systemId = source.getSystemId();
267:
268: r.add(SDDocumentSource.create(new URL(systemId),
269: xsbr.getXMLStreamBuffer()));
270: } catch (TransformerException te) {
271: throw new ServerRtException("server.rt.err", te);
272: } catch (IOException te) {
273: throw new ServerRtException("server.rt.err", te);
274: }
275: }
276: }
277:
278: return r;
279: }
280:
281: /**
282: * Gets wsdl from @WebService or @WebServiceProvider
283: */
284: private @Nullable
285: SDDocumentSource getPrimaryWsdl() {
286: Class implType = implementor.getClass();
287: // Takes care of @WebService, @WebServiceProvider's wsdlLocation
288: String wsdlLocation = EndpointFactory.getWsdlLocation(implType);
289: if (wsdlLocation != null) {
290: ClassLoader cl = implType.getClassLoader();
291: URL url = cl.getResource(wsdlLocation);
292: if (url != null) {
293: return SDDocumentSource.create(url);
294: }
295: throw new ServerRtException("cannot.load.wsdl",
296: wsdlLocation);
297: }
298: return null;
299: }
300:
301: private void canPublish() {
302: if (isPublished()) {
303: throw new IllegalStateException(
304: "Cannot publish this endpoint. Endpoint has been already published.");
305: }
306: if (stopped) {
307: throw new IllegalStateException(
308: "Cannot publish this endpoint. Endpoint has been already stopped.");
309: }
310: }
311:
312: public EndpointReference getEndpointReference(
313: Element... referenceParameters) {
314: return getEndpointReference(W3CEndpointReference.class,
315: referenceParameters);
316: }
317:
318: public <T extends EndpointReference> T getEndpointReference(
319: Class<T> clazz, Element... referenceParameters) {
320: if (!isPublished()) {
321: throw new WebServiceException(
322: "Endpoint is not published yet");
323: }
324: return ((HttpEndpoint) actualEndpoint).getEndpointReference(
325: clazz, referenceParameters);
326: }
327: }
|