Saturday, February 6, 2016

Get EC2 tag key-value in Java

The following code snippet is a sample for retrieving AWS EC2 instance's tags in a Java program.

import java.util.Collection;
import java.util.LinkedList;

import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.DescribeTagsRequest;
import com.amazonaws.services.ec2.model.DescribeTagsResult;
import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.TagDescription;
import com.amazonaws.util.EC2MetadataUtils;

public class GetTag{
 
 public static void main(String[] args){
  DescribeTagsRequest req = new DescribeTagsRequest();
  
  Collection filters = new LinkedList<>();
  LinkedList instanceList = new LinkedList<>();
                
                //Get instanceId of the current instance
  String instanceId = EC2MetadataUtils.getInstanceId();
  instanceList.add(instanceId);
  Filter filter = new Filter("resource-id", instanceList);
  filters.add(filter);
  req.setFilters(filters);
  AmazonEC2Client client = new AmazonEC2Client();
  client.setRegion(Region.getRegion(Regions.US_WEST_2));
  DescribeTagsResult tagResult = client.describeTags(req);
  if(tagResult != null){
   for(TagDescription tag:tagResult.getTags()){
    System.out.println(tag.getKey());
    System.out.println(tag.getValue());
   }
  }
 }
 
}


Monday, February 9, 2015

Perforce eclipse error: Error occurred during the SSL handshake: invalid SSL session

How to fix the eclipse's perforce plugin error "Error occurred during the SSL handshake: invalid SSL session"


If you are getting the above error, it means your JVM and Perforce servers are not successfully exchanging SSL certificate. You have install JCE (Java Cryptography Extension) to make it work.

These are the steps to fix this issue:

Download JCE



If the above didn't work, search with the keywords "JCE Java 6" or "JCE Java 7" or "JCE Java 8" depending on the Java version you are using.

Backup existing libraries


Backup the following libraries available under the location "$JAVA_HOME/jre/lib/security" to a safe location. You have to overwritten these libraries in next step.
  • local_policy.jar
  • US_export_policy.jar

Copy libraries

The JCE package downloaded in step one would have two jars (local_policy.jar, US_export_policy.jar). Extract these two files to the location "$JAVA_HOME/jre/lib/security"

Restart Eclipse

Close your eclipse if it is already running. After restarting the eclipse, you should be able to successfully connect to perforce.

Wednesday, August 18, 2010

Arrange Number - Blackberry game

I created my first Blackberry application. It is a simple puzzle game. Objective of the game is to arrange numbers in order by moving the numbers over one empty space available.

It runs all the device with Blackberry OS 4.6. It works very well in my Blackberry pearl flip.
Blackberry user please try this out.



Sunday, May 16, 2010

Deleting all data of a site in Firefox

The latest version of FireFox has good feature which suggest the URLs from your history as you type. But it would be annoying if suggest list the site you are not interested on.

To delete those sites from history.
1. Goto Menu History > Show all history (or) ctrl+shift+H
2. Select the site you can to delete by searching (or) browsing through the history.
3. Right click and say 'Forget about this site'

It will delete all the history of the selected site.

Tuesday, October 27, 2009

Memory Calculator

I have created a utility to calculated the size of memory occupied by different scope variable in ColdFusion.
I have submitted the same in Adobe Cookbooks. It is available at Memory Calculator

Have a look at it and provide your comments/suggestion/rating.

Tuesday, October 14, 2008

Coldfusion debugger common mistake


Step-through debugging is the best method of debugging I ever know. With the introduction of ColdFusion Step-through debugger as eclipse plug-in, coldfusion development became much handy. But I have noticed most of them do a simple careless mistake while setting up the debugger for the first time (including me). Just thought of mentioning it here.

In the RDS configuration under ColdFusion preference, the value of the port number should be the http port number of the ColdFusion server. But as a continuation of server side debugger setup, people used to enter the debugger port specified in the ColdFusion administer.

It would be better if Adobe includes some description for the Port Number field as it does for the Context Root field

Wednesday, July 9, 2008

Coldfusion CustomTag for Query batch processing

I have created my first coldfusion custom tag. The purpose of this tag to open the query batch processing feature provided by JDBC to the coldfusion developer. It is just build over the JDBC prepared statement. I tried to make it as close as regular coldfusion tags, so that their won't be much confusion in using the tag.
The sample code to use the custom tag is shown below:

<cf_preparedstmt datasource="webrequest" query="insert into Employee(id, name) values(?,?)">
<cf_batch>
<cf_batchparam position="1" value="26" type="INT">
<cf_batchparam position="2" value="name1" type="STRING">
</cf_batch>
<cf_batch>
<cf_batchparam position="1" value="27" type="INT">
<cf_batchparam position="2" value="name2" type="STRING">
</cf_batch>

</cf_preparedstmt>


The usage of this tag is almost similar to cfquery or cfstoredproc.

The source code for all the three tags are available here.

1. cf_preparedstmt:
<cfif thisTag.ExecutionMode is 'start'>

<!--- Arribute validation --->
<cfif not StructKeyExists(Attributes, "datasource") or len(Attributes.datasource) eq 0>
<cfthrow message="Attribute datasource missing in cf_preparedstmt tag">
</cfif>
<cfif not StructKeyExists(Attributes, "query") or len(Attributes.query) eq 0>
<cfthrow message="Attribute query missing in cf_preparedstmt tag">
</cfif>
<cfif StructKeyExists(Request, 'preparedstatement') and len(Request.preparedstatement)>
<cfthrow message="Nesting cf_preparedstmt tag is not allowed">
</cfif>

<!--- Create Connection and PreparedStatment --->
<cfscript>
factory = createObject("java", "coldfusion.server.ServiceFactory");
connection = factory.getDataSourceService().getDataSource(Attributes.datasource).getConnection();
Request.preparedstatement = connection.prepareStatement(Attributes.query);
</cfscript>

<cfelseif thisTag.ExecutionMode is 'end'>

<!--- Execute and close the resource --->
<cfscript>
Request.preparedstatement.executeBatch();
Request.preparedstatement.close();
Request.preparedstatement = "";
</cfscript>

</cfif>


2. cf_batch:
<cfif thisTag.ExecutionMode is 'start'>
<cfif not StructKeyExists(Request, 'preparedstatement')>
<cfthrow message="cf_batchparam tag should be used inside cf_prepatedstmt tag">
</cfif>

<cfelseif thisTag.ExecutionMode is 'end'>
<cfscript>
Request.preparedstatement.addBatch();
</cfscript>
</cfif>


3. cf_batchparam
<cfif thisTag.ExecutionMode is 'start'>

<!--- Arribute validation --->
<cfif not StructKeyExists(Attributes, 'position') or not IsNumeric(Attributes.position)>
<cfthrow message="Attribute position should be numeric">
</cfif>
<cfif not StructKeyExists(Attributes, 'value')>
<cfthrow message="The Required Attribute value is undefined">
</cfif>
<cfif not StructKeyExists(Request, 'preparedstatement')>
<cfthrow message="cf_batchparam tag should be used inside cf_prepatedstmt tag">
</cfif>

<!--- set values --->
<cfswitch expression="#Attributes.type#">
<cfcase value="BOOLEAN">
<cfset Request.preparedstatement.setBoolean(Attributes.position, Attributes.value)>
</cfcase>
<cfcase value="BYTE">
<cfset Request.preparedstatement.setByte(Attributes.position, Attributes.value)>
</cfcase>
<cfcase value="SHORT">
<cfset Request.preparedstatement.setShort(Attributes.position, Attributes.value)>
</cfcase>
<cfcase value="INT">
<cfset Request.preparedstatement.setInt(Attributes.position, Attributes.value)>
</cfcase>
<cfcase value="LONG">
<cfset Request.preparedstatement.setLong(Attributes.position, Attributes.value)>
</cfcase>
<cfcase value="FLOAT">
<cfset Request.preparedstatement.setFloat(Attributes.position, Attributes.value)>
</cfcase>
<cfcase value="DOUBLE">
<cfset Request.preparedstatement.setDouble(Attributes.position, Attributes.value)>
</cfcase>
<cfcase value="BIGDECIMAL">
<cfset Request.preparedstatement.setBigDecimal(Attributes.position, Attributes.value)>
</cfcase>
<cfcase value="STRING">
<cfset Request.preparedstatement.setString(Attributes.position, Attributes.value)>
</cfcase>
<cfdefaultcase>
<cfthrow message="The Required Attribute type is undefined">
</cfdefaultcase>
</cfswitch>
</cfif>



To make use of these tags save the source code in the file preparedstmt.cfm, batch.cfm, batchparam.cfm respectively and place it under the customtags folder specified in the coldfusion administrator.

Please provide your comments.