Thursday 30 June 2011

Camel producerTemplate in ruby

The aim is to send messagges to a queue with the camel producerTemplate. producerTemplate can be invoked from a Processor. This time I'm going to use a jruby processor.

First of all generate a camel project with:
mvn archetype:generate \\
-DarchetypeGroupId=org.apache.camel.archetypes \\ 
-DarchetypeArtifactId=camel-archetype-java \\
-DarchetypeVersion=2.7.0 -DgroupId=com.test -DartifactId=producer

Then modify pom.xml to import all dependencied:
<name>## Producer ##</name>
  <url>http://www.myorganization.org</url>
  <properties>
    <camel-version>2.7.0</camel-version>
    <log4j-version>1.2.16</log4j-version>
    <logback-version>0.9.24</logback-version>
    <activemq-version>5.5.0</activemq-version>
    <jruby-version>1.6.2</jruby-version> 
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>${camel-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring</artifactId>
      <version>${camel-version}</version>
    </dependency>
    
    <!-- logging -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.5.11</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.16</version> 
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>${logback-version}</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${logback-version}</version>
    </dependency>
    <dependency>
      <groupId>org.jruby</groupId>
      <artifactId>jruby-complete</artifactId>
      <version>${jruby-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jms</artifactId>
      <version>${camel-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-camel</artifactId>
      <version>${activemq-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-core</artifactId>
      <version>${activemq-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-script</artifactId>
      <version>${camel-version}</version>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
      <version>2.1_3</version>
    </dependency>
  </dependencies>

Define the resources needed in file src/main/resources/META-INF/spring/camel-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->

<!-- Configures the Camel Context-->

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/lang 
      http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
      http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
  

 <bean id="credentials" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name='location'>
      <value>credential.properties</value>
    </property>
  </bean>

<!--
<bean id="credentials" class="org.apache.camel.component.properties.PropertiesComponent">
  <property name="location" value="classpath:credential.properties" />
</bean>
-->
  <bean id="local-activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
    <property name="connectionFactory">
      <bean class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="failover://(tcp://localhost:61616)" />
        <property name="userName" value="${activemq.username}"/>
        <property name="password" value="${activemq.password}"/>
      </bean>
    </property>
  </bean>

  <lang:jruby id="producer"
              script-interfaces="org.apache.camel.Processor"
              script-source="classpath:ruby/producer.rb">
    <lang:property name="template" ref="producerTemplate" />
  </lang:jruby>


  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <package>com.test</package>

    <template id="producerTemplate"/>
  </camelContext>

</beans>

Please note the added 'lang' namespace in premable and relative expanded xsi:schemaLocation. Missing that will issue a error.

Note also that credential for activemq should be written in src/main/resources/credential.properties, for example:
activemq.username=system
activemq.password=manager

Than create the route in file src/main/java/com/test/MyRouteBuilder.java:
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.test;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.Main;

import static org.apache.camel.builder.xml.XPathBuilder.xpath;

/**
 * A Camel Router
 */
public class MyRouteBuilder extends RouteBuilder {

    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    public static void main(String... args) throws Exception {
        Main.main(args);
    }

    /**
     * Lets configure the Camel routing rules using Java code...
     */
    public void configure() {

 from("timer://producer_timer?fixedRate=true&delay=5000&period=10000").
     errorHandler(loggingErrorHandler("com.test")).
     id("producer").
     setBody(constant("Hallo from producer")).
     beanRef("producer");

    }
}
This is a simple route which triggers each 10 seconds, creates a empty message, changes its body to 'Hallo from producer' then sends it to producer bean.



Now it's time to write the processor, which in turn uses producerTemplate. According to camel-context.xml source file should be in src/main/resources/ruby/producer.rb:
require 'java'

java_import "org.apache.camel.Exchange"
java_import "org.apache.camel.Processor"

java_import "org.slf4j.Logger"
java_import "org.slf4j.LoggerFactory"
java_import "ch.qos.logback.classic.LoggerContext"

java_package "it.unimore.cesia"

class Producer
  include Processor

  def initialize
    @logger = LoggerFactory.getLogger("com.test.producer")
    @logger.info("Hello from Producer")
  end  

  def setTemplate(template)
    @@template = template
  end

#java_annotation 'Exchange'
java_signature 'void process(Exchange exchange)'
  def process(exchange)
    msg = exchange.in.body
    @logger.info("Producing at #{Time.now.strftime('%H:%M')}")
    @@template.sendBody("local-activemq:queue:producer.audit", "#{msg} at #{Time.now.strftime('%H:%M')}")

  end

end

Route can be activated with:
mvn camel:run

If you want to activate logging, create a logback.xml file in src/main/resource. For example for a syslog/file logging:
<configuration scan="true" scanPeriod="30 seconds">
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>producer.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <FileNamePattern>producer-%d{yyyy-MM-dd}.log</FileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--      <pattern>%date %level [%thread] %logger{10} %msg%n</pattern> -->
      <Pattern>%date %msg%n</Pattern>
      <charset>UTF-8</charset>
    </encoder>
  </appender>

  <appender name="SYSLOG"
    class="ch.qos.logback.classic.net.SyslogAppender">
    <SyslogHost>localhost</SyslogHost>
    <Facility>DAEMON</Facility>
    <SuffixPattern>[%logger] %msg</SuffixPattern>
  </appender>

  <logger name="com.test.producer" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="FILE" /> 
    <appender-ref ref="SYSLOG" />
  </logger>

  <logger name="com.test" additivity="false">
    <level value="INFO"/>
    <appender-ref ref="FILE" /> 
    <appender-ref ref="SYSLOG" />
  </logger>


  <root level="ERROR">
<!--
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" /> -->
    <appender-ref ref="SYSLOG" />
  </root>
</configuration>

Monday 13 June 2011

My first camel processor in scala

First create a maven project with:
mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes \\
-DarchetypeArtifactId=camel-archetype-scala -DarchetypeVersion=2.7.2 \\
-DgroupId=com.unitest -DartifactId=my_test

It creates a my_test project with a route defined in src/main/scala/com/unitest/MyRouteBuilder.scala, which can be modified as:

"timer://foo?fixedRate=true&delay=5s&period=10s" ==> {
     bean("myBean") 
     to("log:bean")
   }

Where myBean is referenced in src/main/resources/META-INF/spring/camel-context.xml as:
<bean id="myBean" class="com.unitest.MyBean"/>

and class MyBean, in folder src/main/scala/com/unitest is:
package com.unitest

import org.apache.camel.Exchange

class MyBean {

  def process(exchange: Exchange) {
    exchange.getIn.setBody("EpiReplicaQuery test")
   }

Execute with mvn camel:run and that's all.

Thursday 9 June 2011

juniper ssl vpn appliance and shibboleth2.2.1

First step is reading:
http://shibboleth.1660669.n2.nabble.com/Juniper-SSLVPN-integration-td3575845.html

There are also some very clear instuction by P. Geenens (pgeneens@juniper.net).

Something I was unaware was the need to create a Sign-in policy, for instance:
User URLs  Sign-In Page     Authentication Realm(s)
*/saml/  my Sign-In Page  shibboleth

Where shibboleth is the label of the Authentication Realm which uses the shibboleth authentication server.

Now, change the value of Source Site Inter-Site Transfer Service URL to
https://omissis.unitest.com/idp/profile/Shibboleth/SSO?providerId=vpn.unitest.com&shire=https://vpn.unitest.com/dana-na/auth/saml-consumer.cgi&target=https://vpn.unitest.com/saml
where /saml is the path of the Sign-in policy page.

Now as the user connects to https://vpn.unitest.com/saml, she is redirected to shibboleth IdP login page and than back to ssl vpn.

Add an alternative to update-alternatives

Let's start from the use case: you install firefox4.0.1 anf just want it starts from the 'Web Browser' button in the lxde menu bar.

It looks it was about configuring the x-www-browser alternative.

I started from update-alternatives --install, but wasn't able to figure out how to name the 4 parameters.

While doing some tries, I deleted the /usr/bin/x-www-browser link, which pointed to /etc/alternatives/x-www-browser which was a link to /usr/bin/iceweasel.

I gave up and linked /usr/local/src/firefox/firefox to /etc/alternatives/x-www-browser and again to /usr/bin/x-www-browser and opla'! Not only the 'Web Browser' button works, but also the alternative stuff is ok:

sudo update-alternatives --list x-www-browser
/usr/bin/epiphany-browser
/usr/bin/iceweasel
/usr/local/src/firefox/firefox