Search in sources :

Example 6 with ApplicationInfo

use of org.apache.cxf.jaxrs.model.ApplicationInfo in project cxf by apache.

the class CXFNonSpringJaxrsServlet method createSingletonInstance.

protected Object createSingletonInstance(Class<?> cls, Map<String, List<String>> props, ServletConfig sc) throws ServletException {
    Constructor<?> c = ResourceUtils.findResourceConstructor(cls, false);
    if (c == null) {
        throw new ServletException("No valid constructor found for " + cls.getName());
    }
    boolean isApplication = Application.class.isAssignableFrom(c.getDeclaringClass());
    try {
        ProviderInfo<? extends Object> provider = null;
        if (c.getParameterTypes().length == 0) {
            if (isApplication) {
                provider = new ApplicationInfo((Application) c.newInstance(), getBus());
            } else {
                provider = new ProviderInfo<Object>(c.newInstance(), getBus(), false, true);
            }
        } else {
            Map<Class<?>, Object> values = new HashMap<>();
            values.put(ServletContext.class, sc.getServletContext());
            values.put(ServletConfig.class, sc);
            provider = ProviderFactory.createProviderFromConstructor(c, values, getBus(), isApplication, true);
        }
        Object instance = provider.getProvider();
        injectProperties(instance, props);
        configureSingleton(instance);
        return isApplication ? provider : instance;
    } catch (InstantiationException ex) {
        ex.printStackTrace();
        throw new ServletException("Resource class " + cls.getName() + " can not be instantiated");
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
        throw new ServletException("Resource class " + cls.getName() + " can not be instantiated due to IllegalAccessException");
    } catch (InvocationTargetException ex) {
        ex.printStackTrace();
        throw new ServletException("Resource class " + cls.getName() + " can not be instantiated due to InvocationTargetException");
    }
}
Also used : HashMap(java.util.HashMap) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletException(javax.servlet.ServletException) Application(javax.ws.rs.core.Application)

Aggregations

ApplicationInfo (org.apache.cxf.jaxrs.model.ApplicationInfo)6 Application (javax.ws.rs.core.Application)5 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 ServletException (javax.servlet.ServletException)2 BeanConfig (io.swagger.jaxrs.config.BeanConfig)1 ApiListingResource (io.swagger.jaxrs.listing.ApiListingResource)1 Swagger (io.swagger.models.Swagger)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1 ServletContext (javax.servlet.ServletContext)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1