Java Threading

November 21, 2009

TimerTextField.java

JAVA:
  1. import java.util.Date;
  2.  
  3. import javax.swing.JTextField;
  4.  
  5.  
  6. public class TimerTextField extends JTextField implements Runnable {
  7.     private boolean gogo = true;
  8.     @Override
  9.     public void run() {
  10.         // TODO Auto-generated method stub
  11.         Date d;
  12.    
  13.         while(gogo)
  14.         {
  15.             d = new Date();
  16.             this.setText(d.toString());
  17.             this.repaint();
  18.             try {
  19.                 Thread.sleep(1000);
  20.             } catch (InterruptedException e) {
  21.                 // TODO Auto-generated catch block
  22.                 e.printStackTrace();
  23.             }
  24.         }
  25.     }
  26.    
  27.     public void closeMe()
  28.     {
  29.         gogo = false;
  30.     }
  31. }

Apps.java

JAVA:
  1. import java.awt.Component;
  2. import java.awt.Rectangle;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.JTextArea;
  12.  
  13.  
  14. public class Apps extends JFrame {
  15.  
  16.     private TimerTextField ttf;
  17.     private JTextArea ta;
  18.     private JScrollPane jp;
  19.     private JButton jb;
  20.     public Apps()
  21.     {
  22.  
  23.         this.setVisible(true);
  24.         this.setLayout(null);
  25.         this.setBounds(10,20,500,400);
  26.         init();  
  27.     }
  28.    
  29.     protected void init()
  30.     {
  31.         ttf = new TimerTextField();
  32.         addComponents(ttf, new Rectangle(10,30,250,25));
  33.        
  34.         ta = new JTextArea(150,150);
  35.         jp = new JScrollPane(ta);
  36.         addComponents(jp, new Rectangle(10,65,150,150));
  37.        
  38.         jb = new JButton();
  39.         jb.setText("Click Me");
  40.         addComponents(jb, new Rectangle(260,30,80,25));
  41.        
  42.         jb.addActionListener(new ActionListener() {
  43.  
  44.             @Override
  45.             public void actionPerformed(ActionEvent e) {
  46.                 // TODO Auto-generated method stub
  47.                 jb_method(e);
  48.                
  49.             }
  50.            
  51.         });
  52.  
  53.     }
  54.    
  55.     protected void jb_method(ActionEvent e)
  56.     {
  57.         ExecutorService es = Executors.newSingleThreadExecutor();
  58.         es.execute(ttf);
  59.         //ttf.run();
  60.     }
  61.    
  62.     protected void addComponents(Component c, Rectangle r)
  63.     {
  64.         this.add(c);
  65.         c.setBounds(r);
  66.        
  67.     }
  68.    
  69.     public static void main(String[] arg)
  70.     {
  71.         new Apps();
  72.     }
  73. }

Java Navigation

November 14, 2009

Main.java

JAVA:
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         new Apps();
  4.     }
  5. }

Apps.java

JAVA:
  1. import java.awt.Component;
  2. import java.awt.Rectangle;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.ListIterator;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.JTextArea;
  15. import javax.swing.JTextField;
  16.  
  17. public class Apps extends JFrame{
  18.     private JButton jbsave;
  19.     private JButton jbdisplay;
  20.     private JButton jbprev;
  21.     private JButton jbnext;
  22.     private JTextField team;
  23.     private JTextField player;
  24.     private JTextField salary;
  25.     private JTextField status;
  26.     private JLabel lblteam;
  27.     private JLabel lblplayer;
  28.     private JLabel lblsalary;
  29.     private List <Player> lm;
  30.    
  31.     private HashMap<String,List <Player>> hm;
  32.     private int num = 1;
  33.     private String currTeam;
  34.     int TotalPlayer = 0;
  35.     int TotalTeam = 0;
  36.     int currPlayerIndex = 0;
  37.     Iterator<String> team_iterator;
  38.     Iterator<Player> player_iterator;
  39.     ListIterator <Player>li;
  40.  
  41.     public Apps() {
  42.         init();
  43.         this.setLayout(null);
  44.         this.setVisible(true);
  45.         this.setBounds(10,20,400,400);
  46.        
  47.         hm = new HashMap<String,List <Player>>();
  48.         lm = new ArrayList<Player>();
  49.     }
  50.     private void init() {
  51.         jbsave = new JButton();
  52.         jbprev = new JButton();
  53.         jbnext = new JButton();
  54.         jbdisplay = new JButton();
  55.         lblteam = new JLabel();
  56.         lblplayer = new JLabel();
  57.         lblsalary = new JLabel();
  58.  
  59.         team = new JTextField();
  60.         player = new JTextField();
  61.         salary = new JTextField();
  62.         status = new JTextField();
  63.        
  64.         addComponents(lblteam,40,40,100,25);
  65.         addComponents(lblplayer,40,70,100,25);
  66.         addComponents(lblsalary,40,100,100,25);
  67.         addComponents(team,150,40,100,25);
  68.         addComponents(player,150,70,100,25);
  69.         addComponents(salary,150,100,100,25);
  70.         addComponents(status,40,130,210,25);
  71.         addComponents(jbsave,40,170,100,25);
  72.         addComponents(jbdisplay,150,170,100,25);
  73.         addComponents(jbprev,40,200,100,25);
  74.         addComponents(jbnext,150,200,100,25);
  75.  
  76.         lblteam.setText("Team");
  77.         lblplayer.setText("Player Name");
  78.         lblsalary.setText("Salary");   
  79.         jbprev.setText("Previous");
  80.         jbnext.setText("Next");  
  81.         
  82.         jbsave.setText("Save");
  83.         jbdisplay.setText("Display");
  84.         status.setEnabled(false);
  85.         /*jbprev.setEnabled(false); 
  86.         jbnext.setEnabled(false);*/ 
  87.         jbsave.addActionListener(new ActionListener() {
  88.             public void actionPerformed(ActionEvent e) {
  89.                 jbsave_actionPerformed(e);
  90.             }
  91.         });
  92.        
  93.         jbdisplay.addActionListener(new ActionListener() {
  94.             public void actionPerformed(ActionEvent e) {
  95.                 jbdisplay_actionPerformed(e);
  96.             }
  97.         });
  98.         jbprev.addActionListener(new ActionListener() {
  99.             public void actionPerformed(ActionEvent e) {
  100.                 jbprev_actionPerformed(e);
  101.             }
  102.         });
  103.         jbnext.addActionListener(new ActionListener() {
  104.             public void actionPerformed(ActionEvent e) {
  105.                 jbnext_actionPerformed(e);
  106.             }
  107.         });  
  108.     }
  109.    
  110.     private void jbdisplay_actionPerformed(ActionEvent e) {
  111.         currPlayerIndex = 0;
  112.         TotalPlayer = 0;       
  113.         team_iterator =  hm.keySet().iterator();       
  114.         TotalTeam = hm.size();   
  115.        
  116.         display();   
  117.         jbnext.setEnabled(true);
  118.     }
  119.    
  120.     private void jbnext_actionPerformed(ActionEvent e) {
  121.         display();     
  122.     }   
  123.     private void jbprev_actionPerformed(ActionEvent e) {
  124.         display();
  125.     }
  126.     private void display()
  127.     {   
  128.         Player p;
  129.         if (team_iterator.hasNext())
  130.         {
  131.             if (TotalPlayer == 0)
  132.             {
  133.                 currTeam = team_iterator.next();
  134.                 System.out.println(currTeam);         
  135.                 lm = (List <Player>) hm.get(currTeam);
  136.                 TotalPlayer = lm.size();
  137.                 li = lm.listIterator();
  138.             }
  139.            
  140.             if (li.hasNext())
  141.             {
  142.                 currPlayerIndex ++;
  143.                 p = (Player) li.next();
  144.                 team.setText(currTeam);
  145.                 player.setText(p.getPlayer());
  146.                 salary.setText(p.getSalary());   
  147.                 status.setText("Player " + currPlayerIndex + " of " + lm.size());
  148.                 if (TotalPlayer == currPlayerIndex)
  149.                     TotalPlayer = 0;
  150.             }                                       
  151.         }
  152.         else
  153.             TotalTeam = 0;
  154.     }
  155.     private void jbsave_actionPerformed(ActionEvent e) {               
  156.         Player p = new Player();
  157.         p.setPlayer(player.getText());
  158.         p.setSalary(salary.getText());
  159.        
  160.         lm = hm.get(team.getText());
  161.         if (lm == null)
  162.         {
  163.             lm = new ArrayList<Player>();
  164.         }
  165.         lm.add(p);
  166.        
  167.         hm.put(team.getText(), lm);
  168.         status.setText("Record Saved");
  169.         team.setText("");
  170.         player.setText("");
  171.         salary.setText("");
  172.        
  173.     }
  174.     private void addComponents(Component comp, int x,int y,int width, int height){
  175.         this.add(comp);
  176.         comp.setBounds(x, y, width, height);
  177.     }   
  178. }

Player.java

JAVA:
  1. public class Player {
  2.     private String player;
  3.     private String salary;
  4.    
  5.     public String getPlayer() {
  6.         return player;
  7.     }
  8.     public void setPlayer(String player) {
  9.         this.player = player;
  10.     }
  11.     public String getSalary() {
  12.         return salary;
  13.     }
  14.     public void setSalary(String salary) {
  15.         this.salary = salary;
  16.     }
  17. }

Java Hashmap

November 13, 2009

Pet.java

JAVA:
  1. public class Pet {
  2.    private String name;
  3.    private int age;
  4.  
  5.    public String getName()
  6.    {
  7.       return name;
  8.    }
  9.    public void setName(String name)
  10.    {
  11.       this.name = name;
  12.    }
  13.    public int getAge()
  14.    {
  15.        return age;
  16.    }
  17.  
  18.    public void setAge(int age)
  19.    {
  20.       this.age = age;
  21.    }
  22. }

Apps.java

JAVA:
  1. import java.awt.Component;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. import java.util.ListIterator;
  14.  
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.JTextArea;
  19. import javax.swing.JTextField;
  20.  
  21.  
  22. public class Apps extends JFrame
  23. {
  24.    private JButton jbsave;
  25.    private JButton jbdisplay;
  26.    private JTextField name;
  27.    private JTextField age;
  28.    private JTextArea ta;
  29.    private HashMap<String,Integer> hm;
  30.    private List <Pet> lm;
  31.    private JScrollPane jscroll;
  32.  
  33.    public Apps()
  34.    {
  35.       init();
  36.       this.setLayout(null);
  37.       this.setVisible(true);
  38.       this.setBounds(10,20,400,300);
  39.  
  40.       hm = new HashMap<String,Integer>();
  41.       lm = new ArrayList<Pet>();
  42. }
  43.  
  44. private void init()
  45. {
  46.    jbsave = new JButton();
  47.    jbdisplay = new JButton();
  48.    name = new JTextField();
  49.    age = new JTextField();
  50.    ta = new JTextArea();
  51.    jscroll = new JScrollPane();
  52.  
  53.    addComponents(name,40,40,100,25);
  54.    addComponents(age,40,70,50,25);
  55.    addComponents(jbsave,40,100,70,25);
  56.    addComponents(jbdisplay,120,100,100,25);
  57.    addComponents(jscroll,40,160,150,100);
  58.  
  59.    jscroll.getViewport().add(ta);
  60.    jbsave.setText("Save");
  61.    jbdisplay.setText("Display");
  62.    jbsave.addActionListener(new ActionListener() {
  63.       public void actionPerformed(ActionEvent e)
  64.      {
  65.         jbsave_actionPerformed(e);
  66.      }
  67.   });
  68.  
  69.    jbdisplay.addActionListener(new ActionListener()
  70.    {
  71.       public void actionPerformed(ActionEvent e)
  72.       {
  73.          try
  74.         {
  75.             jbdispaly_actionPerformed(e);
  76.         }
  77.         catch (IOException e1)
  78.         {
  79.            e1.printStackTrace();
  80.         }
  81.    }
  82.    });
  83. }
  84.  
  85. private void jbdispaly_actionPerformed(ActionEvent e) throws IOException
  86. {
  87.     String key;
  88.     ta.setText("HashMap size : " + hm.size());
  89.     Iterator<String> iterator = hm.keySet().iterator();
  90.  
  91.     while( iterator. hasNext() )
  92.     {
  93.        key = iterator.next();
  94.        ta.append("\n"+key + " , " + hm.get(key));
  95.     }
  96.  
  97.     Pet p;
  98.     ta.append("\nList size : " + lm.size());
  99.     ListIterator <Pet>li = lm.listIterator();
  100.  
  101.      while(li.hasNext())
  102.     {
  103.        p = (Pet) li.next();
  104.        ta.append("\n" + p.getName() + " , " + p.getAge());
  105.     }
  106. }
  107.    private void jbsave_actionPerformed(ActionEvent e)
  108.    {
  109.        Pet p = new Pet();
  110.        int i_age;
  111.        p.setName(name.getText());
  112.        i_age = Integer.parseInt(age.getText());
  113.        p.setAge(i_age);
  114.  
  115.        hm.put(name.getText(), i_age);
  116.        lm.add(p);
  117. }
  118.  
  119.    private void addComponents(Component comp, int x,int y,int width, int height)
  120.    {
  121.       this.add(comp);
  122.       comp.setBounds(x, y, width, height);
  123.    }
  124. }

Main.java

JAVA:
  1. public class Main
  2. {
  3.    public static void main(String[] args)
  4.    {
  5.       new Apps();
  6.     }
  7. }

Java Array

November 13, 2009

JAVA:
  1. import java.util.Arrays;
  2.  
  3. public class MainProg {
  4.  
  5. /**
  6. * @param args
  7. */
  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. int num1[] = {1,2,3,4};
  11. int num2[] = new int[4];
  12. int num3[] = new int[4];
  13.  
  14. // to populate
  15. for (int i=0 ; i <4 ; i++) {
  16. num2[i] = i;
  17. }
  18.  
  19. Arrays.fill(num3,0);
  20.  
  21. // to display
  22. for ( int value : num2) {
  23. System.out.println(value);
  24. }
  25.  
  26. int value;
  27. for (int x=0 ; x <4 ; x++) {
  28. value = num2[x];
  29. System.out.println(value);
  30. }
  31.  
  32. }
  33. }

JAVA:
  1. import java.util.Arrays;
  2. import java.util.Random;
  3.  
  4.  
  5. public class ArrayEx2 {
  6.  
  7. /**
  8. * @param args
  9. */
  10. public static void main(String[] args) {
  11. // TODO Auto-generated method stub
  12. int e_matrix[][] = new int[3][3] ;
  13. int w_matrix[][] = new int[4][];
  14. int n_items;
  15.  
  16. Random num = new Random();
  17.  
  18. for (int i=0 ; i <e_matrix.length; i++) {
  19. Arrays.fill(e_matrix[i], i+1);
  20. }
  21. display(e_matrix);
  22.  
  23. for (int i=0 ; i <w_matrix.length; i++) {
  24. n_items = num.nextInt(4) + 1 ;
  25. System.out.println("index : " + i + ", length : " + n_items);
  26. w_matrix[i] = new int[n_items];
  27.  
  28. for (int x=0 ; x <w_matrix[i].length ; x++) {
  29. w_matrix[i][x] = num.nextInt(50);
  30. }
  31. }
  32. display(w_matrix);
  33. }
  34.  
  35. public static void display(int the_matrix[][]){
  36. for (int y=0 ; y <the_matrix.length ; y++) {
  37. System.out.println("[row : " + y + "] ");
  38. for (int x=0 ; x <the_matrix[y].length ; x++) {
  39. System.out.print(the_matrix[y][x] + " ");
  40. }
  41. System.out.println();
  42. }
  43.  
  44. }
  45.  
  46. }

New Oracle SOA Book

June 25, 2009

Oracle SOA Suite Developers Guide

Are you new to SOA and would like to learn how Oracle SOA Suite fits into the SOA world ? Or are you a SOA professional and looking for an all in one book on Oracle SOA Suite? Then wait no more, after a few months of waiting, Matt Wright and Antony Reynolds finished the Oracle SOA Suite Developer's Guide book and now available in the market.

I was assigned to research on how to integrate Oracle Retail application and our client's legacy system. I have done some research and found some online tutorials in Oracle website but I found most of them are using the BPEL Designer plugin of Eclipse. I have no luck of finding this plugin so I installed JDeveloper to create simple BPEL modules. Good thing Oracle SOA Suite Developer's Guide book came in to save my time finding the equivalent steps in the tutorial to JDeveloper because thoughout the book, the examples are using JDeveloper.

I'm familiar to basic SOA concepts because I have been reading IBM's SOA Redbook in the past. With my limited time to read the whole book, I jumped reading Chapter 3: Service-enabling existing systems which covers integration of new and legacy system. As most system designers and architects know, to integrate a legacy system to the another application, the integration modules are usually reading files to transfer data from one system to another.

Chapter 3 covers File Adapter for reading local files, FTP adapter for remotes files and DB Adapter which is use to data manipulation in the database. The steps are easy to follow and you will not get lost because there are screenshots provided to guide you.

For full details of the book, please visit the official book page in PackPub website.

Reading CSV File Using C Program

June 2, 2008

Since I'm on an integration project, I'm mostly dealing with transferring of data from two or more systems whether it's from the legacy system or newly implemented system. Most of the type of data movement is sending CSV files to and from the different system. Here is a simple tutorial on how to read CSV. The CSV file contains the following:

CODE:
  1. 1111,1414,1000
  2. 1112,1010, 1001
  3. 1113,1112,1002

The fields are Item Number, Class Number and Supplier Number.

The example below is the simple code of reading CSV file in C.

CODE:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char* argv[])
  6. {
  7.    if (argc <2)
  8.    {
  9.       fprintf(stderr,"Usage: %s csv_file\n",argv[0]);
  10.       return(1);
  11.    }
  12.  
  13.     FILE *f = fopen(argv[1], "rt");
  14.     char Line[256];
  15.     unsigned int AllocSize = 0, Size = 0, n;
  16.     char *L_text;
  17.  
  18.     while(fgets(Line, sizeof(Line), f))
  19.     {
  20.  
  21.       printf("Item = %s \n",strtok(Line, ", "));
  22.       printf("Class = %s \n",strtok(NULL, ", "));
  23.       printf("Supplier = %s \n",strtok(NULL, ", "));
  24.  
  25.     }
  26.    return(0);
  27. }

Determine the File Count in Unix Directory

May 21, 2008

To determine the number of files in the Unix directory, use the following command.

CODE:
  1. ls -l | grep -c "^-.*"

Merging Files in Unix

May 9, 2008

You have multiple files in containing data in similar format and wanted to consolidate in a single file. This is usually the case if you have a daily log that you want to place an archive copy in a single file in weekly or in monthly, yearly.

The basic syntax of merging file is

CODE:
  1. cat <source file> >>  <target file>

If you have more than 10 files to merge it is better to write a shell script that will loop through the files and merge in a file. The following is the code for merging the files in a folder into a single file.

CODE:
  1. #!/bin/sh
  2. echo enter file name
  3. read filename
  4.  
  5. for arg in `ls;`
  6.    do
  7.    echo $arg
  8.    cat $arg >> $filename
  9. done

Lines 2 and 3 prompts the user for the filename of the file which will contain the consolidated contents of all the files in the current folder.

Lines 5 to 9 loops through the files in the current folder and merge the files to a single file.

Simplest File Merging

CODE:
  1. #!/bin/sh
  2. echo enter file name
  3. read source_filemask
  4. read master_filename
  5.  
  6. cat $arg >> $filename

A simple file merging

CODE:
  1. cat $<filemask>*> $<filename>

Example

CODE:
  1. cat $Test*> $FinalTest.txt

Writing Files in Unix

May 9, 2008

To write files in Unix, the syntax is

CODE:
  1. echo <text> > <filename>

Example

CODE:
  1. echo "This is my message." > myfile.txt

To add another line in the myfile.txt, use the following syntax.

CODE:
  1. echo "This is my second message." >>  myfile.txt

You may notice that double greater than sign (>>) is used instead of single greater than sign. This means that the message is written in append mode.

Let's open the myfile.txt by using the following command.

CODE:
  1. tail  myfile.txt

The content of the myfile.txt should have the following.

CODE:
  1. This is my message.
  2. This is my second message.

How to display newline in Excel?

January 12, 2008

The commonly used to display newline in Visual Basic is using VbCrLF. But the problem is you will see a small rectangle at the end of the line.

Excel New Line

Visual Basic:
  1. Dim strFirstLine As String
  2. Dim strSecondLine As String
  3.  
  4. strFirstLine = "This is the first line"
  5. strSecondLine = "This is second line"
  6.  
  7. Worksheets("Sheet1").Range("b4").Value = _
  8. strFirstLine & vbCrLf & strSecondLine

To solve this problem, you will need to change the code from VbCrLF to VbLf.

Visual Basic:
  1. Dim strFirstLine As String
  2. Dim strSecondLine As String
  3.  
  4. strFirstLine = "This is the first line"
  5. strSecondLine = "This is second line"
  6.  
  7. Worksheets("Sheet1").Range("b4").Value = _
  8. strFirstLine & vbLf & strSecondLine

Excel New Line

Next Page »