Search in sources :

Example 16 with ServiceConstructionException

use of org.apache.cxf.service.factory.ServiceConstructionException in project tomee by apache.

the class JAXRSServerFactoryBean method create.

/**
 * Creates the JAX-RS Server instance
 * @return the server
 */
public Server create() {
    ClassLoaderHolder origLoader = null;
    try {
        Bus bus = getBus();
        ClassLoader loader = bus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        serviceFactory.setBus(bus);
        checkResources(true);
        if (serviceFactory.getService() == null) {
            serviceFactory.create();
        }
        Endpoint ep = createEndpoint();
        getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server);
        server = new ServerImpl(getBus(), ep, getDestinationFactory(), getBindingFactory());
        Invoker invoker = serviceFactory.getInvoker();
        if (invoker == null) {
            ep.getService().setInvoker(createInvoker());
        } else {
            ep.getService().setInvoker(invoker);
        }
        ServerProviderFactory factory = setupFactory(ep);
        ep.put(Application.class.getName(), appProvider);
        factory.setRequestPreprocessor(new RequestPreprocessor(languageMappings, extensionMappings));
        ep.put(Bus.class.getName(), getBus());
        if (documentLocation != null) {
            ep.put(JAXRSUtils.DOC_LOCATION, documentLocation);
        }
        if (rc != null) {
            ep.put("org.apache.cxf.jaxrs.comparator", rc);
        }
        checkPrivateEndpoint(ep);
        applyBusFeatures(getBus());
        applyFeatures();
        updateClassResourceProviders(ep);
        injectContexts(factory, (ApplicationInfo) ep.get(Application.class.getName()));
        factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
        getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED, server, null, null);
        if (start) {
            try {
                server.start();
            } catch (RuntimeException re) {
                if (!(re instanceof ServiceConstructionException && re.getMessage().startsWith("There is an endpoint already running on"))) {
                    // avoid destroying another server on the same endpoint url
                    // prevent resource leak if server really started by itself
                    server.destroy();
                }
                throw re;
            }
        }
    } catch (Exception e) {
        throw new ServiceConstructionException(e);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
    }
    return server;
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Bus(org.apache.cxf.Bus) RequestPreprocessor(org.apache.cxf.jaxrs.impl.RequestPreprocessor) Endpoint(org.apache.cxf.endpoint.Endpoint) ServerImpl(org.apache.cxf.endpoint.ServerImpl) Invoker(org.apache.cxf.service.invoker.Invoker) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) Application(javax.ws.rs.core.Application) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException)

Example 17 with ServiceConstructionException

use of org.apache.cxf.service.factory.ServiceConstructionException in project tomee by apache.

the class JAXBDataBinding method initialize.

@SuppressWarnings("unchecked")
public synchronized void initialize(Service service) {
    inInterceptors.addIfAbsent(JAXBAttachmentSchemaValidationHack.INSTANCE);
    inFaultInterceptors.addIfAbsent(JAXBAttachmentSchemaValidationHack.INSTANCE);
    // context is already set, don't redo it
    if (context != null) {
        return;
    }
    contextClasses = new LinkedHashSet<>();
    for (ServiceInfo serviceInfo : service.getServiceInfos()) {
        JAXBContextInitializer initializer = new JAXBContextInitializer(getBus(), serviceInfo, contextClasses, typeRefs, this.getUnmarshallerProperties());
        initializer.walk();
        if (serviceInfo.getProperty("extra.class") != null) {
            Set<Class<?>> exClasses = serviceInfo.getProperty("extra.class", Set.class);
            contextClasses.addAll(exClasses);
        }
    }
    String tns = getNamespaceToUse(service);
    final CachedContextAndSchemas cachedContextAndSchemas;
    try {
        cachedContextAndSchemas = createJAXBContextAndSchemas(contextClasses, tns);
    } catch (JAXBException e1) {
        throw new ServiceConstructionException(e1);
    }
    final JAXBContext ctx = cachedContextAndSchemas.getContext();
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "CREATED_JAXB_CONTEXT", new Object[] { ctx, contextClasses });
    }
    setContext(ctx);
    for (ServiceInfo serviceInfo : service.getServiceInfos()) {
        SchemaCollection col = serviceInfo.getXmlSchemaCollection();
        if (col.getXmlSchemas().length > 1) {
            // someone has already filled in the types
            justCheckForJAXBAnnotations(serviceInfo);
            continue;
        }
        boolean schemasFromCache = false;
        Collection<DOMSource> schemas = getSchemas();
        if (schemas == null || schemas.isEmpty()) {
            schemas = cachedContextAndSchemas.getSchemas();
            if (schemas != null) {
                schemasFromCache = true;
            }
        } else {
            schemasFromCache = true;
        }
        Set<DOMSource> bi = new LinkedHashSet<>();
        if (schemas == null) {
            schemas = new LinkedHashSet<>();
            try {
                for (DOMResult r : generateJaxbSchemas()) {
                    DOMSource src = new DOMSource(r.getNode(), r.getSystemId());
                    if (isInBuiltInSchemas(r)) {
                        bi.add(src);
                    } else {
                        schemas.add(src);
                    }
                }
                // put any builtins at the end.   Anything that DOES import them
                // will cause it to load automatically and we'll skip them later
                schemas.addAll(bi);
            } catch (IOException e) {
                throw new ServiceConstructionException("SCHEMA_GEN_EXC", LOG, e);
            }
        }
        for (DOMSource r : schemas) {
            if (bi.contains(r)) {
                String ns = ((Document) r.getNode()).getDocumentElement().getAttribute("targetNamespace");
                if (serviceInfo.getSchema(ns) != null) {
                    continue;
                }
            }
            // StaxUtils.print(r.getNode());
            // System.out.println();
            addSchemaDocument(serviceInfo, col, (Document) r.getNode(), r.getSystemId());
        }
        JAXBSchemaInitializer schemaInit = new JAXBSchemaInitializer(serviceInfo, col, context, this.qualifiedSchemas, tns);
        schemaInit.walk();
        if (cachedContextAndSchemas != null && !schemasFromCache) {
            cachedContextAndSchemas.setSchemas(schemas);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) IOException(java.io.IOException) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) CachedContextAndSchemas(org.apache.cxf.common.jaxb.JAXBContextCache.CachedContextAndSchemas)

Example 18 with ServiceConstructionException

use of org.apache.cxf.service.factory.ServiceConstructionException in project cxf by apache.

the class TypeClassInitializer method begin.

@Override
public void begin(MessagePartInfo part) {
    OperationInfo op = part.getMessageInfo().getOperation();
    if (!isFault && !allowWrapperOperations && op.isUnwrappedCapable() && !op.isUnwrapped()) {
        return;
    }
    QName name;
    if (part.isElement()) {
        name = part.getElementQName();
    } else {
        name = part.getTypeQName();
    }
    Mapping mapping = model.get(name);
    // String clsName = null;
    JType jType = null;
    if (mapping != null) {
        jType = mapping.getType().getTypeClass();
    }
    if (jType == null) {
        TypeAndAnnotation typeAndAnnotation = model.getJavaType(part.getTypeQName());
        if (typeAndAnnotation != null) {
            jType = typeAndAnnotation.getTypeClass();
        }
    }
    if (jType == null && part.isElement() && part.getXmlSchema() instanceof XmlSchemaElement && ((XmlSchemaElement) part.getXmlSchema()).getSchemaTypeName() == null) {
        // anonymous inner thing.....
        UnwrappedOperationInfo oInfo = (UnwrappedOperationInfo) op;
        op = oInfo.getWrappedOperation();
        if (part.getMessageInfo() == oInfo.getInput()) {
            mapping = model.get(op.getInput().getFirstMessagePart().getElementQName());
        } else {
            mapping = model.get(op.getOutput().getFirstMessagePart().getElementQName());
        }
        if (mapping != null) {
            jType = mapping.getType().getTypeClass();
            try {
                Iterator<JType> i = jType.classes();
                while (i.hasNext()) {
                    JType jt = i.next();
                    if (jt.name().equalsIgnoreCase(part.getElementQName().getLocalPart())) {
                        jType = jt;
                    }
                }
            } catch (Throwable t) {
            // ignore, JType is a type that doesn't have a classes method
            }
        }
    }
    if (jType == null) {
        throw new ServiceConstructionException(new Message("NO_JAXB_CLASSMapping", LOG, name));
    }
    Class<?> cls;
    try {
        int arrayCount = 0;
        JType rootType = jType;
        while (rootType.isArray()) {
            rootType = rootType.elementType();
            arrayCount++;
        }
        if (arrayCount == 0 && part.isElement() && part.getXmlSchema() instanceof XmlSchemaElement && ((XmlSchemaElement) part.getXmlSchema()).getMaxOccurs() > 1) {
            arrayCount = 1;
        }
        cls = getClassByName(rootType);
        // an array object on the way.
        if (arrayCount > 0) {
            int[] dimensions = new int[arrayCount];
            while (arrayCount > 0) {
                arrayCount--;
                dimensions[arrayCount] = 0;
            }
            Object emptyArray = Array.newInstance(cls, dimensions);
            cls = emptyArray.getClass();
        }
    } catch (ClassNotFoundException e) {
        throw new ServiceConstructionException(e);
    }
    part.setTypeClass(cls);
    if (isFault) {
        // need to create an Exception class for this
        try {
            part.getMessageInfo().setProperty(Class.class.getName(), createFaultClass(cls));
        } catch (Throwable t) {
        // ignore - probably no asm
        }
    }
    super.begin(part);
}
Also used : UnwrappedOperationInfo(org.apache.cxf.service.model.UnwrappedOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) UnwrappedOperationInfo(org.apache.cxf.service.model.UnwrappedOperationInfo) Message(org.apache.cxf.common.i18n.Message) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Mapping(org.apache.cxf.common.jaxb.JAXBUtils.Mapping) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) TypeAndAnnotation(org.apache.cxf.common.jaxb.JAXBUtils.TypeAndAnnotation) JType(org.apache.cxf.common.jaxb.JAXBUtils.JType)

Example 19 with ServiceConstructionException

use of org.apache.cxf.service.factory.ServiceConstructionException in project cxf by apache.

the class JAXRSServerFactoryBean method create.

/**
 * Creates the JAX-RS Server instance
 * @return the server
 */
public Server create() {
    ClassLoaderHolder origLoader = null;
    try {
        Bus bus = getBus();
        ClassLoader loader = bus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        serviceFactory.setBus(bus);
        checkResources(true);
        if (serviceFactory.getService() == null) {
            serviceFactory.create();
        }
        Endpoint ep = createEndpoint();
        getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server);
        server = new ServerImpl(getBus(), ep, getDestinationFactory(), getBindingFactory());
        Invoker invoker = serviceFactory.getInvoker();
        if (invoker == null) {
            ep.getService().setInvoker(createInvoker());
        } else {
            ep.getService().setInvoker(invoker);
        }
        ServerProviderFactory factory = setupFactory(ep);
        ep.put(Application.class.getName(), appProvider);
        factory.setRequestPreprocessor(new RequestPreprocessor(languageMappings, extensionMappings));
        ep.put(Bus.class.getName(), getBus());
        if (documentLocation != null) {
            ep.put(JAXRSUtils.DOC_LOCATION, documentLocation);
        }
        if (rc != null) {
            ep.put("org.apache.cxf.jaxrs.comparator", rc);
        }
        checkPrivateEndpoint(ep);
        applyBusFeatures(getBus());
        applyFeatures();
        updateClassResourceProviders(ep);
        injectContexts(factory, (ApplicationInfo) ep.get(Application.class.getName()));
        factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
        getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED, server, null, null);
        if (start) {
            try {
                server.start();
            } catch (RuntimeException re) {
                if (!(re instanceof ServiceConstructionException && re.getMessage().startsWith("There is an endpoint already running on"))) {
                    // avoid destroying another server on the same endpoint url
                    // prevent resource leak if server really started by itself
                    server.destroy();
                }
                throw re;
            }
        }
    } catch (Exception e) {
        throw new ServiceConstructionException(e);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
    }
    return server;
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Bus(org.apache.cxf.Bus) RequestPreprocessor(org.apache.cxf.jaxrs.impl.RequestPreprocessor) Endpoint(org.apache.cxf.endpoint.Endpoint) ServerImpl(org.apache.cxf.endpoint.ServerImpl) Invoker(org.apache.cxf.service.invoker.Invoker) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) Application(javax.ws.rs.core.Application) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException)

Example 20 with ServiceConstructionException

use of org.apache.cxf.service.factory.ServiceConstructionException in project cxf by apache.

the class JaxRsWebClientConfiguration method setJaxrsResources.

@Override
protected void setJaxrsResources(JAXRSClientFactoryBean factory) {
    factory.setServiceClass(WebClient.class);
    if (!StringUtils.isEmpty(scanPackages)) {
        try {
            final Map<Class<? extends Annotation>, Collection<Class<?>>> classes = ClasspathScanner.findClasses(scanPackages, Provider.class);
            factory.setProviders(JAXRSClientFactoryBeanDefinitionParser.getProviders(context, classes.get(Provider.class)));
        } catch (Exception ex) {
            throw new ServiceConstructionException(ex);
        }
    }
}
Also used : Collection(java.util.Collection) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) Annotation(java.lang.annotation.Annotation) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException)

Aggregations

ServiceConstructionException (org.apache.cxf.service.factory.ServiceConstructionException)32 Message (org.apache.cxf.common.i18n.Message)10 Endpoint (org.apache.cxf.endpoint.Endpoint)8 Method (java.lang.reflect.Method)7 QName (javax.xml.namespace.QName)7 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)7 BusException (org.apache.cxf.BusException)5 Annotation (java.lang.annotation.Annotation)4 Collection (java.util.Collection)4 Bus (org.apache.cxf.Bus)4 EndpointException (org.apache.cxf.endpoint.EndpointException)4 IOException (java.io.IOException)3 Application (javax.ws.rs.core.Application)3 Provider (javax.ws.rs.ext.Provider)3 JAXBContext (javax.xml.bind.JAXBContext)3 JAXBException (javax.xml.bind.JAXBException)3 ClassLoaderHolder (org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)3 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)3 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)3 MalformedURLException (java.net.MalformedURLException)2