tag:blogger.com,1999:blog-72166914451307708492024-02-19T11:31:35.547+05:30Jayanga Dissanayake's BlogJayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.comBlogger34125tag:blogger.com,1999:blog-7216691445130770849.post-28159064695637931802021-01-09T23:37:00.000+05:302021-01-09T23:37:16.125+05:30Easily managing your build environments<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<span data-preserver-spaces="true" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0pt; margin-top: 0pt;">Have you come across situations where your build tools suddenly stop
working after you update the operating system? Do you want a simple way of
setting up your build environment each time you change or format your
machine?</span>
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<br />
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<span data-preserver-spaces="true" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0pt; margin-top: 0pt;">One option is to use Docker-based build environments. In this approach, you
can create Docker images with the necessary build tools and dependencies.
However, you will still have to remember the tags, mount your source code
manually, and execute docker commands with multiple arguments each time.
This approach is right but needs some effort.</span>
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<br />
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<span data-preserver-spaces="true" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0pt; margin-top: 0pt;">To address the issues in the approach mentioned above,
<a href="https://github.com/jsdjayanga/condo" target="_blank">The Condo</a> [1] was developed. The Condo is a simple bash script that wraps all
the necessary docker commands you need to manage your build
environments.</span>
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<span data-preserver-spaces="true" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0pt; margin-top: 0pt;"><br /></span>
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<span data-preserver-spaces="true" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0pt; margin-top: 0pt;">You can install condo by running the following command</span>
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><div style="background: rgb(255, 255, 255); border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;">/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/jsdjayanga/condo/main/scripts/install.sh)"
</pre>
</div>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br />
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
Following are four simple commands that the Condo supports.
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<br />
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
List build environments
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<span> </span><span style="font-family: courier;">condo list</span>
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
Run a build environment. condo <build-env-name>
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<span> <span style="font-family: courier;">condo devj11</span><br /></span>
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
Stop a build environment. condo stop <build-env-name>
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: courier;"><span> </span>condo stop devj11</span>
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
Clean a build environment. condo clean <build-env-name>
</p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: courier;"><span> </span>condo clean devj11</span>
</p>
<br />The configuration file (~/.condo/condo.json) allows to store all the
necessary information to run the respective Docker images hence allowing you to
run the docker image with just the name of the environment.<div><br /></div><div>You can configure and add as many as build environment you like, e.g., java, golang, spring, or any custom docker image of your choice. Furthermore, the "additional-arguments" section in the configuration allows you to provide any docker compliant arguments to your docker image.<br /><br />Try the
Condo your self share your experience!<br /><br />[1] <a href="https://github.com/jsdjayanga/condo">https://github.com/jsdjayanga/condo</a>
</div>Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-78522218010195002322019-01-17T11:54:00.002+05:302019-01-17T11:54:23.401+05:30Building your own JDK distributionsEveryone started to talk about openJDK[1] with the new licensing that Oracle introduced in January 2019. According to Oracle[2] public updates for Java SE8[3] will be available until the end of 2020 for individual personal use but not for business, commercial or production use[4].<br />
<br />
This lead people to look into alternative JDK distributions. AdoptOpenJDK[5] and AWS Corretto [6] seems to be good options. However, by the time this post is written, AWS Corretto still in its preview state. <br />
<br />
By the way, the aim of this post is not to talk about other OpenJDK distributions but to build your JDK distribution.<br />
<br />
Let's get our hands dirty.<br />
Hmmmmm, no, you don't have to get your hands dirty. I have made it super simple for you :). I have made a docker image[7] which contains all the necessary dependent tools and libraries. Just follow the below mantioned steps and you will have your own OpenJDK distribution.<br />
<br />
Note: Install Docker[1], if you haven't done it yet.<br />
<br />
1. Pull the docker image<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">docker pull jsdjayanga/ubuntu-openjdk8-build
</pre>
</div>
<br />
2. Spin up a container with the above image<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">docker run -it jsdjayanga/ubuntu-openjdk8-build
</pre>
</div>
<br />
3. Go to source the directory<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #007020;">cd </span>jdk8u
</pre>
</div>
<br />
4. Checkout the tag<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">hg checkout jdk8u192-b12
</pre>
</div>
<br />
5. Run the configure. You can set your name as the release user if you wish by setting --with-user-release-suffix<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">bash configure --enable-unlimited-crypto --with-user-release-suffix<span style="color: #333333;">=</span>jsdjayanga --with-build-number<span style="color: #333333;">=</span>b01 --with-milestone<span style="color: #333333;">=</span>192
</pre>
</div>
<br />
6. Create images<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">make images
</pre>
</div>
<br />
This process would take 30min to 1h and finally, you have your own OpenJDK distribution in the following location.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">/jdk8u/build/linux-x86_64-normal-server-release/images
</pre>
</div>
<br />
Try this is out and share your experience :)<br />
<br />
<br />
[1] <a href="https://openjdk.java.net/">https://openjdk.java.net/</a><br />
[2] <a href="https://www.oracle.com/index.html">https://www.oracle.com/index.html</a><br />
[3] <a href="https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html">https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html</a> <br />
[4] <a href="https://java.com/en/download/release_notice.jsp">https://java.com/en/download/release_notice.jsp</a><br />
[5] <a href="https://adoptopenjdk.net/">https://adoptopenjdk.net/</a><br />
[6] <a href="https://aws.amazon.com/corretto/">https://aws.amazon.com/corretto/</a><br />
[7] <a href="https://hub.docker.com/r/jsdjayanga/ubuntu-openjdk8-build">https://hub.docker.com/r/jsdjayanga/ubuntu-openjdk8-build</a><br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-26181343657069303682018-08-05T23:44:00.002+05:302018-08-06T10:32:46.068+05:30WUM3 New FeaturesWSO2 had been looking into the possibilities of improving the Update delivery mechanism for several years. As a result, WSO2 Update Manager (WUM) [1], a groundbreaking tool which enables shipping updates proactively, was developed two years ago. The latest version, WUM 3.0, was released two weeks back in parallel to the WSO2 US Con [4]. This article will discuss the new features of the WUM 3.0.<br />
<br />
The novelty begins with the auto-migration mechanism. Your migration experience from WUM 2.0 to WUM 3.0 is seamless. At the very first run of the WUM 3.0, it will automatically detect the previous WUM installation ("Older WUM home(/Users/<yourname>/.wum-wso2) detected.") and ask whether you want to migrate. If you select "yes" a new WUM home will be created at "/Users/<yourname>/.wum3" with all your previous product packs and updated packs. However, you have the flexibility of starting off with a fresh WUM home by selecting "no" if you don't want to continue with the previous packs.<br />
<br />
Next, the most awaited feature for WUM, WUM Channels. A channel is nothing but a mechanism to deliver a filtered set of updates. As an initiative, we released two channels.<br />
<br />
<ul>
<li><b>Channel "full":</b> this channel delivers all the fixes, including bug fixes, security fixes, improvements, etc.</li>
</ul>
<div style="text-align: left;">
This channel is preferred by many, who are managing dynamic requirements and continuous development. As this channel delivers various types of updates, it helps to stabilize the system in a highly evolving environment by proactively providing fixes. </div>
<div style="text-align: left;">
<br /></div>
<ul>
<li><b>Channel "security":</b> this channel only delivers security fixes.</li>
</ul>
<div style="text-align: left;">
As opposed to the channel "full" the channel "security" provides only the security updates. This channel will help the users with static requirements who infrequently push changes to the production environment. As the production system is stable and seldom changed, channel "security" helps to push only security fixes into the production system. </div>
<br />
Another new feature is to create WUM updated packs using a timestamp. This feature was initially developed to help WSO2 support, in building a WUM updated product pack to a specific timestamp given by WSO2 clients. However, we decided to expose this feature to all WUM users as this would be useful to create a WUM updated pack (to particular timestamp) in case you accidentally delete an updated pack form WUM home.<br />
<br />
Try new WUM 3.0 [2] and WSO2 products. Subscribe for Free Trial Subscription [3] to get access to WSO2 updates and all benefits of a WSO2 Subscription. <br />
<br />
[1] <a href="https://docs.wso2.com/display/WUM300/Introduction">https://docs.wso2.com/display/WUM300/Introduction</a><br />
[2] <a href="https://wso2.com/updates/wum">https://wso2.com/updates/wum</a><br />
[3] <a href="https://wso2.com/subscription">https://wso2.com/subscription</a><br />
[4] <a href="https://us18.wso2con.com/">https://us18.wso2con.com/</a> <br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-67860191759309353122017-01-24T16:23:00.002+05:302017-01-24T16:23:36.581+05:30How to increase max connection in MySqlWhen you try to make a large number of connections to the MySQL server, sometimes you might get the following error.<br />
<br />
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections" <br />
<br />
There are two values governing the max number of connections one can create<br />
<br />
<ol>
<li>max_user_connections</li>
<li>global.max_connections</li>
</ol>
<br />
<h3>
1. max_user_connections</h3>
The max_user_connections is a user level parameter which you can set for each user. To let a user to create any number of connections set the above mentioned value to zero '0'.<br />
<br />
First view the current max_user_connections:<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">SELECT</span> max_user_connections <span style="color: #008800; font-weight: bold;">FROM</span> mysql.<span style="color: #008800; font-weight: bold;">user</span> <span style="color: #008800; font-weight: bold;">WHERE</span> <span style="color: #008800; font-weight: bold;">user</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">'my_user'</span> <span style="color: #008800; font-weight: bold;">AND</span> <span style="color: #008800; font-weight: bold;">host</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">'localhost'</span>;
</pre>
</div>
<br />
Then set it to zero<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">GRANT</span> <span style="color: #008800; font-weight: bold;">USAGE</span> <span style="color: #008800; font-weight: bold;">ON</span> <span style="color: #333333;">*</span>.<span style="color: #333333;">*</span> <span style="color: #008800; font-weight: bold;">TO</span> my_user<span style="color: #333333;">@</span>localhost MAX_USER_CONNECTIONS <span style="color: #0000dd; font-weight: bold;">0</span>;
</pre>
</div>
<br />
<h3>
2. global.max_connections</h3>
The global.max_connections is a global parameter and has the precedence over <br />
max_user_connections. Hence just increasing the max_user_connections is not enough. Hence you have to increase the max_user_connections as well.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">set</span> <span style="color: #333333;">@@</span><span style="color: #008800; font-weight: bold;">global</span>.max_connections <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">1500</span>;
</pre>
</div>
<br />
<br />
Reference:<br />
<br />
[1] http://dba.stackexchange.com/questions/47131/how-to-get-rid-of-maximum-user-connections-error<br />
<br />
[2] https://www.netadmintools.com/art573.html<br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-90573438914871689882017-01-20T12:54:00.001+05:302017-01-20T12:55:43.247+05:30Installing and Configuring NGINX in ubuntu (for a Simple Setup)In this post I am going to present you, how to install NGINX and setup it to operate with simple HTTP routing.<br />
<br />
Below are the two easy steps to install NGINX in your ubuntu system. <br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">sudo apt-get update
sudo apt-get install nginx
</pre>
</div>
<br />
Once you are done go to any web browser and type in "http://localhost", in case you are installing in the local machine or "http://[IP_ADDRESS]"<br />
<br />
This will show you the default HTTP page hosted by NGINX<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
</pre>
</div>
<br />
Below are few easy commands to "Stop", "Start" or "Restart"<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">sudo service nginx stop
sudo service nginx start
sudo service nginx restart
</pre>
</div>
<br />
<br />
By now you have NGINX installed, up and running on your system. <br />
<br />
We will next we how to configure NGINX to listen to a particular port and route the traffic to some other end points.<br />
<br />
Below is a sample configuration file you need to create. Let's first see what each of these configuration means.<br />
<br />
"upstream" : represents a group of endpoints that you need to route you requests.<br />
<br />
"upstream/server" : an endpoint that you need to route you requests.<br />
<br />
"server" : represent the configurations for listing ports and routing locations<br />
<br />
"server/listen" : this is the port that NGINX will listen to<br />
<br />
"server/server_name" : the server name this machine (where you install the NGINX)<br />
<br />
"server/location/proxy_pass" : the group name of the back end servers you need to route your requests to. <br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">upstream</span> <span style="background-color: #fff0f0;">backends</span> {
<span style="color: #008800; font-weight: bold;">server</span> 192.168.58.118:<span style="color: #0000dd; font-weight: bold;">8280</span>;
<span style="color: #008800; font-weight: bold;">server</span> 192.168.88.118:<span style="color: #0000dd; font-weight: bold;">8280</span>;
}
<span style="color: #008800; font-weight: bold;">server</span> {
<span style="color: #008800; font-weight: bold;">listen</span> <span style="color: #0000dd; font-weight: bold;">8280</span>;
<span style="color: #008800; font-weight: bold;">server_name</span> <span style="color: #0000dd; font-weight: bold;">192</span><span style="background-color: #fff0f0;">.168.58.123</span>;
<span style="color: #008800; font-weight: bold;">location</span> <span style="background-color: #fff0f0;">/</span> {
<span style="color: #008800; font-weight: bold;">proxy_pass</span> <span style="background-color: #fff0f0;">http://backends</span>;
}
}
</pre>
</div>
<br />
The above configuration instructs NGINX to route requests that is coming into "192.168.58.123:8280", to be routed into "192.168.58.118:8280" or "192.168.88.119:8280" in round robin manner.<br />
<br />
1. To make that happen you have to create a file with above configuration at "/etc/nginx/sites-available/mysite1". You can use any name you want. In this example I named it as "mysite1".<br />
<br />
2. Now you have to enable this configuration by creating a symbolic link to the above file in "/etc/nginx/sites-enabled/" location<br />
/etc/nginx/sites-enabled/mysite1 -> /etc/nginx/sites-available/mysite1<br />
<br />
3. Now the last step. You have to restart the NGINX to get the new configurations affected.<br />
<br />
Once restarted, any request you send to "192.168.58.123:8280" will be load balanced in to "192.168.58.118:8280" or "192.168.88.119:8280" in round robin manner.<br />
<br />
Hope this helps you to quickly setup NGINX for you simple routing requirements <br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-41751741784428500762016-05-27T14:38:00.000+05:302016-05-27T14:38:46.712+05:30Deploying artifacts to WSO2 Servers using Admin ServicesIn this post I am going to show you, how to deploy artifacts on WSO2 Enterprise Service Bus [1] and WSO2 Business Process Server [2] using Admin Services [3]<br />
<br />
Usual practice with WSO2 artifacts deployment is to, enable DepSync [4] (Deployement Synchronization). And upload the artifacts via the management console of master node. Which will then upload the artifacts to the configured SVN repository and notify the worker nodes regarding this new artifact via a cluster message. Worker nodes then download the new artifacts from the SVN repository and apply those.<br />
<br />
In this approach you have to log in to the management console and do the artifacts deployment manually.<br />
<br />
With the increasing use of continuous integration tools, people are looking in to the possibility of automating this task. There is a simple solution in which you need to configure a remote file copy to the relevant directory inside the [WSO2_SERVER_HOME]/repository/deployment/server directory. But this is a very low level solution.<br />
<br />
Following is how to use Admin Services to do the same in much easier and much manageable manner.<br />
<br />
NOTE: Usually all WSO2 servers accept deployable as .car file but WSO2 BPS prefer .zip for deploying BPELs.<br />
<br />
For ESB,<br />
<ol>
<li>Call 'deleteApplication' in ApplicationAdmin service and delete the<br />application existing application</li>
<li>Wait for 1 min.</li>
<li>Call 'uploadApp' in CarbonAppUploader service</li>
<li>Wait for 1 min.</li>
<li>Call 'getAppData' in ApplicationAdmin, if it returns application data<br />continue. Else break</li>
</ol>
For BPS,<br />
<ol>
<li>Call the 'listDeployedPackagesPaginated' in<br />BPELPackageManagementService with page=0 and<br />packageSearchString=”Name_”</li>
<li>Save the information<br /><ns1:version><br /><ns1:name>HelloWorld2‐1</ns1:name><br /><ns1:isLatest>true</ns1:isLatest><br /><ns1:processes/><br /></ns1:version></li>
<li>Use the 'uploadService' in BPELUploader, to upload the new BPEL zip<br />file</li>
<li>Again call the 'listDeployedPackagesPaginated' in<br />BPELPackageManagementService with 15 seconds intervals for 3mins.</li>
<li>If it finds the name getting changed (due to version upgrade. Eg:<br />HelloWorld2‐4), then continue. (Deployment is success)</li>
<li>If the name doesn't change for 3mins, break. Deployment has some<br />issues. Hence need human intervention </li>
</ol>
<br />
[1] <a href="http://wso2.com/products/enterprise-service-bus/">http://wso2.com/products/enterprise-service-bus/</a><br />
[2] <a href="http://wso2.com/products/business-process-server/">http://wso2.com/products/business-process-server/</a><br />
[3] <a href="https://docs.wso2.com/display/BPS320/Calling+Admin+Services+from+Apps">https://docs.wso2.com/display/BPS320/Calling+Admin+Services+from+Apps</a><br />
[4] <a href="https://docs.wso2.com/display/CLUSTER420/SVN-Based+Deployment+Synchronizer">https://docs.wso2.com/display/CLUSTER420/SVN-Based+Deployment+Synchronizer</a>Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-19262978595522738932016-03-06T20:35:00.000+05:302016-03-06T20:35:09.783+05:30How to write OSGi tests for C5 compomentsWSO2 C5 Carbon Kernel will be the heart of all the next generation Carbon products. With Kernel version 5.0.0 we introduced PAX OSGi testing.<br />
<br />
Now we are trying to ease the life of C5 components developers by providing a utility, which will take care of most of the generic configurations need in OSGi testing. This will enable the C5 component developer to just specify a small number of dependencies and start writing PAX tests for C5 components.<br />
<br />
You will have to depend on the following library, except for other PAX dependencies<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;">1
2
3
4
5
6</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.wso2.carbon<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>carbon-kernel-osgi-test-util<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>5.1.0-SNAPSHOT<span style="color: #007700;"></version></span>
<span style="color: #007700;"><scope></span>test<span style="color: #007700;"></scope></span>
<span style="color: #007700;"></dependency></span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<br />
You can find a working sample on the following Git repo<br />
<a href="https://github.com/jsdjayanga/c5-sample-osgi-test">https://github.com/jsdjayanga/c5-sample-osgi-test</a><br />
<br />
Above will load the dependencies you need by default to test Carbon Kernel functionalities. But as a component developer you will have to specify your components jars into the testing environment. This is done via @Configuration annotation in your test class.<br />
<br />
Lets assume you work on a bundle org.wso2.carbon.jndi:org.wso2.carbon.jndi, below is how you should specify your dependencies.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10</pre>
</td><td><pre style="line-height: 125%; margin: 0;"> <span style="color: #555555; font-weight: bold;">@Configuration</span>
<span style="color: #008800; font-weight: bold;">public</span> Option<span style="color: #333333;">[]</span> <span style="color: #0066bb; font-weight: bold;">createConfiguration</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Option<span style="color: #333333;">></span> customOptions <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><>();</span>
customOptions<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>mavenBundle<span style="color: #333333;">().</span><span style="color: #0000cc;">artifactId</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"org.wso2.carbon.jndi"</span><span style="color: #333333;">).</span><span style="color: #0000cc;">groupId</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"org.wso2.carbon.jndi"</span><span style="color: #333333;">)</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">versionAsInProject</span><span style="color: #333333;">());</span>
CarbonOSGiTestEnvConfigs configs <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> CarbonOSGiTestEnvConfigs<span style="color: #333333;">();</span>
configs<span style="color: #333333;">.</span><span style="color: #0000cc;">setCarbonHome</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"/home/jayanga/WSO2/Training/TestUser/target/carbon-home"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> CarbonOSGiTestUtils<span style="color: #333333;">.</span><span style="color: #0000cc;">getAllPaxOptions</span><span style="color: #333333;">(</span>configs<span style="color: #333333;">,</span> customOptions<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<br />
Once these are done, your test should ideally work :)<br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-67941587080421585862015-12-25T00:22:00.000+05:302017-02-28T17:32:16.991+05:30How to create a heap dump of your Java applicationHeap in a JVM, is the place where it keeps all your runtime objects. The JVM create a dedicated space for the heap at the JVM startup, which can be controlled via JVM option <span class="_Tgc">-Xms</span><span class="_Tgc"><size> </span><span class="_Tgc">eg: </span><span class="_Tgc">-Xms</span><span class="_Tgc">100m (this will allocate 100MBs for the heap). JVM is capable of increasing and decreasing the size of the heap [1] based on the demand, and JVM has another </span><span class="_Tgc">option which allows to set max size for the heap, </span><span class="_Tgc">-Xmx<size>, eg: </span><span class="_Tgc">-Xmx6g (this allows the heap to grow up to 6GBs)</span><br />
<br />
JVM automatically perform Garbage Collection (GC), when it detects its about to reach the heap size limits. But the GC can only clean the objects which are eligible for GC. If the JVM can't allocate required memory even after GC, JVM will crash with "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space"<br />
<br />
If your Java application in production crashes due to some issue like this, you cant just ignore the incident, and restart your application. You have to analyze the what cause the JVM crash, and take the necessary actions to avoid it happening again. This is where the JVM heap dump comes in to the play.<br />
<br />
JVM heap dumps are by default disabled, you have to enable heap dumps explicitly by providing following JVM option, -XX:+HeapDumpOnOutOfMemoryError<br />
<br />
The below sample code, tries to create a multiple, large arrays of chars, and keep the references in list. Which cause those large arrays ineligible for garbage collection. <br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">test</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.ArrayList</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">TestClass</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Object<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><</span>Object<span style="color: #333333;">>();</span>
<span style="color: #008800; font-weight: bold;">for</span> <span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span> i <span style="color: #333333;"><</span> <span style="color: #0000dd; font-weight: bold;">1000</span><span style="color: #333333;">;</span> i<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">char</span><span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">1000000</span><span style="color: #333333;">]);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
If you run the above code with following command lines,<br />
<br />
1. java -XX:+HeapDumpOnOutOfMemoryError -Xms10m -Xmx3g com.test.TestClass<br />
<br />
Result: Program runs and exit without any error. The heap size starts from 10MB and then grows as needed. Above needs memory less than 3GB. So, it completes without any error.<br />
<br />
2. java -XX:+HeapDumpOnOutOfMemoryError -Xms10m -Xmx1g com.test.TestClass<br />
<br />
Result: JVM crashes with OOM. <br />
<br />
If we change the above code a bit to remove the char array from the list, after adding to the list. what would be the result<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">test</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.ArrayList</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">TestClass</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Object<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><</span>Object<span style="color: #333333;">>();</span>
<span style="color: #008800; font-weight: bold;">for</span> <span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span> i <span style="color: #333333;"><</span> <span style="color: #0000dd; font-weight: bold;">1000</span><span style="color: #333333;">;</span> i<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">char</span><span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">1000000</span><span style="color: #333333;">]);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">remove</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
3. java -XX:+HeapDumpOnOutOfMemoryError -Xms10m -Xmx10m com.test.TestClass<br />
<br />
Result: This code runs without any issue even with a heap of 10MBs. <br />
<br />
NOTE:<br />
1. There is no impact to your application if you enable the heap dump in the JVM. So, it is better to always enable -XX:+HeapDumpOnOutOfMemoryError in your applications<br />
<br />
2. You can create a heap dump of a running Java application with the use of jmap. jmap come with the JDK. Creating a heap dump of a running application cause the application to halt everything for a while. So, not recommended to use in production system. (unless there is a extreme situation)<br />
eg: jmap -dump:format=b,file=test-dump.hprof [PID]<br />
<br />
3. Above sample codes are just for understanding the concept. <br />
<br />
[1] https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/garbage_collect.html<br />
<br />
<br />
Edit:<br />
<br />
Following are few other important flags that could be useful in generating heap dumps;<br />
<br />
<b>-XX:HeapDumpPath=/tmp/heaps</b><br />
<b>-XX:OnOutOfMemoryError="kill -9 %p"</b> : with this you can execute command at the JVM exit<br />
<b>-XX:+ExitOnOutOfMemoryError</b> : When you enable this option, the JVM exits on the first occurrence of an out-of-memory error. It can be used if you prefer restarting an instance of the JVM rather than handling out of memory errors [2].<br />
<b>-XX:+CrashOnOutOfMemoryError</b> : CrashOnOutOfMemoryError - If this option is enabled, when an out-of-memory error occurs, the JVM crashes and produces text and binary crash files (if core files are enabled) [2]. <br />
<br />
[2] http://www.oracle.com/technetwork/java/javase/8u92-relnotes-2949471.html<br />
<span class="_Tgc"><br /></span>
Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-53211381266510247372015-12-23T17:27:00.000+05:302015-12-23T17:27:59.100+05:30The core of the next-generation WSO2 Carbon platform : WSO2 Carbon Kernel 5.0.0A whole new revamp of the heart of all WSO2 products : WSO2 Carbon Kernel 5.0.0, was released 21 Dec 2015.<br />
<br />
Previous versions of the WSO2 Carbon Kernel, (1.x.x to 4.x.x) had a much similar architecture and was tightly coupled with relatively old technologies (axiom, axis2, SOAP, etc.). Which is the same reason, which made us to re-think and re-architecture everything from the ground, and to come up with WSO2 Carbon Kernel 5.0.0.<br />
<br />
The new Kernel is armed with the latest technologies and patterns. It will provide the key functionality for server developers on top of the underline OSGi runtime.<br />
<br />
Key Features<br />
<ul>
<li>Transport Management Framework</li>
<li>Logging Framework with Log4j 2.0 as the Backend</li>
<li>Carbon Startup Order Resolver</li>
<li>Dropins Support for OSGi Ready Bundles</li>
<li>Jar to Bundle Conversion Tool</li>
<li>Artifact Deployment Engine</li>
<li>Pluggable Runtime Support </li>
</ul>
<br />
You can download the product from [1], and find more information on [2].<br />
<br />
[1] <a href="http://product-dist.wso2.com/products/carbon/5.0.0/wso2carbon-kernel-5.0.0.zip">http://product-dist.wso2.com/products/carbon/5.0.0/wso2carbon-kernel-5.0.0.zip</a><br />
[2] <a href="https://docs.wso2.com/display/Carbon500/WSO2+Carbon+Documentation">https://docs.wso2.com/display/Carbon500/WSO2+Carbon+Documentation</a>Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-57486799565938619412015-12-18T18:38:00.000+05:302015-12-18T19:01:40.397+05:30Logging with SLF4JThe Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks. It allows you to code just depending on a one dependency namely "<i>slf4j-api.jar", and to plug in the desire l</i><i>ogging framework at runtime.</i><br />
<i><br /></i>
It is very simple to use slf4 logging in your application. You just need to create a slf4j logger and invoke its methods.<br />
<br />
Following is a sample code,<br />
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">test</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.slf4j.Logger</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.slf4j.LoggerFactory</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Main</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">final</span> Logger logger <span style="color: #333333;">=</span> LoggerFactory<span style="color: #333333;">.</span><span style="color: #0000cc;">getLogger</span><span style="color: #333333;">(</span>Main<span style="color: #333333;">.</span><span style="color: #0000cc;">class</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
logger<span style="color: #333333;">.</span><span style="color: #0000cc;">info</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Testing 123"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
You have to add the following dependency in your pom.xml file<br />
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.slf4j<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>slf4j-api<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>1.7.13<span style="color: #007700;"></version></span>
<span style="color: #007700;"></dependency></span>
</pre>
</div>
<br />
This is the bare minimum configuration you need to enable sl4fj logging. But if you run this code. you will get a warning similar to the below.<br />
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
</pre>
</div>
<br />
This is because, it can't find a binding in the class path, by default, if it can't find a binding in the class path, it will bind to no-op logger implementation.<br />
<br />
<h4>
Using java.util.logging </h4>
If you want to use the binding for java.util.logging in your code, you only need to add the following dependency in to your pom.file.<br />
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.slf4j<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>slf4j-jdk14<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>1.7.13<span style="color: #007700;"></version></span>
<span style="color: #007700;"></dependency></span>
</pre>
</div>
<br />
This will output the following,<br />
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">INFO: Testing 123
</pre>
</div>
<br />
<br />
<h4>
Using Log4j</h4>
If you want to use the binding for log4j
version 1.2 in your code, you
only need to add the following dependency in to your pom.file.<br />
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.slf4j<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>slf4j-log4j12<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>1.7.13<span style="color: #007700;"></version></span>
<span style="color: #007700;"></dependency></span>
</pre>
</div>
<br />
Log4j needs an appender to log. Hence, you have to specify the log4j properties.<br />
<br />
Create the file "log4j.properties", in resource directory of your project and add the following into it.<br />
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #888888;"># Set root logger level to DEBUG and its only appender to A1.</span>
<span style="color: #0000cc;">log4j.rootLogger</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">DEBUG, A1</span>
<span style="color: #888888;"># A1 is set to be a ConsoleAppender.</span>
<span style="color: #0000cc;">log4j.appender.A1</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">org.apache.log4j.ConsoleAppender</span>
<span style="color: #888888;"># A1 uses PatternLayout.</span>
<span style="color: #0000cc;">log4j.appender.A1.layout</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">org.apache.log4j.PatternLayout</span>
<span style="color: #0000cc;">log4j.appender.A1.layout.ConversionPattern</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">%-4r [%t] %-5p %c %x - %m%n</span>
</pre>
</div>
<br />
This will output the following,<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">0 [main] INFO com.test.Main - Testing 123
</pre>
</div>
<br />
<br />
[1] http://www.slf4j.org/<br />
[2] http://logging.apache.org/log4j/1.2/index.html<br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-48593703975492650182015-12-18T14:34:00.002+05:302015-12-18T14:34:52.224+05:30WSO2GREG : Upload filesWSO2 Governance registry (WSO2GREG) is a metadata repository. It supports storing, cataloging, indexing, managing and governing your enterprise metadata related to any kind of asset.<br />
<br />
Uploading files directly in to the GREG is not recommended. What is recommended is to upload the files in to a file server and use a link to the file as a metadata.<br />
<br />
But there may be situations where you really need to keep the files in GREG itself. Let's see how you can do this in GREG.<br />
<br />
<br />
1. Download the file.rxt file from [1].<br />
2. Create a new artifact type from the WSO2GREG management console. Extensions-> Configure->Artifact Types->Add new Artifact<br />
3. Download the "file" extension from [2]<br />
4. Copy the "file" directory to "[GREG_HOME]/repository/deployment/server/jaggeryapps/publisher/extensions/assets/"<br />
<br />
This will add a new item "file" in to the menu and you should be able to upload and download files in WSO2GREG.<br />
<br />
If you need to make any association to "files" you upload here, you have to do the following to list the files in the associations view.<br />
<br />
Let's assume you want link these files to "soapservice" artifact. Then do the following.<br />
<br />
1. Edit the Association types for "soapservice" in [GREG_HOME]/repository/conf/governance.xml file to have the file details<br />
<file>file</file><br />
<br />
Following is a sample configuration for Association type="soapservice", once you add the "file" type<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><Association</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"soapservice"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><security></span>policy<span style="color: #007700;"></security></span>
<span style="color: #007700;"><ownedBy></span>soapservice,restservice,wsdl<span style="color: #007700;"></ownedBy></span>
<span style="color: #007700;"><usedBy></span>soapservice,restservice,wsdl<span style="color: #007700;"></usedBy></span>
<span style="color: #007700;"><dependancies></span>soapservice,restservice,wsdl,endpoint<span style="color: #007700;"></dependancies></span>
<span style="color: #007700;"><file></span>file<span style="color: #007700;"></file></span>
<span style="color: #007700;"></Association></span>
</pre>
</div>
<br />
[1] http://www.mediafire.com/view/dtog4qdrs1v0mw1/file.rxt<br />
[2] http://www.mediafire.com/download/6df8eofni2tqtwr/file.zip<br />
<br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-21023591851183881372015-12-13T20:05:00.002+05:302015-12-18T14:49:21.031+05:30WSO2GREG : Categorized view of your assests<br />
Let's assume you have five services, namely, Service1, Service2, Service3, Service4 and Service5. If you just create these service in the WSO2GREG, all those services will be displayed as follows,<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirW-CLjBsC-KvdFjieCN0CcZffEKdbtFhcpKkuK7wvvzF8bi2ju_JGo0e25CDL1_Wh1YVqN2LH2lQbvy_eURPCUzb3oDQ0RqJSwQWBZZmtcd5sn6q5OmbSx3IPpcE_uFuYVJTRTT-Ta10/s1600/Screenshot+2015-12-10+17%253A53%253A29.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="174" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirW-CLjBsC-KvdFjieCN0CcZffEKdbtFhcpKkuK7wvvzF8bi2ju_JGo0e25CDL1_Wh1YVqN2LH2lQbvy_eURPCUzb3oDQ0RqJSwQWBZZmtcd5sn6q5OmbSx3IPpcE_uFuYVJTRTT-Ta10/s640/Screenshot+2015-12-10+17%253A53%253A29.png" width="640" /></a></div>
<br />
Sometime it is needed to group the assets based on some criteria. Let's say, Service1 and Service2 belongs to DepartmentA and Service3, Service4, Service5 belongs to DepartmentB.<br />
<br />
If we need to have a categorized view based on the department. Follow the below mentioned steps.<br />
<br />
1. Add the following filed to the "<span style="-webkit-text-stroke-width: 0px; background-color: #eeeffb; color: #111111; display: inline !important; float: none; font-family: "lucida grande" , "lucida sans" , "microsoft sans serif" , "lucida sans unicode" , "verdana" , sans-serif , "trebuchet ms"; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">soapservice"</span> artifact. You edit it via management console. Extensions->Configure->Artifact Types->soapservice->Edit<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><field</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"options"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><name</span> <span style="color: #0000cc;">label=</span><span style="background-color: #fff0f0;">"Category"</span><span style="color: #007700;">></span>Category<span style="color: #007700;"></name></span>
<span style="color: #007700;"><values></span>
<span style="color: #007700;"><value></span>Department1<span style="color: #007700;"></value></span>
<span style="color: #007700;"><value></span>Department2<span style="color: #007700;"></value></span>
<span style="color: #007700;"></values></span>
<span style="color: #007700;"></field></span>
</pre>
</div>
<br />
2. Download the asset.js file in the following location and copy it to the following location [GREG_HOME]/repository/deployment/server/jaggeryapps/publisher/extensions/assets/soapservice (replace the existing asset.js file)<br />
<br />
<a href="http://www.mediafire.com/view/ka31hd4q5eqt44u/asset.js">http://www.mediafire.com/view/ka31hd4q5eqt44u/asset.js</a><br />
<br />
3. Restart the server<br />
<br />
4. You can now see, a new dropdown got appear in front of the search box.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB1UwrVbFKn4KdnQe4S94e9zjNHNrIlfkGviLVwkwe0AqCVutgKdzBgG4YlECpy9lfXeEARtIp8C_SjSxLVkb58WqKGbs4glK7wZ9r3yM_B-Ib1ocIC7MUU5Mcfo3cLO8AMvQ4wT2d8BM/s1600/Screenshot+2015-12-10+18%253A28%253A59.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="174" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB1UwrVbFKn4KdnQe4S94e9zjNHNrIlfkGviLVwkwe0AqCVutgKdzBgG4YlECpy9lfXeEARtIp8C_SjSxLVkb58WqKGbs4glK7wZ9r3yM_B-Ib1ocIC7MUU5Mcfo3cLO8AMvQ4wT2d8BM/s640/Screenshot+2015-12-10+18%253A28%253A59.png" width="640" /></a></div>
<br />
<br />
5. Go to each of those service and edit those service to have its corresponding department in the "Category" field.<br />
<br />
6. You can now view and search on the selected category.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_h7IDZF2_bDkvlti6huAtAh8qPygDI84Vk_uR5Mhv92Y50hP96zpKeL_u2Tv2zuWkCX33gSjicbLdy5z4EpTNZzPjAEDys_dHebqIxLFsw_vnLRDfZxGwpyrPyRsjckK8d2wE6bboyBU/s1600/Screenshot+2015-12-10+18%253A30%253A37.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="174" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_h7IDZF2_bDkvlti6huAtAh8qPygDI84Vk_uR5Mhv92Y50hP96zpKeL_u2Tv2zuWkCX33gSjicbLdy5z4EpTNZzPjAEDys_dHebqIxLFsw_vnLRDfZxGwpyrPyRsjckK8d2wE6bboyBU/s640/Screenshot+2015-12-10+18%253A30%253A37.png" width="640" /></a></div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixsb3A5xcGlILoOtHn-3UaVjJb4NVRQ9yguTQZgNARgrKjMkhql2E8DsB4wI-aej5iFwz3AqWvmQRUjpr1FKDu_nmREwSuAHZVzEWTrHcBB52BnXb14jb_31nVnp3Y9a6HI27DXGyxnCA/s1600/Screenshot+2015-12-10+18%253A30%253A46.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="174" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixsb3A5xcGlILoOtHn-3UaVjJb4NVRQ9yguTQZgNARgrKjMkhql2E8DsB4wI-aej5iFwz3AqWvmQRUjpr1FKDu_nmREwSuAHZVzEWTrHcBB52BnXb14jb_31nVnp3Y9a6HI27DXGyxnCA/s640/Screenshot+2015-12-10+18%253A30%253A46.png" width="640" /></a></div>
<br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-81461736550931811472015-12-11T18:30:00.000+05:302015-12-11T18:43:10.591+05:30WSO2GREG Publisher and StoreNew WSO2 Governance registry (WSO2GREG) comes with a brand new two applications<br />
<br />
1. Governance Center : Publisher<br />
2. Governance Center : Store<br />
<br />
These
two will be the main UIs to deal with the assets (assets governance),
WSO2 will no longer encourage to use the management console for artifact
management. (you can use the management console to setup assets,
lifecycles, users, etc. but not for artifact management)<br />
<br />
Governance Center : Publisher<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQ9c05e0bokziHyZU_lmCJtfD1XfodK-8nlf10jyZRw8cn3cswdrG723UTII5MbYSXIq1eadiCHSFCGsJdZPTNh1t5nT1-0-DqdtVy60RjLexXszH39h_CwfSe_WNRiw1R77iFtjLmayE/s1600/Screenshot+2015-12-10+17%253A39%253A03.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="208" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQ9c05e0bokziHyZU_lmCJtfD1XfodK-8nlf10jyZRw8cn3cswdrG723UTII5MbYSXIq1eadiCHSFCGsJdZPTNh1t5nT1-0-DqdtVy60RjLexXszH39h_CwfSe_WNRiw1R77iFtjLmayE/s640/Screenshot+2015-12-10+17%253A39%253A03.png" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZNqfu0TuI3zhpYnmbiggI4dIY6koZdRnHIDIhX3Lha3QXXKybnDMvfqCDhOPAPvOR1UdD4ft74LbWgmMYvm3t11rGCCcLlbwGHBdzJhkCrK-VHeyoz6t5JGys7Vi47JWkno5tbQ2RERI/s1600/Screenshot+2015-12-10+17%253A36%253A14.png" style="margin-left: 1em; margin-right: 1em;"><br /></a></div>
<br />
Governance Center : Store<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUGv9aPj0ctbXc4L57TrHElXYa5A8X7LnyZlkcmmjhWUqQMG3NE82cBbCK20kpXBaTm5my9ZgVzUPTU45xVDGOhXSc-eCypqu9Fb8ZmDjIFZzxHFtN3eKIYYkOnWyVg5r5xVfug3sCyiQ/s1600/Screenshot+2015-12-10+17%253A40%253A55.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUGv9aPj0ctbXc4L57TrHElXYa5A8X7LnyZlkcmmjhWUqQMG3NE82cBbCK20kpXBaTm5my9ZgVzUPTU45xVDGOhXSc-eCypqu9Fb8ZmDjIFZzxHFtN3eKIYYkOnWyVg5r5xVfug3sCyiQ/s640/Screenshot+2015-12-10+17%253A40%253A55.png" width="640" /></a></div>
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-36599078469652012002015-07-29T13:21:00.001+05:302015-08-04T03:07:03.675+05:30Binding a processes into CPUs in UbuntuIn this post I'm going to show you how to bind a process into a particular CPU in Ubuntu. Usually the OS manages the processes and schedules the threads. There is no guarantee on which CPU your process is running, OS will schedule it based on the resource availability.<br />
<br />
But there is a way to specify the CPU and bind your process into a CPU.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">taskset <span style="color: #333333;">-</span>cp <span style="color: #333333;"><</span>CPU ID <span style="color: #333333;">|</span> CPU IDs<span style="color: #333333;">></span> <span style="color: #333333;"><</span>Process ID<span style="color: #333333;">></span>
</pre>
</div>
<br />
<i>Following is an sample to demonstrate how you can do that.</i><br />
<br />
1. Sample code which consumes 100% CPU (for demo purposes)<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Test</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String args<span style="color: #333333;">[])</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">while</span> <span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
i<span style="color: #333333;">++;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
2. Compile and run the above simple program<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">javac Test<span style="color: #333333;">.</span><span style="color: #0000cc;">java</span>
java Test
</pre>
</div>
<br />
3. Use the 'htop' to view the CPU usage<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7GAvfN-VHdSs2kHICtuXwrpf2Yyj-eXW_s5UVYmMed7jqNSiBLPvUfnIW8es2I2Ko8dcbdO4ZsHpBoVgbjjeDCmlQa2p1mMUgri8yRdDClwTJLj62VnVQVYvoD12LWX5Xzgo13GL5cls/s1600/cpu2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="112" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7GAvfN-VHdSs2kHICtuXwrpf2Yyj-eXW_s5UVYmMed7jqNSiBLPvUfnIW8es2I2Ko8dcbdO4ZsHpBoVgbjjeDCmlQa2p1mMUgri8yRdDClwTJLj62VnVQVYvoD12LWX5Xzgo13GL5cls/s640/cpu2.png" width="640" /></a></div>
<br />
In the above screen shot you can see that my sample process is running in the CPU 2. But its not guaranteed that it will always remain in CPU2. The OS might assign it to another CPU at some point.<br />
<br />
4. Run the following command, it will assign the process 5982 permanently into 5th CPU<i> (CPU # start at zero, hence the index 4 refers to 5th CPU.)</i><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">taskset <span style="color: #333333;">-</span>cp <span style="color: #0000dd; font-weight: bold;">4</span> <span style="color: #0000dd; font-weight: bold;">5982</span>
</pre>
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivJssHazqWuQWEwzpNLG1rfShpBbHvviwLILdBfy13kKDuYoyB72GDMJ0Yg_T5pvGdJzj_ol_aFaL1LAbcDiirGGFHugmOwLKF2RPm1utzew45TvTeHUcsH316gEmQEVaAkheosrp7gkM/s1600/cpu5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="112" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivJssHazqWuQWEwzpNLG1rfShpBbHvviwLILdBfy13kKDuYoyB72GDMJ0Yg_T5pvGdJzj_ol_aFaL1LAbcDiirGGFHugmOwLKF2RPm1utzew45TvTeHUcsH316gEmQEVaAkheosrp7gkM/s640/cpu5.png" width="640" /></a></div>
<br />
In the above screen shot you can see, that 100% CPU usage is now indicated in the CPU 5.<br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-25847359963600711802015-07-13T20:36:00.000+05:302015-07-13T20:36:35.595+05:30WSO2 BAM : How to change the scheduled time of a scripts in a toolboxWSO2 Business Activity Monitor (WSO2BAM) [1] is a fully open source,
complete solution for monitor/store a large amount of business related
activities and understand business activities within SOA and Cloud
deployments.<br />
<br />
WSO2 BAM comes with predefined set of toolboxes.<br />
<br />
A toolbox consist of several components<br />
1. Stream definitions<br />
2. Analytics scripts<br />
3. Visualizations components<br />
<br />
Non of the above 3 components are mandatory.<br />
You can have a toolbox which has only Stream definitions and Analytics scripts but not Visualization components.<br />
<br />
In WSO2 BAM, toolbox always get the precedence. Which means if you manually change anything related to any component published via a toolbox. It will be override once the server is restarted.<br />
<br />
If you update,<br />
1. Schedule time<br />
This will update the schedule time, and newly update value will be only effective until the next restart. This will not get persisted. Once the server is started, schedule time will have the original value form the toolbox <br />
<br />
2. Stream definition<br />
If you change anything related to stream definition, it might cause some consistency issues. When the server is restarted, it will find that there is already a stream definition exist with the given name and the configurations are different. So an error will be logged.<br />
<br />
So it is highly discouraged to manually modify the components deployed via a toolbox <br />
<br />
The recommended way to change anything associated with a toolbox, is to, <br />
1. Unzip the toolbox.<br />
2. Make the necessary changes.<br />
3. Create a zip the files again.<br />
4. Rename the file as <toolbox_name>.tbox<br />
5. Redeploy the toolbox<br />
<br />
So, if you need to change the scheduled time of <span style="-webkit-text-stroke-width: 0px; background-color: white; color: #111111; display: inline !important; float: none; font-family: 'Lucida Grande', 'Lucida Sans', 'Microsoft Sans Serif', 'Lucida Sans Unicode', Verdana, Sans-serif, 'trebuchet ms'; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">Service_Statistics_Monitoring</span> Toolbox,<br />
Get a copy of Service_Statistics_Monitoring.tbox file resides in [BAM_HOME]/repository/deployment/server/bam-toolbox directory.<br />
<br />
Unzip the file. Open the file Service_Statistics_Monitoring/analytics/analyzers.properties<br />
<br />
Set the following configuration according to your requirement<br />
analyzers.scripts.script1.cron=0 0/20 * * * ?<br />
<br />
Create a zip file and change the name of the file to Service_Statistics_Monitoring.tbox<br />
<br />
And redeploy the toolbox. <br />
<br />
Now your changes is embed into the toolbox and each time the toolbox is deployed, it will have the modified value.<br />
<br />
[1] <a href="http://wso2.com/products/business-activity-monitor/">http://wso2.com/products/business-activity-monitor/</a> Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-6899240342892841952015-07-07T07:57:00.000+05:302015-07-13T17:13:16.079+05:30Publishing WSO2 APIM Statistics to WSO2 BAMWSO2 API Manager (WSO2APIM) [1] is a fully open source, complete solution for creating, publishing and managing all aspects of an API and its lifecycle.<br />
<br />
WSO2 Business Activity Monitor (WSO2BAM) [2] is a fully open source, complete solution for monitor/store a large amount of business related activities and understand business activities within SOA and Cloud deployments.<br />
<br />
Users can use these two products together, which collectively gives total control over management and monitoring of APIs.<br />
<br />
In this post I'm going to explain how APIM stat publishing and monitoring happens in WSO2APIM and WSO2BAM.<br />
<br />
<u>Configuring WSO2 APIM to publish statistics</u><br />
<br />
You can find more information on setting up statistics publishing in [3]. Once you do your configurations, it should look like the below.<br />
<br />
<span style="-webkit-text-stroke-width: 0px; background-color: white; color: #333333; display: inline !important; float: none; font-family: monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;"><APIM_HOME>/repository/conf/api-manager.xml</span><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><APIUsageTracking></span>
<span style="color: #888888;"><!-- Enable/Disable the API usage tracker. --></span>
<span style="color: #007700;"><Enabled></span>true<span style="color: #007700;"></Enabled></span>
<span style="color: #007700;"><PublisherClass></span>org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageDataBridgeDataPublisher<span style="color: #007700;"></PublisherClass></span>
<span style="color: #007700;"><ThriftPort></span>7614<span style="color: #007700;"></ThriftPort></span>
<span style="color: #007700;"><BAMServerURL></span>tcp://<span style="color: #007700;"><BAM</span> <span style="background-color: #ffaaaa; color: red;">host</span> <span style="background-color: #ffaaaa; color: red;">IP</span><span style="color: #007700;">></span>:7614/<span style="color: #007700;"></BAMServerURL></span>
<span style="color: #007700;"><BAMUsername></span>admin<span style="color: #007700;"></BAMUsername></span>
<span style="color: #007700;"><BAMPassword></span>admin<span style="color: #007700;"></BAMPassword></span>
<span style="color: #888888;"><!-- JNDI name of the data source to be used for getting BAM statistics. This data source should</span>
<span style="color: #888888;"> be defined in the master-datasources.xml file in conf/datasources directory. --></span>
<span style="color: #007700;"><DataSourceName></span>jdbc/WSO2AM_STATS_DB<span style="color: #007700;"></DataSourceName></span>
<span style="color: #007700;"></APIUsageTracking></span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<span style="-webkit-text-stroke-width: 0px; background-color: white; color: #333333; display: inline !important; float: none; font-family: monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;"><APIM_HOME>/repository/conf/datasources/master-datasources.xml</span><br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><datasource></span>
<span style="color: #007700;"><name></span>WSO2AM_STATS_DB<span style="color: #007700;"></name></span>
<span style="color: #007700;"><description></span>The datasource used for getting statistics to API Manager<span style="color: #007700;"></description></span>
<span style="color: #007700;"><jndiConfig></span>
<span style="color: #007700;"><name></span>jdbc/WSO2AM_STATS_DB<span style="color: #007700;"></name></span>
<span style="color: #007700;"></jndiConfig></span>
<span style="color: #007700;"><definition</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"RDBMS"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><configuration></span>
<span style="color: #007700;"><url></span>jdbc:mysql://localhost:3306/stats_db?autoReconnect=true<span style="color: #880000; font-weight: bold;">&amp;</span><span style="color: #007700;"></url></span>
<span style="color: #007700;"><username></span>db_username<span style="color: #007700;"></username></span>
<span style="color: #007700;"><password></span>db_password<span style="color: #007700;"></password></span>
<span style="color: #007700;"><driverClassName></span>com.mysql.jdbc.Driver<span style="color: #007700;"></driverClassName></span>
<span style="color: #007700;"><maxActive></span>50<span style="color: #007700;"></maxActive></span>
<span style="color: #007700;"><maxWait></span>60000<span style="color: #007700;"></maxWait></span>
<span style="color: #007700;"><testOnBorrow></span>true<span style="color: #007700;"></testOnBorrow></span>
<span style="color: #007700;"><validationQuery></span>SELECT 1<span style="color: #007700;"></validationQuery></span>
<span style="color: #007700;"><validationInterval></span>30000<span style="color: #007700;"></validationInterval></span>
<span style="color: #007700;"></configuration></span>
<span style="color: #007700;"></definition></span>
<span style="color: #007700;"></datasource></span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<br />
<u>Configuring WSO2 BAM</u><br />
<br />
<br />
You can find more information on setting up statistics publishing in
[3].<br />
<br />
Note that you only need to copy <span style="-webkit-text-stroke-width: 0px; background-color: white; color: #333333; display: inline !important; float: none; font-family: monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">API_Manager_Analytics.tbox</span> into super tenant space. (No need to do any configuration in tenant space)<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7bdO4C-7vUzOKwBtJX4ot4UHp0rS-op1s1E7e_Z04hyUN_3eqMqaDXHuVCXI7tUl606Qrv4bOQ5GRtDvFNikomw3B-k-eKb3BF68CR2VWp8eLnSfPknBbxeOiRRLs0ney5UKFkiwpbAQ/s1600/apim-bam%25281%2529.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="272" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7bdO4C-7vUzOKwBtJX4ot4UHp0rS-op1s1E7e_Z04hyUN_3eqMqaDXHuVCXI7tUl606Qrv4bOQ5GRtDvFNikomw3B-k-eKb3BF68CR2VWp8eLnSfPknBbxeOiRRLs0ney5UKFkiwpbAQ/s400/apim-bam%25281%2529.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_UBLaVC5T0WEC40AKi9tSDdOhbxlmgr03GT6xkihiCpqmzyOYeCksLPt-E-_F9ctb_P4R8EVC6nrN_8BE9Y4uwuKQok6VSBwedhiTXJLxfrHSv-Ni0pkjLSfjpp-usKJsbr0Qy7EGw-g/s1600/apim-bam.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><br /></a></div>
<br />
Above digram illustrate how the stat data is published and eventually view though the APIM Statistic view.<br />
<br />
1. Statistics information about APIs from all the tenants are published to the WSO2 BAM via a single data publisher.<br />
<br />
2. <span style="-webkit-text-stroke-width: 0px; background-color: white; color: #333333; display: inline !important; float: none; font-family: monospace; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">API_Manager_Analytics.tbox</span> has stream definitions and hive scripts needed to summarize statistics. These hive scripts get periodically executed and summarized data is pushed into a RDBMS.<br />
<br />
3. When you visit statistics page in WSO2 APIM, it will retrieve summerized statistics form the RDBMS and shows it to you.<br />
<br />
Note: If you need to view statistics of a API which is deployed in a particular tenant. Login in to WSO2 APIM in particular tenant and view statistics page.<br />
(You don't need to do any additional configuration to support tenant specific statistics.)<br />
<br />
<br />
[1] <a href="http://wso2.com/api-management/">http://wso2.com/api-management/</a><br />
[2] <a href="http://wso2.com/products/business-activity-monitor/">http://wso2.com/products/business-activity-monitor/</a><br />
[3] <a href="https://docs.wso2.com/display/AM180/Publishing+API+Runtime+Statistics">https://docs.wso2.com/display/AM180/Publishing+API+Runtime+Statistics </a>Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-15113953120184892552015-04-06T15:57:00.002+05:302015-04-06T20:48:35.740+05:30Error occurred while applying patches {org.wso2.carbon.server.extensions.PatchInstaller}I have seen people complaining that WSO2 servers logs the following error message at server start up. <br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13</pre>
</td><td><pre style="line-height: 125%; margin: 0;">[<span style="color: #0000dd; font-weight: bold;">2015</span><span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">04</span><span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">06</span> <span style="color: #0000dd; font-weight: bold;">15</span><span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">48</span><span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">57</span>,<span style="color: #0000dd; font-weight: bold;">572</span>] ERROR {org.wso2.carbon.server.extensions.PatchInstaller} <span style="color: #333333;">-</span> <span style="color: #007020;">Error</span> occurred <span style="color: #008800; font-weight: bold;">while</span> applying patches
java.io.IOException<span style="color: #333333;">:</span> Destination <span style="background-color: #fff0f0;">'/home/jayanga/WSO2/wso2am-1.7.0/repository/components/plugins/org.eclipse.equinox.launcher.gtk.linux.x86_1.1.200.v20120522-1813'</span> exists but is a directory
at org.wso2.carbon.server.util.FileUtils.copyFile(FileUtils.java<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">145</span>)
at org.wso2.carbon.server.util.PatchUtils.copyNewPatches(PatchUtils.java<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">211</span>)
at org.wso2.carbon.server.extensions.PatchInstaller.perform(PatchInstaller.java<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">80</span>)
at org.wso2.carbon.server.Main.invokeExtensions(Main.java<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">152</span>)
at org.wso2.carbon.server.Main.main(Main.java<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">94</span>)
at <span style="color: #007020;">sun</span>.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at <span style="color: #007020;">sun</span>.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">39</span>)
at <span style="color: #007020;">sun</span>.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">25</span>)
at java.lang.reflect.Method.invoke(Method.java<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">597</span>)
at org.wso2.carbon.bootstrap.Bootstrap.loadClass(Bootstrap.java<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">63</span>)
at org.wso2.carbon.bootstrap.Bootstrap.main(Bootstrap.java<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">45</span>)
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<br />
The main reason to see such error message is, some erroneous entries in the patch metadata files for files itself. <br />
<br />
This might happen if you try to forcefully stop the server as soon as you start the server<br />
<br />
When the server start up, it copies the patch files. If the server is forcefully stopped at that time using [ctrl+c], patching processes get immediately stopped and patch meta-data will get corrupted.<br />
<br />
You can get rid of this issue by removing corrupted patch related meta-data manually and restating the server, so that the server will apply all the patches form the beginning.<br />
<br />
<ol>
<li>remove [CARBON_HOME]/repository/components/patches/.metadata</li>
<li>restart the server. (do not interrupt while starting up)</li>
</ol>
Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com1tag:blogger.com,1999:blog-7216691445130770849.post-72778380201750920692015-03-26T08:57:00.001+05:302015-04-06T20:46:21.212+05:30Everyday Git (Git commands you need in your everyday work)Git [1] is one of the most popular version control systems. In this post I am going to show you how to work with GitHub [2]. When it comes to GitHub there are thousands of public repositories. If you are interested in a project you can start working on it and contributing it. Followings are the steps and commands you will have to use while you work with GitHub.<br />
<br />
1. Forking a repository<br />
This is done via the GithHub [2] web site.<br />
<br />
2. Clone a new repository<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git clone <span style="color: #997700; font-weight: bold;">https:</span><span style="color: #888888;">//github.com/jsdjayanga/carbon4-kernel.git</span>
</pre>
</div>
<br />
3. Get updates from the remote repository (origin/master)<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git pull origin master
</pre>
</div>
<br />
4. Push the updates to the the remote repository (origin/master)<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git push origin master
</pre>
</div>
<br />
5. Add updated files to staging<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git add TestFile<span style="color: #333333;">.</span><span style="color: #0000cc;">java</span>
</pre>
</div>
<br />
6. Commit the local changes to the remote repository<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git commit <span style="color: #333333;">-</span>m <span style="background-color: #fff0f0;">"Modifications to TestFile.java"</span> <span style="color: #333333;">--</span>signoff
</pre>
</div>
<br />
7. Set the upstream repository<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git remote add upstream <span style="color: #997700; font-weight: bold;">https:</span><span style="color: #888888;">//github.com/wso2/carbon4-kernel.git</span>
</pre>
</div>
<br />
8. Fetch from upstream repository<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git fetch upstream
</pre>
</div>
<br />
9. Fetch from all the remote repositories<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git fetch <span style="color: #333333;">--</span>all
</pre>
</div>
<br />
10. Merge new changes from upstream repository for the master branch<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git checkout master
git merge upstream<span style="color: #333333;">/</span>master
</pre>
</div>
<br />
11. Merge new changes from upstream repository for the "otherbranch" branch<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git checkout otherbranch
git merge upstream<span style="color: #333333;">/</span>otherbranch
</pre>
</div>
<br />
12. View the history of commits <br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git log
</pre>
</div>
<br />
13. If needed to discard some commits in the local repository<br />
First find the commit ID to which you want to revert back to. The user the following command<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git reset <span style="color: #333333;">--</span>hard <span style="background-color: #ffaaaa; color: red;">#</span>commitId</pre>
</div>
<br />
14. To tag a particular commit<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">git checkout <span style="background-color: #ffaaaa; color: red;">#</span>commitid
git tag <span style="color: #333333;">-</span>a v1.<span style="color: #6600ee; font-weight: bold;">1.1</span> <span style="color: #333333;">-</span>m <span style="background-color: #fff0f0;">'Tagging version v1.1.1'</span>
git push origin <span style="color: #333333;">--</span>tags
</pre>
</div>
<br />
<br />
[1] <a href="http://git-scm.com/">http://git-scm.com/</a><br />
[2] <a href="https://github.com/">https://github.com/</a> Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-46791336450169543562015-03-26T08:52:00.002+05:302015-04-06T20:44:49.230+05:30WSO2 Carbon : Get notified just after the server start and just before server shutdownWSO2 Carbon [1] is a 100% open source, integrated and componentized middleware platform which enables you to develop your business and enterprise solutions rapidly. WSO2 Carbon is based on OSGi framework [2]. It inherits molecularity and dynamism from the OSGi.<br />
<div>
<br /></div>
<div>
In this post I am going to show you how to get notified, when the server is starting up and when the server is about to shut down. </div>
<div>
<br /></div>
<div>
In OSGi, bundle start up sequence is random. So you can't rely on the bundle start up sequence.</div>
<div>
<br /></div>
<div>
There are real world scenarios where you have some dependencies amount bundles, hence need to perform some actions before other dependent bundles get deactivated in the server shutdown.</div>
<div>
<br /></div>
<div>
Eg. Let's say you have to send messages to a external system. Your message sending module use your authentication module to authenticate the request and send it to the external system and your message sending module try to send all the buffered messages before the server shutdown.<br />
<br />
Bundle unloading sequence in OSGi not happened in a guaranteed sequence. So, what would happen if your authentication bundle get deactivated before your message sending bundle get deactivated. In this case message sending module can't send the messages<br />
<br />
To help these type of scenarios WSO2 Carbon framework provide you with a special OSGi service which can be used to detect the server start up and server shutdown<br />
<br />
1. How to get notified the server startup<br />
<br />
Implement the interface org.wso2.carbon.core.ServerStartupObserver [3], and register it as a service via the bundle context.<br />
<br />
When the server is starting you will receive notifications via completingServerStartup() and completedServerStartup()<br />
<br />
<br />
2. How to get notified the server shutdown<br />
<br />
Implement the interface org.wso2.carbon.core.ServerShutdownHandler [4], and register it as a service via the bundle context.<br />
<br />
When the server is about to shutdown you will receive the notification via invoke()<br />
<br />
eg:<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;">1
2
3
4
5
6
7</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">activate</span><span style="color: #333333;">(</span>ComponentContext componentContext<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
componentContext<span style="color: #333333;">.</span><span style="color: #0000cc;">getBundleContext</span><span style="color: #333333;">().</span><span style="color: #0000cc;">registerService</span><span style="color: #333333;">(</span>ServerStartupObserver<span style="color: #333333;">.</span><span style="color: #0000cc;">class</span><span style="color: #333333;">.</span><span style="color: #0000cc;">getName</span><span style="color: #333333;">(),</span> <span style="color: #008800; font-weight: bold;">new</span> CustomServerStartupObserver<span style="color: #333333;">(),</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span> <span style="color: #333333;">;</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>Throwable e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
log<span style="color: #333333;">.</span><span style="color: #0000cc;">error</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Failed to activate the bundle "</span><span style="color: #333333;">,</span> e<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br /></div>
<div>
[1] <a href="http://wso2.com/products/carbon/">http://wso2.com/products/carbon/</a><br />
[2] <a href="http://www.osgi.org/Main/HomePage">http://www.osgi.org/Main/HomePage</a></div>
<div>
[3] <a href="https://github.com/wso2/carbon4-kernel/blob/master/core/org.wso2.carbon.core/src/main/java/org/wso2/carbon/core/ServerStartupObserver.java">https://github.com/wso2/carbon4-kernel/blob/master/core/org.wso2.carbon.core/src/main/java/org/wso2/carbon/core/ServerStartupObserver.java</a><br />
[4] <a href="https://github.com/wso2/carbon4-kernel/blob/master/core/org.wso2.carbon.core/src/main/java/org/wso2/carbon/core/ServerShutdownHandler.java">https://github.com/wso2/carbon4-kernel/blob/master/core/org.wso2.carbon.core/src/main/java/org/wso2/carbon/core/ServerShutdownHandler.java</a></div>
<div>
<br /></div>
<div>
<br /></div>
Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-37717474174231515712014-10-05T23:29:00.000+05:302014-10-05T23:29:09.596+05:30How to register a custom deployer in CarbonDeployers in Axis2 are used to track the new file additions, file updates and file deletes. Writing an custom deployer is not a difficult task. A deployer is an implementation of org.apache.axis2.deployment.Deployer interface. You can find more details on how to write a deployer on : http://wso2.com/library/3708/<br />
<br />
Once you write your custom deployer, you have to register it. Following is how to register a custom deployer.<br />
<br />
<b>Add the deployer details to the component.xml file</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><component</span> <span style="color: #0000cc;">xmlns=</span><span style="background-color: #fff0f0;">"http://products.wso2.org/carbon"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><deployers></span>
<span style="color: #007700;"><deployer></span>
<span style="color: #007700;"><directory></span>sample<span style="color: #007700;"></directory></span>
<span style="color: #007700;"><extension></span>xml<span style="color: #007700;"></extension></span>
<span style="color: #007700;"><class></span>
org.wso2.carbon.samples.deployer.CustomDeployer
<span style="color: #007700;"></class></span>
<span style="color: #007700;"></deployer></span>
<span style="color: #007700;"></deployers></span>
<span style="color: #007700;"></component></span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
As the information given in the above configuration, a directory named ‘sample’ in the location ‘repository/deployment/server’ will be monitored. Whenever file with extension ‘xml’ is added, modified or removed deployed() method will be called.<br />
<br />
<b>Add the following entry to the <configuration> <instructions> list of the maven-bundle-plugin in your pox.xml file.</b><br />
<br />
<Axis2Deployer>CustomDeployer</Axis2Deployer><br />
<br />
<b>To be responsive for real time file additions, updates and deletions configuration ‘hotupdate’ in Axis2.xml has to be set to true.</b><br />
<br />
<parameter name="hotupdate" locked="false">true</parameter><br />
<br />
You can find a sample code in : https://github.com/jsdjayanga/How-to-register-a-custom-deployer-in-CarbonJayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-30341471279375862962014-10-03T11:23:00.000+05:302014-10-03T11:25:00.073+05:30Internal synchronization of carbon kernel (Holding transports until the kernel get ready)<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Starting up sequence of internal components of the carbon kernel is crucial for the kernel to operate properly. Most importantly kernel should not start accepting external messages until it is ready to process messages. So it is needed to delay the activation of transports until the kernel is ready. Carbon kernel is made up of OSGi based components. According to the OSGi standards there is no definite order in which the bundles get activated.</span></div>
<b id="docs-internal-guid-bc87382a-c097-7dfa-f1a7-b414800af425" style="font-weight: normal;"><br /></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">To overcome the this sequencing problem, In carbon kernel there is special component which handles this synchronization. ‘StartupFinalizerServiceComponent’ an OSGi component, which delays the activation of transports.</span></div>
<b style="font-weight: normal;"><br /></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">If all the required services are ready by the bundle activation time, then the ‘StartupFinalizerServiceComponent’ call the ‘completeInitialization()’ method which performs the initialization of transports. But if the required services are not available at the bundle activation time, transports will not get activated. And it will wait until the required services are available.</span></div>
<b style="font-weight: normal;"><br /></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">‘StartupFinalizerServiceComponent’ is a ServiceListener. Each time a service change happens </span><span style="line-height: normal; white-space: pre-wrap;">serviceChanged()</span><span style="font-family: Arial; font-size: 15px; line-height: 1.15; white-space: pre-wrap;"> method is called, and this will check for the required service list. Once all the required services are available, it calls the ‘completeInitialization()’ and activate the transports.</span></div>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">You can find the implementation of StartupFinalizerServiceComponent at:</span><a href="https://svn.wso2.org/repos/wso2/carbon/kernel/branches/4.2.0/core/org.wso2.carbon.core/4.2.0/src/main/java/org/wso2/carbon/core/internal/StartupFinalizerServiceComponent.java" style="text-decoration: none;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span><span style="background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">https://svn.wso2.org/repos/wso2/carbon/kernel/branches/4.2.0/core/org.wso2.carbon.core/4.2.0/src/main/java/org/wso2/carbon/core/internal/StartupFinalizerServiceComponent.java</span></a></div>
Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-67403935593191631562014-09-30T21:11:00.000+05:302014-09-30T21:11:44.045+05:30How to register a servlet from a Carbon ComponentThere are three ways to register a servlet in carbon.<br />
Specifying the servlet details in the web.xml file<br />
Specifying the servlet details in the component.xml file<br />
Registering the servlet with httpService in your component<br />
<br />
You can find the sample code in : https://github.com/jsdjayanga/How-to-register-a-servlet-from-a-Carbon-Component<br />
<br />
<b>Specifying the servlet details in the web.xml file</b><br />
<br />
Specifying the servlet details in the web.xml file is not recommended when working with the carbon framework, as it has less control over the servlet when it is directly specified in the web.xml<br />
<br />
From the remaining two, neither is bad, its totally up to the developer to decide what is best for a given scenario.<br />
<br />
<b>Specifying the servlet details in the component.xml file</b><br />
<br />
Specifying the servlet details in the component.xml file is the easiest way of doing this.<br />
<br />
In this approach, you need to have your HttpServlet implementation. Then you have to specify the details about your servlet in the component.xml file. Following is how you should specify details<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><component</span> <span style="color: #0000cc;">xmlns=</span><span style="background-color: #fff0f0;">"http://products.wso2.org/carbon"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><servlets></span>
<span style="color: #007700;"><servlet</span> <span style="color: #0000cc;">id=</span><span style="background-color: #fff0f0;">"SampleServlet"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><servlet-name></span>sampleServlet<span style="color: #007700;"></servlet-name></span>
<span style="color: #007700;"><url-pattern></span>/sampleservlet<span style="color: #007700;"></url-pattern></span>
<span style="color: #007700;"><display-name></span>Sample Servlet<span style="color: #007700;"></display-name></span>
<span style="color: #007700;"><servlet-class></span>
org.wso2.carbon.samples.xmlbased.SampleServlet
<span style="color: #007700;"></servlet-class></span>
<span style="color: #007700;"></servlet></span>
<span style="color: #007700;"></servlets></span>
<span style="color: #007700;"></component></span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
Once you restart the servlet, with your compiled .jar in the dropins directory (repository/components/dropins), all the request to the http://ip:port/sampleservlet will be routed to your custom servlet (org.wso2.carbon.samples.xmlbased.SampleServlet).<br />
<br />
<br />
<b>Registering the servlet with httpService</b><br />
<br />
Registering the servlet with httpService allows dynamically register and unregister services. This allows you to have more control over the availability of the servlet.<br />
<br />
In this approach, you need to have your HttpServlet implementation. Then you have to register your servlet with the org.osgi.service.http.HttpService once your bundle get activated.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">httpService<span style="color: #333333;">.</span><span style="color: #0000cc;">registerServlet</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"/sampledynamicservlet"</span><span style="color: #333333;">,</span> <span style="color: #008800; font-weight: bold;">new</span> SampleDynamicServlet<span style="color: #333333;">(),</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">,</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">);</span>
</pre>
</div>
<br />
Then onwards, requests received for the http://ip:port/sampledynamicservlet will be routed to your custom servlet.<br />
<br />
In this approach you can unregister your servlet, this cause the http://ip:port/sampledynamicservlet to be unavailable.<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">httpService<span style="color: #333333;">.</span><span style="color: #0000cc;">unregister</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"/sampledynamicservlet"</span><span style="color: #333333;">);</span>
</pre>
</div>
<br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-86043549205135700072014-09-30T09:04:00.003+05:302015-04-03T12:18:16.582+05:30Custom Authenticator for WSO2 Identity Server (WSO2IS) with Custom ClaimsWSO2IS is one of the best Identity Servers, which enables you to offload your identity and user entitlement management burden totally from your application. It comes with many features, supports many industry standards and most importantly it allows you to extent it according to your security requirements.<br />
<br />
In this post I am going to show you how to write your own Authenticator, which uses some custom claim to validate users and how to invoke your custom authenticator with your web app.<br />
<br />
<b>Create your Custom Authenticator Bundle</b><br />
<br />
WSO2IS is based OSGi, so if you want to add a new authenticator you have to crate an OSGi bungle. Following is the source of the OSGi bundle you have to prepare.<br />
<br />
This bundle will consist of three files,<br />
1. CustomAuthenticatorServiceComponent<br />
2. CustomAuthenticator<br />
3. CustomAuthenticatorConstants<br />
<br />
CustomAuthenticatorServiceComponent is an OSGi service component it basically registers the CustomAuthenticator (service). CustomAuthenticator is an implementation of org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator, which actually provides our custom authentication.<br />
<br />
<br />
1. CustomAuthenticatorServiceComponent<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> org<span style="color: #333333;">.</span><span style="color: #0000cc;">wso2</span><span style="color: #333333;">.</span><span style="color: #0000cc;">carbon</span><span style="color: #333333;">.</span><span style="color: #0000cc;">identity</span><span style="color: #333333;">.</span><span style="color: #0000cc;">application</span><span style="color: #333333;">.</span><span style="color: #0000cc;">authenticator</span><span style="color: #333333;">.</span><span style="color: #0000cc;">customauth</span><span style="color: #333333;">.</span><span style="color: #0000cc;">internal</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Hashtable</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.apache.commons.logging.Log</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.apache.commons.logging.LogFactory</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.osgi.service.component.ComponentContext</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authenticator.customauth.CustomAuthenticator</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.user.core.service.RealmService</span><span style="color: #333333;">;</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * @scr.component name="identity.application.authenticator.customauth.component" immediate="true"</span>
<span style="color: #888888;"> * @scr.reference name="realm.service"</span>
<span style="color: #888888;"> * interface="org.wso2.carbon.user.core.service.RealmService"cardinality="1..1"</span>
<span style="color: #888888;"> * policy="dynamic" bind="setRealmService" unbind="unsetRealmService"</span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">CustomAuthenticatorServiceComponent</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> Log log <span style="color: #333333;">=</span> LogFactory<span style="color: #333333;">.</span><span style="color: #0000cc;">getLog</span><span style="color: #333333;">(</span>CustomAuthenticatorServiceComponent<span style="color: #333333;">.</span><span style="color: #0000cc;">class</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> RealmService realmService<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">activate</span><span style="color: #333333;">(</span>ComponentContext ctxt<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
CustomAuthenticator customAuth <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> CustomAuthenticator<span style="color: #333333;">();</span>
Hashtable<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> String<span style="color: #333333;">></span> props <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Hashtable<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> String<span style="color: #333333;">>();</span>
ctxt<span style="color: #333333;">.</span><span style="color: #0000cc;">getBundleContext</span><span style="color: #333333;">().</span><span style="color: #0000cc;">registerService</span><span style="color: #333333;">(</span>ApplicationAuthenticator<span style="color: #333333;">.</span><span style="color: #0000cc;">class</span><span style="color: #333333;">.</span><span style="color: #0000cc;">getName</span><span style="color: #333333;">(),</span> customAuth<span style="color: #333333;">,</span> props<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>log<span style="color: #333333;">.</span><span style="color: #0000cc;">isDebugEnabled</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
log<span style="color: #333333;">.</span><span style="color: #0000cc;">info</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"CustomAuthenticator bundle is activated"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">deactivate</span><span style="color: #333333;">(</span>ComponentContext ctxt<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>log<span style="color: #333333;">.</span><span style="color: #0000cc;">isDebugEnabled</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
log<span style="color: #333333;">.</span><span style="color: #0000cc;">info</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"CustomAuthenticator bundle is deactivated"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setRealmService</span><span style="color: #333333;">(</span>RealmService realmService<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
log<span style="color: #333333;">.</span><span style="color: #0000cc;">debug</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Setting the Realm Service"</span><span style="color: #333333;">);</span>
CustomAuthenticatorServiceComponent<span style="color: #333333;">.</span><span style="color: #0000cc;">realmService</span> <span style="color: #333333;">=</span> realmService<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">unsetRealmService</span><span style="color: #333333;">(</span>RealmService realmService<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
log<span style="color: #333333;">.</span><span style="color: #0000cc;">debug</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"UnSetting the Realm Service"</span><span style="color: #333333;">);</span>
CustomAuthenticatorServiceComponent<span style="color: #333333;">.</span><span style="color: #0000cc;">realmService</span> <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> RealmService <span style="color: #0066bb; font-weight: bold;">getRealmService</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> realmService<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<br />
2. CustomAuthenticator<br />
<br />
This is where your actual authentication logic is implemented<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> org<span style="color: #333333;">.</span><span style="color: #0000cc;">wso2</span><span style="color: #333333;">.</span><span style="color: #0000cc;">carbon</span><span style="color: #333333;">.</span><span style="color: #0000cc;">identity</span><span style="color: #333333;">.</span><span style="color: #0000cc;">application</span><span style="color: #333333;">.</span><span style="color: #0000cc;">authenticator</span><span style="color: #333333;">.</span><span style="color: #0000cc;">customauth</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.io.IOException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Map</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.servlet.http.HttpServletRequest</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.servlet.http.HttpServletResponse</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.apache.commons.logging.Log</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.apache.commons.logging.LogFactory</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authentication.framework.AbstractApplicationAuthenticator</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authentication.framework.AuthenticatorFlowStatus</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authentication.framework.LocalApplicationAuthenticator</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authentication.framework.config.ConfigurationFacade</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authentication.framework.exception.InvalidCredentialsException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authentication.framework.exception.LogoutFailedException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.application.authenticator.customauth.internal.CustomAuthenticatorServiceComponent</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.base.IdentityException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.identity.core.util.IdentityUtil</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.user.api.UserRealm</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.user.core.UserStoreManager</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.wso2.carbon.utils.multitenancy.MultitenantUtils</span><span style="color: #333333;">;</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * Username Password based Authenticator</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">CustomAuthenticator</span> <span style="color: #008800; font-weight: bold;">extends</span> AbstractApplicationAuthenticator
<span style="color: #008800; font-weight: bold;">implements</span> LocalApplicationAuthenticator <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">final</span> <span style="color: #333399; font-weight: bold;">long</span> serialVersionUID <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">192277307414921623L</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> Log log <span style="color: #333333;">=</span> LogFactory<span style="color: #333333;">.</span><span style="color: #0000cc;">getLog</span><span style="color: #333333;">(</span>CustomAuthenticator<span style="color: #333333;">.</span><span style="color: #0000cc;">class</span><span style="color: #333333;">);</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">canHandle</span><span style="color: #333333;">(</span>HttpServletRequest request<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
String userName <span style="color: #333333;">=</span> request<span style="color: #333333;">.</span><span style="color: #0000cc;">getParameter</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"username"</span><span style="color: #333333;">);</span>
String password <span style="color: #333333;">=</span> request<span style="color: #333333;">.</span><span style="color: #0000cc;">getParameter</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"password"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>userName <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span> <span style="color: #333333;">&&</span> password <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> AuthenticatorFlowStatus <span style="color: #0066bb; font-weight: bold;">process</span><span style="color: #333333;">(</span>HttpServletRequest request<span style="color: #333333;">,</span>
HttpServletResponse response<span style="color: #333333;">,</span> AuthenticationContext context<span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">throws</span> AuthenticationFailedException<span style="color: #333333;">,</span> LogoutFailedException <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>context<span style="color: #333333;">.</span><span style="color: #0000cc;">isLogoutRequest</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> AuthenticatorFlowStatus<span style="color: #333333;">.</span><span style="color: #0000cc;">SUCCESS_COMPLETED</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">super</span><span style="color: #333333;">.</span><span style="color: #0000cc;">process</span><span style="color: #333333;">(</span>request<span style="color: #333333;">,</span> response<span style="color: #333333;">,</span> context<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">initiateAuthenticationRequest</span><span style="color: #333333;">(</span>HttpServletRequest request<span style="color: #333333;">,</span>
HttpServletResponse response<span style="color: #333333;">,</span> AuthenticationContext context<span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">throws</span> AuthenticationFailedException <span style="color: #333333;">{</span>
String loginPage <span style="color: #333333;">=</span> ConfigurationFacade<span style="color: #333333;">.</span><span style="color: #0000cc;">getInstance</span><span style="color: #333333;">().</span><span style="color: #0000cc;">getAuthenticationEndpointURL</span><span style="color: #333333;">();</span>
String queryParams <span style="color: #333333;">=</span> FrameworkUtils
<span style="color: #333333;">.</span><span style="color: #0000cc;">getQueryStringWithFrameworkContextId</span><span style="color: #333333;">(</span>context<span style="color: #333333;">.</span><span style="color: #0000cc;">getQueryParams</span><span style="color: #333333;">(),</span>
context<span style="color: #333333;">.</span><span style="color: #0000cc;">getCallerSessionKey</span><span style="color: #333333;">(),</span>
context<span style="color: #333333;">.</span><span style="color: #0000cc;">getContextIdentifier</span><span style="color: #333333;">());</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
String retryParam <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">""</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>context<span style="color: #333333;">.</span><span style="color: #0000cc;">isRetrying</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
retryParam <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">"&authFailure=true&authFailureMsg=login.fail.message"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
response<span style="color: #333333;">.</span><span style="color: #0000cc;">sendRedirect</span><span style="color: #333333;">(</span>response<span style="color: #333333;">.</span><span style="color: #0000cc;">encodeRedirectURL</span><span style="color: #333333;">(</span>loginPage <span style="color: #333333;">+</span> <span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"?"</span> <span style="color: #333333;">+</span> queryParams<span style="color: #333333;">))</span>
<span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"&authenticators="</span> <span style="color: #333333;">+</span> getName<span style="color: #333333;">()</span> <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">":"</span> <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"LOCAL"</span> <span style="color: #333333;">+</span> retryParam<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>IOException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">AuthenticationFailedException</span><span style="color: #333333;">(</span>e<span style="color: #333333;">.</span><span style="color: #0000cc;">getMessage</span><span style="color: #333333;">(),</span> e<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">processAuthenticationResponse</span><span style="color: #333333;">(</span>HttpServletRequest request<span style="color: #333333;">,</span>
HttpServletResponse response<span style="color: #333333;">,</span> AuthenticationContext context<span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">throws</span> AuthenticationFailedException <span style="color: #333333;">{</span>
String username <span style="color: #333333;">=</span> request<span style="color: #333333;">.</span><span style="color: #0000cc;">getParameter</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"username"</span><span style="color: #333333;">);</span>
String password <span style="color: #333333;">=</span> request<span style="color: #333333;">.</span><span style="color: #0000cc;">getParameter</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"password"</span><span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">boolean</span> isAuthenticated <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #888888;">// Check the authentication</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">int</span> tenantId <span style="color: #333333;">=</span> IdentityUtil<span style="color: #333333;">.</span><span style="color: #0000cc;">getTenantIdOFUser</span><span style="color: #333333;">(</span>username<span style="color: #333333;">);</span>
UserRealm userRealm <span style="color: #333333;">=</span> CustomAuthenticatorServiceComponent<span style="color: #333333;">.</span><span style="color: #0000cc;">getRealmService</span><span style="color: #333333;">()</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">getTenantUserRealm</span><span style="color: #333333;">(</span>tenantId<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>userRealm <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
UserStoreManager userStoreManager <span style="color: #333333;">=</span> <span style="color: #333333;">(</span>UserStoreManager<span style="color: #333333;">)</span>userRealm<span style="color: #333333;">.</span><span style="color: #0000cc;">getUserStoreManager</span><span style="color: #333333;">();</span>
isAuthenticated <span style="color: #333333;">=</span> userStoreManager<span style="color: #333333;">.</span><span style="color: #0000cc;">authenticate</span><span style="color: #333333;">(</span>MultitenantUtils<span style="color: #333333;">.</span><span style="color: #0000cc;">getTenantAwareUsername</span><span style="color: #333333;">(</span>username<span style="color: #333333;">),</span>password<span style="color: #333333;">);</span>
Map<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> String<span style="color: #333333;">></span> parameterMap <span style="color: #333333;">=</span> getAuthenticatorConfig<span style="color: #333333;">().</span><span style="color: #0000cc;">getParameterMap</span><span style="color: #333333;">();</span>
String blockSPLoginClaim <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>parameterMap <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
blockSPLoginClaim <span style="color: #333333;">=</span> parameterMap<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"BlockSPLoginClaim"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>blockSPLoginClaim <span style="color: #333333;">==</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
blockSPLoginClaim <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">"http://wso2.org/claims/blockSPLogin"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>log<span style="color: #333333;">.</span><span style="color: #0000cc;">isDebugEnabled</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
log<span style="color: #333333;">.</span><span style="color: #0000cc;">debug</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"BlockSPLoginClaim has been set as : "</span> <span style="color: #333333;">+</span> blockSPLoginClaim<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
String blockSPLogin <span style="color: #333333;">=</span> userStoreManager<span style="color: #333333;">.</span><span style="color: #0000cc;">getUserClaimValue</span><span style="color: #333333;">(</span>MultitenantUtils<span style="color: #333333;">.</span><span style="color: #0000cc;">getTenantAwareUsername</span><span style="color: #333333;">(</span>username<span style="color: #333333;">),</span>
blockSPLoginClaim<span style="color: #333333;">,</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">boolean</span> isBlockSpLogin <span style="color: #333333;">=</span> Boolean<span style="color: #333333;">.</span><span style="color: #0000cc;">parseBoolean</span><span style="color: #333333;">(</span>blockSPLogin<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>isAuthenticated <span style="color: #333333;">&&</span> isBlockSpLogin<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>log<span style="color: #333333;">.</span><span style="color: #0000cc;">isDebugEnabled</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
log<span style="color: #333333;">.</span><span style="color: #0000cc;">debug</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"user authentication failed due to user is blocked for the SP"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">AuthenticationFailedException</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"SPs are blocked"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">AuthenticationFailedException</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Cannot find the user realm for the given tenant: "</span> <span style="color: #333333;">+</span> tenantId<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>IdentityException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
log<span style="color: #333333;">.</span><span style="color: #0000cc;">error</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"CustomAuthentication failed while trying to get the tenant ID of the use"</span><span style="color: #333333;">,</span> e<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">AuthenticationFailedException</span><span style="color: #333333;">(</span>e<span style="color: #333333;">.</span><span style="color: #0000cc;">getMessage</span><span style="color: #333333;">(),</span> e<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>org<span style="color: #333333;">.</span><span style="color: #0000cc;">wso2</span><span style="color: #333333;">.</span><span style="color: #0000cc;">carbon</span><span style="color: #333333;">.</span><span style="color: #0000cc;">user</span><span style="color: #333333;">.</span><span style="color: #0000cc;">api</span><span style="color: #333333;">.</span><span style="color: #0000cc;">UserStoreException</span> e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
log<span style="color: #333333;">.</span><span style="color: #0000cc;">error</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"CustomAuthentication failed while trying to authenticate"</span><span style="color: #333333;">,</span> e<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">AuthenticationFailedException</span><span style="color: #333333;">(</span>e<span style="color: #333333;">.</span><span style="color: #0000cc;">getMessage</span><span style="color: #333333;">(),</span> e<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(!</span>isAuthenticated<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>log<span style="color: #333333;">.</span><span style="color: #0000cc;">isDebugEnabled</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
log<span style="color: #333333;">.</span><span style="color: #0000cc;">debug</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"user authentication failed due to invalid credentials."</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">InvalidCredentialsException</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
context<span style="color: #333333;">.</span><span style="color: #0000cc;">setSubject</span><span style="color: #333333;">(</span>username<span style="color: #333333;">);</span>
String rememberMe <span style="color: #333333;">=</span> request<span style="color: #333333;">.</span><span style="color: #0000cc;">getParameter</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"chkRemember"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>rememberMe <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span> <span style="color: #333333;">&&</span> <span style="background-color: #fff0f0;">"on"</span><span style="color: #333333;">.</span><span style="color: #0000cc;">equals</span><span style="color: #333333;">(</span>rememberMe<span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
context<span style="color: #333333;">.</span><span style="color: #0000cc;">setRememberMe</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">retryAuthenticationEnabled</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">getContextIdentifier</span><span style="color: #333333;">(</span>HttpServletRequest request<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> request<span style="color: #333333;">.</span><span style="color: #0000cc;">getParameter</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"sessionDataKey"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">getFriendlyName</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> CustomAuthenticatorConstants<span style="color: #333333;">.</span><span style="color: #0000cc;">AUTHENTICATOR_FRIENDLY_NAME</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">getName</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> CustomAuthenticatorConstants<span style="color: #333333;">.</span><span style="color: #0000cc;">AUTHENTICATOR_NAME</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
3. CustomAuthenticatorConstants<br />
<br />
This is a helper class to just to hold the constants you are using in your authenticaator<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> org<span style="color: #333333;">.</span><span style="color: #0000cc;">wso2</span><span style="color: #333333;">.</span><span style="color: #0000cc;">carbon</span><span style="color: #333333;">.</span><span style="color: #0000cc;">identity</span><span style="color: #333333;">.</span><span style="color: #0000cc;">application</span><span style="color: #333333;">.</span><span style="color: #0000cc;">authenticator</span><span style="color: #333333;">.</span><span style="color: #0000cc;">customauth</span><span style="color: #333333;">;</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * Constants used by the CustomAuthenticator</span>
<span style="color: #888888;"> *</span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">abstract</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">CustomAuthenticatorConstants</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">final</span> String AUTHENTICATOR_NAME <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">"CustomAuthenticator"</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">final</span> String AUTHENTICATOR_FRIENDLY_NAME <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">"custom"</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">final</span> String AUTHENTICATOR_STATUS <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">"CustomAuthenticatorStatus"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
Once you are done with these files, your authenticator is ready. Now you can build you OSGi bundle and place the bundle inside <CRBON_HOME>/repository/components/dropins.<br />
<br />
*sample pom.xml file [3] <br />
<br />
<b>Create new Claim</b><br />
<br />
Now you have to create a new claim in WSO2IS. To do this, log into the management console of WSO2IS and do the steps described in [1]. In this example, I am going to create new claim "Block SP Login".<br />
<br />
So, goto configuration section of the management console click on "Claim Management", then select "http://wso2.org/claims" Dialect<br />
<br />
Click on "Add New Claim Mapping", and fill the details related to your claim.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #e3d2d2; color: #a61717;">Display Name</span> <span style="background-color: #e3d2d2; color: #a61717;">Block</span> <span style="background-color: #e3d2d2; color: #a61717;">SP</span> <span style="background-color: #e3d2d2; color: #a61717;">Login</span></pre>
<pre style="line-height: 125%; margin: 0;"><pre><span style="background-color: #e3d2d2; color: #a61717;">Description</span> <span style="background-color: #e3d2d2; color: #a61717;">Block</span> <span style="background-color: #e3d2d2; color: #a61717;">SP</span> <span style="background-color: #e3d2d2; color: #a61717;">Login</span></pre>
<span style="color: red;">Claim Uri http</span>:<span style="color: blue;">//wso2.org/claims/blockSPLogin</span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="background-color: #e3d2d2; color: #a61717;">Mapped</span> <span style="background-color: #e3d2d2; color: #a61717;">Attribute</span> <span style="background-color: #e3d2d2; color: #a61717;">(s)</span> <span style="background-color: #e3d2d2; color: #a61717;">localityName</span>
<span style="background-color: #e3d2d2; color: #a61717;">Regular</span> <span style="background-color: #e3d2d2; color: #a61717;">Expression</span>
<span style="background-color: #e3d2d2; color: #a61717;">Display</span> <span style="background-color: #e3d2d2; color: #a61717;">Order</span> <span style="background-color: #e3d2d2; color: #a61717;">0</span>
<span style="background-color: #e3d2d2; color: #a61717;">Supported</span> <span style="background-color: #e3d2d2; color: #a61717;">by</span> <span style="background-color: #e3d2d2; color: #a61717;">Default</span> <span style="background-color: #e3d2d2; color: #a61717;">true</span>
<span style="background-color: #e3d2d2; color: #a61717;">Required</span> <span style="background-color: #e3d2d2; color: #a61717;">false</span>
<span style="background-color: #e3d2d2; color: #a61717;">Read-only</span> <span style="background-color: #e3d2d2; color: #a61717;">false</span>
</pre>
</div>
<div>
<br />
Now, your new claim is ready in WSO2IS. As you select "Supported by Default" as true, this claim will be available in your user profile. So you will see this field appear, when you try to create a user, but this field in not mandatory as you didn't specify it as "Required"<br />
<br />
<b>Change application-authentication.xml</b><br />
<br />
There is another configuration change you have to do, as it is going to take the claim name from the configuration file (CustomAuthenticator.java, 107-114). Add the information about the your new claim into repository/conf/security/application-authentication.xml
<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;">1
2
3</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><AuthenticatorConfig</span> <span style="color: #0000cc;">name=</span><span style="background-color: #fff0f0;">"CustomAuthenticator"</span> <span style="color: #0000cc;">enabled=</span><span style="background-color: #fff0f0;">"true"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><Parameter</span> <span style="color: #0000cc;">name=</span><span style="background-color: #fff0f0;">"BlockSPLoginClaim"</span><span style="color: #007700;">></span>http://wso2.org/claims/blockSPLogin<span style="color: #007700;"></Parameter></span>
<span style="color: #007700;"></AuthenticatorConfig></span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
If you check the code CustomAuthenticator.java line,107-128. You will see in the <span style="background-color: white; color: #0066bb; font-weight: bold; line-height: 16.25px;">processAuthenticationResponse</span>, in addition to authenticating the user from the user store, it checks for the new claim,<br />
<br />
So, this finishes the, basic steps to setup your custom authentication. Now you have to setup new Service Provider in WSO2IS and set you custom authentication to it. So that when ever your SP try to authenticate a user from WSO2IS, it will use your custom authenticator.<br />
<br />
<b>Create Service Provider and set the Authenticator</b><br />
<br />
Follow the basic steps given in [2] to create a new Service Provider.<br />
<br />
Then, goto, "Inbound Authentication Configuration"->"SAML2 Web SSO Configuration", and make the following changes,<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;">1
2
3
4
5
6</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #0000cc;">Issuer*</span> <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;"><name of you SP></span>
<span style="color: #0000cc;">Assertion Consumer URL</span> <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;"><http://localhost:8080/your-app/samlsso-home.jsp></span>
<span style="color: #0000cc;">Enable Response Signing</span> <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">true</span>
<span style="color: #0000cc;">Enable Assertion Signing</span> <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">true </span>
<span style="color: #0000cc;">Enable Single Logout</span> <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">true</span>
<span style="color: #0000cc;">Enable Attribute Profile</span> <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">true</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
Then goto, "Local & Outbound Authentication Configuration" section,<br />
select "Local Authentication" as the authentication type, and select your authenticator, here "custom".<br />
<br />
Now you have completed all the steps needed to setup your custom autheticator with your custom claims<br />
<br />
You can now start the WSO2IS, and start using your service. Meanwhile, change the value of the "Block SP Login" of a particular user and see the effect.<br />
<br />
<br />
[1] <a href="https://docs.wso2.com/display/IS500/Adding+New+Claim+Mapping">https://docs.wso2.com/display/IS500/Adding+New+Claim+Mapping</a><br />
[2] <a href="https://docs.wso2.com/display/IS500/Adding+a+Service+Provider">https://docs.wso2.com/display/IS500/Adding+a+Service+Provider</a><br />
[3] <a href="https://drive.google.com/file/d/0B25Kjdxz8EhCQktfdG5MYkFnTUk/view?usp=sharing">https://drive.google.com/file/d/0B25Kjdxz8EhCQktfdG5MYkFnTUk/view?usp=sharing</a><br />
<br />
<br /></div>
Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com9tag:blogger.com,1999:blog-7216691445130770849.post-58286828596325475742014-06-13T22:57:00.000+05:302014-06-13T23:02:36.222+05:30Mounting a remote repository (WSO2 GREG) to WSO2 ESBWSO2 Governance Registry [1] is basically a metadata repository which basically helps to store and manage metadata. WSO2 Enterprise Service Bus (WSO2 ESB) [2] is an integration middle-ware tool which is virtually capable of interconnecting ANYTHING.<br />
<br />
There several ways of mounting a remote repository to a WSO2 product (In this case WSO2 EB). You can find more information on [3]. In this post I am trying to explain, how to mount a remote repository to WSO2 ESB via JDBC-based configuration. <br />
<br />
In this approach you have to move the local DB of the WSO2 GREG to an external DB. So any change you do the registry will be reflected in the external DB. In this example I will be using a MYSQL database.<br />
<br />
<b>Moving WSO2 GREG repository to external DB</b><br />
<ol>
<li>Create a new database schema (regdb), a new user (wso2carbon) with
password (wso2carbon) and grant all permissions to wso2carbon.</li>
<li> Change the data source details of WSO2_CARBON_DB in
master-datasources.xml file, which is located in
GREG_HOME/repository/conf/datasources/, with your DB information.<br />
eg:<br />
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><datasource></span>
<span style="color: #007700;"><name></span>WSO2_CARBON_DB<span style="color: #007700;"></name></span>
<span style="color: #007700;"><description></span>The datasource used for registry and user manager<span style="color: #007700;"></description></span>
<span style="color: #007700;"><jndiConfig></span>
<span style="color: #007700;"><name></span>jdbc/WSO2CarbonDB<span style="color: #007700;"></name></span>
<span style="color: #007700;"></jndiConfig></span>
<span style="color: #007700;"><definition</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"RDBMS"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><configuration></span>
<span style="color: #007700;"><url></span>jdbc:mysql://localhost:3306/regdb<span style="color: #007700;"></url></span>
<span style="color: #007700;"><username></span>wso2carbon<span style="color: #007700;"></username></span>
<span style="color: #007700;"><password></span>wso2carbon<span style="color: #007700;"></password></span>
<span style="color: #007700;"><driverClassName></span>com.mysql.jdbc.Driver<span style="color: #007700;"></driverClassName></span>
<span style="color: #007700;"><maxActive></span>50<span style="color: #007700;"></maxActive></span>
<span style="color: #007700;"><maxWait></span>60000<span style="color: #007700;"></maxWait></span>
<span style="color: #007700;"><testOnBorrow></span>true<span style="color: #007700;"></testOnBorrow></span>
<span style="color: #007700;"><validationQuery></span>SELECT 1<span style="color: #007700;"></validationQuery></span>
<span style="color: #007700;"><validationInterval></span>30000<span style="color: #007700;"></validationInterval></span>
<span style="color: #007700;"></configuration></span>
<span style="color: #007700;"></definition></span>
<span style="color: #007700;"></datasource></span>
</pre>
</td></tr>
</tbody></table>
</div>
</li>
<li>Start the server with -Dseup argument <br />eg: <br />
./wso2server.sh -Dseup<br />
<br />
This will setup all the tables in the DB and all the initial configurations
needed. And WSO2 GREG is now ready with external registry.<br />
</li>
</ol>
<br />
<b>Mounting remote repository to WSO2 ESB</b><br />
<ol>
<li>Add a new data source to the master-datasources.xml file, which is
located in ESB_HOME/repository/conf/datasources/. NOTE: This entry is
exactly same as the record we entered in WSO2 GREG, except for the
<name> and <jndiConfig>/<name><br />
eg: <br />
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><datasource></span>
<span style="color: #007700;"><name></span>WSO2_REG_DB<span style="color: #007700;"></name></span>
<span style="color: #007700;"><description></span>The datasource used for registry and user manager<span style="color: #007700;"></description></span>
<span style="color: #007700;"><jndiConfig></span>
<span style="color: #007700;"><name></span>jdbc/WSO2RegDB<span style="color: #007700;"></name></span>
<span style="color: #007700;"></jndiConfig></span>
<span style="color: #007700;"><definition</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"RDBMS"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><configuration></span>
<span style="color: #007700;"><url></span>jdbc:mysql://localhost:3306/regdb<span style="color: #007700;"></url></span>
<span style="color: #007700;"><username></span>wso2carbon<span style="color: #007700;"></username></span>
<span style="color: #007700;"><password></span>wso2carbon<span style="color: #007700;"></password></span>
<span style="color: #007700;"><driverClassName></span>com.mysql.jdbc.Driver<span style="color: #007700;"></driverClassName></span>
<span style="color: #007700;"><maxActive></span>50<span style="color: #007700;"></maxActive></span>
<span style="color: #007700;"><maxWait></span>60000<span style="color: #007700;"></maxWait></span>
<span style="color: #007700;"><testOnBorrow></span>true<span style="color: #007700;"></testOnBorrow></span>
<span style="color: #007700;"><validationQuery></span>SELECT 1<span style="color: #007700;"></validationQuery></span>
<span style="color: #007700;"><validationInterval></span>30000<span style="color: #007700;"></validationInterval></span>
<span style="color: #007700;"></configuration></span>
<span style="color: #007700;"></definition></span>
<span style="color: #007700;"></datasource></span>
</pre>
</td></tr>
</tbody></table>
</div>
</li>
<li>Add a new record <dbConfig> to registry.xml, which is located at ESB_HOME/repository/conf/<br />
eg:<br />
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;">1
2
3</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><dbConfig</span> <span style="color: #0000cc;">name=</span><span style="background-color: #fff0f0;">"wso2remoteregistry"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><dataSource></span>jdbc/WSO2RegDB<span style="color: #007700;"></dataSource></span>
<span style="color: #007700;"></dbConfig></span>
</pre>
</td></tr>
</tbody></table>
</div>
</li>
<li>Uncomment the <remoteInstance> and <mount> sections in the registry.xml file and update with the correct details.<br />
eg:<br />
<br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><remoteInstance</span> <span style="color: #0000cc;">url=</span><span style="background-color: #fff0f0;">"https://localhost:9443/registry"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><id></span>instanceid<span style="color: #007700;"></id></span>
<span style="color: #007700;"><dbConfig></span>wso2remoteregistry<span style="color: #007700;"></dbConfig></span>
<span style="color: #007700;"><readOnly></span>false<span style="color: #007700;"></readOnly></span>
<span style="color: #007700;"><enableCache></span>true<span style="color: #007700;"></enableCache></span>
<span style="color: #007700;"><registryRoot></span>/<span style="color: #007700;"></registryRoot></span>
<span style="color: #007700;"><cacheId></span>wso2carbon@jdbc:mysql://localhost:3306/regdb<span style="color: #007700;"></cacheId></span>
<span style="color: #007700;"></remoteInstance></span>
<span style="color: #007700;"><mount</span> <span style="color: #0000cc;">path=</span><span style="background-color: #fff0f0;">"/_system/config/nodes"</span> <span style="color: #0000cc;">overwrite=</span><span style="background-color: #fff0f0;">"true"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><instanceId></span>instanceid<span style="color: #007700;"></instanceId></span>
<span style="color: #007700;"><targetPath></span>/_system/nodes<span style="color: #007700;"></targetPath></span>
<span style="color: #007700;"></mount></span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
</li>
<li>Start the WSO2 ESB. If you are running both WSO2 GREG and WSO2 ESB on
same machine, you will have to set port offsets on one of them.eg:<br />
./wso2server.sh -DportOffset=2</li>
</ol>
Once you start the WSO2 ESB, you should be able to access the remote repository from the WSO2 ESB.<br />
<br />
To verify this go to resource browser of the admin console of the WSO2 ESB, https://localhost:9445, which you can find on the following URL if you start with postOffset=2<br />
<br />
Then browse resources,<br />
<ol>
<li>You should find the mounted remote repository in _system/config/nodes with a folder icon having a blue arrow in it</li>
<li> You should find the mounted remote repository details on _system/local/repository/components/org.wso2.carbon.registry/mount</li>
</ol>
<br />
References: <br />
[1] http://wso2.com/products/governance-registry/<br />
[2] http://wso2.com/products/enterprise-service-bus/<br />
[3] https://docs.wso2.org/display/Governance460/Remote+Instance+and+Mount+Configuration+Details<br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com0tag:blogger.com,1999:blog-7216691445130770849.post-45522598332395944272014-05-23T23:20:00.001+05:302014-05-25T08:51:26.488+05:30Simple SecurePasswordVault in JavaThere are some instances, you want to store your passwords in files to be used by the programs or scripts. But storing your passwords in plain text is not a good idea. Use the SecurePasswordVault to encrypt your passwords before storing and get it decrypted when you want to use it.<br />
<br />
You can use the SecurePasswordVault described here to store any number of encrypted passwords. Passwords are stored as key value pairs.<br />
<br />
Key - any name given by the user for the password<br />
Value - encrypted password<br />
<br />
SecurePasswordVault will create a file with the given name in the working directory if it doesn't exist. If a file exist then the information in that file will be read.<br />
<br />
Passwords are encrypted using the MAC address of the network card. SecurePasswordVault will use the first network card MAC which is not the loop back interface. So the encrypted file can only be decrypted with that particular MAC address. If you want to reset the pass word details, just delete the password file and run the SecurePasswordVault.<br />
<br />
You can download the sample code from the following Github repository<br />
https://github.com/jsdjayanga/secure_password <br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">wso2</span><span style="color: #333333;">.</span><span style="color: #0000cc;">devgov</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.bouncycastle.util.encoders.Base64</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.crypto.*</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.crypto.spec.SecretKeySpec</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.io.*</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.net.NetworkInterface</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.net.SocketException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.security.InvalidKeyException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.security.NoSuchAlgorithmException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.security.Security</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.*</span><span style="color: #333333;">;</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;">* Created by jayanga on 3/31/14.</span>
<span style="color: #888888;">*/</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">SecurePasswordVault</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">final</span> <span style="color: #333399; font-weight: bold;">int</span> AES_KEY_LEN <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">32</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">final</span> <span style="color: #333399; font-weight: bold;">int</span> PASSWORD_LEN <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">256</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">boolean</span> initialized<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">final</span> String secureFile<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">final</span> <span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[]</span> networkHardwareHaddress<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> Map<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> String<span style="color: #333333;">></span> secureDataMap<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> secureDataList<span style="color: #333333;">;</span>
SecretKeySpec secretKey<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">SecurePasswordVault</span><span style="color: #333333;">(</span>String filename<span style="color: #333333;">,</span> String<span style="color: #333333;">[]</span> secureData<span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">throws</span> IOException <span style="color: #333333;">{</span>
Security<span style="color: #333333;">.</span><span style="color: #0000cc;">addProvider</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> org<span style="color: #333333;">.</span><span style="color: #0000cc;">bouncycastle</span><span style="color: #333333;">.</span><span style="color: #0000cc;">jce</span><span style="color: #333333;">.</span><span style="color: #0000cc;">provider</span><span style="color: #333333;">.</span><span style="color: #0000cc;">BouncyCastleProvider</span><span style="color: #333333;">());</span>
initialized <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
secureFile <span style="color: #333333;">=</span> filename<span style="color: #333333;">;</span>
networkHardwareHaddress <span style="color: #333333;">=</span> SecurePasswordVault<span style="color: #333333;">.</span><span style="color: #0000cc;">readNetworkHardwareAddress</span><span style="color: #333333;">();</span>
secureDataMap <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> HashMap<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> String<span style="color: #333333;">>();</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">secureDataList</span> <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><</span>String<span style="color: #333333;">>(</span>secureData<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">);</span>
Collections<span style="color: #333333;">.</span><span style="color: #0000cc;">addAll</span><span style="color: #333333;">(</span>secureDataList<span style="color: #333333;">,</span> secureData<span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[]</span> key <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[</span>AES_KEY_LEN<span style="color: #333333;">];</span>
Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">fill</span><span style="color: #333333;">(</span>key<span style="color: #333333;">,</span> <span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">)</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> index <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span> index <span style="color: #333333;"><</span> networkHardwareHaddress<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">;</span> index<span style="color: #333333;">++){</span>
key<span style="color: #333333;">[</span>index<span style="color: #333333;">]</span> <span style="color: #333333;">=</span> networkHardwareHaddress<span style="color: #333333;">[</span>index<span style="color: #333333;">];</span>
<span style="color: #333333;">}</span>
secretKey <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> SecretKeySpec<span style="color: #333333;">(</span>key<span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"AES"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(!</span>isInitialized<span style="color: #333333;">()){</span>
readSecureData<span style="color: #333333;">(</span>secureDataList<span style="color: #333333;">);</span>
persistSecureData<span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
readSecureDataFromFile<span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">isInitialized</span><span style="color: #333333;">(){</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>initialized <span style="color: #333333;">==</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">){</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span><span style="color: #008800; font-weight: bold;">else</span><span style="color: #333333;">{</span>
File file <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> File<span style="color: #333333;">(</span>secureFile<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>file<span style="color: #333333;">.</span><span style="color: #0000cc;">exists</span><span style="color: #333333;">()){</span>
initialized <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">return</span> initialized<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[]</span> <span style="color: #0066bb; font-weight: bold;">readNetworkHardwareAddress</span><span style="color: #333333;">()</span> <span style="color: #008800; font-weight: bold;">throws</span> SocketException <span style="color: #333333;">{</span>
Enumeration<span style="color: #333333;"><</span>NetworkInterface<span style="color: #333333;">></span> networkInterfaceEnumeration <span style="color: #333333;">=</span> NetworkInterface<span style="color: #333333;">.</span><span style="color: #0000cc;">getNetworkInterfaces</span><span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>networkInterfaceEnumeration <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">){</span>
NetworkInterface networkInterface <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">while</span> <span style="color: #333333;">(</span>networkInterfaceEnumeration<span style="color: #333333;">.</span><span style="color: #0000cc;">hasMoreElements</span><span style="color: #333333;">()){</span>
networkInterface <span style="color: #333333;">=</span> networkInterfaceEnumeration<span style="color: #333333;">.</span><span style="color: #0000cc;">nextElement</span><span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(!</span>networkInterface<span style="color: #333333;">.</span><span style="color: #0000cc;">isLoopback</span><span style="color: #333333;">()){</span>
<span style="color: #008800; font-weight: bold;">break</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>networkInterface <span style="color: #333333;">==</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">){</span>
networkInterface <span style="color: #333333;">=</span> networkInterfaceEnumeration<span style="color: #333333;">.</span><span style="color: #0000cc;">nextElement</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[]</span> hwaddr <span style="color: #333333;">=</span> networkInterface<span style="color: #333333;">.</span><span style="color: #0000cc;">getHardwareAddress</span><span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">return</span> hwaddr<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span><span style="color: #008800; font-weight: bold;">else</span><span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">RuntimeException</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Cannot initialize. Failed to generate unique id."</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[]</span> <span style="color: #0066bb; font-weight: bold;">encrypt</span><span style="color: #333333;">(</span>String word<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[]</span> password <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[</span>PASSWORD_LEN<span style="color: #333333;">];</span>
Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">fill</span><span style="color: #333333;">(</span>password<span style="color: #333333;">,</span> <span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">)</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[]</span> pw <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">];</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
pw <span style="color: #333333;">=</span> word<span style="color: #333333;">.</span><span style="color: #0000cc;">getBytes</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"UTF-8"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> index <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span> index <span style="color: #333333;"><</span> pw<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">;</span> index<span style="color: #333333;">++){</span>
password<span style="color: #333333;">[</span>index<span style="color: #333333;">]</span> <span style="color: #333333;">=</span> pw<span style="color: #333333;">[</span>index<span style="color: #333333;">];</span>
<span style="color: #333333;">}</span>
<span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[]</span> cipherText <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[</span>password<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">];</span>
Cipher cipher <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
cipher <span style="color: #333333;">=</span> Cipher<span style="color: #333333;">.</span><span style="color: #0000cc;">getInstance</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"AES/ECB/NoPadding"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
cipher<span style="color: #333333;">.</span><span style="color: #0000cc;">init</span><span style="color: #333333;">(</span>Cipher<span style="color: #333333;">.</span><span style="color: #0000cc;">ENCRYPT_MODE</span><span style="color: #333333;">,</span> secretKey<span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">int</span> ctLen <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
ctLen <span style="color: #333333;">=</span> cipher<span style="color: #333333;">.</span><span style="color: #0000cc;">update</span><span style="color: #333333;">(</span>password<span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">,</span> password<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">,</span> cipherText<span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">);</span>
ctLen <span style="color: #333333;">+=</span> cipher<span style="color: #333333;">.</span><span style="color: #0000cc;">doFinal</span><span style="color: #333333;">(</span>cipherText<span style="color: #333333;">,</span> ctLen<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> cipherText<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>ShortBufferException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>BadPaddingException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>IllegalBlockSizeException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>InvalidKeyException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>NoSuchAlgorithmException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>NoSuchPaddingException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>UnsupportedEncodingException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> String <span style="color: #0066bb; font-weight: bold;">decrypt</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[]</span> cipherText<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[]</span> plainText <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">byte</span><span style="color: #333333;">[</span>PASSWORD_LEN<span style="color: #333333;">];</span>
Cipher cipher <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
cipher <span style="color: #333333;">=</span> Cipher<span style="color: #333333;">.</span><span style="color: #0000cc;">getInstance</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"AES/ECB/NoPadding"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
cipher<span style="color: #333333;">.</span><span style="color: #0000cc;">init</span><span style="color: #333333;">(</span>Cipher<span style="color: #333333;">.</span><span style="color: #0000cc;">DECRYPT_MODE</span><span style="color: #333333;">,</span> secretKey<span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">int</span> plainTextLen <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
plainTextLen <span style="color: #333333;">=</span> cipher<span style="color: #333333;">.</span><span style="color: #0000cc;">update</span><span style="color: #333333;">(</span>cipherText<span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">,</span> PASSWORD_LEN<span style="color: #333333;">,</span> plainText<span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
plainTextLen <span style="color: #333333;">+=</span> cipher<span style="color: #333333;">.</span><span style="color: #0000cc;">doFinal</span><span style="color: #333333;">(</span>plainText<span style="color: #333333;">,</span> plainTextLen<span style="color: #333333;">);</span>
String password <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> String<span style="color: #333333;">(</span>plainText<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> password<span style="color: #333333;">.</span><span style="color: #0000cc;">trim</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>IllegalBlockSizeException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>BadPaddingException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>ShortBufferException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>InvalidKeyException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>NoSuchAlgorithmException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>NoSuchPaddingException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">readSecureData</span><span style="color: #333333;">(</span>List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> secureDataList<span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">throws</span> IOException <span style="color: #333333;">{</span>
BufferedReader bufferRead <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> BufferedReader<span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> InputStreamReader<span style="color: #333333;">(</span>System<span style="color: #333333;">.</span><span style="color: #0000cc;">in</span><span style="color: #333333;">));</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> index <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span> index <span style="color: #333333;"><</span> secureDataList<span style="color: #333333;">.</span><span style="color: #0000cc;">size</span><span style="color: #333333;">();</span> index<span style="color: #333333;">++){</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Please enter the value for :"</span> <span style="color: #333333;">+</span> secureDataList<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span>index<span style="color: #333333;">));</span>
String value <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> String<span style="color: #333333;">(</span>Base64<span style="color: #333333;">.</span><span style="color: #0000cc;">encode</span><span style="color: #333333;">(</span>encrypt<span style="color: #333333;">(</span>bufferRead<span style="color: #333333;">.</span><span style="color: #0000cc;">readLine</span><span style="color: #333333;">())));</span>
secureDataMap<span style="color: #333333;">.</span><span style="color: #0000cc;">put</span><span style="color: #333333;">(</span>secureDataList<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span>index<span style="color: #333333;">),</span> value<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">getSecureData</span><span style="color: #333333;">(</span>String key<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
String value <span style="color: #333333;">=</span> secureDataMap<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span>key<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>value <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">){</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0066bb; font-weight: bold;">decrypt</span><span style="color: #333333;">(</span>Base64<span style="color: #333333;">.</span><span style="color: #0000cc;">decode</span><span style="color: #333333;">(</span>value<span style="color: #333333;">.</span><span style="color: #0000cc;">getBytes</span><span style="color: #333333;">()));</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">RuntimeException</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Given key is unknown. [key="</span> <span style="color: #333333;">+</span> key <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"]"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">readSecureDataFromFile</span><span style="color: #333333;">()</span> <span style="color: #008800; font-weight: bold;">throws</span> IOException <span style="color: #333333;">{</span>
BufferedReader br <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> BufferedReader<span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> FileReader<span style="color: #333333;">(</span>secureFile<span style="color: #333333;">));</span>
String line<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">while</span> <span style="color: #333333;">((</span>line <span style="color: #333333;">=</span> br<span style="color: #333333;">.</span><span style="color: #0000cc;">readLine</span><span style="color: #333333;">())</span> <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">){</span>
<span style="color: #333399; font-weight: bold;">int</span> dividerPoint <span style="color: #333333;">=</span> line<span style="color: #333333;">.</span><span style="color: #0000cc;">indexOf</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"="</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>dividerPoint <span style="color: #333333;">></span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">){</span>
secureDataMap<span style="color: #333333;">.</span><span style="color: #0000cc;">put</span><span style="color: #333333;">(</span>line<span style="color: #333333;">.</span><span style="color: #0000cc;">substring</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">,</span> dividerPoint<span style="color: #333333;">),</span> line<span style="color: #333333;">.</span><span style="color: #0000cc;">substring</span><span style="color: #333333;">(</span>dividerPoint <span style="color: #333333;">+</span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">persistSecureData</span><span style="color: #333333;">()</span> <span style="color: #008800; font-weight: bold;">throws</span> IOException <span style="color: #333333;">{</span>
FileWriter fileWriter <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> FileWriter<span style="color: #333333;">(</span>secureFile<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span>String key <span style="color: #333333;">:</span> secureDataMap<span style="color: #333333;">.</span><span style="color: #0000cc;">keySet</span><span style="color: #333333;">()){</span>
fileWriter<span style="color: #333333;">.</span><span style="color: #0000cc;">append</span><span style="color: #333333;">(</span>key <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"="</span> <span style="color: #333333;">+</span> secureDataMap<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span>key<span style="color: #333333;">)</span> <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"\n"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
fileWriter<span style="color: #333333;">.</span><span style="color: #0000cc;">close</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<br />Jayanga Dissanayakehttp://www.blogger.com/profile/03611971874005993155noreply@blogger.com1