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.core.ivm.naming;
018:
019: import org.apache.openejb.util.Logger;
020: import org.apache.openejb.util.LogCategory;
021:
022: import javax.naming.NamingException;
023: import javax.xml.namespace.QName;
024: import javax.xml.rpc.ServiceFactory;
025: import javax.xml.rpc.Service;
026: import javax.xml.rpc.ServiceException;
027: import java.net.MalformedURLException;
028: import java.net.URI;
029: import java.net.URL;
030:
031: // todo deal with handlers
032: public class JaxRpcServiceReference extends Reference {
033: private static final Logger logger = Logger.getInstance(
034: LogCategory.OPENEJB, JaxRpcServiceReference.class);
035:
036: private String serviceClassName;
037: private ClassLoader classLoader;
038: private URI wsdlURI;
039: private QName serviceQName;
040: private String referenceClassName;
041:
042: public JaxRpcServiceReference(QName serviceQName, URI wsdlURI,
043: String referenceClassName, String serviceClassName,
044: ClassLoader classLoader) {
045: this .classLoader = classLoader;
046: this .serviceQName = serviceQName;
047: this .wsdlURI = wsdlURI;
048: this .referenceClassName = referenceClassName;
049: this .serviceClassName = serviceClassName;
050: }
051:
052: public Object getObject() throws NamingException {
053: Class<? extends Service> serviceClass = loadClass(
054: serviceClassName).asSubclass(Service.class);
055: Class<?> referenceClass = getReferenceClass();
056:
057: if (referenceClass != null
058: && Service.class.isAssignableFrom(referenceClass)) {
059: serviceClass = referenceClass.asSubclass(Service.class);
060: }
061:
062: try {
063: Service instance;
064: if (Service.class.equals(serviceClass)) {
065: instance = ServiceFactory.newInstance().createService(
066: getWsdlURL(), serviceQName);
067: } else {
068: try {
069: instance = serviceClass.getConstructor(URL.class,
070: QName.class).newInstance(getWsdlURL(),
071: serviceQName);
072: } catch (Throwable e) {
073: throw (NamingException) new NamingException(
074: "Could not instantiate jax-ws service class "
075: + serviceClass.getName())
076: .initCause(e);
077: }
078: }
079:
080: if (referenceClass != null
081: && !Service.class.isAssignableFrom(referenceClass)) {
082: // do port lookup
083: return instance.getPort(referenceClass);
084: } else {
085: // return service
086: return instance;
087: }
088: } catch (ServiceException e) {
089: throw (NamingException) new NamingException(
090: "Error creating service proxy").initCause(e);
091: }
092: }
093:
094: private Class getReferenceClass() throws NamingException {
095: if (referenceClassName == null)
096: return null;
097: return loadClass(referenceClassName);
098: }
099:
100: private Class<?> loadClass(String name) throws NamingException {
101: try {
102: return classLoader.loadClass(name);
103: } catch (ClassNotFoundException e) {
104: NamingException exception = new NamingException(
105: "Count not load class " + name);
106: exception.initCause(e);
107: throw exception;
108: }
109: }
110:
111: private URL getWsdlURL() {
112: if (wsdlURI == null)
113: return null;
114:
115: try {
116: return new URL(wsdlURI.toString());
117: } catch (MalformedURLException e) {
118: URL wsdlURL = classLoader.getResource(this .wsdlURI
119: .toString());
120: if (wsdlURL == null) {
121: logger.warning("Error obtaining WSDL: " + this.wsdlURI,
122: e);
123: }
124: return wsdlURL;
125: }
126: }
127: }
|