001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.client;
018:
019: import javax.naming.InitialContext;
020: import javax.naming.NamingException;
021: import javax.xml.namespace.QName;
022: import javax.xml.ws.Service;
023: import javax.xml.ws.handler.HandlerResolver;
024: import java.net.URL;
025: import java.util.List;
026: import java.util.ArrayList;
027: import java.io.Serializable;
028:
029: public class WsMetaData implements Serializable {
030: private static final long serialVersionUID = -895152184216070327L;
031: private String serviceClassName;
032: private String referenceClassName;
033: private String wsdlUrl;
034: private String serviceQName;
035: private final List<HandlerChainMetaData> handlerChains = new ArrayList<HandlerChainMetaData>();
036: private final List<PortRefMetaData> portRefs = new ArrayList<PortRefMetaData>();
037:
038: public String getServiceClassName() {
039: return serviceClassName;
040: }
041:
042: public void setServiceClassName(String serviceClassName) {
043: this .serviceClassName = serviceClassName;
044: }
045:
046: public String getReferenceClassName() {
047: return referenceClassName;
048: }
049:
050: public void setReferenceClassName(String referenceClassName) {
051: this .referenceClassName = referenceClassName;
052: }
053:
054: public String getWsdlUrl() {
055: return wsdlUrl;
056: }
057:
058: public void setWsdlUrl(String wsdlUrl) {
059: this .wsdlUrl = wsdlUrl;
060: }
061:
062: public String getServiceQName() {
063: return serviceQName;
064: }
065:
066: public void setServiceQName(String serviceQName) {
067: this .serviceQName = serviceQName;
068: }
069:
070: public List<HandlerChainMetaData> getHandlerChains() {
071: return handlerChains;
072: }
073:
074: public List<PortRefMetaData> getPortRefs() {
075: return portRefs;
076: }
077:
078: public Object createWebservice() throws Exception {
079: // load service class which is used to construct the port
080: Class<? extends Service> serviceClass = loadClass(
081: serviceClassName).asSubclass(Service.class);
082: if (serviceClass == null) {
083: throw new NamingException(
084: "Could not load service type class "
085: + serviceClassName);
086: }
087:
088: // load the reference class which is the ultimate type of the port
089: Class<?> referenceClass = loadClass(referenceClassName);
090:
091: // if ref class is a subclass of Service, use it for the service class
092: if (referenceClass != null
093: && Service.class.isAssignableFrom(referenceClass)) {
094: serviceClass = referenceClass.asSubclass(Service.class);
095: }
096:
097: // Service QName
098: QName serviceQName = QName.valueOf(this .serviceQName);
099:
100: // WSDL URL
101: URL wsdlLocation = new URL(this .wsdlUrl);
102:
103: JaxWsProviderWrapper.beforeCreate(portRefs);
104: Service instance;
105: try {
106: instance = null;
107: if (Service.class.equals(serviceClass)) {
108: instance = Service.create(wsdlLocation, serviceQName);
109: } else {
110: try {
111: instance = serviceClass.getConstructor(URL.class,
112: QName.class).newInstance(wsdlLocation,
113: serviceQName);
114: } catch (Throwable e) {
115: throw (NamingException) new NamingException(
116: "Could not instantiate jax-ws service class "
117: + serviceClass.getName())
118: .initCause(e);
119: }
120: }
121: } finally {
122: JaxWsProviderWrapper.afterCreate();
123: }
124:
125: if (handlerChains != null && !handlerChains.isEmpty()) {
126: InjectionMetaData injectionMetaData = ClientInstance.get()
127: .getComponent(InjectionMetaData.class);
128: List<Injection> injections = injectionMetaData
129: .getInjections();
130: HandlerResolver handlerResolver = new ClientHandlerResolverImpl(
131: handlerChains, injections, new InitialContext());
132: instance.setHandlerResolver(handlerResolver);
133: }
134:
135: Object port;
136: if (referenceClass != null
137: && !Service.class.isAssignableFrom(referenceClass)) {
138: // do port lookup
139: port = instance.getPort(referenceClass);
140: } else {
141: // return service
142: port = instance;
143: }
144: return port;
145: }
146:
147: public static Class<?> loadClass(String className) {
148: if (className == null)
149: return null;
150: try {
151: ClassLoader classLoader = Thread.currentThread()
152: .getContextClassLoader();
153: if (classLoader != null) {
154: try {
155: Class clazz = classLoader.loadClass(className);
156: return clazz;
157: } catch (ClassNotFoundException e) {
158: }
159: }
160: return Class.forName(className);
161: } catch (ClassNotFoundException e) {
162: return null;
163: }
164: }
165: }
|