001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.ws.client;
026:
027: import com.sun.xml.internal.ws.handler.HandlerResolverImpl;
028: import com.sun.xml.internal.ws.handler.PortInfoImpl;
029: import com.sun.xml.internal.ws.model.RuntimeModel;
030: import com.sun.xml.internal.ws.modeler.RuntimeModeler;
031: import com.sun.xml.internal.ws.server.RuntimeContext;
032: import com.sun.xml.internal.ws.util.HandlerAnnotationInfo;
033: import com.sun.xml.internal.ws.util.HandlerAnnotationProcessor;
034: import com.sun.xml.internal.ws.util.JAXWSUtils;
035: import com.sun.xml.internal.ws.wsdl.WSDLContext;
036: import org.xml.sax.EntityResolver;
037: import javax.jws.HandlerChain;
038: import javax.xml.namespace.QName;
039: import javax.xml.ws.Service;
040: import javax.xml.ws.WebEndpoint;
041: import javax.xml.ws.WebServiceClient;
042: import javax.xml.ws.WebServiceException;
043: import javax.jws.WebService;
044: import java.lang.reflect.Method;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.security.AccessController;
048: import java.security.PrivilegedAction;
049: import java.util.ArrayList;
050:
051: /**
052: * $author: WS Development Team
053: */
054: public abstract class ServiceContextBuilder {
055: private ServiceContextBuilder() {
056: } // no instantication please
057:
058: /**
059: * Creates a new {@link ServiceContext}.
060: */
061: public static ServiceContext build(URL wsdlLocation,
062: QName serviceName, final Class service, EntityResolver er)
063: throws WebServiceException {
064: ServiceContext serviceContext = new ServiceContext(service,
065: serviceName, er);
066:
067: if (wsdlLocation != null) {
068: WSDLContext wsCtx = new WSDLContext(wsdlLocation, er);
069:
070: //check if the serviceName is a valid one, if its not in the given WSDL fail
071: if (!wsCtx.contains(serviceName))
072: throw new ClientConfigurationException(
073: "service.invalidServiceName", serviceName,
074: wsdlLocation);
075:
076: serviceContext.setWsdlContext(wsCtx);
077: }
078:
079: //if @HandlerChain present, set HandlerResolver on service context
080: HandlerChain handlerChain = (HandlerChain) AccessController
081: .doPrivileged(new PrivilegedAction() {
082: public Object run() {
083: return service
084: .getAnnotation(HandlerChain.class);
085: }
086: });
087: if (handlerChain != null) {
088: HandlerResolverImpl hresolver = new HandlerResolverImpl(
089: serviceContext);
090: serviceContext.setHandlerResolver(hresolver);
091: }
092: return serviceContext;
093: }
094:
095: public static void completeServiceContext(QName portName,
096: ServiceContext serviceContext, Class portInterface) {
097: if (portInterface != null)
098: processAnnotations(portName, serviceContext, portInterface);
099: }
100:
101: private static void processAnnotations(QName portName,
102: ServiceContext serviceContext, Class portInterface)
103: throws WebServiceException {
104: WSDLContext wsdlContext = serviceContext.getWsdlContext();
105: EndpointIFContext eifc = serviceContext
106: .getEndpointIFContext(portInterface.getName());
107: if ((eifc != null) && (eifc.getRuntimeContext() != null)) {
108: return;
109: }
110: if (eifc == null) {
111: eifc = new EndpointIFContext(portInterface);
112: serviceContext.addEndpointIFContext(eifc);
113: }
114:
115: QName serviceName = serviceContext.getServiceName();
116:
117: //if portName is null get it from the WSDL
118: if (portName == null) {
119: //get the first port corresponding to the SEI
120: QName portTypeName = RuntimeModeler
121: .getPortTypeName(portInterface);
122: portName = wsdlContext.getWsdlDocument().getPortName(
123: serviceContext.getServiceName(), portTypeName);
124: }
125:
126: //still no portName, fail
127: if (portName == null)
128: throw new ClientConfigurationException(
129: "service.noPortName", portInterface.getName(),
130: wsdlContext.getWsdlLocation().toString());
131:
132: eifc.setPortName(portName);
133: String bindingId = wsdlContext.getBindingID(serviceName,
134: portName);
135: RuntimeModeler modeler = new RuntimeModeler(portInterface,
136: serviceName, bindingId);
137: modeler.setPortName(portName);
138: RuntimeModel model = modeler.buildRuntimeModel();
139:
140: eifc.setRuntimeContext(new RuntimeContext(model));
141: }
142:
143: private ArrayList<Class<?>> getSEI(final Class sc) {
144:
145: if (sc == null) {
146: throw new WebServiceException();
147: }
148:
149: //check to make sure this is a service
150: if (!Service.class.isAssignableFrom(sc)) {
151: throw new WebServiceException("service.interface.required"
152: + sc.getName());
153: }
154:
155: final ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
156: AccessController.doPrivileged(new PrivilegedAction() {
157: public Object run() {
158: Method[] methods = sc.getDeclaredMethods();
159: for (final Method method : methods) {
160: method.setAccessible(true);
161: Class<?> seiClazz = method.getReturnType();
162: if ((seiClazz != null)
163: && (!seiClazz.equals("void")))
164: classes.add(seiClazz);
165:
166: }
167: return null;
168: }
169: });
170:
171: return classes;
172: }
173:
174: }
|