001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.cxf.util;
018
019 import java.lang.annotation.Annotation;
020
021 import javax.jws.WebService;
022 import javax.xml.namespace.QName;
023 import javax.xml.ws.WebServiceProvider;
024
025 import org.apache.camel.CamelException;
026 import org.apache.camel.component.cxf.CxfConstants;
027 import org.apache.camel.component.cxf.CxfEndpoint;
028 import org.apache.camel.component.cxf.CxfSpringEndpoint;
029 import org.apache.camel.component.cxf.spring.CxfEndpointBean;
030 import org.apache.camel.util.ObjectHelper;
031 import org.apache.cxf.frontend.ClientProxyFactoryBean;
032 import org.apache.cxf.frontend.ServerFactoryBean;
033 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
034 import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
035
036 public final class CxfEndpointUtils {
037
038 private CxfEndpointUtils() {
039 // not constructed
040 }
041
042 public static QName getQName(final String name) {
043 QName qName = null;
044 if (name != null) {
045 try {
046 qName = QName.valueOf(name);
047 } catch (Exception ex) {
048 ex.printStackTrace();
049 }
050 }
051 return qName;
052 }
053
054 // only used by test currently
055 public static QName getPortName(final CxfEndpoint endpoint) {
056 if (endpoint.getPortName() != null) {
057 return getQName(endpoint.getPortName());
058 } else {
059 String portLocalName = getCxfEndpointPropertyValue((CxfSpringEndpoint)endpoint, CxfConstants.PORT_LOCALNAME);
060 String portNamespace = getCxfEndpointPropertyValue((CxfSpringEndpoint)endpoint, CxfConstants.PORT_NAMESPACE);
061 if (portLocalName != null) {
062 return new QName(portNamespace, portLocalName);
063 } else {
064 return null;
065 }
066 }
067 }
068
069 // only used by test currently
070 public static QName getServiceName(final CxfEndpoint endpoint) {
071 if (endpoint.getServiceName() != null) {
072 return getQName(endpoint.getServiceName());
073 } else {
074 String serviceLocalName = getCxfEndpointPropertyValue((CxfSpringEndpoint)endpoint, CxfConstants.SERVICE_LOCALNAME);
075 String serviceNamespace = getCxfEndpointPropertyValue((CxfSpringEndpoint)endpoint, CxfConstants.SERVICE_NAMESPACE);
076 if (serviceLocalName != null) {
077 return new QName(serviceNamespace, serviceLocalName);
078 } else {
079 return null;
080 }
081 }
082 }
083
084 public static boolean hasWebServiceAnnotation(Class<?> cls) {
085 return hasAnnotation(cls, WebService.class) || hasAnnotation(cls, WebServiceProvider.class);
086 }
087
088 public static boolean hasAnnotation(Class<?> cls, Class<? extends Annotation> annotation) {
089 if (cls == null || cls == Object.class) {
090 return false;
091 }
092
093 if (null != cls.getAnnotation(annotation)) {
094 return true;
095 }
096
097 for (Class<?> interfaceClass : cls.getInterfaces()) {
098 if (null != interfaceClass.getAnnotation(annotation)) {
099 return true;
100 }
101 }
102 return hasAnnotation(cls.getSuperclass(), annotation);
103 }
104
105 public static ServerFactoryBean getServerFactoryBean(Class<?> cls) throws CamelException {
106 ServerFactoryBean serverFactory = null;
107 try {
108 if (cls == null) {
109 serverFactory = new ServerFactoryBean();
110 serverFactory.setServiceFactory(new WSDLSoapServiceFactoryBean());
111
112 } else {
113 boolean isJSR181SEnabled = CxfEndpointUtils.hasWebServiceAnnotation(cls);
114 serverFactory = isJSR181SEnabled ? new JaxWsServerFactoryBean()
115 : new ServerFactoryBean();
116 }
117 return serverFactory;
118 } catch (Exception e) {
119 throw new CamelException(e);
120 }
121
122 }
123
124 public static ClientProxyFactoryBean getClientFactoryBean(Class<?> cls) throws CamelException {
125 ClientProxyFactoryBean clientFactory = null;
126 try {
127 if (cls == null) {
128 clientFactory = new ClientProxyFactoryBean();
129 clientFactory.setServiceFactory(new WSDLSoapServiceFactoryBean());
130 } else {
131 boolean isJSR181SEnabled = CxfEndpointUtils.hasWebServiceAnnotation(cls);
132 clientFactory = isJSR181SEnabled ? new JaxWsProxyFactoryBean()
133 : new ClientProxyFactoryBean();
134 }
135 return clientFactory;
136 } catch (Exception e) {
137 throw new CamelException(e);
138 }
139 }
140
141 // only used by test currently
142 public static void checkServiceClassName(String className) throws CamelException {
143 if (ObjectHelper.isEmpty(className)) {
144 throw new CamelException("serviceClass is required for CXF endpoint configuration");
145 }
146 }
147
148 // only used by test currently
149 public static String getCxfEndpointPropertyValue(CxfSpringEndpoint endpoint, String property) {
150 String result = null;
151 CxfEndpointBean cxfEndpointBean = endpoint.getBean();
152 if (cxfEndpointBean != null && cxfEndpointBean.getProperties() != null) {
153 result = (String) cxfEndpointBean.getProperties().get(property);
154 }
155 return result;
156 }
157
158
159 }
160
161
162