private Object getManagedObjectForProcessor(CamelContext context, Processor processor, Route route) { // a bit of magic here as the processors we want to manage have already been registered // in the wrapped processors map when Camel have instrumented the route on route initialization // so the idea is now to only manage the processors from the map KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor> holder = wrappedProcessors.get(processor); if (holder == null) { // skip as its not an well known processor we want to manage anyway, such as Channel/UnitOfWork/Pipeline etc. return null; } // get the managed object as it can be a specialized type such as a Delayer/Throttler etc. Object managedObject = getManagementObjectStrategy().getManagedObjectForProcessor(context, processor, holder.getKey(), route); // only manage if we have a name for it as otherwise we do not want to manage it anyway if (managedObject != null) { // is it a performance counter then we need to set our counter if (managedObject instanceof PerformanceCounter) { InstrumentationProcessor counter = holder.getValue(); if (counter != null) { // change counter to us counter.setCounter(managedObject); } } } return managedObject; }
/** * Removes the wrapped processors for the given routes, as they are no longer in use. * <p/> * This is needed to avoid accumulating memory, if a lot of routes is being added and removed. * * @param routes the routes */ private void removeWrappedProcessorsForRoutes(Collection<Route> routes) { // loop the routes, and remove the route associated wrapped processors, as they are no longer in use for (Route route : routes) { String id = route.getId(); Iterator<KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor>> it = wrappedProcessors.values().iterator(); while (it.hasNext()) { KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor> holder = it.next(); RouteDefinition def = ProcessorDefinitionHelper.getRoute(holder.getKey()); if (def != null && id.equals(def.getId())) { it.remove(); } } } }
public Processor wrapProcessorInInterceptors(CamelContext context, ProcessorDefinition<?> definition, Processor target, Processor nextTarget) throws Exception { // do not double wrap it if (target instanceof InstrumentationProcessor) { return target; } // only wrap a performance counter if we have it registered in JMX by the jmx agent PerformanceCounter counter = registeredCounters.get(definition); if (counter != null) { InstrumentationProcessor wrapper = new InstrumentationProcessor(counter); wrapper.setProcessor(target); wrapper.setType(definition.getShortName()); // add it to the mapping of wrappers so we can later change it to a decorated counter // that when we register the processor KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor> holder = new KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor>(definition, wrapper); wrappedProcessors.put(target, holder); return wrapper; } return target; }
@Override protected void addServicesOnStartup(final Map<String, KeyValueHolder<Object, Dictionary>> services) { final String cacheDir = System.getProperty("project.build.directory", "target") + "/ldcache-" + randomAlphabetic(5); final LDCachingBackend backend; try { backend = new LDCachingFileBackend(new File(cacheDir)); backend.initialize(); } catch (final RepositoryException ex) { throw new RuntimeException("Could not initialize LDCache backend at " + cacheDir, ex); } services.put(LDCachingBackend.class.getName(), asService(backend, "osgi.jndi.service.name", "fcrepo/LDCacheBackend")); }
/** * Creates a holder for the given service, which make it easier to use {@link #addServicesOnStartup(java.util.Map)} */ protected KeyValueHolder<Object, Dictionary> asService(Object service, String key, String value) { Properties prop = new Properties(); if (key != null && value != null) { prop.put(key, value); } return new KeyValueHolder<Object, Dictionary>(service, prop); }
@Override @SuppressWarnings("rawtypes") protected void addServicesOnStartup(List<KeyValueHolder<String, KeyValueHolder<Object, Dictionary>>> services) { Dictionary<String, String> dict1 = new Hashtable<String, String>(); dict1.put("component", "fakeservice1"); Dictionary<String, String> dict2 = new Hashtable<String, String>(); dict2.put("component", "fakeservice2"); services.add(asKeyValueService(ComponentResolver.class.getName(), mockComponentOne, dict1)); services.add(asKeyValueService(ComponentResolver.class.getName(), mockComponentTwo, dict2)); }
/** * @return Messages from input folder according to the search and sort criteria stored in the endpoint * @throws MessagingException If message retrieval fails */ private List<KeyValueHolder<String, Message>> retrieveMessages() throws MessagingException { List<KeyValueHolder<String, Message>> answer = new ArrayList<>(); Message[] messages; final SortTerm[] sortTerm = getEndpoint().getSortTerm(); final SearchTerm searchTerm = computeSearchTerm(); if (sortTerm != null && serverCanSort) { final IMAPFolder imapFolder = (IMAPFolder) folder; if (searchTerm != null) { // Sort and search using server capability messages = imapFolder.getSortedMessages(sortTerm, searchTerm); } else { // Only sort using server capability messages = imapFolder.getSortedMessages(sortTerm); } } else { if (searchTerm != null) { messages = folder.search(searchTerm, retrieveAllMessages()); } else { messages = retrieveAllMessages(); } // Now we can sort (emulate email sort but restrict sort terms) if (sortTerm != null) { MailSorter.sortMessages(messages, sortTerm); } } for (Message message : messages) { String key = getEndpoint().getMailUidGenerator().generateUuid(getEndpoint(), message); if (isValidMessage(key, message)) { answer.add(new KeyValueHolder<>(key, message)); } } return answer; }
@Override protected void addServicesOnStartup(final Map<String, KeyValueHolder<Object, Dictionary>> services) { final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616"); final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080"); final ActiveMQComponent component = new ActiveMQComponent(); component.setBrokerURL("tcp://localhost:" + jmsPort); component.setExposeAllQueues(true); final FcrepoComponent fcrepo = new FcrepoComponent(); fcrepo.setBaseUrl("http://localhost:" + webPort + "/fcrepo/rest"); services.put("broker", asService(component, "osgi.jndi.service.name", "fcrepo/Broker")); services.put("fcrepo", asService(fcrepo, "osgi.jndi.service.name", "fcrepo/Camel")); }
@Override protected void addServicesOnStartup(final Map<String, KeyValueHolder<Object, Dictionary>> services) { final String fcrepoPort = System.getProperty("fcrepo.dynamic.test.port", "8080"); final FcrepoComponent component = new FcrepoComponent(); component.setBaseUrl("http://localhost:" + fcrepoPort + "/fcrepo/rest"); services.put("fcrepo", asService(component, "osgi.jndi.service.name", "fcrepo/Camel")); }
@Override protected void addServicesOnStartup(final Map<String, KeyValueHolder<Object, Dictionary>> services) { final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616"); final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080"); final ActiveMQComponent amq = new ActiveMQComponent(); amq.setBrokerURL("tcp://localhost:" + jmsPort); amq.setExposeAllQueues(true); final FcrepoComponent fcrepo = new FcrepoComponent(); fcrepo.setBaseUrl("http://localhost:" + webPort + "/fcrepo/rest"); services.put("broker", asService(amq, "osgi.jndi.service.name", "fcrepo/Broker")); services.put("fcrepo", asService(fcrepo, "osgi.jndi.service.name", "fcrepo/Camel")); }
@Override protected void addServicesOnStartup(final Map<String, KeyValueHolder<Object, Dictionary>> services) { final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616"); final ActiveMQComponent component = new ActiveMQComponent(); component.setBrokerURL("tcp://localhost:" + jmsPort); component.setExposeAllQueues(true); services.put("broker", asService(component, "osgi.jndi.service.name", "fcrepo/Broker")); }
@Override protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { MBeanServer mBeanServer = Mockito.mock(MBeanServer.class); ConnectionFactory amqPooledConnectionFactory = Mockito.mock(ConnectionFactory.class); services.put(MBeanServer.class.getCanonicalName(), asService(mBeanServer, null)); services.put(ConnectionFactory.class.getCanonicalName(), asService(amqPooledConnectionFactory, null)); }
@SuppressWarnings("rawtypes") @Override protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { MBeanServer mBeanServer = Mockito.mock(MBeanServer.class); try { Mockito.when(mBeanServer.registerMBean(Mockito.anyObject(), Mockito.any(ObjectName.class))); Mockito.when(mBeanServer.unregisterMBean(Mockito.any(ObjectName.class))); } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException | InstanceNotFoundException e) { e.printStackTrace(); } services.put(MBeanServer.class.getCanonicalName(), asService(mBeanServer, null)); }
@SuppressWarnings("rawtypes") @Override protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { PlatformTransactionManager platformTransactionManager = Mockito.mock(PlatformTransactionManager.class); EntityManagerFactory entityManagerFactory = Mockito.mock(EntityManagerFactory.class); DataSource dataSource = Mockito.mock(DataSource.class); provideMockMethods(platformTransactionManager); provideMockMethods(entityManagerFactory); provideMockMethods(dataSource); services.put(PlatformTransactionManager.class.getCanonicalName(), asService(platformTransactionManager, null)); services.put(EntityManagerFactory.class.getCanonicalName(), asService(entityManagerFactory, "osgi.unit.name", "playground-persistence")); services.put(DataSource.class.getCanonicalName(), asService(dataSource, "osgi.jndi.service.name", "jdbc/mysqlBasicManagedDataSource")); }
@SuppressWarnings("rawtypes") @Override protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { ConnectionFactory amqPooledConnectionFactory = Mockito.mock(ConnectionFactory.class); services.put(ConnectionFactory.class.getCanonicalName(), asService(amqPooledConnectionFactory, null)); }
@SuppressWarnings("rawtypes") @Override protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { MBeanServer mBeanServer = Mockito.mock(MBeanServer.class); services.put(MBeanServer.class.getCanonicalName(), asService(mBeanServer, null)); }
public InstrumentationInterceptStrategy(Map<ProcessorDefinition<?>, PerformanceCounter> registeredCounters, Map<Processor, KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor>> wrappedProcessors) { this.registeredCounters = registeredCounters; this.wrappedProcessors = wrappedProcessors; }
@Override protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { ComponentResolver testResolver = new DefaultComponentResolver(); services.put(ComponentResolver.class.getName(), asService(testResolver, "component", "mllp")); }
/** * Creates a holder for the given service, which make it easier to use {@link #addServicesOnStartup(java.util.Map)} */ protected KeyValueHolder<Object, Dictionary> asService(Object service, Dictionary dict) { return new KeyValueHolder<Object, Dictionary>(service, dict); }
/** * Creates a holder for the given service, which make it easier to use {@link #addServicesOnStartup(java.util.List)} */ protected KeyValueHolder<String, KeyValueHolder<Object, Dictionary>> asKeyValueService(String name, Object service, Dictionary dict) { return new KeyValueHolder<String, KeyValueHolder<Object, Dictionary>>(name, new KeyValueHolder<Object, Dictionary>(service, dict)); }
@Override protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { services.put("myService", asService(myService, "beer", "Carlsberg")); }
protected Queue<Exchange> createExchanges(List<KeyValueHolder<String, Message>> messages) throws MessagingException { Queue<Exchange> answer = new LinkedList<Exchange>(); int fetchSize = getEndpoint().getConfiguration().getFetchSize(); int count = fetchSize == -1 ? messages.size() : Math.min(fetchSize, messages.size()); if (LOG.isDebugEnabled()) { LOG.debug("Fetching {} messages. Total {} messages.", count, messages.size()); } for (int i = 0; i < count; i++) { KeyValueHolder<String, Message> holder = messages.get(i); String key = holder.getKey(); Message message = holder.getValue(); if (LOG.isTraceEnabled()) { LOG.trace("Mail #{} is of type: {} - {}", new Object[]{i, ObjectHelper.classCanonicalName(message), message}); } if (!message.getFlags().contains(Flags.Flag.DELETED)) { Exchange exchange = getEndpoint().createExchange(message); if (getEndpoint().getConfiguration().isMapMailMessage()) { // ensure the mail message is mapped, which can be ensured by touching the body/header/attachment LOG.trace("Mapping #{} from javax.mail.Message to Camel MailMessage", i); exchange.getIn().getBody(); exchange.getIn().getHeaders(); exchange.getIn().getAttachments(); } // If the protocol is POP3 we need to remember the uid on the exchange // so we can find the mail message again later to be able to delete it // we also need to remember the UUID for idempotent repository exchange.setProperty(MAIL_MESSAGE_UID, key); answer.add(exchange); } else { if (LOG.isDebugEnabled()) { LOG.debug("Skipping message as it was flagged as deleted: {}", MailUtils.dumpMessage(message)); } } } return answer; }
@Override protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { // create our fake mock service which will send the message to "mock:audit" MockAuditService mock = new MockAuditService(); services.put(AuditService.class.getName(), asService(mock, null)); }
@Override protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { ConnectionFactory amqPooledConnectionFactory = Mockito.mock(ConnectionFactory.class); services.put(ConnectionFactory.class.getCanonicalName(), asService(amqPooledConnectionFactory, null)); }
@Override protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { MBeanServer mBeanServer = Mockito.mock(MBeanServer.class); services.put(MBeanServer.class.getCanonicalName(), asService(mBeanServer, null)); }
@Override protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { TransactionManager transactionManager = Mockito.mock(TransactionManager.class); services.put(TransactionManager.class.getCanonicalName(), asService(transactionManager, null)); }
/** * Override this method to add services to be registered on startup. * <p/> * You can use the builder methods {@link #asService(Object, java.util.Dictionary)}, {@link #asService(Object, String, String)} * to make it easy to add the services to the map. */ protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { // noop }
/** * Override this method to add services to be registered on startup. * <p/> * You can use the builder methods {@link #asKeyValueService(String, Object, Dictionary)} * to make it easy to add the services to the List. */ protected void addServicesOnStartup(List<KeyValueHolder<String, KeyValueHolder<Object, Dictionary>>> services) { // noop }