@SuppressWarnings("Convert2streamapi") private void unregisterExecutorComponent(Executor... executors) { for (Executor executor : executors) { //unregister beans Registry registry = context.getRegistry(); if (registry instanceof PropertyPlaceholderDelegateRegistry) { registry = ((PropertyPlaceholderDelegateRegistry) registry).getRegistry(); } SimpleRegistry openexRegistry = (SimpleRegistry) registry; Set<Map.Entry<String, Object>> beansEntries = executor.beans().entrySet(); for (Map.Entry<String, Object> beansEntry : beansEntries) { if (openexRegistry.containsKey(beansEntry.getKey())) { openexRegistry.remove(beansEntry.getKey()); } } //unregister components Set<String> keys = executor.components().keySet(); for (String key : keys) { if (context.getComponentNames().contains(key)) { context.removeComponent(key); } } } }
@SuppressWarnings("Convert2streamapi") private void registerExecutorComponent(Executor... executors) { for (Executor executor : executors) { //register beans Registry registry = context.getRegistry(); if (registry instanceof PropertyPlaceholderDelegateRegistry) { registry = ((PropertyPlaceholderDelegateRegistry) registry).getRegistry(); } SimpleRegistry openexRegistry = (SimpleRegistry) registry; Set<Map.Entry<String, Object>> beansEntries = executor.beans().entrySet(); for (Map.Entry<String, Object> beansEntry : beansEntries) { if (!openexRegistry.containsKey(beansEntry.getKey())) { openexRegistry.put(beansEntry.getKey(), beansEntry.getValue()); } } //register components Set<Map.Entry<String, org.apache.camel.Component>> components = executor.components().entrySet(); for (Map.Entry<String, org.apache.camel.Component> entry : components) { if (!context.getComponentNames().contains(entry.getKey())) { context.addComponent(entry.getKey(), entry.getValue()); } } } }
@Override protected RouteBuilder createRouteBuilder() throws Exception { // we have to do it here, because we need the context created first CatalogManager.getStaticManager().setIgnoreMissingProperties(true); CatalogResolver catalogResolver = new CatalogResolver(true); URL catalogUrl = ResourceHelper.resolveMandatoryResourceAsUrl(context.getClassResolver(), "org/apache/camel/component/validator/catalog.cat"); catalogResolver.getCatalog().parseCatalog(catalogUrl); LSResourceResolver resourceResolver = new CatalogLSResourceResolver(catalogResolver); JndiRegistry registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry(); registry.bind("resourceResolver", resourceResolver); return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .doTry() .to("validator:org/apache/camel/component/validator/report.xsd?resourceResolver=#resourceResolver") .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid") .doFinally() .to("mock:finally") .end(); } }; }
@Test public void createEndpointWithMinimalConfigurationAndProvidedClient() throws Exception { AmazonSDBClientMock mock = new AmazonSDBClientMock(); ((JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry()) .bind("amazonSDBClient", mock); SdbComponent component = new SdbComponent(context); SdbEndpoint endpoint = (SdbEndpoint) component.createEndpoint("aws-sdb://TestDomain?" + "amazonSDBClient=#amazonSDBClient"); assertEquals("TestDomain", endpoint.getConfiguration().getDomainName()); assertNull(endpoint.getConfiguration().getAccessKey()); assertNull(endpoint.getConfiguration().getSecretKey()); assertSame(mock, endpoint.getConfiguration().getAmazonSDBClient()); assertEquals(SdbOperations.PutAttributes, endpoint.getConfiguration().getOperation()); assertNull(endpoint.getConfiguration().getAmazonSdbEndpoint()); assertFalse(endpoint.getConfiguration().isConsistentRead()); assertNull(endpoint.getConfiguration().getMaxNumberOfDomains()); }
@Test public void createEndpointWithMinimalConfigurationAndProvidedClient() throws Exception { AmazonSQSClientMock mock = new AmazonSQSClientMock(); ((JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry()).bind("amazonSQSClient", mock); SqsComponent component = new SqsComponent(context); SqsEndpoint endpoint = (SqsEndpoint) component.createEndpoint("aws-sqs://MyQueue?amazonSQSClient=#amazonSQSClient"); assertEquals("MyQueue", endpoint.getConfiguration().getQueueName()); assertNull(endpoint.getConfiguration().getAccessKey()); assertNull(endpoint.getConfiguration().getSecretKey()); assertSame(mock, endpoint.getConfiguration().getAmazonSQSClient()); assertNull(endpoint.getConfiguration().getAttributeNames()); assertNull(endpoint.getConfiguration().getMessageAttributeNames()); assertNull(endpoint.getConfiguration().getDefaultVisibilityTimeout()); assertNull(endpoint.getConfiguration().getVisibilityTimeout()); assertNull(endpoint.getConfiguration().getAmazonSQSEndpoint()); assertNull(endpoint.getConfiguration().getMaximumMessageSize()); assertNull(endpoint.getConfiguration().getMessageRetentionPeriod()); assertNull(endpoint.getConfiguration().getPolicy()); assertNull(endpoint.getConfiguration().getRedrivePolicy()); assertNull(endpoint.getConfiguration().getRegion()); }
@Test public void createEndpointWithMinimalConfigurationAndProvidedClient() throws Exception { AmazonSNSClientMock mock = new AmazonSNSClientMock(); ((JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry()).bind("amazonSNSClient", mock); SnsComponent component = new SnsComponent(context); SnsEndpoint endpoint = (SnsEndpoint) component.createEndpoint("aws-sns://MyTopic?amazonSNSClient=#amazonSNSClient&amazonSNSEndpoint=sns.ap-southeast-2.amazonaws.com"); assertEquals("MyTopic", endpoint.getConfiguration().getTopicName()); assertNull(endpoint.getConfiguration().getAccessKey()); assertNull(endpoint.getConfiguration().getSecretKey()); assertNull(endpoint.getConfiguration().getTopicArn()); assertNull(endpoint.getConfiguration().getSubject()); assertNotNull(endpoint.getConfiguration().getAmazonSNSEndpoint()); assertNull(endpoint.getConfiguration().getPolicy()); endpoint.start(); assertEquals("arn:aws:sns:us-east-1:541925086079:MyTopic", endpoint.getConfiguration().getTopicArn()); // check the setting of AmazonSNSEndpoint assertEquals("sns.ap-southeast-2.amazonaws.com", mock.getEndpoint()); endpoint.stop(); }
@Test public void createEndpointWithMinimalConfigurationAndProvidedClient() throws Exception { AmazonS3ClientMock mock = new AmazonS3ClientMock(); ((JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry()).bind("amazonS3Client", mock); S3Component component = new S3Component(context); S3Endpoint endpoint = (S3Endpoint) component.createEndpoint("aws-s3://MyBucket?amazonS3Client=#amazonS3Client"); assertEquals("MyBucket", endpoint.getConfiguration().getBucketName()); assertNull(endpoint.getConfiguration().getAccessKey()); assertNull(endpoint.getConfiguration().getSecretKey()); assertSame(mock, endpoint.getConfiguration().getAmazonS3Client()); assertNull(endpoint.getConfiguration().getRegion()); assertTrue(endpoint.getConfiguration().isDeleteAfterRead()); assertEquals(10, endpoint.getMaxMessagesPerPoll()); assertNull(endpoint.getConfiguration().getAmazonS3Endpoint()); assertNull(endpoint.getConfiguration().getPolicy()); assertNull(endpoint.getConfiguration().getPrefix()); assertTrue(endpoint.getConfiguration().isIncludeBody()); }
public static void registerBean(Registry registry, String beanName, Object bean) { if (registry instanceof SimpleRegistry) { ((SimpleRegistry) registry).put(beanName, bean); } else if (registry instanceof PropertyPlaceholderDelegateRegistry) { Registry wrappedRegistry = ((PropertyPlaceholderDelegateRegistry) registry).getRegistry(); registerBean(wrappedRegistry, beanName, bean); } else if (registry instanceof JndiRegistry) { ((JndiRegistry) registry).bind(beanName, bean); } else { throw new RuntimeException("could not identify the registry type while registering core beans"); } }
public void testRef() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("Hello World"); PropertyPlaceholderDelegateRegistry delegate = (PropertyPlaceholderDelegateRegistry) context.getRegistry(); JndiRegistry jndi = (JndiRegistry) delegate.getRegistry(); bindToRegistry(jndi); template.sendBody("ref:foo", "Hello World"); assertMockEndpointsSatisfied(); }
protected CamelContext createCamelContext() throws Exception { CamelContext camelContext = super.createCamelContext(); JndiRegistry registry = (JndiRegistry)((PropertyPlaceholderDelegateRegistry)camelContext.getRegistry()).getRegistry(); registry.bind("amqpConnection", discoverAMQP(camelContext)); camelContext.addComponent("amqp-customized", amqpComponent("amqp://localhost:" + amqpPort)); return camelContext; }
public static void bindCamelRegistryToContext(final CamelContext context) { if (ds == null) { try { Class.forName(driver); } catch (final ClassNotFoundException e) { throw new IllegalStateException("Failed to register driver.", e); } ds = createDataSource(); } ((JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry()).bind( CamelRegistrySupportApi.DATASOURCE, ds); }
@Test public void testReadWithReadSuffix() throws Exception { if (!canTest()) { return; } String[] beforeFiles = new File("target/test").list(); int before = beforeFiles != null ? beforeFiles.length : 0; final Path file = new Path(new File("target/test/test-camel-boolean").getAbsolutePath()); Configuration conf = new Configuration(); SequenceFile.Writer writer = createWriter(conf, file, NullWritable.class, BooleanWritable.class); NullWritable keyWritable = NullWritable.get(); BooleanWritable valueWritable = new BooleanWritable(); valueWritable.set(true); writer.append(keyWritable, valueWritable); writer.sync(); writer.close(); context.addRoutes(new RouteBuilder() { public void configure() { from("hdfs2:localhost/" + file.getParent().toUri() + "?scheduler=#myScheduler&pattern=*&fileSystemType=LOCAL&fileType=SEQUENCE_FILE&initialDelay=0&readSuffix=handled") .to("mock:result"); } }); ScheduledExecutorService pool = context.getExecutorServiceManager().newScheduledThreadPool(null, "unitTestPool", 1); DefaultScheduledPollConsumerScheduler scheduler = new DefaultScheduledPollConsumerScheduler(pool); ((JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry()).bind("myScheduler", scheduler); context.start(); MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class); resultEndpoint.expectedMessageCount(1); resultEndpoint.assertIsSatisfied(); // synchronize on pool that was used to run hdfs consumer thread scheduler.getScheduledExecutorService().shutdown(); scheduler.getScheduledExecutorService().awaitTermination(5000, TimeUnit.MILLISECONDS); Set<String> files = new HashSet<String>(Arrays.asList(new File("target/test").list())); // there may be some leftover files before, so test that we only added 2 new files assertThat(files.size() - before, equalTo(2)); assertTrue(files.remove("test-camel-boolean.handled")); assertTrue(files.remove(".test-camel-boolean.handled.crc")); }
@Test public void testReadWithReadSuffix() throws Exception { if (!canTest()) { return; } String[] beforeFiles = new File("target/test").list(); int before = beforeFiles != null ? beforeFiles.length : 0; final Path file = new Path(new File("target/test/test-camel-boolean").getAbsolutePath()); Configuration conf = new Configuration(); FileSystem fs1 = FileSystem.get(file.toUri(), conf); SequenceFile.Writer writer = createWriter(fs1, conf, file, NullWritable.class, BooleanWritable.class); NullWritable keyWritable = NullWritable.get(); BooleanWritable valueWritable = new BooleanWritable(); valueWritable.set(true); writer.append(keyWritable, valueWritable); writer.sync(); writer.close(); context.addRoutes(new RouteBuilder() { public void configure() { from("hdfs:localhost/" + file.getParent().toUri() + "?scheduler=#myScheduler&pattern=*&fileSystemType=LOCAL&fileType=SEQUENCE_FILE&initialDelay=0&readSuffix=handled") .to("mock:result"); } }); ScheduledExecutorService pool = context.getExecutorServiceManager().newScheduledThreadPool(null, "unitTestPool", 1); DefaultScheduledPollConsumerScheduler scheduler = new DefaultScheduledPollConsumerScheduler(pool); ((JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry()).bind("myScheduler", scheduler); context.start(); MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class); resultEndpoint.expectedMessageCount(1); resultEndpoint.assertIsSatisfied(); // synchronize on pool that was used to run hdfs consumer thread scheduler.getScheduledExecutorService().shutdown(); scheduler.getScheduledExecutorService().awaitTermination(5000, TimeUnit.MILLISECONDS); Set<String> files = new HashSet<String>(Arrays.asList(new File("target/test").list())); // there may be some leftover files before, so test that we only added 2 new files assertThat(files.size() - before, equalTo(2)); assertTrue(files.remove("test-camel-boolean.handled")); assertTrue(files.remove(".test-camel-boolean.handled.crc")); }
@Before public void setup() { PropertyPlaceholderDelegateRegistry registry = (PropertyPlaceholderDelegateRegistry)context.getRegistry(); JndiRegistry reg = (JndiRegistry)registry.getRegistry(); reg.bind("defaultFactoryRegistry", new DefaultFactoryRegistry()); }
public void testInjectedStrategy() throws Exception { CamelContext context = createCamelContext(); PropertyPlaceholderDelegateRegistry delegate = (PropertyPlaceholderDelegateRegistry) context.getRegistry(); assertTrue(delegate.getRegistry() instanceof JndiRegistry); }