Aug 18 2010

Resolve Hostnames from Given IP address in Powershell

I was asked to resolve a list of ip addresses from a given text file. Now I could have written a java or c# application to do this, but I believe a small powershell script would be sufficient for this. So just to share this with everyone here it is:

$ips = cat D:\ips.txt; foreach($ip in $ips){ [System.Net.Dns]::GetHostEntry($ip) }

you can replace the D:\ips.txt with your own text file with a simple list of ips… remember one ip per line…

Continued..

So I got a little generic and just wrote a Powershell script to do it for me [where args[0] is an array of ipaddresses].

function Get-Hostname-From-IP([array]$listofIPs) {�
 foreach ($ip in $listofIPs)
    {         �
         #Use the DNS Static .Net class for the reverse lookup
         [System.Net.Dns]::GetHostEntry($ip)
    }
}

Get-Hostname-From-IP($args[0])

so save this in a file called get-hostname-from-ip.ps1 and then the usage would be ./get-hostname-from-ip.ps1 cat ips.txt for example..

The reason I did this was to show my friend site http://www.jcell.co.za that scripting is just as easy on windows :-) .


Aug 11 2010

Splash Screen for Blackberry

I am a bit confused as to why there isn’t a standard library for splash screens in mobile apps. So here is an implementation of a blackberry splashscreen. I believe the comments are sufficient to work out what is going on.


import java.util.Timer;
import java.util.TimerTask;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * <p>Every application needs a splash screen.</p>
 * <p>A splash screen should have the following rules</p>
 * <ul>
 * 	<li>Display effectively what the product is.</li>
 *  <li>Be removed automatically after a duration if not exited manually.</li>
 *  <li>You must be able to manually exit the splash screen before the desired amount of time has been exceeded.</li>
 * </ul>
 *
 * 	@author Kevin Bayes
 *
 *	<p><i>This was modified by the splash screen tutorial found on blackberry.com.</i></p>
 *
 */
final public class SplashScreen extends MainScreen {

	  	private MainScreen next;
	    private Timer timer = new Timer();
	    private UiApplication application;

	    //This splash screen is an image read from the root folder of the application
	    private static final Bitmap _bitmap = Bitmap.getBitmapResource("splash.png");

	    public SplashScreen(UiApplication ui, MainScreen next) {
	        super (Field.USE_ALL_HEIGHT|Field.FIELD_LEFT);
	        this.application = ui;
	        this.next = next;
	        this.add(new BitmapField(_bitmap));

	        /*
	         * create a listener to allow the user to exit the splash screen
	         * manually or override the navigation events as done below.
	         *
	         * the splash screen listener should implement net.rim.device.api.system.KeyListener.
	         *
	        */
	        //SplashScreenListener listener = new SplashScreenListener(this);
	        //this.addKeyListener(listener);

	        //create a timer to count down to the splash screens exit
	        timer.schedule(new ExitSplashCountDown(), 5000);
	        application.pushScreen (this);
	    }

	    /**
	     * <p>When the splash screen exits three things happen</p>
	     * <ol>
	     * 	<li>The timer is canceled.</li>
	     * <li>You pop the splash screen off the stack.</li>
	     * <li>You push the main screen onto the stack.</li>
	     * </ol>
	     */	    public void dismiss() {
	        timer.cancel();
	        application.popScreen (this);
	        application.pushScreen (next);
	    }

	 	    /**
	     *  <p>This {@link TimerTask} is used to make sure the splash screen exits and
	     *  <b>does not display forever</b>.</p>
	     *
	     * @author Kevin Bayes
	     *
	     */	    private final class ExitSplashCountDown extends TimerTask {
	        public void run() {
	           ExitSplashThread dThread = new ExitSplashThread();
	           application.invokeLater (dThread);
	        }
	    }

	  	    /**
	     * <p>Used by the {@link ExitSplashCountDown} to exit the splash screen.</p>
	     *
	     * @author Kevin Bayes
	     *
	     */	    private final class ExitSplashThread implements Runnable {
	        public void run() {
	             dismiss();
	        }
	    }    

	    /*
	     * When you click the navigation then the screen should exit and go to your applications main screen
	     *
	     * (non-Javadoc)
	     * @see net.rim.device.api.ui.Screen#navigationClick(int, int)
	     */
	    protected boolean navigationClick(int status, int time) {
	    	 dismiss();
	         return true;
	    }

	    /*
	     * Make sure nothing happens when the navigation senses movement
	     *
	     * (non-Javadoc)
	     * @see net.rim.device.api.ui.Screen#navigationMovement(int, int, int, int)
	     */
	    protected boolean navigationMovement(int dx, int dy, int status, int time) {
	    	return false;
	    }

	    /*
	     * Make sure nothing happens when unclick occurs
	     *
	     * (non-Javadoc)
	     * @see net.rim.device.api.ui.Screen#navigationUnclick(int, int)
	     */
	    protected boolean navigationUnclick(int status, int time) {
		    return false;
	    }

}


Aug 4 2010

Spring Security Minimal Setup

Sometimes you need to do a project and it doesn’t have to have the worlds best security. But rather something small that is easy to implement where you can statically add a user with roles.

So as an introduction I would like to show you the minimum to add security to a java web application.

1. Add the spring-context.xml to your WEB-INF folder.

The spring-context xml should look something like this:

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans
 xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
 xmlns:sec=”http://www.springframework.org/schema/security
 xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.2.xsd“>

 <sec:http auto-config=”true” access-denied-page=”/denied.htm”>
  <sec:intercept-url pattern=”/**” access=”ROLE_USER”/>
  <sec:concurrent-session-control max-sessions=”100″ exception-if-maximum-exceeded=”true”/>
 </sec:http>

 <sec:authentication-provider>
  <sec:user-service>
   <sec:user password=”pass” name=”user” authorities=”ROLE_USER”/>
  </sec:user-service>
 </sec:authentication-provider>
</beans>

Above you can see we have declared a user “user” with password “pass”, and this user has the authority of a “ROLE_USER”.

In the <sec section above we declare the security to filter out anything that tries to get into the root or below folders, unless the user logs in and his/her role is that of “ROLE_USER”.

2. Considering there is nothing linking or indicating to the web application that this security context even exists we shall inform the webapplication that it should use a filter for all requests. The filter will dispatch all requests to the setup spring security. To do this you should add the following lines to the web.xml file in the WEB-INF directory.

   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
     /WEB-INF/security-context.xml
    </param-value>
  </context-param>

<filter>
 <filter-name>springSecurityFilterChain</filter-name>
 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
 <filter-name>springSecurityFilterChain</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

3. Dont feels its odd that you have not created a login page as spring will provide a default.

That is how you add simple authentication to your application.


Jul 22 2010

Read an Oracle Stored Procedures Cursor using Spring

I came accross the problem where hibernate is restricted in the way it can access an oracles stored procedure that returns a reference cursor. In hibernate the reference cursor should be the first parameter in the stored procedure. Now working with stored procedures writted differently you could always just extend the “org.springframework.jdbc.object.StoredProcedure” abstract class, if you are using spring that is. 

Since my blog is all about the samples here it is:


package com.bayestech.sampleprocedure; 

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types; 

import javax.sql.DataSource; 

import oracle.jdbc.driver.OracleTypes; 

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure; 

import uk.co.pruhealth.admin.communication.notificationengine.PolicyLinks; 

/**
 * This class reads a stored procedure named 'sample_procedure' which has the following usag:
 * call sample_procedure (? <= p_Number_Input, ? <= p_String_Input, ? <= p_Date, ? => Pr_Returned_Cursor, ? => Pr_Error_Code, ? => Pr_Error_Message)
 *
 *
 * @author Kevin Bayes
 *
 */
public class SampleStoredProcedure extends StoredProcedure {

 /** Name of procedure in database. */
 public static final String PROC_NAME = "sample_procedure";�
 public GetPolicyLinksStoredProcedure(DataSource ds) {
  super(ds, PROC_NAME);

  declareParameter(new SqlParameter("p_Number_Input", Types.NUMERIC));
  declareParameter(new SqlParameter("p_String_Input", Types.VARCHAR));
  declareParameter(new SqlParameter("p_Date", Types.DATE));
  declareParameter(new SqlOutParameter("Pr_Returned_Cursor", OracleTypes.CURSOR, new RowMapper() {
   public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
    SampleReturnClass sampleClass = new SampleReturnClass();
    policyLink.setColumn1Name(rs.getString("COLUMN_1_NAME"));
    policyLink.setColumn2Name(rs.getString("COLUMN_2_NAME"));
    policyLink.setColumn3Name(rs.getString("COLUMN_3_NAME"));
    return sampleClass;
   }
  }));
  declareParameter(new SqlOutParameter("Pr_Error_Code", Types.NUMERIC));
  declareParameter(new SqlOutParameter("Pr_Error_Message", Types.VARCHAR));

     compile();
 }   

}

Once you have created the above class you need to execute it by setting the datasource and passing in the desired parameters like so:


public Collection getList(DataSource dataSource, Long inputNumber, String inputString, Date effectiveDate) {

	SampleStoredProcedure sampleStoredProcedure = new SampleStoredProcedure(dataSource);
	Map parameters = new HashMap();
	parameters.put("p_Number_Input", targetEntityNumber);
	parameters.put("p_String_Input", null);
	parameters.put("p_Date", effectiveDate);

       	Map m = SampleStoredProcedure.execute(parameters);
       	return (Collection) m.get("Pr_Returned_Cursor");
}

And that is all there is to it!


Jul 13 2010

When Did it Become About a Career

There was a turning point in my life, where I went from wanting to develope software to change the way we live to developing software to change the way we do business. This is why I am going to lay all my ideas down in the ideas page above. Read them and send me feedback….