////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //PIMS: Personal Invstment Management System //Class : ProxyLoader //Use:Downloads the current prices from the net and updates the //share price database //Author: Raghu ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.lang.*; import java.io.*; import java.util.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.*; public class ProxyLoader extends JFrame implements ActionListener, Runnable{ private dataRepository DR; private int fill=0; private String host = "202.87.40.188"; private String fileName = "stok/nse/pric.xls"; private int numShares; private String shares[][]; private boolean downloadOption; private String proxy,port,username,password; private JButton b1; private JTextField t1,t2,t3; private JPasswordField t4; private JLabel l1,l2,l3,l4; public ProxyLoader(dataRepository DR){ this.DR = DR; try{ setSize(500,400); setTitle("Download Prices"); setBackground(Color.white); Button submit = new Button("Ok"); Container cont = getContentPane(); cont.setLayout(new GridLayout(9,2)); l1 = new JLabel("Proxy"); l2 = new JLabel("Port"); l3 = new JLabel("User"); l4 = new JLabel("Password"); t1 = new JTextField(15); t2 = new JTextField(15); t3 = new JTextField(15); t4 = new JPasswordField(15); t4.setEchoChar('*'); cont.add(l1); cont.add(t1); cont.add(l2); cont.add(t2); cont.add(l3); cont.add(t3); cont.add(l4); cont.add(t4); cont.add(submit); submit.addActionListener(this); setVisible(false); System.out.println("Download prices using proxy"); }catch(Exception e){ System.out.println(e); e.printStackTrace(); new Error("Failure to download current prices"); //return; } } public void run(){ setVisible(true); } public void actionPerformed(ActionEvent ae){ String str=ae.getActionCommand(); try{ if(str.equals("Ok")){ proxy=t1.getText(); port=t2.getText(); username=t3.getText(); password=t4.getText(); //download the current prices from the net downloadPrices(host, fileName); //toHtml is a program that converts an .xls file to html file that is easier to parse. Process P = java.lang.Runtime.getRuntime().exec("./toHTML"); P.waitFor(); //parse the html file to get the prices. shares = parseFile(); //Update the share price database DR.writeSharePrices(numShares, shares); setVisible(false); } } catch(Exception e){} } public String downloadPrices(String host,String file_name)throws Exception{ String result=null; String request=""; String req="http://"+host+"/"+file_name; try{ MyAuthenticator Auth = new MyAuthenticator(username,password,proxy,port); Authenticator.setDefault(Auth); URL urlSource = new URL(req); FileDownload objFileDownload = new FileDownload(); File fileTarget = new File("./price.xls"); objFileDownload.download(urlSource, fileTarget); }catch(Exception e){System.out.println(e);e.printStackTrace();} return(result); } //Parses the html file containing the prices in a table and extracts the prices. private String[][] parseFile()throws Exception{ int i; int st,end; numShares = 0; String Line; String companyName, price; String shares[][] = new String[1000][2]; FileReader FR = new FileReader("price.html"); BufferedReader BR = new BufferedReader(FR); //Eat away the initial lines of the file. for(i=0;i<26;++i){ BR.readLine(); } while(true){ Line = BR.readLine(); if(Line == null)break; if(Line.startsWith(""); end = Line.indexOf("<",st); Line = Line.substring(st+1, end); //} companyName = Line; for(i=0;i<3;++i)BR.readLine();//eat the intermediate lines. Line = BR.readLine();//read the price //for(i=0;i<1;++i){ st = Line.indexOf(">"); end = Line.indexOf("<",st); Line = Line.substring(st+1, end); //} price = Line; shares[numShares][0] = companyName; shares[numShares][1] = price; numShares++; } } return(shares); } } /** * * */ class MyAuthenticator extends Authenticator { private PasswordAuthentication pa; private String s_username,s_password; public MyAuthenticator() { } public MyAuthenticator(String user,String pwd,String proxy,String port) { super(); s_username = user; s_password = pwd; System.getProperties().put("proxyHost",proxy); System.getProperties().put("proxyPort",port); } protected PasswordAuthentication getPasswordAuthentication() { try { pa = new PasswordAuthentication(s_username,s_password.toCharArray()); return pa; } catch(Exception ex) { System.out.println("couldn't set proxy"); } return null; } }