Friday, February 17, 2012

Manage application configuration with JMX in JBOSS application server

Most of the time developers likes manage their application configuration on separate file, which contains name value pair. In my current project one of my team member also implements such a configuration through Spring. Put the file on Jboss %jboss-as%/server/xyz/conf folder which will picked by the spring on the startup of the server. I have asked him, what should i do to change the value of the configuration. He replied, you have to change the file and restart the server or start and stop the application ))). Certainly we face a lof of times these type of use cases. I told him about JMX and decided to make some quick change on code. Todays post is about JMX. For more information about JMX use cases, check the following links JMX use cases. At first we will create one interface and his implements, which will be our resource to manage by JMX. here is the fragment code of the classes:
public interface ConfigMBean {
    public void setURL(String url);
    public String getURL();

    public void setUserName(String useName);
    public String getUserName();

    public void setPassword(String password);
    //public String getPassword();

    public void setDownloadTimeout(long timeout);
    public long getDownloadTimeout();
}
Implemention of the interface goes here:
public class ConfigImpl implements ConfigMBean {
    private String url;
    private String userName;
    private String password;
    private long   downloadTimeout;

    @Override
    public void setURL(String url) {
        this.url = url;
    }

    @Override
    public String getURL() {
        return url;
    }

    @Override
    public void setUserName(String useName) {
        this.userName = useName;
    }

    @Override
    public String getUserName() {
        return userName;
    }

    @Override
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public void setDownloadTimeout(long timeout) {
        this.downloadTimeout = timeout;
    }

    @Override
    public long getDownloadTimeout() {
        return downloadTimeout;
    }
}
Now it's time to configure spring context to initialize our beans.
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    <import resource="classpath:META-INF/cxf/osgi/cxf-extension-osgi.xml"/>


    <context:component-scan base-package="com.blu"/>


    <bean name="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:local-serviceregistry.properties</value>
                <value>classpath*:serviceregistry.properties</value>
            </list>
        </property>
    </bean>

    <bean id="ConfigJMX" class="xyz.config.ConfigImpl">
        <property name="URL" value="${BasePath}"/>
        <property name="userName" value="${Login}"/>
        <property name="password" value="${Password}"/>
        <property name="downloadTimeout" value="${DownloadTimeout}"/>
    </bean>
    <!-- JMX config-->
    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
        <property name="beans">
            <map>
                <entry key="xyz:name=JConfig" value-ref="ConfigJMX"/>
            </map>
        </property>
    </bean>

</beans>
Code snippet is self explainable, however, bean named placeholderConfig used for reading name value pair from the file system when application starts. Configuration file is as follows:
BasePath=http://172.18.5.78:8080/jui
Login=jboss
Password=
DownloadTimeout=1000
Through spring Mbean exporter we exposes our pojo ConfigImpl to MBean server, and that's it. We can also use annotation @ManagedResource to expose pojo as a Mbean and auto wire by annotation @Component. Now we can deploy our application on Jboss and our Mbean is ready to use. We can use Jconsole to use our Mbean, which is shipped with JDK 1.5 and above. In order to use Jconsole to display Jboss Mbeans we have to add some properties on server boot time. Add these followings params to %JBOSS_HOME%/bin/run.conf.sh
# Enable the jconsole agent locally
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote"
# Tell JBossAS to use the platform MBean server
JAVA_OPTS="$JAVA_OPTS -Djboss.platform.mbeanserver"
# Make the platform MBean server able to work with JBossAS MBeans
JAVA_OPTS="$JAVA_OPTS -Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl"
see the following link for more information. Now you can run Jconsole and connect to your Jboss application server locally or remotely and set URL or user name, see the image below.
You can also use Jboss JMX Console to edit Mbean properties.
For more information to configure JMX through spring see here. Happy coding)))

Thursday, January 26, 2012

Configure Nginx to working with WebLogic 12C

Nginx is a free, open-source, high-performance HTTP server and reverse proxy server, which can be use with WebLogic application server to cache static page. It's also able to load balancing between servers. However nginx default proxy pass configuration not working properly with WebLogic server, because WebLogic server reset his http header which changes host and port. Here is the configuration for Ngnix proxy pass:
 
        proxy_cache_path usr/apps/nignx/nginx-1.1.12/cache/ levels=1:2 keys_zone=data-cache:8m max_size=1000m inactive=600m;
 proxy_temp_path usr/apps/nignx/nginx-1.1.12/cache/temp; 

 upstream osbapp{
   server 192.168.52.101:7001;
   server 192.168.52.101:7002;
        }
        server {
          listen       8001;
          server_name  192.168.52.103;

  location / {
   proxy_set_header Host $http_host; # set the parameter for fine granned header
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_store off;
   proxy_redirect off;
   proxy_buffering off;  
   #cache
   proxy_cache data-cache;
   proxy_cache_valid 200 302 60m;
   proxy_pass  http://osbapp; 
       }

Saturday, May 28, 2011

Code review with Sonar

Last week Sonar announced their new version 2.8 with a few new features and bug fixes. The main new feature is the support of custom code review. Crucible and Review board are another alternative code review system. Sonar come across with code coverage and review in one system, which is easy to maintain with small effort.
As usual sonar administrator must create users to assign tasks and collaborations. Sonar provide LDAP plugin which enables the delegation of Sonar authentication to an external system. Currently LDAP plugin supports LDAP and Active directory. In our corporation we are using active directory and first of all i tried to configure the LADP plugin. LDAP plugin wiki fully describes the installation of the plugin with LDAP system but poorly with AD. With some effort with my boss we were able to configure the plugin with our AD system. Follows i am sharing the configuration:
#-------------------
# Sonar LDAP Plugin
#-------------------

# IMPORTANT : before activation, make sure that one Sonar administrator is defined in the external system
# Activates the plugin. Leave blank or comment out to use default sonar authentication.
sonar.authenticator.class: org.sonar.plugins.ldap.LdapAuthenticator

# Ignore failure at startup if the connection to external system is refused.
# Users can browse sonar but not log in as long as the connection fails.
# When set to true, Sonar will not start if connection to external system fails.
# Default is false.
#sonar.authenticator.ignoreStartupFailure: true

# Automatically create users (available since Sonar 2.0).
# When set to true, user will be created after successful authentication, if doesn't exists.
# The default group affected to new users can be defined online, in Sonar general settings. The default value is "sonar-users".
# Default is false.
#sonar.authenticator.createUsers: true

# (omit if you use autodiscovery) URL of the LDAP server.
# If you are using ldaps, then you should install server certificate into java truststore.
# eg. ldap://localhost:10389
ldap.url: ldap://mycompany.com

# (optional) Distinguished Name (DN) of the root node in LDAP from which to search for users,
# eg. “ou=users,o=mycompany”
ldap.baseDn: dc=mycompany,dc=com

# (optional) Bind DN is the username of an LDAP user to connect (or bind) with.
# This is a Distinguished Name of a user who has administrative rights,
# eg. “cn=sonar,ou=users,o=mycompany”. Leave blank for anonymous access to the LDAP directory.
ldap.bindDn: ADADMIN

# (optional) Bind Password is the password of the user to connect with.
# Leave blank for anonymous access to the LDAP directory.
ldap.bindPassword: ADADMIN_PASSWORD

# Login Attribute is the attribute in LDAP holding the user’s login.
# Default is ‘uid’. Set ’sAMAccountName’ for Microsoft Active Directory
ldap.loginAttribute: sAMAccountName

# Object class of LDAP users.
# Default is 'inetOrgPerson'. Set ‘user’ for Microsoft Active Directory.
ldap.userObjectClass: user

# (advanced option) See http://java.sun.com/products/jndi/tutorial/ldap/security/auth.html
# Default is 'simple'. Possible values: 'simple', 'CRAM-MD5', 'DIGEST-MD5', 'GSSAPI'.
ldap.authentication: simple

# (advanced option)
# See
# http://java.sun.com/products/jndi/tutorial/ldap/security/digest.html
# http://java.sun.com/products/jndi/tutorial/ldap/security/crammd5.html
# eg. example.org
#ldap.realm:

# (advanced option) Context factory class.
# Default is 'com.sun.jndi.ldap.LdapCtxFactory'.
#ldap.contextFactoryClass: com.sun.jndi.ldap.LdapCtxFactory
Configuration may vary on your AD system, strongly guess system administrator may help in this issue.
For now in the time of the authentication, Sonar will ignore the password from it's own system and delegate the username and password to the active directory for authentication. Also sonar administrator must configure the role for each user independently.
After installing plugin we are ready to go for code review. On the violations tab we should see the review link as follows:
Now we can add comments on violations, by default task will assign to the author of the comment:
After creating the task we also can reassign the task to another user as follows:
All the reviews you can get from the dash board
For more screen shots you should visit this link (sonar-2-8-in-screenshots).
One shortage of the sonar code review is the lack of notification, when any comment or task assign to the users. I believe that, in future release sonar will add this notification functionality in code review.

Wednesday, May 25, 2011

Apache maven incremental build

Apache maven is one of the popular tool for building and managing java projects. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.
However when you have projects with multiple modules, it follows some issue when you compiling your project. One of them is incremental building, which means when you updates your project from the version control, you have to build the entire system by command mvn clean install. Consider the following maven project structure:
IncrementalBuild
|_ _ test-api
|_ _ test-api-impl
|_ _ test-donothing
where module test-api-impl dependent on module test-api. Whenever we will make some change on module test-api, we have to recompile and build the module test-api-impl.
If we will enter the command mvn install module test-api-impl will not get the updated version from the module test-api. You have to run command mvn clean install which will rebuild the entire project. Sometime it's time consuming and just unnecessary. You can download the project from here and check your self.
Apache maven currently doesn't support for the incremental build even on version 3.0.3.
But there is a plugin called Maven-Incremental build plugin, which can build project incrementally.
Just add the following plugin in the root pom file and you are ready for go
<plugin>
 <groupId>net.java.maven-incremental-build</groupId>
 <artifactId>incremental-build-plugin</artifactId>
 <version>1.4</version>
 <executions>
  <execution>
   <goals>
   <goal>incremental-build</goal>
   </goals>
  </execution>
 </executions>
</plugin>
Now you can run mvn install without goal clean and the project will detects the updated code and recompile modules if need.
UPD:- Note that, following page http://maven-incremental-build.java.net/site/usage.html contains incorrect groupId on example "net.java.incremental-build-plugin" which will not uploaded on central maven repository.
Resource:
1) Apache Maven Incremental Build support for WSO2 Carbon
2) Apache maven incremental plugin mojo

Saturday, April 16, 2011

Analyse with ANT - a sonar way

After the Javaone conference in Moscow, i have found some free hours to play with Sonar. Here is a quick steps to start analyzing with ANT projects. Sonar provides Analyze with ANT document to play around with ANT, i have just modify some parts.
Here is it.
1) Download the Sonar Ant Task and put it in your ${ANT_HOME}/lib directory
2) Modify your ANT build.xml as follows:
<?xml version = '1.0' encoding = 'windows-1251'?>

<project name="abc" default="build" basedir=".">
 <!-- Define the Sonar task if this hasn't been done in a common script -->
 <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
  <classpath path="E:\java\ant\1.8\apache-ant-1.8.0\lib" />
 </taskdef>
 <!-- Out-of-the-box those parameters are optional -->
 <property name="sonar.jdbc.url" value="jdbc:oracle:thin:@xyz/sirius.xyz" />
 <property name="sonar.jdbc.driverClassName" value="oracle.jdbc.driver.OracleDriver" />
 <property name="sonar.jdbc.username" value="sonar" />
 <property name="sonar.jdbc.password" value="sonar" />
 <!-- Additional Sonar configuration (PMD need 1.5 when using annotations)-->
 <property name="sonar.java.source" value="1.5"/>
 <property name="sonar.java.target" value="1.5"/>


 <!-- SERVER ON A REMOTE HOST -->
 <property name="sonar.host.url" value="http://sunny.fors.ru/sonar" />


 <property name="ear.file" value="konfiskat.ear"/>

 <property file="build.properties"/>

 <property name="build.dir"    value="build"/>
 <property name="classes.dir"  value="${build.dir}/classes"/>
 <property name="classes2.dir" value="classes"/>
 <property name="deploy.dir"   value="deploy"/>
 <property name="doc.dir"      value="docs"/>
 <property name="jar.dir"      value="${build.dir}/jar"/>
 <property name="lib.dir.1"    value="lib"/>
 <property name="lib.dir.2"    value="${lib.common.dir}"/>
 <property name="lib.dir.3"    value="common_lib"/>
 <property name="lib.dir.4"    value="${jdev.libs.dir}"/>
 <property name="src.dir"      value="src"/>
 <property name="config.dir"   value="${src.dir}/META-INF"/>
 <property name="temp.dir"   value="${src.dir}/temp"/>

 <path id="classpath">
  <fileset dir="${lib.dir.3}" includes="**/*.jar"/>
  <fileset dir="${lib.dir.2}" includes="**/com.ibm.mq.jar, **/jboss-j2ee.jar"/>
  <fileset dir="${lib.dir.4}" includes="**/*.jar"/>
 </path>

 <path id="srcpath">
  <pathelement location="${src.dir}"/>
 </path>
 <!-- Add the target -->
 <target name="sonar">
  <!-- The workDir directory is used by Sonar to store temporary files -->
  <sonar:sonar workDir="${temp.dir}" key="org.example:example" version="0.1-SNAPSHOT" xmlns:sonar="antlib:org.sonar.ant">

   <!-- source directories (required) -->
   <sources>
    <path location="${src.dir}" />
   </sources>

   <!-- binaries directories, which contain for example the compiled Java bytecode (optional) -->
   <binaries>
    <path location="${classes.dir}" />
   </binaries>

   <!-- path to libraries (optional). These libraries are for example used by the Java Findbugs plugin -->
   <libraries>
    <path refid="classpath"/>
   </libraries>
  </sonar:sonar>
 </target>

 <target name="clean">
 </target>

 <target name="init" depends="clean"/>

 <target name="compile" depends="init">
  <mkdir dir="${classes.dir}"/>
  <javac destdir="${classes.dir}"
    classpathref="classpath"
    debug="on">
   <src refid="srcpath"/>
  </javac>
 </target>

 <target name="doc" depends="compile">

 </target>

 <target name="build" depends="doc">
  <mkdir dir="${jar.dir}"/>
  <jar destfile="${jar.dir}/toOAS.jar">
   <manifest>
    <attribute name="Class-Path" value="com.ibm.mq.jar xercesImpl.jar"/>
   </manifest>
   <metainf dir="${config.dir}">
    <include name="ejb-jar.xml"/>
    <include name="orion-ejb-jar.xml"/>
   </metainf>
   <fileset dir="${classes.dir}">
    <include name="**/*.*"/>
   </fileset>
   <fileset dir="${src.dir}">
    <include name="*.properties"/>
   </fileset>
  </jar>

  <mkdir dir="${deploy.dir}"/>

  <echo file="${jar.dir}/application.xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?> <application> <display-name>ReadMQ</display-name> <module> <ejb>toOAS.jar</ejb> </module> </application>]]>
  </echo>

  <ear destfile="${deploy.dir}/${ear.file}"
    appxml="${jar.dir}/application.xml">
   <fileset dir="${jar.dir}" includes="*.jar"/>
   <fileset dir="${lib.dir.2}">
    <include name="com.ibm.mq.jar"/>
    <include name="xercesImpl.jar"/>
   </fileset>
  </ear>

 </target>

 <target name="all" depends="build"/>
</project>