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;
}
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);
}
}
}
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);
}
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;
}
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);
}
}
}
Aggregations