Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.as.webservices.webserviceref;
 
 import static org.jboss.as.webservices.WSLogger.ROOT_LOGGER;
 
 import java.net.URL;
 import java.util.List;
 import java.util.Set;
 
 
 import  org.jboss.metadata.javaee.jboss.JBossPortComponentRef;
 import  org.jboss.metadata.javaee.jboss.JBossServiceReferenceMetaData;
 import  org.jboss.metadata.javaee.jboss.StubPropertyMetaData;
 import  org.jboss.metadata.javaee.spec.Addressing;
 import  org.jboss.metadata.javaee.spec.ParamValueMetaData;
 import  org.jboss.metadata.javaee.spec.PortComponentRef;
 import  org.jboss.metadata.javaee.spec.ServiceReferenceHandlerChainMetaData;
 import  org.jboss.metadata.javaee.spec.ServiceReferenceHandlerChainsMetaData;
 import  org.jboss.metadata.javaee.spec.ServiceReferenceHandlerMetaData;
 import  org.jboss.metadata.javaee.spec.ServiceReferenceMetaData;
Translates WS Refs from JBossAS MD to JBossWS UMDM format. Some of the methods on this class are public to allow weld

Author(s):
Richard Opalka
 
 final class WSRefUtils {
 
     private WSRefUtils() {
     }
 
     static UnifiedServiceRefMetaData translate(final ServiceReferenceMetaData serviceRefMD) {
         builder.setServiceRefName(serviceRefMD.getName());
         builder.setServiceRefType(serviceRefMD.getServiceRefType());
         builder.setServiceInterface(serviceRefMD.getServiceInterface());
         builder.setWsdlFile(serviceRefMD.getWsdlFile());
         builder.setMappingFile(serviceRefMD.getJaxrpcMappingFile());
         builder.setServiceQName(serviceRefMD.getServiceQname());
 
         // propagate port components
         final Collection<? extends PortComponentRef> portComponentsMD = serviceRefMD.getPortComponentRef();
         if (portComponentsMD != null) {
             for (final PortComponentRef portComponentMD : portComponentsMD) {
                 final UnifiedPortComponentRefMetaData portComponentUMDM = getUnifiedPortComponentRefMetaData(portComponentMD);
                 if (portComponentUMDM.getServiceEndpointInterface() != null || portComponentUMDM.getPortQName() != null) {
                     builder.addPortComponentRef(portComponentUMDM);
                 } else {
                     .ignoringPortComponentRef(portComponentUMDM);
                 }
            }
        }
        // propagate handlers
        final Collection<ServiceReferenceHandlerMetaData> handlersMD = serviceRefMD.getHandlers();
        if (handlersMD != null) {
           for (final ServiceReferenceHandlerMetaData handlerMD : handlersMD) {
              final UnifiedHandlerMetaData handlerUMDM = getUnifiedHandlerMetaData(handlerMD);
              builder.addHandler(handlerUMDM);
           }
        }
        // propagate handler chains
        ServiceReferenceHandlerChainsMetaData handlerChainsMD = serviceRefMD.getHandlerChains();
        if (handlerChainsMD != null) {
           final UnifiedHandlerChainsMetaData handlerChainsUMDM = getUnifiedHandlerChainsMetaData(handlerChainsMD);
           builder.setHandlerChains(handlerChainsUMDM);
        }
        // propagate jboss specific MD
        if (serviceRefMD instanceof JBossServiceReferenceMetaData) {
           final JBossServiceReferenceMetaData jbossServiceRefMD = (JBossServiceReferenceMetaData) serviceRefMD;
           builder.setServiceImplClass(jbossServiceRefMD.getServiceClass());
           builder.setConfigName(jbossServiceRefMD.getConfigName());
           builder.setConfigFile(jbossServiceRefMD.getConfigFile());
           builder.setWsdlOverride(jbossServiceRefMD.getWsdlOverride());
           builder.setHandlerChain(jbossServiceRefMD.getHandlerChain());
        }
        return builder.build();
    }
    private static UnifiedPortComponentRefMetaData getUnifiedPortComponentRefMetaData(final PortComponentRef portComponentMD) {
        // propagate service endpoint interface
        builder.setServiceEndpointInterface(portComponentMD.getServiceEndpointInterface());
        // propagate MTOM properties
        builder.setMtomEnabled(portComponentMD.isEnableMtom());
        builder.setMtomThreshold(portComponentMD.getMtomThreshold());
        // propagate addressing properties
        final Addressing addressingMD = portComponentMD.getAddressing();
        if (addressingMD != null) {
            builder.setAddressingAnnotationSpecified(true);
            builder.setAddressingEnabled(addressingMD.isEnabled());
            builder.setAddressingRequired(addressingMD.isRequired());
            builder.setAddressingResponses(addressingMD.getResponses());
        }
        // propagate respect binding properties
        if (portComponentMD.getRespectBinding() != null) {
            builder.setRespectBindingAnnotationSpecified(true);
            builder.setRespectBindingEnabled(true);
        }
        // propagate link
        builder.setPortComponentLink(portComponentMD.getPortComponentLink());
        // propagate jboss specific MD
        if (portComponentMD instanceof JBossPortComponentRef) {
            final JBossPortComponentRef jbossPortComponentMD = (JBossPortComponentRef) portComponentMD;
            // propagate port QName
            builder.setPortQName(jbossPortComponentMD.getPortQname());
            // propagate configuration properties
            builder.setConfigName(jbossPortComponentMD.getConfigName());
            builder.setConfigFile(jbossPortComponentMD.getConfigFile());
            // propagate stub properties
            final List<StubPropertyMetaData> stubPropertiesMD = jbossPortComponentMD.getStubProperties();
            if (stubPropertiesMD != null) {
                for (final StubPropertyMetaData stubPropertyMD : stubPropertiesMD) {
                    builder.addStubProperty(new UnifiedStubPropertyMetaData(stubPropertyMD.getPropName(), stubPropertyMD.getPropValue()));
                }
            }
        }
        return builder.build();
    }
    private static UnifiedHandlerMetaData getUnifiedHandlerMetaData(ServiceReferenceHandlerMetaData srhmd) {
        List<UnifiedInitParamMetaDataunifiedInitParamMDs = new LinkedList<UnifiedInitParamMetaData>();
        List<ParamValueMetaData> initParams = srhmd.getInitParam();
        if (initParams != null) {
            for (ParamValueMetaData initParam : initParams) {
                unifiedInitParamMDs.add(new UnifiedInitParamMetaData(initParam.getParamName(), initParam.getParamValue()));
            }
        }
        List<QNamesoapHeaders = srhmd.getSoapHeader();
        Set<QNamesoapHeaderList = soapHeaders != null ? new HashSet<QName>(soapHeaders) : null;
        List<StringsoapRoles = srhmd.getSoapRole();
        Set<StringsoapRolesList = soapRoles != null ? new HashSet<String>(soapRoles) : null;
        List<StringportNames = srhmd.getPortName();
        Set<StringportNameList = portNames != null ? new HashSet<String>(portNames) : null;
        return new UnifiedHandlerMetaData(srhmd.getHandlerClass(), srhmd.getHandlerName(), unifiedInitParamMDssoapHeaderListsoapRolesListportNameList);
    }
    private static UnifiedHandlerChainsMetaData getUnifiedHandlerChainsMetaData(final ServiceReferenceHandlerChainsMetaData handlerChainsMD) {
        for (final ServiceReferenceHandlerChainMetaData handlerChainMD : handlerChainsMD.getHandlers()) {
            List<UnifiedHandlerMetaDatauhmds = new LinkedList<UnifiedHandlerMetaData>();
            for (final ServiceReferenceHandlerMetaData handlerMD : handlerChainMD.getHandler()) {
                final UnifiedHandlerMetaData handlerUMDM = getUnifiedHandlerMetaData(handlerMD);
                uhmds.add(handlerUMDM);
            }
            uhcmds.add(new UnifiedHandlerChainMetaData(handlerChainMD.getServiceNamePattern(), handlerChainMD.getPortNamePattern(),
                    handlerChainMD.getProtocolBindings(), uhmdsfalsenull));
        }
        return new UnifiedHandlerChainsMetaData(uhcmds);
    }
    static void processAnnotatedElement(final AnnotatedElement anElementfinal UnifiedServiceRefMetaData serviceRefUMDM) {
       processAddressingAnnotation(anElementserviceRefUMDM);
       processMTOMAnnotation(anElementserviceRefUMDM);
       processRespectBindingAnnotation(anElementserviceRefUMDM);
       processHandlerChainAnnotation(anElementserviceRefUMDM);
       processServiceRefType(anElementserviceRefUMDM);
    }
    private static void processAddressingAnnotation(final AnnotatedElement anElementfinal UnifiedServiceRefMetaData serviceRefUMDM) {
         final javax.xml.ws.soap.Addressing addressingAnnotation = getAnnotation(anElementjavax.xml.ws.soap.Addressing.class);
         if (addressingAnnotation != null) {
             serviceRefUMDM.setAddressingMedadata(new AddressingMetadata(true,
                    addressingAnnotation.enabled(),
                    addressingAnnotation.required(),
                    addressingAnnotation.responses().toString()));
         }
      }
      private static void processMTOMAnnotation(final AnnotatedElement anElementfinal UnifiedServiceRefMetaData serviceRefUMDM) {
         final MTOM mtomAnnotation = getAnnotation(anElementMTOM.class);
         if (mtomAnnotation != null) {
            serviceRefUMDM.setMTOMMetadata(new MTOMMetadata(truemtomAnnotation.enabled(), mtomAnnotation.threshold()));
         }
      }
      private static void processRespectBindingAnnotation(final AnnotatedElement anElementfinal UnifiedServiceRefMetaData serviceRefUMDM) {
         final javax.xml.ws.RespectBinding respectBindingAnnotation = getAnnotation(anElementjavax.xml.ws.RespectBinding.class);
         if (respectBindingAnnotation != null) {
            serviceRefUMDM.setRespectBindingMetadata(new RespectBindingMetadata(truerespectBindingAnnotation.enabled()));
         }
      }
      private static  void processServiceRefType(final AnnotatedElement anElementfinal UnifiedServiceRefMetaData serviceRefUMDM) {
         if (anElement instanceof Field) {
            final Class<?> targetClass = ((FieldanElement).getType();
            serviceRefUMDM.setServiceRefType(targetClass.getName());
            if (Service.class.isAssignableFrom(targetClass))
               serviceRefUMDM.setServiceInterface(targetClass.getName());
         } else if (anElement instanceof Method) {
            final Class<?> targetClass = ((MethodanElement).getParameterTypes()[0];
            serviceRefUMDM.setServiceRefType(targetClass.getName());
            if (Service.class.isAssignableFrom(targetClass))
               serviceRefUMDM.setServiceInterface(targetClass.getName());
         } else {
            final WebServiceRef serviceRefAnnotation = getWebServiceRefAnnotation(anElementserviceRefUMDM);
            Class<?> targetClass = null;
            if (serviceRefAnnotation != null && (serviceRefAnnotation.type() != Object.class))
            {
               targetClass = serviceRefAnnotation.type();
               serviceRefUMDM.setServiceRefType(targetClass.getName());
               if (Service.class.isAssignableFrom(targetClass))
                  serviceRefUMDM.setServiceInterface(targetClass.getName());
            }
         }
      }
      private static void processHandlerChainAnnotation(final AnnotatedElement anElementfinal UnifiedServiceRefMetaData serviceRefUMDM) {
         final javax.jws.HandlerChain handlerChainAnnotation = getAnnotation(anElementjavax.jws.HandlerChain.class);
         if (handlerChainAnnotation != null) {
            // Set the handlerChain from @HandlerChain on the annotated element
            String handlerChain = null;
            if (handlerChainAnnotation.file().length() > 0)
               handlerChain = handlerChainAnnotation.file();
            // Resolve path to handler chain
            if (handlerChain != null) {
               try {
                  new URL(handlerChain);
               } catch (MalformedURLException ignored) {
                  final Class<?> declaringClass = getDeclaringClass(anElement);
                  handlerChain = declaringClass.getPackage().getName().replace('.''/') + "/" + handlerChain;
               }
               serviceRefUMDM.setHandlerChain(handlerChain);
            }
         }
      }
      private static <T extends Annotation> T getAnnotation(final AnnotatedElement anElementfinal Class<T> annotationClass) {
         return anElement != null ? (T) anElement.getAnnotation(annotationClass) : null;
      }
      private static Class<?> getDeclaringClass(final AnnotatedElement annotatedElement) {
         Class<?> declaringClass = null;
         if (annotatedElement instanceof Field) {
            declaringClass = ((FieldannotatedElement).getDeclaringClass();
         } else if (annotatedElement instanceof Method) {
            declaringClass = ((MethodannotatedElement).getDeclaringClass();
         } else if (annotatedElement instanceof Class) {
            declaringClass = (Class<?>) annotatedElement;
         }
         return declaringClass;
      }
      private static WebServiceRef getWebServiceRefAnnotation(final AnnotatedElement anElementfinal UnifiedServiceRefMetaData serviceRefUMDM) {
          final WebServiceRef webServiceRefAnnotation = getAnnotation(anElementWebServiceRef.class);
          final WebServiceRefs webServiceRefsAnnotation = getAnnotation(anElementWebServiceRefs.class);
          if (webServiceRefAnnotation == null && webServiceRefsAnnotation == null) {
              return null;
          }
          // Build the list of @WebServiceRef relevant annotations
          final List<WebServiceRefwsrefList = new ArrayList<WebServiceRef>();
          if (webServiceRefAnnotation != null) {
              wsrefList.add(webServiceRefAnnotation);
          }
          if (webServiceRefsAnnotation != null) {
              for (final WebServiceRef webServiceRefAnn : webServiceRefsAnnotation.value()) {
                  wsrefList.add(webServiceRefAnn);
              }
          }
          // Return effective @WebServiceRef annotation
          WebServiceRef returnValue = null;
          if (wsrefList.size() == 1) {
              returnValue = wsrefList.get(0);
          } else {
              for (WebServiceRef webServiceRefAnn : wsrefList) {
                  if (serviceRefUMDM.getServiceRefName().endsWith(webServiceRefAnn.name())) {
                      returnValue = webServiceRefAnn;
                      break;
                  }
              }
          }
          return returnValue;
      }
New to GrepCode? Check out our FAQ X