BowlerKernel
GoogleChatEngine.java
Go to the documentation of this file.
1 package com.neuronrobotics.application.xmpp.GoogleChat;
2 
3 
4 
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.ArrayList;
8 
9 import javax.xml.parsers.DocumentBuilder;
10 import javax.xml.parsers.DocumentBuilderFactory;
11 import javax.xml.parsers.ParserConfigurationException;
12 
13 import org.jivesoftware.smack.Chat;
14 import org.jivesoftware.smack.ChatManager;
15 import org.jivesoftware.smack.ChatManagerListener;
16 import org.jivesoftware.smack.ConnectionConfiguration;
17 import org.jivesoftware.smack.MessageListener;
18 import org.jivesoftware.smack.XMPPConnection;
19 import org.jivesoftware.smack.XMPPException;
20 import org.jivesoftware.smack.packet.Presence;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23 import org.w3c.dom.Node;
24 import org.w3c.dom.NodeList;
25 import org.xml.sax.SAXException;
26 
27 import com.neuronrobotics.application.xmpp.IConversationFactory;
28 
29 
30 
31 
32 // TODO: Auto-generated Javadoc
36 public class GoogleChatEngine implements ChatManagerListener {
37 
39  private static String username = "user@gmail.com";
40 
42  private static String password = "pass1234";
43 
44 
46  private static String host = "talk.google.com";
47 
49  private static String service = "gmail.com";
50 
52  private static int port = 5222;
53 
55  private ConnectionConfiguration connConfig;
56 
58  private XMPPConnection connection;
59 
61  private Presence presence;
62 
64  private ChatManager chatmanager;
65 
67  ArrayList<GoogleChat> googleChats = new ArrayList<GoogleChat> ();
68 
71 
80  public GoogleChatEngine(IConversationFactory responder,String user,String pass) throws XMPPException {
81  username=user;
82  password=pass;
84  }
85 
93  public GoogleChatEngine(IConversationFactory responder,InputStream config) throws XMPPException {
94  setLoginInfo(config);
96  }
97 
104  private void setup(IConversationFactory responder) throws XMPPException {
105  this.responder=responder;
106  if((MessageListener.class.isInstance(responder)))
107  throw new RuntimeException("Instance of IConversationFactory must also implement org.jivesoftware.smack.MessageListener");
108  connConfig = new ConnectionConfiguration(host, port, service);
109  connection = new XMPPConnection(connConfig);
110  connection.connect();
111  connection.login(username, password);
112  presence = new Presence(Presence.Type.available);
113  connection.sendPacket(presence);
114  chatmanager = connection.getChatManager();
115  chatmanager.addChatListener(this);
116  }
117 
123  private void setLoginInfo(InputStream config) {
124  //InputStream config = GoogleChatEngine.class.getResourceAsStream("loginInfo.xml");
125  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
126  DocumentBuilder dBuilder;
127  Document doc = null;
128  try {
129  dBuilder = dbFactory.newDocumentBuilder();
130  doc = dBuilder.parse(config);
131  doc.getDocumentElement().normalize();
132  } catch (ParserConfigurationException e) {
133  throw new RuntimeException(e);
134  } catch (SAXException e) {
135  throw new RuntimeException(e);
136  } catch (IOException e) {
137  throw new RuntimeException(e);
138  }
139  //System.out.println("Parsing File...");
140  NodeList nList = doc.getElementsByTagName("login");
141  for (int temp = 0; temp < nList.getLength(); temp++) {
142  //System.out.println("Leg # "+temp);
143  Element eElement = (Element)nList.item(temp);
144  username = getTagValue("username",eElement);
145  password = getTagValue("password",eElement);
146 
147  }
148  }
149 
157  public static String getTagValue(String sTag, Element eElement){
158  NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
159  Node nValue = (Node) nlList.item(0);
160  //System.out.println("\t\t"+sTag+" = "+nValue.getNodeValue());
161  return nValue.getNodeValue();
162  }
163 
169  private MessageListener getNewMessageListener(){
170  return (MessageListener)responder.getConversation();
171  }
172 
173  /* (non-Javadoc)
174  * @see org.jivesoftware.smack.ChatManagerListener#chatCreated(org.jivesoftware.smack.Chat, boolean)
175  */
176  @Override
177  public void chatCreated(Chat arg0, boolean arg1) {
178  // TODO Auto-generated method stub
179  arg0.addMessageListener( getNewMessageListener());
180  googleChats.add(new GoogleChat(arg0));
181  }
182 
189  public GoogleChat startChat(String user){
190  Chat chat = chatmanager.createChat(user,getNewMessageListener());
191  GoogleChat c = new GoogleChat(chat);
192  googleChats.add(c);
193  return c;
194  }
195 
203  public GoogleChat startChat(String user, MessageListener listener){
204  Chat chat = chatmanager.createChat(user, listener);
205  GoogleChat c = new GoogleChat(chat);
206  googleChats.add(c);
207  return c;
208  }
209 
215  public ArrayList<GoogleChat> getChats(){
216  ArrayList<GoogleChat> tmp = new ArrayList<GoogleChat>();
217  for(GoogleChat c:googleChats){
218  if(c!=null && c.isAlive() )
219  tmp.add(c);
220  }
221  googleChats=tmp;
222  tmp = new ArrayList<GoogleChat>();
223  for(GoogleChat c:googleChats){
224  if(c!=null && c.isAlive() )
225  tmp.add(c);
226  }
227  return tmp;
228  }
229 
233  public void disconnect(){
234  connection.disconnect();
235  }
236 
237 
238 
239 }
GoogleChatEngine(IConversationFactory responder, String user, String pass)
static String getTagValue(String sTag, Element eElement)
GoogleChat startChat(String user, MessageListener listener)
GoogleChatEngine(IConversationFactory responder, InputStream config)