import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.net.*; import java.util.*; import MDMSupport.*; import com.objectspace.jgl.*; public class MDBuddyServlet extends HttpServlet { //Global variables for chat String message; MDChatUserRepository usernames; java.sql.Connection connect; java.sql.Statement st; java.sql.ResultSet rs; //Initialize global variables MDBuddySource source = new MDBuddySource(); //This hashmap has all the users who are not offline. //It is internally used by the online user checker. //whenever a user does not respond to it in specified //time his status is broadcasted as offline. HashMap onlineUserRepository = new HashMap(); //This stack will be used to update those users //who had gone offline momentarily ResumedUserRepository rur; onlineChecker oc; //This hashtable will contain the status of users to be rebroadcasted //in case some user did not receive the first time. //The information inside will be of the format //key: userId //Value: message i.e. user*status java.util.Hashtable statusChangedUsers; statusChangedRebroadcastThread statusChangedRebroadcast; MDMessageRepository mmr; DBServerInterface dbm; FMSInterface fms; public void init(ServletConfig config) throws ServletException { try{ super.init(config); //This servlet also has to take care of any client //crashes that might take place rur = ResumedUserRepository.getInstance(); mmr = MDMessageRepository.getInstance(); statusChangedUsers = new java.util.Hashtable(); oc = new onlineChecker(); oc.setPriority(Thread.MIN_PRIORITY); oc.setDaemon(true); oc.start(); statusChangedRebroadcast = new statusChangedRebroadcastThread(); statusChangedRebroadcast.setPriority(Thread.MIN_PRIORITY); statusChangedRebroadcast.setDaemon(true); statusChangedRebroadcast.start(); dbm = (DBServerInterface)System.getProperties().get("MDM.DBServer"); fms = (FMSInterface)System.getProperties().get("MDM.FMServer"); } catch(Exception ex){ ex.printStackTrace(); } //init method for chat is initialised here try { // Properties props = getProperties(); Class.forName("postgresql.Driver"); // connect=java.sql.DriverManager.getConnection((String)props.get("DBConURL"), (String)props.get("UName"), (String)props.get("PWord")); connect=java.sql.DriverManager.getConnection("jdbc:postgresql://192.165.1.4/mdm", "postgres", "postgres"); // Class.forName("oracle.jdbc.driver.OracleDriver"); // connect=java.sql.DriverManager.getConnection("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(PORT=1521)(HOST=128.12.12.70)))(CONNECT_DATA=(SID=ORCL)))", "mdm", "mdm"); System.out.println("Entered in init() and established connection"); st=connect.createStatement(); //Get the chat user repository; usernames = MDChatUserRepository.getInstance(); } catch (Exception e){ e.printStackTrace(); } } private Properties getProperties(){ boolean found = true; Properties props=null; ObjectInputStream oin=null; FileInputStream in=null; try{ in=new FileInputStream("DBProperties.props"); } catch(FileNotFoundException ex){ System.out.println("File not found "+ ex.getMessage()); try{ in.close(); } catch(Exception subEx){ subEx.printStackTrace(); } found = false; } catch(SecurityException ex){System.out.println("Security exception-I "+ex.getMessage());} if(found){ try{ oin=new ObjectInputStream(in); } catch(StreamCorruptedException ex){ System.out.println("StreamCorruptedException-I "+ex.getMessage()); } catch(IOException ex){ System.out.println("IOException-I "+ex.getMessage()); } try{ props=(Properties)oin.readObject(); } catch(ClassNotFoundException ex){ System.out.println("ClassNotFoundException "+ex.getMessage()); } catch(InvalidClassException ex){ System.out.println("InvalidClassException "+ex.getMessage()); } catch(StreamCorruptedException ex){ System.out.println("StreamCorruptedException-II "+ex.getMessage()); } catch(OptionalDataException ex){ System.out.println("OPtionalDataException "+ex.getMessage()); } catch(IOException ex){ System.out.println("IOException-II "+ex.getMessage()); } } try{ oin.close(); } catch(Exception ex){ ex.printStackTrace(); } return props; } //Process the HTTP Get request public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String mode = req.getParameter("mode"); if (mode.equalsIgnoreCase("get")){ PrintWriter out = res.getWriter(); try{ res.setContentType("text/plain"); //Get the values of the parameters message String message = req.getParameter("message"); String userName = message.substring(0, message.indexOf("*")); //Update the status of the user repository if(onlineUserRepository.count(userName) == 0){ if(!rur.contains(userName)) rur.addElement(userName); broadcastMessage(message); } onlineUserRepository.put(userName, "1"); out.println(getNextMessage()); } catch(Exception ex){ ex.printStackTrace(); } finally{ out.close(); } } else if(mode.equalsIgnoreCase("post")){ //Process the post here. Donot use doPost try{ //Get the values of the parameters message String message = req.getParameter("message"); //Message has user*status format if(message != null) { if(message.startsWith("MDMessengerCHAT")){ processChatRequest(message.substring(15), req, res); } else{ broadcastMessage(message); String userName = message.substring(0, message.indexOf("*")); onlineUserRepository.put(userName, "1"); res.setStatus(res.SC_NO_CONTENT); } } } catch(NullPointerException ex){ try{ System.out.println("Recreating connection to FMServer"); org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(); fms = FMSInterfaceHelper.bind(orb, "FMServer"); System.getProperties().put("MDM.FMServer", fms); dbm = DBServerInterfaceHelper.bind(orb, "DBServer"); System.getProperties().put("MDM.DBServer", dbm); } catch(Exception subEx){ subEx.printStackTrace(); } } catch(ClassCastException ex){ try{ System.out.println("Recreating connection to FMServer"); org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(); fms = FMSInterfaceHelper.bind(orb, "FMServer"); System.getProperties().put("MDM.FMServer", fms); dbm = DBServerInterfaceHelper.bind(orb, "DBServer"); System.getProperties().put("MDM.DBServer", dbm); } catch(Exception subEx){ subEx.printStackTrace(); } } catch(Exception ex){ ex.printStackTrace(); } //.............End of post.................... } } //This method contains the complete doPost of the chatServlet New private void processChatRequest(String message, HttpServletRequest req, HttpServletResponse res){ String sub=""; String userarray[]=new String[50]; String key=""; PrintWriter out = null; try{ out = res.getWriter(); if (message != null){ System.out.println("Inside message loop: " + message); // broadcastChatMessage(message); res.setContentType("text/plain"); // Return the next message (blocking) out.println(message); if (message.charAt(0)=='*') // * is to get the subject, randid and the invited buddies { System.out.println("Inside the invitation"); int n=parse(userarray, message); int lens=message.length(); int lenj=0; for(int i=0;i