Java Code Examples for org.apache.cxf.common.classloader.ClassLoaderUtils#loadClass()

The following examples show how to use org.apache.cxf.common.classloader.ClassLoaderUtils#loadClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ASMHelper.java    From cxf with Apache License 2.0 6 votes vote down vote up
public Label createLabel() {
    try {
        final Class<?> cls = ClassLoaderUtils.loadClass(cwClass.getPackage().getName() + ".Label",
                                                  cwClass);
        @SuppressWarnings("unused")
        Label l = new Label() {
            Object l = cls.newInstance();
            public Object getValue() {
                return l;
            }
            public Class<?> realType() {
                return cls;
            }
        };
        return l;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: ProviderFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected static Object createProvider(String className, Bus bus) {

        try {
            Class<?> cls = ClassLoaderUtils.loadClass(className, ProviderFactory.class);
            for (Constructor<?> c : cls.getConstructors()) {
                if (c.getParameterTypes().length == 1 && c.getParameterTypes()[0] == Bus.class) {
                    return c.newInstance(bus);
                }
            }
            return cls.newInstance();
        } catch (Throwable ex) {
            String message = "Problem with creating the default provider " + className;
            if (ex.getMessage() != null) {
                message += ": " + ex.getMessage();
            } else {
                message += ", exception class : " + ex.getClass().getName();
            }
            LOG.fine(message);
        }
        return null;
    }
 
Example 3
Source File: ResourceUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static Class<?> loadClass(String cName) {
    try {
        return ClassLoaderUtils.loadClass(cName.trim(), ResourceUtils.class);
    } catch (ClassNotFoundException ex) {
        throw new RuntimeException("No class " + cName.trim() + " can be found", ex);
    }
}
 
Example 4
Source File: OpenApiParseUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static void setJavaType(Parameter userParam, String typeName) {
    Class<?> javaType = OPENAPI_TYPE_MAP.get(typeName);
    if (javaType == null) {
        try {
            // May work if the model has already been compiled
            // TODO: need to know the package name
            javaType = ClassLoaderUtils.loadClass(typeName, OpenApiParseUtils.class); 
        } catch (Throwable t) {
            // ignore
        }
    }

    userParam.setJavaType(javaType);
}
 
Example 5
Source File: NioMessageBodyWriter.java    From cxf with Apache License 2.0 5 votes vote down vote up
public NioMessageBodyWriter() {
    try {
        ClassLoaderUtils.loadClass("javax.servlet.WriteListener", HttpServletRequest.class);
        is31 = true;
    } catch (Throwable t) {
        is31 = false;
    }

}
 
Example 6
Source File: WebFaultOutInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Object createFaultInfoBean(WebFault fault, Throwable cause) {
    if (!StringUtils.isEmpty(fault.faultBean())) {
        try {
            Class<?> cls = ClassLoaderUtils.loadClass(fault.faultBean(),
                                                      cause.getClass());
            if (cls != null) {
                Object ret = cls.newInstance();
                //copy props
                Method[] meth = cause.getClass().getMethods();
                for (Method m : meth) {
                    if (m.getParameterTypes().length == 0
                        && (m.getName().startsWith("get")
                        || m.getName().startsWith("is"))) {
                        try {
                            String name;
                            if (m.getName().startsWith("get")) {
                                name = "set" + m.getName().substring(3);
                            } else {
                                name = "set" + m.getName().substring(2);
                            }
                            Method m2 = cls.getMethod(name, m.getReturnType());
                            m2.invoke(ret, m.invoke(cause));
                        } catch (Exception e) {
                            //ignore
                        }
                    }
                }
                return ret;
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e1) {
            //ignore
        }
    }

    LOG.fine("Using @WebFault annotated class "
             + cause.getClass().getName()
             + " as faultInfo since getFaultInfo() was not found");
    return cause;
}
 
Example 7
Source File: JaxWsWebServicePublisherBeanPostProcessor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public JaxWsWebServicePublisherBeanPostProcessor() throws SecurityException,
       NoSuchMethodException, ClassNotFoundException {
    try {
        servletClass = ClassLoaderUtils.loadClass(CXF_SERVLET_CLASS_NAME, getClass());
    } catch (ClassNotFoundException e) {
        Message message = new Message("SERVLET_CLASS_MISSING", LOG, CXF_SERVLET_CLASS_NAME);
        LOG.severe(message.toString());
        throw e;
    }
    servletGetBusMethod = servletClass.getMethod("getBus");
}
 
Example 8
Source File: RestClientBean.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Class<? extends Annotation> readScope() {
    // first check to see if the value is set
    String configuredScope = ConfigFacade.getOptionalValue(REST_SCOPE_FORMAT, clientInterface, String.class)
                                         .orElse(null);
    if (configuredScope != null) {
        try {
            return ClassLoaderUtils.loadClass(configuredScope, getClass(), Annotation.class);
        } catch (Exception e) {
            throw new IllegalArgumentException("The scope " + configuredScope + " is invalid", e);
        }
    }

    List<Annotation> possibleScopes = new ArrayList<>();
    Annotation[] annotations = clientInterface.getDeclaredAnnotations();
    for (Annotation annotation : annotations) {
        if (beanManager.isScope(annotation.annotationType())) {
            possibleScopes.add(annotation);
        }
    }
    if (possibleScopes.isEmpty()) {
        return Dependent.class;
    } else if (possibleScopes.size() == 1) {
        return possibleScopes.get(0).annotationType();
    } else {
        throw new IllegalArgumentException("The client interface " + clientInterface
                + " has multiple scopes defined " + possibleScopes);
    }
}
 
Example 9
Source File: PluginLoader.java    From cxf with Apache License 2.0 5 votes vote down vote up
private FrontEndProfile loadFrontEndProfile(String fullClzName) {
    FrontEndProfile profile = null;
    try {
        Class<?> clz = ClassLoaderUtils.loadClass(fullClzName, this.getClass());
        profile = (FrontEndProfile)clz.newInstance();
    } catch (Exception e) {
        Message msg = new Message("FRONTEND_PROFILE_LOAD_FAIL", LOG, fullClzName);
        LOG.log(Level.SEVERE, msg.toString());
        throw new ToolException(msg, e);
    }
    return profile;
}
 
Example 10
Source File: CXFNonSpringJaxrsServlet.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Class<?> loadClass(String cName, String classType) throws ServletException {
    try {

        Class<?> cls = null;
        if (classLoader == null) {
            cls = ClassLoaderUtils.loadClass(cName, CXFNonSpringJaxrsServlet.class);
        } else {
            cls = classLoader.loadClass(cName);
        }
        return cls;
    } catch (ClassNotFoundException ex) {
        throw new ServletException("No " + classType + " class " + cName.trim() + " can be found", ex);
    }
}
 
Example 11
Source File: SecureAnnotationsInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void setAnnotationClassName(String name) {
    try {
        ClassLoaderUtils.loadClass(name, SecureAnnotationsInterceptor.class);
        annotationClassName = name;
    } catch (Throwable ex) {
        ex.printStackTrace();
        throw new IllegalArgumentException("Annotation class " + name + " is not available");
    }
}
 
Example 12
Source File: CXFNonSpringJaxrsServlet.java    From JaxRSProviders with Apache License 2.0 5 votes vote down vote up
protected Class<?> loadClass(String cName, String classType) throws ServletException {
    try {

        Class<?> cls = null;
        if (classLoader == null) {
            cls = ClassLoaderUtils.loadClass(cName, CXFNonSpringJaxrsServlet.class);
        } else {
            cls = classLoader.loadClass(cName);
        }
        return cls;
    } catch (ClassNotFoundException ex) {
        throw new ServletException("No " + classType + " class " + cName.trim() + " can be found", ex);
    }
}
 
Example 13
Source File: AbstractSTSClient.java    From steady with Apache License 2.0 5 votes vote down vote up
protected CallbackHandler createHandler() {
    Object o = getProperty(SecurityConstants.CALLBACK_HANDLER);
    if (o instanceof String) {
        try {
            Class<?> cls = ClassLoaderUtils.loadClass((String)o, this.getClass());
            o = cls.newInstance();
        } catch (Exception e) {
            throw new Fault(e);
        }
    }
    return (CallbackHandler)o;
}
 
Example 14
Source File: SpringClasspathScanner.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Class<?> loadClass(String className, ClassLoader loader)
    throws ClassNotFoundException {
    if (loader == null) {
        return ClassLoaderUtils.loadClass(className, getClass());
    }
    return loader.loadClass(className);
}
 
Example 15
Source File: SearchContextImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
private <T> SearchConditionParser<T> getParser(Class<T> cls,
                                               Map<String, String> beanProperties,
                                               Map<String, String> parserProperties) {

    Object parserProp = message.getContextualProperty(CUSTOM_SEARCH_PARSER_PROPERTY);
    if (parserProp != null) {
        return getCustomParser(parserProp);
    }

    Map<String, String> props = null;
    if (parserProperties == null) {
        props = new LinkedHashMap<>(4);
        props.put(SearchUtils.DATE_FORMAT_PROPERTY,
                  (String)message.getContextualProperty(SearchUtils.DATE_FORMAT_PROPERTY));
        props.put(SearchUtils.TIMEZONE_SUPPORT_PROPERTY,
                  (String)message.getContextualProperty(SearchUtils.TIMEZONE_SUPPORT_PROPERTY));
        props.put(SearchUtils.LAX_PROPERTY_MATCH,
                  (String)message.getContextualProperty(SearchUtils.LAX_PROPERTY_MATCH));
        props.put(SearchUtils.DECODE_QUERY_VALUES,
                  (String)message.getContextualProperty(SearchUtils.DECODE_QUERY_VALUES));
        // FIQL specific
        props.put(FiqlParser.SUPPORT_SINGLE_EQUALS,
                  (String)message.getContextualProperty(FiqlParser.SUPPORT_SINGLE_EQUALS));
    } else {
        props = parserProperties;
    }

    Map<String, String> beanProps = null;

    if (beanProperties == null) {
        beanProps = CastUtils.cast((Map<?, ?>)message.getContextualProperty(SearchUtils.BEAN_PROPERTY_MAP));
    } else {
        beanProps = beanProperties;
    }

    String parserClassProp = (String) message.getContextualProperty(CUSTOM_SEARCH_PARSER_CLASS_PROPERTY);
    if (parserClassProp != null) {
        try {
            final Class<?> parserClass = ClassLoaderUtils.loadClass(parserClassProp, SearchContextImpl.class);
            final Constructor<?> constructor = parserClass.getConstructor(Class.class, Map.class, Map.class);
            @SuppressWarnings("unchecked")
            SearchConditionParser<T> customParser =
                (SearchConditionParser<T>)constructor.newInstance(cls, props, beanProps);
            return customParser;
        } catch (Exception ex) {
            throw new SearchParseException(ex);
        }
    }
    return new FiqlParser<T>(cls, props, beanProps);
}
 
Example 16
Source File: ResourceUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static void getAllTypesForResource(ClassResourceInfo resource,
                                           ResourceTypes types,
                                           boolean jaxbOnly,
                                           MessageBodyWriter<?> jaxbWriter) {
    Class<?> jaxbElement = null;
    try {
        jaxbElement = ClassLoaderUtils.loadClass("javax.xml.bind.JAXBElement", ResourceUtils.class);
    } catch (final ClassNotFoundException e) {
        // no-op
    }

    for (OperationResourceInfo ori : resource.getMethodDispatcher().getOperationResourceInfos()) {
        Method method = ori.getAnnotatedMethod() == null ? ori.getMethodToInvoke() : ori.getAnnotatedMethod();
        Class<?> realReturnType = method.getReturnType();
        Class<?> cls = realReturnType;
        if (cls == Response.class || ori.isAsync()) {
            cls = getActualJaxbType(cls, method, false);
        }
        Type type = method.getGenericReturnType();
        if (jaxbOnly) {
            checkJaxbType(resource.getServiceClass(), cls, realReturnType == Response.class || ori.isAsync()
                ? cls : type, types, method.getAnnotations(), jaxbWriter, jaxbElement);
        } else {
            types.getAllTypes().put(cls, type);
        }

        for (Parameter pm : ori.getParameters()) {
            if (pm.getType() == ParameterType.REQUEST_BODY) {
                Class<?> inType = method.getParameterTypes()[pm.getIndex()];
                if (inType != AsyncResponse.class) {
                    Type paramType = method.getGenericParameterTypes()[pm.getIndex()];
                    if (jaxbOnly) {
                        checkJaxbType(resource.getServiceClass(), inType, paramType, types,
                                      method.getParameterAnnotations()[pm.getIndex()], jaxbWriter, jaxbElement);
                    } else {
                        types.getAllTypes().put(inType, paramType);
                    }
                }
            }
        }

    }

    for (ClassResourceInfo sub : resource.getSubResources()) {
        if (!isRecursiveSubResource(resource, sub)) {
            getAllTypesForResource(sub, types, jaxbOnly, jaxbWriter);
        }
    }
}
 
Example 17
Source File: AnnotationHandlerChainBuilder.java    From cxf with Apache License 2.0 4 votes vote down vote up
private HandlerChainAnnotation findHandlerChainAnnotation(Class<?> clz, boolean searchSEI) {
    if (clz == null) {
        return null;
    }
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Checking for HandlerChain annotation on " + clz.getName());
    }
    HandlerChainAnnotation hcAnn = null;
    HandlerChain ann = clz.getAnnotation(HandlerChain.class);
    if (ann == null) {
        if (searchSEI) {
            /* HandlerChain annotation can be specified on the SEI
             * but the implementation bean might not implement the SEI.
             */
            WebService ws = clz.getAnnotation(WebService.class);
            if (ws != null && !StringUtils.isEmpty(ws.endpointInterface())) {
                String seiClassName = ws.endpointInterface().trim();
                Class<?> seiClass = null;
                try {
                    seiClass = ClassLoaderUtils.loadClass(seiClassName, clz);
                } catch (ClassNotFoundException e) {
                    throw new WebServiceException(BUNDLE.getString("SEI_LOAD_FAILURE_EXC"), e);
                }

                // check SEI class and its interfaces for HandlerChain annotation
                hcAnn = findHandlerChainAnnotation(seiClass, false);
            }
        }
        if (hcAnn == null) {
            // check interfaces for HandlerChain annotation
            for (Class<?> iface : clz.getInterfaces()) {
                if (LOG.isLoggable(Level.FINE)) {
                    LOG.fine("Checking for HandlerChain annotation on " + iface.getName());
                }
                ann = iface.getAnnotation(HandlerChain.class);
                if (ann != null) {
                    hcAnn = new HandlerChainAnnotation(ann, iface);
                    break;
                }
            }
            if (hcAnn == null) {
                hcAnn = findHandlerChainAnnotation(clz.getSuperclass(), false);
            }
        }
    } else {
        hcAnn = new HandlerChainAnnotation(ann, clz);
    }

    return hcAnn;
}
 
Example 18
Source File: JaxWsServiceConfiguration.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public Class<?> getResponseWrapper(Method selected) {
    if (this.responseMethodClassNotFoundCache.contains(selected)) {
        return null;
    }
    Class<?> cachedClass = responseMethodClassCache.get(selected);
    if (cachedClass != null) {
        return cachedClass;
    }

    Method m = getDeclaredMethod(selected);

    ResponseWrapper rw = m.getAnnotation(ResponseWrapper.class);
    String clsName = "";
    if (rw == null) {
        clsName = getPackageName(selected) + ".jaxws." + StringUtils.capitalize(selected.getName())
                  + "Response";
    } else {
        clsName = rw.className();
    }

    if (clsName.length() > 0) {
        cachedClass = responseMethodClassCache.get(clsName);
        if (cachedClass != null) {
            responseMethodClassCache.put(selected, cachedClass);
            return cachedClass;
        }
        try {
            Class<?> r = ClassLoaderUtils.loadClass(clsName, implInfo.getEndpointClass());
            responseMethodClassCache.put(clsName, r);
            responseMethodClassCache.put(selected, r);

            if (r.equals(m.getReturnType())) {
                LOG.log(Level.WARNING, "INVALID_RESPONSE_WRAPPER", new Object[] {clsName,
                        m.getReturnType().getName()});
            }

            return r;
        } catch (ClassNotFoundException e) {
            //do nothing, we will mock a schema for wrapper bean later on
        }
    }
    responseMethodClassNotFoundCache.add(selected);
    return null;
}
 
Example 19
Source File: JaxWsServiceConfiguration.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public Class<?> getRequestWrapper(Method selected) {
    if (this.requestMethodClassNotFoundCache.contains(selected)) {
        return null;
    }
    Class<?> cachedClass = requestMethodClassCache.get(selected);
    if (cachedClass != null) {
        return cachedClass;
    }

    Method m = getDeclaredMethod(selected);

    RequestWrapper rw = m.getAnnotation(RequestWrapper.class);
    String clsName = "";
    if (rw == null) {
        clsName = getPackageName(selected) + ".jaxws." + StringUtils.capitalize(selected.getName());
    } else {
        clsName = rw.className();
    }

    if (clsName.length() > 0) {
        cachedClass = requestMethodClassCache.get(clsName);
        if (cachedClass != null) {
            requestMethodClassCache.put(selected, cachedClass);
            return cachedClass;
        }
        try {
            Class<?> r = ClassLoaderUtils.loadClass(clsName, implInfo.getEndpointClass());
            requestMethodClassCache.put(clsName, r);
            requestMethodClassCache.put(selected, r);
            if (m.getParameterTypes().length == 1 && r.equals(m.getParameterTypes()[0])) {
                LOG.log(Level.WARNING, "INVALID_REQUEST_WRAPPER", new Object[] {clsName,
                        m.getParameterTypes()[0].getName()});
            }
            return r;
        } catch (ClassNotFoundException e) {
            //do nothing, we will mock a schema for wrapper bean later on
        }
    }
    requestMethodClassNotFoundCache.add(selected);
    return null;
}
 
Example 20
Source File: JaxbAssertionBuilder.java    From cxf with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a JAXBAssertionBuilder from the specified class name and schema type.
 * @param className the name of the class to which the schema type is mapped
 * @param qn the schema type
 * @throws JAXBException
 * @throws ClassNotFoundException
 */
@SuppressWarnings("unchecked")
public JaxbAssertionBuilder(String className, QName qn) throws JAXBException, ClassNotFoundException {
    this((Class<T>)ClassLoaderUtils.loadClass(className, JaxbAssertionBuilder.class), qn);
}