private ComponentProxyComponent resolveComponent(CamelContext context, Connector step) { ComponentProxyFactory factory = ComponentProxyComponent::new; if (step.getFactory() != null ) { final ClassResolver resolver = context.getClassResolver(); final Injector injector = context.getInjector(); Class<? extends ComponentProxyFactory> type = resolver.resolveClass(step.getFactory(), ComponentProxyFactory.class); if (type == null) { throw new IllegalArgumentException("Unable to resolve a ComponentProxyFactory of type: " + step.getFactory()); } factory = injector.newInstance(type); if (factory == null) { throw new IllegalArgumentException("Unable to instantiate a ComponentProxyFactory of type: " + step.getFactory()); } } return factory.newInstance(step.getComponentId(), step.getComponentScheme()); }
/** * Resolves the resource. * * @param classResolver the class resolver to load the resource from the classpath * @param uri uri of the resource * @return the resource as an {@link java.net.URL}. Or <tt>null</tt> if not found. * @throws java.net.MalformedURLException if the URI is malformed */ public static URL resolveResourceAsUrl(ClassResolver classResolver, String uri) throws MalformedURLException { if (uri.startsWith("file:")) { // check if file exists first String name = ObjectHelper.after(uri, "file:"); uri = tryDecodeUri(uri); LOG.trace("Loading resource: {} from file system", uri); File file = new File(name); if (!file.exists()) { return null; } return new URL(uri); } else if (uri.startsWith("http:")) { LOG.trace("Loading resource: {} from HTTP", uri); return new URL(uri); } else if (uri.startsWith("classpath:")) { uri = ObjectHelper.after(uri, "classpath:"); uri = tryDecodeUri(uri); } // load from classpath by default String resolvedName = resolveUriPath(uri); LOG.trace("Loading resource: {} from classpath", resolvedName); return classResolver.loadResourceAsURL(resolvedName); }
public String getEipParameterJsonSchema(String eipName) throws IOException { // the eip json schema may be in some of the sub-packages so look until we find it String[] subPackages = new String[]{"", "/config", "/dataformat", "/language", "/loadbalancer", "/rest"}; for (String sub : subPackages) { String path = CamelContextHelper.MODEL_DOCUMENTATION_PREFIX + sub + "/" + eipName + ".json"; ClassResolver resolver = getClassResolver(); InputStream inputStream = resolver.loadResourceAsStream(path); if (inputStream != null) { log.debug("Loading eip JSON Schema for: {} using class resolver: {} -> {}", new Object[]{eipName, resolver, inputStream}); try { return IOHelper.loadText(inputStream); } finally { IOHelper.close(inputStream); } } } return null; }
/** * Determines if the given parameter type is assignable to the expected type. * <p/> * This implementation will check if the given parameter type matches the expected type as class using either * <ul> * <li>FQN class name - com.foo.MyOrder</li> * <li>Simple class name - MyOrder</li> * </ul> * If the given parameter type is <b>not</b> a class, then <tt>null</tt> is returned * * @param resolver the class resolver * @param parameterType the parameter type as a String, can be a FQN or a simple name of the class * @param expectedType the expected type * @return <tt>null</tt> if parameter type is <b>not</b> a class, <tt>true</tt> if parameter type is assignable, <tt>false</tt> if not assignable */ public static Boolean isAssignableToExpectedType(ClassResolver resolver, String parameterType, Class<?> expectedType) { // if its a class, then it should be assignable Class<?> parameterClass = resolver.resolveClass(parameterType); if (parameterClass == null && parameterType.equals(expectedType.getSimpleName())) { // it was not the FQN class name, but the simple name instead, which matched return true; } // not a class so return null if (parameterClass == null) { return null; } // if there was a class, then it must be assignable to match return parameterClass.isAssignableFrom(expectedType); }
public HttpServerSharedPipelineFactory(NettySharedHttpServerBootstrapConfiguration configuration, HttpServerConsumerChannelFactory channelFactory, ClassResolver classResolver) { this.configuration = configuration; this.channelFactory = channelFactory; // fallback and use default resolver this.classResolver = classResolver != null ? classResolver : new DefaultClassResolver(); try { this.sslContext = createSSLContext(); } catch (Exception e) { throw ObjectHelper.wrapRuntimeCamelException(e); } if (sslContext != null) { LOG.info("Created SslContext {}", sslContext); } }
public HttpServerSharedInitializerFactory(NettySharedHttpServerBootstrapConfiguration configuration, HttpServerConsumerChannelFactory channelFactory, ClassResolver classResolver) { this.configuration = configuration; this.channelFactory = channelFactory; // fallback and use default resolver this.classResolver = classResolver != null ? classResolver : new DefaultClassResolver(); try { this.sslContext = createSSLContext(); } catch (Exception e) { throw ObjectHelper.wrapRuntimeCamelException(e); } if (sslContext != null) { LOG.info("Created SslContext {}", sslContext); } }
/** * Read the REST-DSL definition's and parse that as a Swagger model representation * * @param rests the rest-dsl * @param route optional route path to filter the rest-dsl to only include from the chose route * @param config the swagger configuration * @param classResolver class resolver to use * @return the swagger model */ public Swagger read(List<RestDefinition> rests, String route, BeanConfig config, String camelContextId, ClassResolver classResolver) { Swagger swagger = new Swagger(); for (RestDefinition rest : rests) { if (ObjectHelper.isNotEmpty(route) && !route.equals("/")) { // filter by route if (!rest.getPath().equals(route)) { continue; } } parse(swagger, rest, camelContextId, classResolver); } // configure before returning swagger = config.configure(swagger); return swagger; }
protected List<Class<? extends Throwable>> createExceptionClasses(ClassResolver resolver) throws ClassNotFoundException { List<String> list = getExceptions(); List<Class<? extends Throwable>> answer = new ArrayList<Class<? extends Throwable>>(list.size()); for (String name : list) { Class<? extends Throwable> type = resolver.resolveMandatoryClass(name, Throwable.class); answer.add(type); } return answer; }
/** * Resolves the resource. * <p/> * If possible recommended to use {@link #resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver, String)} * * @param classResolver the class resolver to load the resource from the classpath * @param uri URI of the resource * @return the resource as an {@link InputStream}. Remember to close this stream after usage. Or <tt>null</tt> if not found. * @throws java.io.IOException is thrown if error loading the resource */ public static InputStream resolveResourceAsInputStream(ClassResolver classResolver, String uri) throws IOException { if (uri.startsWith("file:")) { uri = ObjectHelper.after(uri, "file:"); uri = tryDecodeUri(uri); LOG.trace("Loading resource: {} from file system", uri); return new FileInputStream(uri); } else if (uri.startsWith("http:")) { URL url = new URL(uri); LOG.trace("Loading resource: {} from HTTP", uri); URLConnection con = url.openConnection(); con.setUseCaches(false); try { return con.getInputStream(); } catch (IOException e) { // close the http connection to avoid // leaking gaps in case of an exception if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).disconnect(); } throw e; } } else if (uri.startsWith("classpath:")) { uri = ObjectHelper.after(uri, "classpath:"); uri = tryDecodeUri(uri); } // load from classpath by default String resolvedName = resolveUriPath(uri); LOG.trace("Loading resource: {} from classpath", resolvedName); return classResolver.loadResourceAsStream(resolvedName); }
/** * Resolves the mandatory resource. * * @param classResolver the class resolver to load the resource from the classpath * @param uri uri of the resource * @return the resource as an {@link java.net.URL}. * @throws java.io.FileNotFoundException is thrown if the resource file could not be found * @throws java.net.MalformedURLException if the URI is malformed */ public static URL resolveMandatoryResourceAsUrl(ClassResolver classResolver, String uri) throws FileNotFoundException, MalformedURLException { URL url = resolveResourceAsUrl(classResolver, uri); if (url == null) { String resolvedName = resolveUriPath(uri); throw new FileNotFoundException("Cannot find resource: " + resolvedName + " in classpath for URI: " + uri); } else { return url; } }
/** * Finds all possible Components on the classpath, already registered in {@link org.apache.camel.CamelContext}, * and from the {@link org.apache.camel.spi.Registry}. */ public static SortedMap<String, Properties> findComponents(CamelContext camelContext) throws LoadPropertiesException { ClassResolver resolver = camelContext.getClassResolver(); LOG.debug("Finding all components using class resolver: {} -> {}", new Object[]{resolver}); Enumeration<URL> iter = resolver.loadAllResourcesAsURL(COMPONENT_DESCRIPTOR); return findComponents(camelContext, iter); }
public String getComponentDocumentation(String componentName) throws IOException { // use the component factory finder to find the package name of the component class, which is the location // where the documentation exists as well FactoryFinder finder = getFactoryFinder(DefaultComponentResolver.RESOURCE_PATH); try { Class<?> clazz = finder.findClass(componentName); if (clazz == null) { // fallback and find existing component Component existing = hasComponent(componentName); if (existing != null) { clazz = existing.getClass(); } else { return null; } } String packageName = clazz.getPackage().getName(); packageName = packageName.replace('.', '/'); String path = packageName + "/" + componentName + ".html"; ClassResolver resolver = getClassResolver(); InputStream inputStream = resolver.loadResourceAsStream(path); log.debug("Loading component documentation for: {} using class resolver: {} -> {}", new Object[]{componentName, resolver, inputStream}); if (inputStream != null) { try { return IOHelper.loadText(inputStream); } finally { IOHelper.close(inputStream); } } // special for ActiveMQ as it is really just JMS if ("ActiveMQComponent".equals(clazz.getSimpleName())) { return getComponentDocumentation("jms"); } else { return null; } } catch (ClassNotFoundException e) { return null; } }
public String getComponentParameterJsonSchema(String componentName) throws IOException { // use the component factory finder to find the package name of the component class, which is the location // where the documentation exists as well FactoryFinder finder = getFactoryFinder(DefaultComponentResolver.RESOURCE_PATH); try { Class<?> clazz = finder.findClass(componentName); if (clazz == null) { // fallback and find existing component Component existing = hasComponent(componentName); if (existing != null) { clazz = existing.getClass(); } else { return null; } } String packageName = clazz.getPackage().getName(); packageName = packageName.replace('.', '/'); String path = packageName + "/" + componentName + ".json"; ClassResolver resolver = getClassResolver(); InputStream inputStream = resolver.loadResourceAsStream(path); log.debug("Loading component JSON Schema for: {} using class resolver: {} -> {}", new Object[]{componentName, resolver, inputStream}); if (inputStream != null) { try { return IOHelper.loadText(inputStream); } finally { IOHelper.close(inputStream); } } // special for ActiveMQ as it is really just JMS if ("ActiveMQComponent".equals(clazz.getSimpleName())) { return getComponentParameterJsonSchema("jms"); } else { return null; } } catch (ClassNotFoundException e) { return null; } }
public String getDataFormatParameterJsonSchema(String dataFormatName) throws IOException { // use the dataformat factory finder to find the package name of the dataformat class, which is the location // where the documentation exists as well FactoryFinder finder = getFactoryFinder(DefaultDataFormatResolver.DATAFORMAT_RESOURCE_PATH); try { Class<?> clazz = finder.findClass(dataFormatName); if (clazz == null) { return null; } String packageName = clazz.getPackage().getName(); packageName = packageName.replace('.', '/'); String path = packageName + "/" + dataFormatName + ".json"; ClassResolver resolver = getClassResolver(); InputStream inputStream = resolver.loadResourceAsStream(path); log.debug("Loading dataformat JSON Schema for: {} using class resolver: {} -> {}", new Object[]{dataFormatName, resolver, inputStream}); if (inputStream != null) { try { return IOHelper.loadText(inputStream); } finally { IOHelper.close(inputStream); } } return null; } catch (ClassNotFoundException e) { return null; } }
public String getLanguageParameterJsonSchema(String languageName) throws IOException { // use the language factory finder to find the package name of the language class, which is the location // where the documentation exists as well FactoryFinder finder = getFactoryFinder(DefaultLanguageResolver.LANGUAGE_RESOURCE_PATH); try { Class<?> clazz = finder.findClass(languageName); if (clazz == null) { return null; } String packageName = clazz.getPackage().getName(); packageName = packageName.replace('.', '/'); String path = packageName + "/" + languageName + ".json"; ClassResolver resolver = getClassResolver(); InputStream inputStream = resolver.loadResourceAsStream(path); log.debug("Loading language JSON Schema for: {} using class resolver: {} -> {}", new Object[]{languageName, resolver, inputStream}); if (inputStream != null) { try { return IOHelper.loadText(inputStream); } finally { IOHelper.close(inputStream); } } return null; } catch (ClassNotFoundException e) { return null; } }
public XsltUriResolver(ClassResolver resolver, String location) { this.resolver = resolver; this.location = location; if (ResourceHelper.hasScheme(location)) { baseScheme = ResourceHelper.getScheme(location); } else { // default to use classpath baseScheme = "classpath:"; } }
protected FTPClient createFtpClient() throws Exception { FTPClient client = new FTPClient(); // If we're in an OSGI environment, set the parser factory to // OsgiParserFactory, because commons-net uses Class.forName in their // default ParserFactory if (isOsgi()) { ClassResolver cr = getCamelContext().getClassResolver(); OsgiParserFactory opf = new OsgiParserFactory(cr); client.setParserFactory(opf); } return client; }
public SSLContext createSSLContext(ClassResolver classResolver, String keyStoreFormat, String securityProvider, String keyStoreResource, String trustStoreResource, char[] passphrase) throws Exception { SSLContext answer; KeyStore ks = KeyStore.getInstance(keyStoreFormat); InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(classResolver, keyStoreResource); try { ks.load(is, passphrase); } finally { IOHelper.close(is); } KeyManagerFactory kmf = KeyManagerFactory.getInstance(securityProvider); kmf.init(ks, passphrase); answer = SSLContext.getInstance(SSL_PROTOCOL); if (trustStoreResource != null) { KeyStore ts = KeyStore.getInstance(keyStoreFormat); is = ResourceHelper.resolveMandatoryResourceAsInputStream(classResolver, trustStoreResource); try { ts.load(is, passphrase); } finally { IOHelper.close(is); } TrustManagerFactory tmf = TrustManagerFactory.getInstance(securityProvider); tmf.init(ts); answer.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); } else { answer.init(kmf.getKeyManagers(), null, null); } return answer; }
@Override protected XStream createXStream(ClassResolver resolver, ClassLoader classLoader) { XStream xs = super.createXStream(resolver, classLoader); if (getMode() != null) { xs.setMode(getModeFromString(getMode())); } else { xs.setMode(XStream.NO_REFERENCES); } return xs; }
/** * A factory method which takes a collection of types to be annotated */ public static XStreamDataFormat processAnnotations(ClassResolver resolver, Iterable<Class<?>> types) { XStreamDataFormat answer = new XStreamDataFormat(); XStream xstream = answer.getXStream(resolver); for (Class<?> type : types) { xstream.processAnnotations(type); } return answer; }
/** * A factory method which takes a number of types to be annotated */ public static XStreamDataFormat processAnnotations(ClassResolver resolver, Class<?>... types) { XStreamDataFormat answer = new XStreamDataFormat(); XStream xstream = answer.getXStream(resolver); for (Class<?> type : types) { xstream.processAnnotations(type); } return answer; }
protected static URL loadMappingFile(ClassResolver classResolver, String mappingFile) { URL url = null; try { url = ResourceHelper.resolveResourceAsUrl(classResolver, mappingFile); } catch (MalformedURLException e) { // ignore } if (url == null) { // using the classloader of DozerClassLoader as a fallback url = DozerClassLoader.class.getClassLoader().getResource(mappingFile); } return url; }
public ResourceHelperKeyPairProvider(String[] resources, PasswordFinder passwordFinder, ClassResolver classResolver) { this.classResolver = classResolver; this.resources = resources; this.passwordFinder = passwordFinder; }
public String getRegisteredType(String className, ClassResolver resolver) throws Exception { synchronized (registeredTypes) { for (final Map.Entry<String, TypeSupportImpl> entry : registeredTypes.entrySet()) { if (entry.getValue().getClass().getName().equals(className)) { return entry.getKey(); } } // if not found register it Class<?> type = resolver.resolveClass(className); if (type == null) { throw new CamelException("Cannot resolve class " + className); } Method getInstanceMethod = null; Method getTypeNameMethod = null; Method registerTypeMethod = null; try { getInstanceMethod = type.getMethod("get_instance"); getTypeNameMethod = type.getMethod("get_type_name"); registerTypeMethod = type.getMethod("register_type", DomainParticipant.class, String.class); } catch (Exception e) { throw new CamelException("Invalid TypeSupport class " + className); } TypeSupportImpl inst = (TypeSupportImpl)getInstanceMethod.invoke(null); String typeName = (String)getTypeNameMethod.invoke(null); if (typeName != null && inst != null) { registerTypeMethod.invoke(null, getValue(), typeName); registeredTypes.put(typeName, inst); } return typeName; } }
/** * Find information about all the EIPs from camel-core. */ public static SortedMap<String, Properties> findEips(CamelContext camelContext) throws LoadPropertiesException { SortedMap<String, Properties> answer = new TreeMap<String, Properties>(); ClassResolver resolver = camelContext.getClassResolver(); LOG.debug("Finding all EIPs using class resolver: {} -> {}", new Object[]{resolver}); URL url = resolver.loadResourceAsURL(MODEL_DESCRIPTOR); if (url != null) { InputStream is = null; try { is = url.openStream(); String all = IOHelper.loadText(is); String[] lines = all.split("\n"); for (String line : lines) { if (line.startsWith("#")) { continue; } Properties prop = new Properties(); prop.put("name", line); String description = null; String label = null; String javaType = null; String title = null; // enrich with more meta-data String json = camelContext.explainEipJson(line, false); if (json != null) { List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("model", json, false); for (Map<String, String> row : rows) { if (row.get("title") != null) { title = row.get("title"); } if (row.get("description") != null) { description = row.get("description"); } if (row.get("label") != null) { label = row.get("label"); } if (row.get("javaType") != null) { javaType = row.get("javaType"); } } } if (title != null) { prop.put("title", title); } if (description != null) { prop.put("description", description); } if (label != null) { prop.put("label", label); } if (javaType != null) { prop.put("class", javaType); } answer.put(line, prop); } } catch (IOException e) { throw new LoadPropertiesException(url, e); } finally { IOHelper.close(is); } } return answer; }
public DefaultFactoryFinder(ClassResolver classResolver, String resourcePath) { this.classResolver = classResolver; this.path = resourcePath; }
public ClassResolver getClassResolver() { return classResolver; }
public void setClassResolver(ClassResolver classResolver) { this.classResolver = classResolver; }
public FactoryFinder resolveDefaultFactoryFinder(ClassResolver classResolver) { return resolveFactoryFinder(classResolver, "META-INF/services/org/apache/camel/"); }
public FactoryFinder resolveFactoryFinder(ClassResolver classResolver, String resourcePath) { return new DefaultFactoryFinder(classResolver, resourcePath); }
CustomXsltUriResolver(ClassResolver resolver, String location) { super(resolver, location); }
@Override public void init(ExtendedProperties configuration) { super.init(configuration); resolver = (ClassResolver) this.rsvc.getProperty("CamelClassResolver"); ObjectHelper.notNull(resolver, "ClassResolver"); }
public OsgiParserFactory(ClassResolver ocr) { this.ocr = ocr; }
private void setupCustomServices() { ModelJAXBContextFactory modelJAXBContextFactory = getBeanForType(ModelJAXBContextFactory.class); if (modelJAXBContextFactory != null) { LOG.info("Using custom ModelJAXBContextFactory: {}", modelJAXBContextFactory); getContext().setModelJAXBContextFactory(modelJAXBContextFactory); } ClassResolver classResolver = getBeanForType(ClassResolver.class); if (classResolver != null) { LOG.info("Using custom ClassResolver: {}", classResolver); getContext().setClassResolver(classResolver); } FactoryFinderResolver factoryFinderResolver = getBeanForType(FactoryFinderResolver.class); if (factoryFinderResolver != null) { LOG.info("Using custom FactoryFinderResolver: {}", factoryFinderResolver); getContext().setFactoryFinderResolver(factoryFinderResolver); } ExecutorServiceManager executorServiceStrategy = getBeanForType(ExecutorServiceManager.class); if (executorServiceStrategy != null) { LOG.info("Using custom ExecutorServiceStrategy: {}", executorServiceStrategy); getContext().setExecutorServiceManager(executorServiceStrategy); } ThreadPoolFactory threadPoolFactory = getBeanForType(ThreadPoolFactory.class); if (threadPoolFactory != null) { LOG.info("Using custom ThreadPoolFactory: {}", threadPoolFactory); getContext().getExecutorServiceManager().setThreadPoolFactory(threadPoolFactory); } ProcessorFactory processorFactory = getBeanForType(ProcessorFactory.class); if (processorFactory != null) { LOG.info("Using custom ProcessorFactory: {}", processorFactory); getContext().setProcessorFactory(processorFactory); } Debugger debugger = getBeanForType(Debugger.class); if (debugger != null) { LOG.info("Using custom Debugger: {}", debugger); getContext().setDebugger(debugger); } UuidGenerator uuidGenerator = getBeanForType(UuidGenerator.class); if (uuidGenerator != null) { LOG.info("Using custom UuidGenerator: {}", uuidGenerator); getContext().setUuidGenerator(uuidGenerator); } NodeIdFactory nodeIdFactory = getBeanForType(NodeIdFactory.class); if (nodeIdFactory != null) { LOG.info("Using custom NodeIdFactory: {}", nodeIdFactory); getContext().setNodeIdFactory(nodeIdFactory); } StreamCachingStrategy streamCachingStrategy = getBeanForType(StreamCachingStrategy.class); if (streamCachingStrategy != null) { LOG.info("Using custom StreamCachingStrategy: {}", streamCachingStrategy); getContext().setStreamCachingStrategy(streamCachingStrategy); } MessageHistoryFactory messageHistoryFactory = getBeanForType(MessageHistoryFactory.class); if (messageHistoryFactory != null) { LOG.info("Using custom MessageHistoryFactory: {}", messageHistoryFactory); getContext().setMessageHistoryFactory(messageHistoryFactory); } }
protected static InputStreamReader createScriptReader(ClassResolver classResolver, String resource) throws IOException { InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(classResolver, resource); return new InputStreamReader(is); }
public JettyClassPathResource(ClassResolver resolver, String path) { ObjectHelper.notNull(resolver, "ClassResolver"); ObjectHelper.notNull(path, "path"); this.resolver = resolver; this.path = path; }