Session Tracking In Swing Applications

September 24, 2009 Abhijeet Iraj Leave a comment

Session handling is pretty common in web applications. Java Servlet technology provides an API for managing sessions and allows several mechanisms for implementing sessions.  But if you have swing application and want to track the session how to do that?

Here is the scenario – Suppose you have a swing based kiosk application. User logs in to the application and does some operations.  Now for some reason he doesn’t perform any operation for 3-4 minutes then for security reasons we need to somehow log out from the swing application.

This can be achieved with the help of Swing timers and AWT default toolkit.

Here are the steps

    • When user logs in start the swing timer by passing SESSION_TIMEOUT value and ActionListener (to handle timeout event).
    • Add AWTEventListener to AWT toolkit with AWTEvent.MOUSE_EVENT_MASK and AWTEvent.KEY_EVENT_MASK. This will track only mouse events and key events.
    • Whenever AWT event occurs (that means user performed some action) restart the timer.
    • When the system is idle for SESSION_TIMEOUT time, timer will call actionPerformed method of its ActionListener.It means session timeout occured and you need to logout.
      import java.awt.AWTEvent;
      import java.awt.Toolkit;
      import java.awt.event.AWTEventListener;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      
      import javax.swing.Timer;
      
      /**
       * @author airaj
       *
       */
      public class SystemTimeoutClient implements AWTEventListener, ActionListener
      {
          private int SESSION_TIMEOUT = 240000; // 4 minutes
          private static SystemTimeoutClient client = null;
          private Timer timer;
      
          private SystemTimeoutClient()
          {
          }
      
          public static SystemTimeoutClient getSystemTimeoutClient()
          {
              if (client == null)
              {
                  client = new SystemTimeoutClient();
              }
              return client;
          }
      
          public void trackSessionTimeout()
          {
              timer = new Timer(SESSION_TIMEOUT, this);
              timer.setInitialDelay(SESSION_TIMEOUT);
              timer.setRepeats(false);
              timer.start();
              trackSystemEvents();
          }
      
          public void stopSessionTracking()
          {
              if (timer.isRunning())
              {
                  timer.stop();
              }
          }
      
          private void trackSystemEvents()
          {
              Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.MOUSE_EVENT_MASK + AWTEvent.KEY_EVENT_MASK);
          }
      
          @Override
          public void eventDispatched(AWTEvent event)
          {
              // User action detected. Restart the timer
              if (timer.isRunning())
              {
                  timer.restart();
              }
          }
      
          @Override
          public void actionPerformed(ActionEvent e)
          {
              // Session Timeout occured. Close resources and log out from system. 
      
          }
      }
      

      Gmail as free online file storage

      September 8, 2009 Abhijeet Iraj Leave a comment

      If you want free online file storage then you can use  GMail Drive. It creates a virtual file system around your Google GMail account, allowing you to use GMail as a storage medium. It enables you to save and retrieve files stored on your GMail account directly from inside Windows Explorer. It adds a new drive to your computer under the My Computer folder, where you can copy and drag n drop files.

      Here are the steps you can follow

      • Create one Gmail account for your storage. You can use your existing Gmail account but its better to have separate account for your online file storage.
      • Download and install GMail drive shell extension from here
      • After installation you will see new GMail Drive

      GMailDrive

      • Double click on GMail Drive it will prompt for your GMail credentials

      GMailDriveLogin

      • After login you can copy files which you want to store into your new GMail drive.GMailDriveCopy
      • Now if you want to access your files from some other machine you can setup GMail Drive and you will have access to all those files.

      GMailDriveAccess

      • You can also access those files from your GMail account. Login to your account and you will see a separate message for each one of your file as an attachment.

      GMailDriveAccount

      Categories: Tools Tags: ,

      abhijeetiraj.com as primary domain

      September 6, 2009 Abhijeet Iraj Leave a comment

      I recently changed my primary domain for my blog to http://abhijeetiraj.com . It was really easy to do this change from wordpress’s Domain setting section.

      If you do not have domain you can register new one from wordpress $14.97  per year. Check here for more information.

      domain

      Categories: Uncategorized Tags:

      Spring MVC Web Portal

      September 2, 2009 Abhijeet Iraj Leave a comment

      Recently we migrated from Backbase based portal to Spring MVC based portal for our kiosk transaction system. This will evolve as Offline system (see davids post on this here).

      We mainly used Spring to build this offline system because it’s lightweight, easy to use, easy to test and it allows to configure and compose complex application from simpler components.

      portal

      Here is the technology stack which we are using to build this offline system

      • Spring MVC: Part of spring and MVC  based Framework for building robust web applications
      • Spring Webflow: Built on Spring MVC and provides the infrastructure for building and running rich web application.
      • JQuery: Fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.
      • Blueprintcss: CSS framework which aims to cut down on your development time.
      • JMesa: Dynamic HTML table rendering API/Tag library that allows you to filter, sort, paginate, export and edit your data however you need to.
      • Sitemesh: Web-page layout and decoration framework and web-application integration framework to aid in creating large sites consisting of many pages for which a consistent look/feel, navigation and layout scheme is required.
      • Hibernate: Powerful, high performance object/relational persistence and query service.
      • Maven: Software project build and  management tool.
      • MySQL: Most popular open source database.
      • SpringSource ToolSuite: Eclipse-powered development environment for building Spring-powered enterprise applications.
      Categories: Spring, web Tags: , , , ,

      Swing Event Dispatcher Thread

      September 1, 2009 Abhijeet Iraj Leave a comment

      Even though swing is very important part of the Java platform, truth is that its not thread safe. That means swing component methods called from multiple threads can cause thread interference or memory consistency errors. There are some exceptions to this. There are some swing component methods which are “thread safe” which can be safely invoked from any thread.

      Reason why swing is not thread safe is that any attempt to create a thread-safe GUI library faces some fundamental problems. For more on this issue, see the following entry in Graham Hamilton’s blog: MultiThreaded toolkits: A failed dream?

      Because of this swing event handling code execution as well as component repaint happens on a special thread known as the event dispatch thread. All the Swing component methods which are not thread safe must be invoked from this event dispatch thread. Programs that ignore this rule may function correctly most of the time, but are subject to unpredictable errors that are difficult to reproduce.

      EDT

      There are two fundamental things to understand about Swing and threads:

      1. Time-consuming tasks should not be run on the Event Dispatch Thread. Otherwise the application becomes unresponsive. The EDT is busy executing your task and hence can’t process GUI events.
      2. Swing components should be accessed on the Event Dispatch Thread only. To access components from other threads, you must use SwingUtilities. invokeAndWait, SwingUtilities. invokeLater or SwingWorker.
      Categories: Java, RIA, Swing Tags: , , ,

      MEI Cash Acceptor with Bunch Note Feeder

      August 29, 2009 Abhijeet Iraj Leave a comment

      Frankly speaking i had never used cash acceptor in my life since in India that technology is not used much. But recently i got a chance to integrate MEI CASHFLOW® SC Series note acceptor with BNF in our system at work and it was really a nice experience. This is one of the world’s most technically advanced cash acceptor.

      Cash Acceptor

      Because of bunch note feeder (BNF) user can deposit cash more efficiently by placing a bundle of up to 30 notes into a BNF.

      Before starting integration, one thing which was really important was to change MEI Cash Acceptor from Stand-Alone mode to EBDS mode. In Stand-Alone mode the communication between the SC and API will not work.

      Integration using MEI provided java api was straightforward..

      /**
       * @author airaj
       *
       */
      public class MEICashAcceptor extends CashAcceptor
                                                 implements AcceptorEventListener
      {
          // serial port to which device is connected
          private String serialPort = "/dev/ttyS3";
      
          private Acceptor cashAcceptor = null;
      
          // connect to the device
          public void connect()
          {
              cashAcceptor = new Acceptor();
              cashAcceptor.addAcceptorEventListener(this);
              Acceptor.listPorts();
              try
              {
                  cashAcceptor.open(serialPort, PowerUp.A);
              }
              catch (AcceptorException e)
              {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      
          // enable acceptor so that it accepts bills
          public void enable()
          {
              cashAcceptor.setEnableAcceptance(true);
          }
      
          // disable acceptor so that it stops accepting bills
          public void disable()
          {
              cashAcceptor.setEnableAcceptance(false);
          }
      
          // This method gets called when any device event occurs
          public void acceptorEventOccurred(AcceptorEvent evt)
          {
              if (evt.getDescription() == null)
              {
                  return;
              }
              if (evt instanceof EscrowEvent)
              {
                  // Event generated for bill Escrow
                  if (DocumentType.Bill.equals(cashAcceptor.getDocType()))
                  {
                      Bill b = cashAcceptor.getBill();
                      try
                      {
                          // stack the bill
                          cashAcceptor.escrowStack();
                      }
                      catch (AcceptorException e)
                      {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  }
              }
              else if (evt instanceof StackedEvent)
              {
                  // Event generated when a document is stacked.
                  if (DocumentType.Bill.equals(cashAcceptor.getDocType()))
                  {
                      Bill bill = cashAcceptor.getBill();
                      String country  = bill.getCountry();
                      String value = bill.getValue();
                  }
              }
              else if (evt instanceof CheatedEvent)
              {
                   // Event generated when the device detects a cheat attempt.
              }
              else if (evt instanceof JamDetectedEvent)
              {
                  // Event generated when the device detects a jam.
              }
              else if (evt instanceof StackerFullEvent)
              {
                  // Event generated when the device stacker is full.
              }
          }
      
      }

      You can view MEI CASHFLOW Bill acceptor with BNF demo here

      Open Flash Chart

      August 27, 2009 Abhijeet Iraj Leave a comment

      Charts are very popular and powerful tools for communicating important information in data centric applications. Flash is the right technology to create such charts because of their interactive and aesthetic features like animation, effects etc. There are lot of commercial charting libraries in the market but if you are looking for free open source flash charting library then you should consider Open Flash Chart.

      I have seen its use in wordpress blog dashboard for showing blog stats

      Open Flash Chart
      Open Flash charts reads data in JSON format which is a text format and is completely language independent. So you can use any language of your choice on server side to generate chart data in JSON format.

      There are libraries available in lot of languages to help you generate JSON file for the chart to use here

      Charts supported by Open Flash Chart:

      • Data Lines Chart
      • Bar Chart
      • 3D Bar Chart
      • Glass Bar Chart
      • Fade Bar Chart
      • Sketch Bars Chart
      • Area Chart
      • Bars + Lines Chart
      • Pie Chart
      • Scatter Chart
      • Candle Chart

      I will write some tutorials about how to use Open Flash Chart in the future.

      Categories: RIA, web Tags: , , ,

      Pixar Movies

      August 26, 2009 Abhijeet Iraj Leave a comment

      I have been a big fan of all the Pixar movies. I have seen all of them. My personal favorite is A Bug’s Life directed by John Lasseter and Andrew Stanton. Each one of the Pixar movies has received critical and commercial success.

      Pixar-Animation-Studios

      Pixar has made 10 animation movies. I have seen 9 of them, 10th one UP is yet to release in India.

      First three movies were directed by Pixar co-founder John Lasseter

      pixar1

      All the movies after monsters,inc.  received Best Animated Feature Academy award

      pixar2

      I enjoyed WALL-E throughly which is a science fiction film and its story of a robot named WALL-E who is designed to clean up a waste-covered Earth far in the future.

      pixar3

      Latest Disney-Pixar movie UP is releasing in India on 11 Sept. I am Eagerly waiting for it :)

      up

      Apart from Pixar animation movies, Ice Age created by Blue Sky Studios and released by 20th Century Fox are also my favorite.

      Categories: Animation Tags: , ,

      Web 3.0 – Semantic web

      August 26, 2009 Abhijeet Iraj Leave a comment

      Web 2.0 is now part of our daily lives. Web 2.0 helps us to collaborate, share information on the web through user centric web applications, social networking sites, video sharing sites, blogs etc. I personally spend good amount of time on orkut, twitter, youtube and blogging.

      But the problem with this is that computers don’t understand what they are doing they just follow our instructions . They just blindly retrieve the web pages. Computers (browsers) only understand the syntax of a web page, they don’t understand meaning behind it.

      web30.jpg

      This is where semantic web comes in to picture. If somehow computers could understand meaning behind web page syntax they can help us to find what we want. In semantic web computers will be able to correlate the words and learn from the past experiences.

      Categories: web Tags: ,