@Lazy
 @Bean(name = "sql-stored-component")
 @ConditionalOnClass(CamelContext.class)
 @ConditionalOnMissingBean(SqlStoredComponent.class)
 public SqlStoredComponent configureSqlStoredComponent(
     CamelContext camelContext, SqlStoredComponentConfiguration configuration) throws Exception {
   SqlStoredComponent component = new SqlStoredComponent();
   component.setCamelContext(camelContext);
   Map<String, Object> parameters = new HashMap<>();
   IntrospectionSupport.getProperties(configuration, parameters, null, false);
   for (Map.Entry<String, Object> entry : parameters.entrySet()) {
     Object value = entry.getValue();
     Class<?> paramClass = value.getClass();
     if (paramClass.getName().endsWith("NestedConfiguration")) {
       Class nestedClass = null;
       try {
         nestedClass = (Class) paramClass.getDeclaredField("CAMEL_NESTED_CLASS").get(null);
         HashMap<String, Object> nestedParameters = new HashMap<>();
         IntrospectionSupport.getProperties(value, nestedParameters, null, false);
         Object nestedProperty = nestedClass.newInstance();
         IntrospectionSupport.setProperties(
             camelContext, camelContext.getTypeConverter(), nestedProperty, nestedParameters);
         entry.setValue(nestedProperty);
       } catch (NoSuchFieldException e) {
       }
     }
   }
   IntrospectionSupport.setProperties(
       camelContext, camelContext.getTypeConverter(), component, parameters);
   return component;
 }
Exemple #2
0
 /**
  * Create the {@link Message}
  *
  * @return jmsMessage or null if the mapping was not successfully
  */
 protected Message createJmsMessageForType(
     Exchange exchange,
     Object body,
     Map<String, Object> headers,
     Session session,
     CamelContext context,
     JmsMessageType type)
     throws JMSException {
   switch (type) {
     case Text:
       {
         TextMessage message = session.createTextMessage();
         if (body != null) {
           String payload = context.getTypeConverter().convertTo(String.class, exchange, body);
           message.setText(payload);
         }
         return message;
       }
     case Bytes:
       {
         BytesMessage message = session.createBytesMessage();
         if (body != null) {
           byte[] payload = context.getTypeConverter().convertTo(byte[].class, exchange, body);
           message.writeBytes(payload);
         }
         return message;
       }
     case Map:
       {
         MapMessage message = session.createMapMessage();
         if (body != null) {
           Map<?, ?> payload = context.getTypeConverter().convertTo(Map.class, exchange, body);
           populateMapMessage(message, payload, context);
         }
         return message;
       }
     case Object:
       ObjectMessage message = session.createObjectMessage();
       if (body != null) {
         try {
           Serializable payload =
               context.getTypeConverter().mandatoryConvertTo(Serializable.class, exchange, body);
           message.setObject(payload);
         } catch (NoTypeConversionAvailableException e) {
           // cannot convert to serializable then thrown an exception to avoid sending a null
           // message
           JMSException cause = new MessageFormatException(e.getMessage());
           cause.initCause(e);
           throw cause;
         }
       }
       return message;
     default:
       break;
   }
   return null;
 }
  public <T> T getIn(Class<T> type) {
    Message in = getIn();

    // eager same instance type test to avoid the overhead of invoking the type converter
    // if already same type
    if (type.isInstance(in)) {
      return type.cast(in);
    }

    // fallback to use type converter
    return context.getTypeConverter().convertTo(type, this, in);
  }
 @Bean(name = "xquery-component")
 @ConditionalOnClass(CamelContext.class)
 @ConditionalOnMissingBean(XQueryComponent.class)
 public XQueryComponent configureXQueryComponent(
     CamelContext camelContext, XQueryComponentConfiguration configuration) throws Exception {
   XQueryComponent component = new XQueryComponent();
   component.setCamelContext(camelContext);
   Map<String, Object> parameters = new HashMap<>();
   IntrospectionSupport.getProperties(configuration, parameters, null, false);
   IntrospectionSupport.setProperties(
       camelContext, camelContext.getTypeConverter(), component, parameters);
   return component;
 }
 @Bean
 @ConditionalOnClass(CamelContext.class)
 @ConditionalOnMissingBean(GsonDataFormat.class)
 public GsonDataFormat configureGsonDataFormat(
     CamelContext camelContext, GsonDataFormatConfiguration configuration) throws Exception {
   GsonDataFormat dataformat = new GsonDataFormat();
   if (dataformat instanceof CamelContextAware) {
     ((CamelContextAware) dataformat).setCamelContext(camelContext);
   }
   Map<String, Object> parameters = new HashMap<>();
   IntrospectionSupport.getProperties(configuration, parameters, null, false);
   IntrospectionSupport.setProperties(
       camelContext, camelContext.getTypeConverter(), dataformat, parameters);
   return dataformat;
 }
 @Bean(name = "jsonpath-language")
 @ConditionalOnClass(CamelContext.class)
 @ConditionalOnMissingBean(JsonPathLanguage.class)
 public JsonPathLanguage configureJsonPathLanguage(
     CamelContext camelContext, JsonPathLanguageConfiguration configuration) throws Exception {
   JsonPathLanguage language = new JsonPathLanguage();
   if (language instanceof CamelContextAware) {
     ((CamelContextAware) language).setCamelContext(camelContext);
   }
   Map<String, Object> parameters = new HashMap<>();
   IntrospectionSupport.getProperties(configuration, parameters, null, false);
   IntrospectionSupport.setProperties(
       camelContext, camelContext.getTypeConverter(), language, parameters);
   return language;
 }
  public void testLoadClasspathAsUrl() throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.start();

    URL url =
        ResourceHelper.resolveMandatoryResourceAsUrl(
            context.getClassResolver(), "classpath:log4j2.properties");
    assertNotNull(url);

    String text = context.getTypeConverter().convertTo(String.class, url);
    assertNotNull(text);
    assertTrue(text.contains("log4j"));

    context.stop();
  }
  public void testLoadClasspathDefault() throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.start();

    InputStream is =
        ResourceHelper.resolveMandatoryResourceAsInputStream(context, "log4j2.properties");
    assertNotNull(is);

    String text = context.getTypeConverter().convertTo(String.class, is);
    assertNotNull(text);
    assertTrue(text.contains("log4j"));
    is.close();

    context.stop();
  }
  public <T> T getOut(Class<T> type) {
    if (!hasOut()) {
      return null;
    }

    Message out = getOut();

    // eager same instance type test to avoid the overhead of invoking the type converter
    // if already same type
    if (type.isInstance(out)) {
      return type.cast(out);
    }

    // fallback to use type converter
    return context.getTypeConverter().convertTo(type, this, out);
  }
  public void testLoadRegistry() throws Exception {
    SimpleRegistry registry = new SimpleRegistry();
    registry.put("myBean", "This is a log4j logging configuation file");

    CamelContext context = new DefaultCamelContext(registry);
    context.start();

    InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, "ref:myBean");
    assertNotNull(is);

    String text = context.getTypeConverter().convertTo(String.class, is);
    assertNotNull(text);
    assertTrue(text.contains("log4j"));
    is.close();

    context.stop();
  }
 @Bean(name = "pgp-dataformat")
 @Scope("prototype")
 @ConditionalOnClass(CamelContext.class)
 @ConditionalOnMissingBean(PGPDataFormat.class)
 public PGPDataFormat configurePGPDataFormat(
     CamelContext camelContext, PGPDataFormatConfiguration configuration) throws Exception {
   PGPDataFormat dataformat = new PGPDataFormat();
   if (CamelContextAware.class.isAssignableFrom(PGPDataFormat.class)) {
     CamelContextAware contextAware = CamelContextAware.class.cast(dataformat);
     if (contextAware != null) {
       contextAware.setCamelContext(camelContext);
     }
   }
   Map<String, Object> parameters = new HashMap<>();
   IntrospectionSupport.getProperties(configuration, parameters, null, false);
   IntrospectionSupport.setProperties(
       camelContext, camelContext.getTypeConverter(), dataformat, parameters);
   return dataformat;
 }
 @Bean(name = "ognl-language")
 @Scope("prototype")
 @ConditionalOnClass(CamelContext.class)
 @ConditionalOnMissingBean(OgnlLanguage.class)
 public OgnlLanguage configureOgnlLanguage(
     CamelContext camelContext, OgnlLanguageConfiguration configuration) throws Exception {
   OgnlLanguage language = new OgnlLanguage();
   if (CamelContextAware.class.isAssignableFrom(OgnlLanguage.class)) {
     CamelContextAware contextAware = CamelContextAware.class.cast(language);
     if (contextAware != null) {
       contextAware.setCamelContext(camelContext);
     }
   }
   Map<String, Object> parameters = new HashMap<>();
   IntrospectionSupport.getProperties(configuration, parameters, null, false);
   IntrospectionSupport.setProperties(
       camelContext, camelContext.getTypeConverter(), language, parameters);
   return language;
 }
  public void testLoadFileWithSpace() throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.start();

    createDirectory("target/my space");
    FileUtil.copyFile(
        new File("src/test/resources/log4j2.properties"),
        new File("target/my space/log4j2.properties"));

    InputStream is =
        ResourceHelper.resolveMandatoryResourceAsInputStream(
            context, "file:target/my%20space/log4j2.properties");
    assertNotNull(is);

    String text = context.getTypeConverter().convertTo(String.class, is);
    assertNotNull(text);
    assertTrue(text.contains("log4j"));
    is.close();

    context.stop();
  }
Exemple #14
0
  protected Message createJmsMessage(
      Exchange exchange,
      Object body,
      Map<String, Object> headers,
      Session session,
      CamelContext context)
      throws JMSException {
    JmsMessageType type = null;

    // special for transferExchange
    if (endpoint != null && endpoint.isTransferExchange()) {
      LOG.trace("Option transferExchange=true so we use JmsMessageType: Object");
      Serializable holder = DefaultExchangeHolder.marshal(exchange);
      Message answer = session.createObjectMessage(holder);
      // ensure default delivery mode is used by default
      answer.setJMSDeliveryMode(Message.DEFAULT_DELIVERY_MODE);
      return answer;
    }

    // use a custom message converter
    if (endpoint != null && endpoint.getMessageConverter() != null) {
      if (LOG.isTraceEnabled()) {
        LOG.trace(
            "Creating JmsMessage using a custom MessageConverter: {} with body: {}",
            endpoint.getMessageConverter(),
            body);
      }
      return endpoint.getMessageConverter().toMessage(body, session);
    }

    // check if header have a type set, if so we force to use it
    if (headers.containsKey(JmsConstants.JMS_MESSAGE_TYPE)) {
      type =
          context
              .getTypeConverter()
              .convertTo(JmsMessageType.class, headers.get(JmsConstants.JMS_MESSAGE_TYPE));
    } else if (endpoint != null && endpoint.getConfiguration().getJmsMessageType() != null) {
      // force a specific type from the endpoint configuration
      type = endpoint.getConfiguration().getJmsMessageType();
    } else {
      type = getJMSMessageTypeForBody(exchange, body, headers, session, context);
    }

    // create the JmsMessage based on the type
    if (type != null) {
      if (body == null && (endpoint != null && !endpoint.getConfiguration().isAllowNullBody())) {
        throw new JMSException(
            "Cannot send message as message body is null, and option allowNullBody is false.");
      }
      LOG.trace("Using JmsMessageType: {}", type);
      Message answer = createJmsMessageForType(exchange, body, headers, session, context, type);
      // ensure default delivery mode is used by default
      answer.setJMSDeliveryMode(Message.DEFAULT_DELIVERY_MODE);
      return answer;
    }

    // check for null body
    if (body == null && (endpoint != null && !endpoint.getConfiguration().isAllowNullBody())) {
      throw new JMSException(
          "Cannot send message as message body is null, and option allowNullBody is false.");
    }

    // warn if the body could not be mapped
    if (body != null && LOG.isWarnEnabled()) {
      LOG.warn(
          "Cannot determine specific JmsMessage type to use from body class."
              + " Will use generic JmsMessage."
              + " Body class: "
              + ObjectHelper.classCanonicalName(body)
              + ". If you want to send a POJO then your class might need to implement java.io.Serializable"
              + ", or you can force a specific type by setting the jmsMessageType option on the JMS endpoint.");
    }

    // return a default message
    Message answer = session.createMessage();
    // ensure default delivery mode is used by default
    answer.setJMSDeliveryMode(Message.DEFAULT_DELIVERY_MODE);
    return answer;
  }