tag:blogger.com,1999:blog-15133302648115581202024-03-14T19:09:23.057+05:30Prabhath's BlogPrabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.comBlogger38125tag:blogger.com,1999:blog-1513330264811558120.post-43759853833122441342020-08-15T08:03:00.003+05:302020-08-15T08:14:41.222+05:30Lambda function to tag all available snapshots<p>Following Lambda function will add a defined tag(s) to all available EBS snapshots.</p><p>Lambda function should be associated with an IAM Role with necessary permissions. E.g with EC2FullAccess permissions.</p>
<pre><code class="python">
from __future__ import print_function
import json
import boto3
import logging
#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.ERROR)
#define the connection region
ec2 = boto3.resource('ec2', region_name="eu-west-2")
ec = boto3.client('ec2', 'eu-west-2')
snapshots = ec.describe_snapshots(MaxResults=1000,OwnerIds=['put account id here'])['Snapshots']
#Set this to True if you don't want the function to perform any actions
debugMode = False
def lambda_handler(event, context):
for snapshot in snapshots:
print('Sanpshot id '+snapshot['SnapshotId'])
snapshotx = ec2.Snapshot(snapshot['SnapshotId'])
snapshotx.create_tags(Tags=[{'Key': 'Tag1','Value': 'Value1'},{'Key': 'Tag2','Value': 'Value2'}])
return 'complete'
</code></pre>Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0Houston, TX, USA29.7604267 -95.36980281.4501928638211545 -130.5260528 58.070660536178849 -60.2135528tag:blogger.com,1999:blog-1513330264811558120.post-15130134120452541152019-08-13T01:43:00.000+05:302019-08-13T01:43:32.945+05:30Hostname and mail configurations in LinuxBy default mail command will set the from address as $user@$hostname in Linux. If you want to change this behavior using the configuration file (/etc/mail.rc ) this is how it can be done.<br />
<br />
Set the entire from address like below.<br />
<br />
Add this to the configuration file.<br />
set from="from address"<br />
<br />
If you need to change the hostname part only, add this.<br />
set hostname="hostname"<br />
<br />Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-24779557747153218712018-07-19T15:34:00.001+05:302018-07-20T09:40:09.625+05:30Getting abbreviated name with daylight saving of a timezone in Java 8Recently I wanted to get the timezone abbreviation of a timezone as a string. There are many tutorials which cover that. But in all those, timezone is given in one of the following formats. "America/Los-Angeles" or "PT".<br />
<br />
But I needed to get the abbreviation with the daylight saving such as "PST" or "PDT". Finally, I was able to get it using the following method.<br />
<br />
<i>ZonedDateTime currentTime;</i><br />
<i>boolean isDayLightSavingEnabled =currentTime.getZone().getRules().isDaylightSavings(currentTime.toInstant());</i><br />
<i>TimeZone timeZone = TimeZone.getTimeZone(currentTime.getZone());</i><br />
<i>String timeZoneName = timeZone.getDisplayName(isDayLightSavingEnabled, TimeZone.SHORT);</i><br />
<i>System.out.println(</i><i>timeZoneName);</i><br />
<i><br /></i>
Output will be<br />
<b><i>PST</i></b>Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-44402265778276251962016-08-04T11:06:00.005+05:302016-08-04T11:06:58.030+05:30Fix issues in upgrading Ubuntu 14.04 to 16.04When I upgraded my Ubuntu 14.04 PC to 16.04 it restarted and terminal screen was loaded without any GUI. I fixed this by doing following steps.<br />
<br />
<ul>
<li>First login with by entering login name(this is your username) and password</li>
<li>Run sudo apt-get update</li>
<li>If above step fails run the command which is given in the error. Usually this is something related to dpkg.</li>
<li>Run sudo apt-get upgrade -f</li>
<li>If confirmations occurs while upgrading accept all</li>
</ul>
<div>
After upgrading is done restart the computer.</div>
<div>
<br /></div>
<div>
If it still goes to terminal run <b>sudo <span style="background-color: #eff0f1; color: #111111; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; white-space: inherit;">do-release-upgrade</span></b></div>
Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-77098699517879129912015-06-19T10:39:00.006+05:302015-06-19T10:42:08.971+05:30Accessing all highcharts in a page at onceRecently I needed to call a reflow method of all hi-charts in the page for a particular event. I used following method for that.<br />
<br />
function redrawHighcharts() {<br />
for (var i = 0; i < Highcharts.charts.length; i++) {<br />
Highcharts.charts[i].reflow();<br />
}<br />
}Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com1tag:blogger.com,1999:blog-1513330264811558120.post-87024201096665835192015-05-11T13:11:00.002+05:302015-05-11T17:15:18.777+05:30Fixing Unicode issues in Pentaho CDARecently I encountered problem when sending a query parameter with unicode text to Pentaho CDA. For these type of queries CDA returns an empty result set although there are matching items. After some research I fixed this issue by adding an additional parameter to the JDBC connection url. Now JDBC connection is like below.<br />
<br />
<DataSources><br />
<Connection id="1" type="sql.jdbc"><br />
<Driver>com.mysql.jdbc.Driver</Driver><br />
<Url>jdbc:mysql://host:3306/DB?useUnicode=true&amp;characterEncoding=UTF-8</Url><br />
<User>user</User><br />
<Pass>pass</Pass><br />
</Connection><br />
</DataSources><br />
<br />
<br />Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-27711736562116069342015-03-14T19:27:00.002+05:302015-03-14T19:28:53.749+05:30Creating a Pentaho BI server cluster<i>Note - This is applicable to Pentaho BI server community edition 5.x only.</i><br />
<br />
Pentaho BI server provides a large set of features which are essential for BI applications. To use this in production we might need to create a CDA cluster to maintain high availability as well as load balancing.<br />
To create a cluster we need to configure BI server instances to use a common data source to store configurations. I configured the following setup for this.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXpbGlKb1ikwGaLUUWgGbKoFgW2-X1GzzFP6vzUUWK-3Z-3Ohv7aeVRdoXkZ5eKrI5iYr6gsOCJG1xePXHPjecUMjyysaFHsyzgUU_bIrO3wNi3lv_Ktb0LqrjGBFIxYSnD-MoFFuArNk/s1600/Screenshot+from+2015-03-14+19:23:13.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXpbGlKb1ikwGaLUUWgGbKoFgW2-X1GzzFP6vzUUWK-3Z-3Ohv7aeVRdoXkZ5eKrI5iYr6gsOCJG1xePXHPjecUMjyysaFHsyzgUU_bIrO3wNi3lv_Ktb0LqrjGBFIxYSnD-MoFFuArNk/s1600/Screenshot+from+2015-03-14+19:23:13.png" height="309" width="320" /></a></div>
<br />
<b><br /></b>
<b>Follow these steps to create the cluster.</b><br />
<br />
<ol style="background-color: white; color: #333333; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; margin: 10px 0px 0px;">
<li><span style="display: block;" talk-marker="4">Install MySQL servers and setup master master replication.</span></li>
<li><span style="display: block;" talk-marker="5">Make sure you have installed Oracle Java 7 in all nodes. Using other java versions will cause runtime errors.</span></li>
<li><span style="display: block;" talk-marker="6">Follow <a class="external-link" href="http://infocenter.pentaho.com/help/index.jsp" rel="nofollow" style="color: #3b73af; text-decoration: none;">this</a> document to to install a CDA instance. Make sure to follow the document named "Install with Your Own BA Repository" and follow the configurations related to MySQL. </span></li>
<li><span style="display: block;" talk-marker="7">Start the server and install all the components needed.</span></li>
<li><span style="display: block;" talk-marker="8">Modify the cluster documentation as mentioned in this document. <a class="external-link" href="https://help.pentaho.com/Documentation/5.2/0P0/000/060" rel="nofollow" style="color: #3b73af; text-decoration: none;">https://help.pentaho.com/Documentation/5.2/0P0/000/060</a></span><ol style="list-style-type: lower-alpha; margin: 0px;">
<li><span style="display: block;" talk-marker="9">This document is missing the information related Quartz clustering with MySQL. Only PostgresQL configuration is there. Use the following configuration instead.</span><ol style="list-style-type: lower-roman; margin: 0px;">
<li><span style="display: block;" talk-marker="10"><div style="padding: 0px;" talk-marker="11">
#_replace_jobstore_properties</div>
<div style="margin-top: 10px; padding: 0px;" talk-marker="12">
org.quartz.jobStore.misfireThreshold = 60000<br />
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate<br />
org.quartz.jobStore.useProperties = false<br />
org.quartz.jobStore.dataSource = myDS<br />
org.quartz.jobStore.tablePrefix = QRTZ5_<br />
org.quartz.jobStore.isClustered = true<br />
org.quartz.jobStore.clusterCheckinInterval = 20000</div>
</span></li>
</ol>
</li>
<li><span style="display: block;" talk-marker="13"><div style="padding: 0px;" talk-marker="14">
When configuring Jackrabbit clustering replace unique ID in<Cluster id="Unique_ID"> with a ID like CDA1.</div>
</span></li>
</ol>
</li>
<li><span style="display: block;" talk-marker="15">Recompress the CDA folder and copy it to all other nodes.</span></li>
<li><span style="display: block;" talk-marker="16">Extract the file and replace unique ID in<Cluster id="Unique_ID"> accordingly. eg- CDA2, CDA3</span></li>
<li><span style="display: block;" talk-marker="17">Start each node and make sure there are no error logs in tomcat/logs/pentaho.log.</span></li>
<li><span style="display: block;" talk-marker="18">Make sure all CDA changes are replicated between cluster nodes.</span></li>
<li><span style="display: block;" talk-marker="19">When configure the ELB make sure to enable sticky sessions. <a class="external-link" href="http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/US_StickySessions.html" rel="nofollow" style="color: #3b73af; text-decoration: none;">http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/US_StickySessions.html</a>.</span></li>
<li><span style="display: block;" talk-marker="20">Alerts should be set to monitor CDA instances as well as MySQL replication.</span></li>
</ol>
Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com4tag:blogger.com,1999:blog-1513330264811558120.post-50530500691545455812015-02-15T21:12:00.000+05:302015-02-15T22:03:10.233+05:30Deploying a HA Redis setup<br/>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkAGi8dEorTVOUNOXgX45l_OeI0iTwQVd_ql-yVHZDe1aPYgo5JExqoPfyaSFzlCRsrr8XS846VHRc2do-P3KsRiRRy_6CkWStHILtwStWDYWmDXFWnc4S5PtSa6LO86rK7Q-899-BVxs/s1600/Resis+HA.jpg"
imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0"
src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkAGi8dEorTVOUNOXgX45l_OeI0iTwQVd_ql-yVHZDe1aPYgo5JExqoPfyaSFzlCRsrr8XS846VHRc2do-P3KsRiRRy_6CkWStHILtwStWDYWmDXFWnc4S5PtSa6LO86rK7Q-899-BVxs/s1600/Resis+HA.jpg"
height="176" width="400"/></a></div>
<br/>
Sentinel process takes the responsibility of electing a slave as master if a failure occurs. For more information refer
<a class="external-link" href="http://redis.io/topics/sentinel" rel="nofollow"
style="color: #3b73af; text-decoration: none;">this</a>.</div>
<li><span style="display: block;" talk-marker="6">Install Redis in each node. Following methods can be used to install Redis.</span>
<ol style="list-style-type: lower-alpha; margin: 0px;">
<li><span style="display: block;" talk-marker="7">Using Ubuntu repositories. </span>
<ol style="list-style-type: lower-roman; margin: 0px;">
<li><span style="display: block;" talk-marker="8">sudo apt-get install redis-server</span></li>
</ol>
</li>
<li><span style="display: block;" talk-marker="9">Manual installation</span>
<ol style="list-style-type: lower-roman; margin: 0px;">
<li><span style="display: block;" talk-marker="10">You can download a Redis distribution from this page <a
class="external-link" href="http://redis.io/download" rel="nofollow"
style="color: #3b73af; text-decoration: none;">http://redis.io/download</a>. Follow the instructions on this page to setup Redis using the downloaded setup <a
class="external-link"
href="https://www.digitalocean.com/community/tutorials/how-to-install-and-use-redis"
rel="nofollow" style="color: #3b73af; text-decoration: none;">https://www.digitalocean.com/community/tutorials/how-to-install-and-use-redis</a></span>
</li>
</ol>
</li>
</ol>
</li>
<li><span style="display: block;" talk-marker="11">Set requirepass property to set the password in the configuration file in /etc/redis. Note that this should be same in all nodes.</span>
</li>
<li><span style="display: block;" talk-marker="12">Set Up replication</span>
<ol style="list-style-type: lower-alpha; margin: 0px;">
<li><span style="display: block;"
talk-marker="13">Set the following properties to set replication on slaves.</span>
<ol style="list-style-type: lower-roman; margin: 0px;">
<li><span style="display: block;" talk-marker="14">slaveof <masterip> <masterport></span>
</li>
<li><span style="display: block;" talk-marker="15">masterauth <master-password> //The same password we used previously</span>
</li>
</ol>
</li>
<li><span style="display: block;" talk-marker="16">Set masterauth property also in the master in case of master goes down and later joins as a slave (Once a slave is elected as a master).</span>
</li>
</ol>
</li>
<li><div style="padding: 0px;" talk-marker="18">
To test whether the replication is working, log in to redis console of the master using redis-cli command and add a
data. Log in to all slaves and see the entered data is there.
</div>
</li>
<li><span style="display: block;" talk-marker="19">Setup sentinel in each node</span>
<ol style="list-style-type: lower-alpha; margin: 0px;">
<li><span style="display: block;" talk-marker="20">create a file named sentinel.conf where your redis configurations exists.</span>
</li>
<li><span style="display: block;" talk-marker="21">add the following content to sentinel.conf (change values according to your setup). </span>
<ol style="list-style-type: lower-roman; margin: 0px;">
<li><span style="display: block;" talk-marker="22"><div style="padding: 0px;" talk-marker="23">
sentinel monitor mymaster <ip> <port> 2<br/>
sentinel down-after-milliseconds mymaster 60000<br/>
sentinel failover-timeout mymaster 180000<br/>
sentinel parallel-syncs mymaster 1
</div>
<div style="margin-top: 10px; padding: 0px;" talk-marker="24">
It tells Redis Sentinel that the master is <ip>, the master's name is "mymaster", and start failover if
more than two Redis Sentinel has detected the master failed.
</div>
</span></li>
</ol>
</li>
</ol>
</li>
<li><span style="display: block;" talk-marker="25"><div style="padding: 0px;" talk-marker="26">
Start sentinel process in each node using the following command.
</div>
<div style="margin-top: 10px; padding: 0px;" talk-marker="27">
sudo redis-sentinel <path to the configuration file> &</div>
<div style="margin-top: 10px; padding: 0px;" talk-marker="28">
<em>To stop a sentinel process use the following command.</em></div>
<div style="margin-top: 10px; padding: 0px;" talk-marker="29">
<em>sudo redis-sentinel <path to the configuration file> shutdown</em></div>
</span></li>
<li><span style="display: block;" talk-marker="30"><div style="padding: 0px;" talk-marker="31">
To test failover you can kill master process and see still you can write to the cluster using your client
application. Not that when master changes sentinel will rewrite the redis configurations in each node. To get back
to the original state you will have to revert back the changes.
</div>
</span></li>
</ol>
<div>
<span style="font-size: 14px; line-height: 20px;"><br/></span></span></div>
<h3>
<span style="font-size: 14px; line-height: 20px;">How to access above setup programmatically.</span></span>
</h3>
<div>
<span style="font-size: 14px; line-height: 20px;">I will be using Java with <a
href="https://github.com/xetorthio/jedis"
target="_blank">Jedis</a> library for this example. </span></span></div>
<div>
<pre style="background-color: white; font-family: 'DejaVu Sans Mono';"><span style="font-size: x-small;">HashSet<String> sentinels = <span
style="color: navy; font-weight: bold;">new </span>HashSet<>();
JedisPoolConfig <span style="background-color: #ffe4ff;">jedisPoolConfig</span> = getJedisPoolConfig();
String[] nodes = //This should contain addresses to sentinel processes;
<span style="color: navy; font-weight: bold;">for </span>(String node : nodes) {
sentinels.add(node);
}
<span style="color: #660e7a; font-weight: bold;">pool </span>= <span style="color: navy; font-weight: bold;">new </span>JedisSentinelPool(getConfig(<span
style="color: #660e7a; font-style: italic;">sentinelMasterKey</span>), sentinels, <span
style="background-color: #36363d;">jedisPoolConfig</span>);</span></pre>
<pre style="background-color: white; font-family: 'DejaVu Sans Mono';"><pre
style="font-family: 'DejaVu Sans Mono';"><span style="font-size: x-small;">Jedis jedis = <span
style="color: #660e7a; font-weight: bold;">pool</span>.getResource();
jedis.auth(<span style="background-color: #2a2a2f;">redisPassword</span>);</span></pre>
<span style="font-size: x-small;">jedis.set(key, value);</span>
<br>
<br>
<span style="font-size: x-small;"><span style="color: navy; font-weight: bold;">private </span>JedisPoolConfig getJedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = <span style="color: navy; font-weight: bold;">new </span>JedisPoolConfig();
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getInt(<span
style="color: green; font-weight: bold;">"redis.pool.maxIdle"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setMaxIdle(<span style="font-style: italic;">application</span>().configuration().getInt(<span
style="color: green; font-weight: bold;">"redis.pool.maxIdle"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getInt(<span
style="color: green; font-weight: bold;">"redis.pool.minIdle"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setMinIdle(<span style="font-style: italic;">application</span>().configuration().getInt(<span
style="color: green; font-weight: bold;">"redis.pool.minIdle"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getInt(<span
style="color: green; font-weight: bold;">"redis.pool.maxTotal"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setMaxTotal(<span style="font-style: italic;">application</span>().configuration().getInt(<span
style="color: green; font-weight: bold;">"redis.pool.maxTotal"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getInt(<span
style="color: green; font-weight: bold;">"redis.pool.maxWaitMillis"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setMaxWaitMillis(<span
style="font-style: italic;">application</span>().configuration().getInt(<span
style="color: green; font-weight: bold;">"redis.pool.maxWaitMillis"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getBoolean(<span
style="color: green; font-weight: bold;">"redis.pool.testOnBorrow"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setTestOnBorrow(<span style="font-style: italic;">application</span>().configuration().getBoolean(<span
style="color: green; font-weight: bold;">"redis.pool.testOnBorrow"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getBoolean(<span
style="color: green; font-weight: bold;">"redis.pool.testOnReturn"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setTestOnReturn(<span style="font-style: italic;">application</span>().configuration().getBoolean(<span
style="color: green; font-weight: bold;">"redis.pool.testOnReturn"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getBoolean(<span
style="color: green; font-weight: bold;">"redis.pool.testWhileIdle"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setTestWhileIdle(<span style="font-style: italic;">application</span>().configuration().getBoolean(<span
style="color: green; font-weight: bold;">"redis.pool.testWhileIdle"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getLong(<span
style="color: green; font-weight: bold;">"redis.pool.timeBetweenEvictionRunsMillis"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(<span style="font-style: italic;">application</span>().configuration().getLong(<span
style="color: green; font-weight: bold;">"redis.pool.timeBetweenEvictionRunsMillis"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getInt(<span
style="color: green; font-weight: bold;">"redis.pool.numTestsPerEvictionRun"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setNumTestsPerEvictionRun(<span style="font-style: italic;">application</span>().configuration().getInt(<span
style="color: green; font-weight: bold;">"redis.pool.numTestsPerEvictionRun"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getLong(<span
style="color: green; font-weight: bold;">"redis.pool.minEvictableIdleTimeMillis"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setMinEvictableIdleTimeMillis(<span style="font-style: italic;">application</span>().configuration().getLong(<span
style="color: green; font-weight: bold;">"redis.pool.minEvictableIdleTimeMillis"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getLong(<span
style="color: green; font-weight: bold;">"redis.pool.softMinEvictableIdleTimeMillis"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(<span style="font-style: italic;">application</span>().configuration().getLong(<span
style="color: green; font-weight: bold;">"redis.pool.softMinEvictableIdleTimeMillis"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getBoolean(<span
style="color: green; font-weight: bold;">"redis.pool.lifo"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setLifo(<span style="font-style: italic;">application</span>().configuration().getBoolean(<span
style="color: green; font-weight: bold;">"redis.pool.lifo"</span>));
}
<span style="color: navy; font-weight: bold;">if </span>(<span style="font-style: italic;">application</span>().configuration().getBoolean(<span
style="color: green; font-weight: bold;">"redis.pool.blockWhenExhausted"</span>) != <span
style="color: navy; font-weight: bold;">null</span>) {
jedisPoolConfig.setBlockWhenExhausted(<span style="font-style: italic;">application</span>().configuration().getBoolean(<span
style="color: green; font-weight: bold;">"redis.pool.blockWhenExhausted"</span>));
}
<span style="color: navy; font-weight: bold;">return </span>jedisPoolConfig;
}</span></pre>
</pre>
</div>
Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-26177848714954415192015-02-05T18:42:00.003+05:302015-02-10T22:30:10.740+05:30Bind a remote server's port to a local port<p dir="ltr">If you have a remote server (say in Amazon EC2) you might want to access a particular port of that server. But there can be situations where it is not that port is not globally open. If you do not want to bother making it globally open you can use it by binding it to a local port of your workstation via ssh. Following command will bind port 9000 of remote machine to your local port 8000. As an example if it is web server you can easily access it by typing localhost:8000 in your web browser. </p>
<p dir="ltr">ssh -L 8000:localhost:9000 <u>username@host</u></p>
Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-82170728371825708582014-09-07T20:42:00.001+05:302014-09-07T20:53:51.748+05:30Implementing session timeout in playframework<div style="text-align: justify;">
According to play documentation "<span style="background-color: #fff7d6;"><span style="color: #4c4742; font-family: Helvetica Neue, Helvetica, sans-serif;"><span style="font-size: 15px; line-height: 20px;">There is no technical timeout for the Session. It expires when the user closes the web browser. If you need a functional timeout for a specific application, just store a timestamp into the user Session and use it however your application needs (e.g. for a maximum session duration, maximum inactivity duration, etc.)."</span></span></span></div>
<span style="font-family: inherit;">
</span>
<br />
<div style="text-align: justify;">
So I used the following way to implement a session timeout. Following custom authenticator class was used to implement this.</div>
<br />
<pre class="brush: java; highlight: [5, 15]; html-script: true"> public class ValidateUserSessionAction extends Security.Authenticator{
@Override
public String getUsername(Http.Context ctx) {
long currentTime=System.currentTimeMillis();
long timeOut=Long.parseLong(Play.application().configuration().getString("sessionTimeout")) * 1000 * 60;
String temp=ctx.session().get(Constants.LAST_SEEN_KEY);
if (temp == null) {
temp = String.valueOf(currentTime);
}
if((currentTime-Long.parseLong(temp))<timeOut) {
//If multiple instances are running, time should be synchronized between nodes
ctx.session().put(Constants.LAST_SEEN_KEY, String.valueOf(System.currentTimeMillis()));
return ctx.session().get(Constants.SESSION_USER_KEY);
}else{
ctx.session().clear();
return null;
}
}
@Override
public Result onUnauthorized(Http.Context ctx) {
return redirect(controllers.routes.UserController.signIn());
}
}
</pre>
<br />
Above authenticator class can be used to validate user actions like below.<br />
<pre class="brush: java; highlight: [5, 15]; html-script: true">
@Security.Authenticated(ValidateUserSessionAction.class)
public static F.Promise<result< updateEmail() {
//do something
return ok();
}
</pre>
Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-54334750328462251742014-09-06T21:26:00.003+05:302014-09-09T12:19:17.409+05:30Optimizing Apache Storm deployment<div style="text-align: justify;">
Recently we happen to run some pretty large <a href="https://storm.incubator.apache.org/" target="_blank">Storm</a> topologies in a Storm cluster which runs on Linux. When we running it there were two main issues occurred due to system limitations. First one was logged in Storm logs as,</div>
<br />
<span style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">“java.lang.OutOfMemoryError : unable to create new native Thread”.</span><br />
<span style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;"><br /></span>
<br />
<div style="text-align: justify;">
We fixed this problem by increasing the <a href="http://www.unixmantra.com/2013/06/resource-limits-on-unix-systems-ulimit.html" target="_blank">Ulimit</a> for Storm. Usually storm spawns processes with an user named Storm. So we have to increase the Ulimit for storm user. You can see the ulimits using command "ulimit -u". To increase the ulimits you can follow an approach like <a href="https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Tuning_and_Optimizing_Red_Hat_Enterprise_Linux_for_Oracle_9i_and_10g_Databases/sect-Oracle_9i_and_10g_Tuning_Guide-Setting_Shell_Limits_for_the_Oracle_User-Limiting_Maximum_Number_of_Processes_Available_for_the_Oracle_User.html" target="_blank">this</a>.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The second problem was communication link failures between Storm nodes as well as communication link failures between other services (e.g. external APIs, databases, etc). To resolve this problem we had to enable tcp time wait reuse and also we increased the port range for tcp. hat can be done in following manner.</div>
<br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> Put these to /etc/sysctl.conf file and issue 'sysctl -f'</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> net.ipv4.tcp_tw_reuse = 1</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> net.ipv4.ip_local_port_range = 18000 65000</span><br />
<br />
Note that we have to apply this setting for every node in Storm cluster. In addition to there were some errors like below due to Netty timeouts (Storm uses Netty as underlying messaging layer).<br />
<br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">java.lang.RuntimeException: java.lang.RuntimeException: Client is being closed, and does not take requests any more</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>at backtype.storm.utils.DisruptorQueue.consumeBatchToCursor(DisruptorQueue.java:107) ~[storm-core-0.9.1.2.1.4.0-632.jar:0.9.1.2.1.4.0-632]</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>at backtype.storm.utils.DisruptorQueue.consumeBatchWhenAvailable(DisruptorQueue.java:78)</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br /></span>
<span style="text-align: justify;">This can be corrected by increasing netty timeout values in configurations.</span>Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com2tag:blogger.com,1999:blog-1513330264811558120.post-58794760541218964942014-06-13T14:08:00.001+05:302014-06-13T14:08:26.245+05:30Introduction to Play-Framework modulesPlay framework inherently support modularization. In other words we can develop play modules and reuse them in different play applications. <a href="http://www.objectify.be/wordpress/?p=363">This</a> article provides a good guide on how to do that. But that is little bit outdated for latest Play distributions. I will describe the changes which is needed to be done to create a play module on latest play versions.<br />
Playframework no longer has a play console. Instead of that it uses Typesafe activator. So you need to download activator and add activator to your environment path.<br />
You can create boilerplate code for a play app using the template called Just play java. In my case I needed to create a authentication module. So I created a Play action called auth in controllers package. Then to publish the module go the project directory and issue <b>clean </b>command. Then issue <b>publish-local </b>command. If it is successful you will get a output like this.<br />
<br />
<i>[info] <span class="Apple-tab-span" style="white-space: pre;"> </span>published ivy to /home/prabhath/.ivy2/local/authmodule/authmodule_2.10/1.0-SNAPSHOT/ivys/ivy.xml</i><br />
<i>[success] Total time: 15 s, completed Jun 13, 2014 10:49:13 AM</i><br />
<i><br /></i>
Then create a new application which will use the previously created auth module. To add the dependency to new project update the build.sbt file. Content of my build.sbt file is like below.<br />
<br />
<i>name := """Test"""</i><br />
<i><br /></i>
<i>version := "1.0-SNAPSHOT"</i><br />
<i><br /></i>
<i>libraryDependencies ++= Seq(</i><br />
<i> javaCore, // The core Java API</i><br />
<i> "junit" % "junit" % "4.8.2",</i><br />
<i> "authmodule"%"authmodule_2.10"%"1.0-SNAPSHOT"</i><br />
<i>)</i><br />
<i><br /></i>
<i>play.Project.playJavaSettings</i><br />
<i><br /></i>
Next you have to tell the path of your local repository to the application. To do that edit plugins.sbt file in /<your project>/project directory. I added the following line to plugins.sbt file.<br />
<br />
resolvers += "Local Play Repository" at "file://home/prabhathp/.ivy2/local/"<br />
<br />
Now build the project and you will be able to use the classes in your module/controllers package inside classes in your controllers. Note that you do not need to add imports when reusing module components.<br />
<div>
<br /></div>
<div>
<br /></div>
Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-64712007358186848932014-04-22T15:11:00.001+05:302014-04-22T15:20:44.278+05:30Add a attachment to CouchDB with play framework JavaAPII tired to write a rest API which act as a mediator between clients and CouchDB API. This was done using Playframework's Java API. I wrote the following controller method which takes a POST request which contains file name, content-type and file to be stored as parameters and send as a PUT request to CouchDB.<br />
<br />
<br />
<pre class="brush: java; highlight: [5, 15]; html-script: true">
public static Result putAttachment(){
Http.MultipartFormData body = request().body().asMultipartFormData();
Http.MultipartFormData.FilePart picture = body.getFile("picture");
String fileName=null;
String contentType=null;
File file=null;
if (picture != null) {
fileName = picture.getFilename();
contentType = picture.getContentType();
file = picture.getFile();
} else {
//TODO implement
}
String restServiceUrl = "http://127.0.0.1:5984/test/a/"+fileName;
F.Promise<play.libs.WS.Response> future =play.libs.WS.url(restServiceUrl)
.setContentType(contentType).setQueryParameter("rev","7-00e01f7f430649d984fc8488358a1953")
.put(file);
return Results.ok(future.get(50000000).getBody());
}
</pre>Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-30191805501552247932014-04-18T16:02:00.000+05:302014-04-18T16:03:00.725+05:30Fixing Error "Failed to load VMMR0.r0 (VERR_SUPLIB_WORLD_WRITABLE)" in VirtualboxRecently I to installed Oracle Virtualbox on my Ubuntu 12.04 computer to run Hortanworks sandbox. When I am trying to start the Sanbox it gave me an error like this.<br />
<br />
<span style="background-color: #e1ebf2; color: #333333; font-family: 'Lucida Grande', 'Trebuchet MS', Verdana, Helvetica, Arial, sans-serif; font-size: 13px; line-height: 18.200000762939453px;">Failed to load VMMR0.r0 (VERR_SUPLIB_WORLD_WRITABLE).</span><br />
<span style="background-color: #e1ebf2; color: #333333; font-family: 'Lucida Grande', 'Trebuchet MS', Verdana, Helvetica, Arial, sans-serif; font-size: 13px; line-height: 18.200000762939453px;">Unknown error creating VM (VERR_SUPLIB_WORLD_WRITABLE).</span><br />
<span style="background-color: #e1ebf2; color: #333333; font-family: 'Lucida Grande', 'Trebuchet MS', Verdana, Helvetica, Arial, sans-serif; font-size: 13px; line-height: 18.200000762939453px;"><br /></span>
What it says is /usr/bin forlder is world writable. Usually this is not world writable. But somehow I have unintentionally changed the permissions. I fixed this using the following command.<br />
<br />
<b>chmod o-w /usr/bin</b>Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com3tag:blogger.com,1999:blog-1513330264811558120.post-36868201810825122002014-02-21T07:56:00.002+05:302014-02-21T08:00:24.041+05:30Integrating an Akka.io actor system with Play framework (A distributed message classifier with Akka.io and Play framework)<div class="separator" style="clear: both; text-align: center;">
<a href="http://i161.photobucket.com/albums/t229/climaxorg/Screenshot316_zpsd6104204.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://i161.photobucket.com/albums/t229/climaxorg/Screenshot316_zpsd6104204.png" height="223" width="400" /></a></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Recently we developed a distributed message classifier. This can process rapid burst of text (email, twitter feeds, etc) and get results.<br />
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://i43.tinypic.com/ibhzja.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://i43.tinypic.com/ibhzja.jpg" height="132" width="400" /></a></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
To implement the processing part we used <a href="http://akka.io/">Akka.io</a> which is an event based distributed framework. Currently to analyse messages we use a web service. But any other processing mechanism (local or remote) can be easily plugged to this.</div>
<div style="text-align: justify;">
After implementing the core we needed to publish messages to the applications using REST. To do that we integrated it with <a href="http://www.playframework.com/" target="_blank">Play Framework</a>. Also I developed an admin panel using MVC features provided by it.</div>
<div style="text-align: justify;">
While integrating the existing actor system with play framework several conflicts occurred. One reason for this was Play framwork internally uses an actor system too. So I had to do several tweaks and change some configurations. I am not going to discuss each of them here because it will be too lengthy. This is the URL to our git-hub repo <a href="https://github.com/Buddhima/MessageClassifier">https://github.com/Buddhima/MessageClassifier</a>. You can fork it and see those by your self. It has a very descriptive user guide and wiki which explains from architecture to UI. Hope this will be useful to starters who are trying to get familiarized with Akka.io and Play framework.</div>
Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-59081923321588798872013-11-14T19:07:00.000+05:302013-11-14T19:08:54.348+05:30Enable access from other hosts to a MySQL server<div style="text-align: justify;">
<span style="font-family: inherit;">Recently I deployed a MySQL server. But there was a problem that I could not access the data base from a application which was hosted in another server. The reason for this is by default MySQL server does not accepts requests from other hosts except the localhost.</span></div>
<span style="font-family: inherit;">To solve this problem we have to do two tasks.</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<ol>
</ol>
<ul>
<li><span style="font-family: inherit;">Create a user which has permissions to read and write to a database from a different host</span></li>
</ul>
<div style="margin-bottom: 0in;">
<b><span style="font-family: inherit;">GRANT ALL PRIVILEGES
</span></b></div>
<b><span style="font-family: inherit;">
</span></b>
<div style="margin-bottom: 0in;">
<b><span style="font-family: inherit;">ON database.*
</span></b></div>
<b><span style="font-family: inherit;">
</span></b>
<div style="margin-bottom: 0in;">
<b><span style="font-family: inherit;">TO ‘user’@'yourremotehost'
</span></b></div>
<b><span style="font-family: inherit;">
</span></b>
<div style="margin-bottom: 0in;">
<b><span style="font-family: inherit;">IDENTIFIED BY 'newpassword';</span></b></div>
<div style="margin-bottom: 0in;">
<br /></div>
<div style="margin-bottom: 0in;">
<span style="font-family: inherit;">As an exmaple following query </span><span style="font-family: inherit;">enables root user to access all the databases from any host.</span></div>
<div style="margin-bottom: 0in;">
<span style="font-family: inherit;"><br /></span></div>
<div style="margin-bottom: 0in;">
<b><span style="font-family: inherit;">GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';</span></b></div>
<div style="margin-bottom: 0in;">
<span style="font-family: inherit;"><br /></span></div>
<div style="margin-bottom: 0in;">
<style type="text/css">PRE.ctl { font-family: "Lohit Hindi",monospace; }P { margin-bottom: 0.08in; }CODE.ctl { font-family: "Lohit Hindi",monospace; }</style>
</div>
<ul>
<li>Bind the ip adress</li>
</ul>
To do this you have to edit the MySQL configuration file. In Ubuntu this is <b>/etc/mysql/my.cnf</b><br />
There you can find an entry like<br />
<b> bind-address = 127.0.0.1 </b><br />
<b><br /></b>If you want to enable access from all the host just remove it. If you want to limit the hosts you can add entries like<br />
<b>bind-address = your ip</b><br />
<br />
After doing that you have to restart the MySQL server. In Ubuntu you can do that like below.<br />
<b>sudo /etc/init.d/mysql restart</b><br />
<br />
<br />Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-33633375894656458992013-11-02T20:22:00.004+05:302013-11-02T20:26:22.309+05:30Install Oracle Java Development Kit on Ubuntu <div style="text-align: justify;">
Installing Java SDK is very straight forward in Windows. Download Java installer double click it and follow the steps. But installing Java in Ubuntu is not easy like that.
In Ubuntu we can easily install Open JDK by providing following command. </div>
<br />
<b>sudo apt-get install openjdk-6-jdk</b><br />
<br />
<div style="text-align: justify;">
But this installs Open JDK not Oracle JDK. This is not a problem for entry level Java developers. But if you work in a production environment you cannot use Open JDK since most products use Oracle Java. Until recently the method I followed to install Oracle Java was downloading the tar.gz file form Oracle site and install it using the terminal. But this is very time consuming and there are lot of configurations has to be followed. But here is a very easy way to install the latest Oracle JDK in you machine without not needing to do any additional configurations. Just issue following three commands in the terminal to install Oracle JDK in your Ubuntu machine.</div>
<br />
<br />
<b>sudo add-apt-repository ppa:webupd8team/java</b><br />
<b>sudo apt-get update </b><br />
<b>sudo apt-get install oracle-java7-installer</b><br />
<b><br /></b>
<b><br /></b>
<b><br /></b>Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-46661607413286944522013-07-15T19:56:00.001+05:302013-07-16T01:15:26.172+05:30How to connect to WSO2 BAM with NodeJs using REST<div style="margin-bottom: 0in;">
Recently I wanted to use WSO2 BAM REST
API to with NodeJs. After playing sometime with NodeJs API I was able
to POST a stream definition to BAM using NodeJs with following
method.</div>
<pre class="brush: java; highlight: [5, 15]; html-script: true">
var https = require('https');
var auth = "Basic " + new Buffer('admin:admin').toString("base64"); //You need to replace admin:admin with your username and password
// prepare the header
var postheaders = {
'Content-Type': 'application/json',
'Accept': 'application/json',
"Authorization": auth
};
// the post options
var optionspost = {
host: '127.0.0.1',
port: '9443',
path: '/datareceiver/1.0.0/streams/',
method: 'POST',
rejectUnauthorized: 'false',
headers: postheaders
};
doPOSTRequest=function(optionspost,jsonObject){
// do the POST call
var reqPost = https.request(optionspost, function(res) {
console.log("statusCode: ", res.statusCode);
// uncomment it for header details
// console.log("headers: ", res.headers);
res.on('data', function(d) {
console.info('POST result:\n');
process.stdout.write(d);
console.info('\n\nPOST completed');
});
});
// write the json data
reqPost.write(jsonObject);
reqPost.end();
reqPost.on('error', function(e) {
console.error(e);
});
}
</pre>
<br />
An important ramark is you need to put
rejectUnauthorized: 'false' if you are using NodeJs default Cas.
Otherwise you may get an error saying [Error:
UNABLE_TO_VERIFY_LEAF_SIGNATURE]. This is because NodeJs rejects the
certificate provided by BAM in default configuration. This tweak can
be applied to any situation where you get the [Error:
UNABLE_TO_VERIFY_LEAF_SIGNATURE] when try to send a https request
using NodeJs.Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com1tag:blogger.com,1999:blog-1513330264811558120.post-23899040156134510452013-06-15T22:10:00.001+05:302013-06-15T22:15:55.445+05:30Facebook SDK for android - How to share Session throughout all activitiesA problem which I faced when using Facebook SDK for android was in my app login happens through one activity and posting to Facebook happens through another activity. So I had to retrieve the session which is opened in the LoginActitvity. Actually solution was simpler than I thought. To get the session from an another activity you can use the below method.<br />
<br />
<pre class="brush: java; highlight: [5, 15]; html-script: true">private void getSession() {
Session.openActiveSession(this, false, callback);
}
private Session.StatusCallback callback = new Session.StatusCallback() {
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
//Do something
}
}
};
</pre>
<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-74350123187739864592013-05-03T22:05:00.002+05:302013-11-02T20:34:20.852+05:30Adding a Foursquare, Google+ like sliding drawer to your android app<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
Currently Android SDK does not support such a feature. But this kind of designs are very popular between the users. But there are many open source libraries which facilitates sch omplementation. <a href="https://github.com/jfeinstein10/SlidingMenu">https://github.com/jfeinstein10/SlidingMenu</a> is an open sorce sliding drawer library which is used many poplar android applications. Using this is prettry simple.
This <a href="http://www.youtube.com/watch?v=OT76zDIeBe8" target="_blank">video</a> explains how to integrate with your android project. After the integration is done you can create a sliding drawer like this.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://i.stack.imgur.com/Ue0LS.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="http://i.stack.imgur.com/Ue0LS.png" width="400" /></a></div>
<br />
<br />
Add the following code to onCreate() method of your activity.<br />
<br />
<pre class="brush: java; highlight: [5, 15]; html-script: true"> menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidth(5);
menu.setFadeDegree(0.0f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setBehindWidth(150); //change width according to your device
menu.setMenu(R.layout.menu_frame);
</pre>
Here menu_frame is the layout of the sliding drawer. It has to be a frame layout and you can add any other layout to the frame layout.<br />
<br />
If your application has a actionbar you may need to link sliding drawer to the app icon like in Google+ and Youtube Android applications. What is usually happen is toggling the sliding drawer when the app icon is clicked. It can be implemented like this.<br />
<br />
<pre class="brush: java; highlight: [5, 15]; html-script: true">@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
menu.toggle();
return true;
}
return true;
}
</pre>
<br />
<br />Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com1tag:blogger.com,1999:blog-1513330264811558120.post-66340284628896085652012-11-28T10:56:00.001+05:302013-02-16T20:19:54.242+05:30Create new Java Class files at runtimeWhen we write programs usual method is to write all the code compile it and run. But when we try to complex things some times we may want to generate a new class and use it at runtime.
As an example I wanted to create a class which parse a xml message and create a new Java object from that data. This is used in web server. So that result object varies according to the web service which is deployed. The solution was to inspect the service at deployment time and create a new parser class according to that in the service deployment time.
It has to be noted that this parser had to be generated as a .class file and dynamically added to the classpath.
So the result was to use a bytecode manupulation library. There are few bytecode manuplulation libraries available as <a href="http://asm.ow2.org/">ASM</a> and <a href="http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/" target="_blank">JavaAssist</a>.<br />
I used JavaAssist for this. The reason for using JavaAssist is it is simple to create a new class from the scratch using JavaAssit. When we create new class files using JavaAssit we have to create all the methods in the new class as Strings and the pass them to Java Assist. As a example if we have to add amethod called sayHellow() to new class we can create it as a String like this.
<br />
<pre class="brush: java; highlight: [5, 15]; html-script: true">String s="public void sayHello(){ System.out.println("Hello"); }
</pre>
<pre class="brush: java; highlight: [5, 15]; html-script: true">
/**
* @param c Any class which can be used to get the class loader (c.getClassLoarder()) can be passed using object.getClass() methood
* @param name
* Name of the to be generated class
* @param methods
* Methods to be created as Strings
* @param interfaces
* interfaces to be implemented as Strings
* @param directory
* the directory generated class files has to be written
*/
public static void createClass(Class<?> c, String name, List<String> methods,
List<String> interfaces, String directory) {
String temp = null;
try {
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ClassClassPath(c));
CtClass cc = pool.makeClass(name);
if (interfaces != null) {
for (String s : interfaces) {
CtClass anInterface = pool.get(s);
cc.addInterface(anInterface);
}
}
for (String s : methods) {
temp = s;
CtMethod m = CtNewMethod.make(s, cc);
cc.addMethod(m);
}
cc.writeFile(directory);
} catch (Exception e) {
// TODO throw
System.out.println(temp);
e.printStackTrace();
}
}
</pre>
To add this class to the classpath following method can be used.
<pre class="brush:java highlight: [5, 15]; html-script: true">
//Here directory is the path which class files were written
public static Class loadClass(String className,String directory, ClassLoader loader) throws Exception {
File f = new File(directory);
java.net.URL[] urls = new java.net.URL[] { f.toURI().toURL() };
ClassLoader cl = new URLClassLoader(urls, loader);
Class cls = cl.loadClass(className);
return cls;
}
</pre>
Note - JavaAssist does not support generics. If we use genarics java assist throws an exception. We have to use fully qualified names for types. As an example we have to use java.util.List for List type.
Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com9tag:blogger.com,1999:blog-1513330264811558120.post-55673787328555568562012-10-08T12:45:00.003+05:302012-10-08T12:47:53.274+05:30How to get the base class of a Array using java reflection.If we use Java reflection to get the class of a object like String[][] strings; Java reflection will return [[Ljava.lang.String. But sometimes you may want to get the bases class e.g in this case java.lang.String. Following method can be used to do that. If you want to get the bass class of array object a you use this as Class c=getTypeOfArray(a.getClass());
<pre class="brush:java">
public static Class getTypeOfArray(Class c){
Class cls=null;
while(true){
if(c.isArray()){
c=c.getComponentType();
}else{
cls=c;
break;
}
}
return cls;
}
</pre>Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-47958540929182670092012-09-03T21:13:00.002+05:302012-09-03T21:17:55.486+05:30GSoC 2012 with Apache Photark, A great experience.<div class="separator" style="clear: both; text-align: center;">
<a href="http://code.google.com/images/GSoC2012_300x200.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://code.google.com/images/GSoC2012_300x200.png" /></a></div>
<br />
<br />
<div style="text-align: justify;">
Google summer of code is one of the worlds leading opensource development programs. It is held anually by Google inc. Many enthusiastic students participate each year for this.</div>
<div style="text-align: justify;">
In every year many number of students try to take part of this program. But nearly 1000 students only get selected for this event. I was lucky to take part in 2012 program for the first time.</div>
<div style="text-align: justify;">
I took part on GSoC 2012 under Apache Software Foundation for their incubator project<a href="http://incubator.apache.org/photark/" target="_blank"> Photark</a>.</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9Q8aKwueTpYrnkGIzNLMcXVpEfr2uBjH9lU2yXPTAkGcI3HfmeBzCcfc3KTM6E2j1LDfM4imffQ9KWOhNeHN5wuBJ0QI0WpnCfytRc8Z0DBhPZfCtVekc69k6nWDTqzRk2YtHMrpVH74E/s320/photark_logo_small.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9Q8aKwueTpYrnkGIzNLMcXVpEfr2uBjH9lU2yXPTAkGcI3HfmeBzCcfc3KTM6E2j1LDfM4imffQ9KWOhNeHN5wuBJ0QI0WpnCfytRc8Z0DBhPZfCtVekc69k6nWDTqzRk2YtHMrpVH74E/s320/photark_logo_small.jpg" /></a></div>
<br />
<div style="text-align: justify;">
The interesting thing was my initial plan was to participate on another organization which develops a CMS. I also worked with them nearly one month. But then I realized what they are expecting and what I am interested is not matching. So I looked for another organization and found this interesting project.</div>
<div style="text-align: justify;">
But then there was little time for application closing period. So I quickly checcout the code base play with it for little time, drafted a proposal and submitted it.</div>
<div style="text-align: justify;">
Then Mr. <span class="gD" name="Suhothayan Sriskandarajah"><a href="http://suhothayan.blogspot.com/" target="_blank">Suhothayan Sriskandarajah</a> who was happened to my mentor contacted me and explained further about what they are expecting and pointed out week points in my proposal. Then I updated my <a href="http://dl.dropbox.com/u/55149410/Proposal.pdf" target="_blank">proposal</a> according to the feedbacks. Then I started familiarizing with other technologies were new to me but need for this project.</span></div>
<div style="text-align: justify;">
<span class="gD" name="Suhothayan Sriskandarajah">Time went results came. Boom, I was taking part in GSoC 2012. Then the community bonding period started. But there was a slight problem I had exams in first two weeks of the community bonding period. But the community was very friendly. They understood my situation and they give me freedom until exams are over.</span></div>
<div style="text-align: justify;">
<span class="gD" name="Suhothayan Sriskandarajah">Then I started cording. Since it was a somewhat green field project (project details will be explained in another blog post) I had to take important design descisions how the UI must be organized, what are the libraries to be used, etc. But I managed to done it in to a significant level when the mid evaluation came.</span></div>
<div style="text-align: justify;">
<span class="gD" name="Suhothayan Sriskandarajah">In the mid evaluation my mentor evaluated my problem and we discussed what have to be done in the next half. Also my mentor suggested some UI improvements which were essential to make the app usable.</span></div>
<div style="text-align: justify;">
<span class="gD" name="Suhothayan Sriskandarajah">Mean time the community decided to give me the commitership for the project. They asked whether I like to be a committer and I agreed instantly. After the normal procedures (signing the ILCA etc.) I officially became a committer at Apache Photark. This helped me to contribute to the project more efficiently. Not I could do the changes immediately without submitting patches waiting until they applied. Also I could quickly do bug fixes.</span></div>
<div style="text-align: justify;">
After the mid term evaluation as the first task I did the UI improvements and gave id a modern and user friendly look and feel. Then I implemented rest of the functionality gradually. When It comes to the final evaluations I have completed most of the planned functionality as they were planned. The app has came to a release level with my contributions and other GSoC participant's contributions. </div>
<div style="text-align: justify;">
Then I did a code review with my mentor and he suggested some changes to code structure and naming conventions to make it more understandable and clean. It is very important for a open source project since many people are going to contribute it in the future. After the soft pencil down date we did an another code review and officially ended out mentoring sessions for GSoC 2012.</div>
<div style="text-align: justify;">
After the evaluation period I was officially notified that I have completed GSoC 2012 successfully. Finally I like to say GSoC 2012 was one of the best experiences in my life. It gave me the opportunity to involve with open source projects, communities and contribute to them. Those contributions will not be limited to the GSoC period and I am going to do those contributions further. </div>
<div style="text-align: justify;">
Finally I like to thank the people who helped me to make this project a success. First of all I like to thank my mentor who gave me this amazing opportunity and helped me throughout this project. Also I like to thank the project lead <a href="http://people.apache.org/~lresende/" target="_blank">Luciano Resende</a>, project member Avdesh Yadav and other GSoC participant Bhargav for their help throughout this project. I will discuss about my project in the next blog post in detail. </div>
<span class="gD" name="Suhothayan Sriskandarajah"><br /></span>
Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-36868465285094632202012-08-12T19:43:00.000+05:302012-08-14T19:22:15.785+05:30How to get current time and date using JavaScript.Getting system date and time using JavaScript is easy. But some steps has to follow to convert it to a readable format. These functions will help you to get rid of the hassle .<br />
<br />
//returns date in format 01/01/2012<br />
<br />
function getCurrentDate() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>var currentTime = new Date()<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>var month = currentTime.getMonth() + 1<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>var day = currentTime.getDate()<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>var year = currentTime.getFullYear()<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return (month + "/" + day + "/" + year);<br />
}<br />
<br />
//returns time in format 05:45 AM<br />
function getCurrentTime() {<br />
<span style="white-space: pre;"> var time = "";
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10) {
minutes = "0" + minutes;
}
var h;
if (hours > 12) {
h = hours - 12;
}
if (h < 10) {
h = '0' + h;
}
time += (h + ":" + minutes + " ");
if (hours > 11) {
time += ("PM");
} else {
time += ("AM");
}
return time;</span><br />
}Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0tag:blogger.com,1999:blog-1513330264811558120.post-81528610384618891192012-06-28T16:18:00.002+05:302013-05-03T22:39:10.955+05:30How to build a maven project without testsWhen we build a large project using maven it takes a lot of time because of tests runs. So we can build quickly by disabling the tests. We can do that using the following command.<br />
<pre class="default prettyprint" style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 18px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; text-align: left; vertical-align: baseline; width: auto;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">mvn </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">-</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">Dmaven</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">test</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">skip</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">true</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> install</span></code></pre>
Prabhathhttp://www.blogger.com/profile/12569817511276442800noreply@blogger.com0