Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements. See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership. The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License. You may obtain a copy of the License at
   *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied. See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 package org.apache.axis2.transport.xmpp.util;
 
 import  org.jivesoftware.smack.ConnectionConfiguration;
 import  org.jivesoftware.smack.ConnectionListener;
 import  org.jivesoftware.smack.XMPPConnection;
 import  org.jivesoftware.smack.XMPPException;
 import  org.jivesoftware.smack.filter.FromContainsFilter;
 import  org.jivesoftware.smack.filter.PacketFilter;
 import  org.jivesoftware.smack.filter.ToContainsFilter;
 
 import java.util.Map;
 
 public class XMPPClientConnectionFactory {
 	private static Log log = LogFactory.getLog(XMPPClientConnectionFactory.class);
 	private XMPPConnection xmppConnection = null;
 	private PacketFilter packetFilter = null;
 	
Connects to a XMPP server based on the details available in serverCredentials object

Parameters:
serverCredentials
Throws:
XMPPException
 
 	public XMPPConnection connect(final XMPPServerCredentials serverCredentialsthrows AxisFault {
 		//XMPPConnection.DEBUG_ENABLED = true;		
 			 = new XMPPConnection(serverCredentials.getServerUrl());
 			try {
 				.connect();
 			} catch (XMPPException e) {
 				.error("Failed to connect to server :"+serverCredentials.getServerUrl(), e);
 				throw new AxisFault("Failed to connect to server :"+serverCredentials.getServerUrl());
 			}
 			//Pause for a small time before trying to login.
 			//This prevents random ssl exception from Smack API
 			try {
 				Thread.sleep(100);
 			} catch (InterruptedException e5) {
 				.debug("Sleep interrupted ",e5);
 			}
 
 			if(.isConnected()){
 				String resource = serverCredentials.getResource()+ new Object().hashCode();
 				if(! .isAuthenticated()){
 					try {
 						//xmppConnection.login(serverCredentials.getAccountName()+"@"+
 						//                serverCredentials.getServerUrl(),
                        .login(serverCredentials.getAccountName(), 
 								serverCredentials.getPassword(),
 								resource,
 								true);
 					} catch (XMPPException e) {
 						try {
 							.error("Login failed for "
 									+serverCredentials.getAccountName()
 									+"@"+serverCredentials.getServerUrl() 
 									+".Retrying in 2 secs",e); 
 							Thread.sleep(2000);
 							//xmppConnection.login(serverCredentials.getAccountName()+"@"+
 							//                serverCredentials.getServerUrl(),
                            .login(serverCredentials.getAccountName(), 
                                             serverCredentials.getPassword(),
                                             resource,
                                             true);
 							
 						} catch (InterruptedException e1) {
 							.error("Sleep interrupted.",e1);
 						} catch (XMPPException e2) {
 							.error("Login failed for : "+serverCredentials.getAccountName()
 									+"@"+serverCredentials.getServerUrl(),e2);
 							throw new AxisFault("Login failed for : "+serverCredentials.getAccountName()
 									+"@"+serverCredentials.getServerUrl());
 						}
 					}
					//Listen for Message type packets from specified server url
					//packetFilter = new AndFilter(new PacketTypeFilter(Message.class), 
					//		new FromContainsFilter(serverCredentials.getServerUrl()));
					 = new FromContainsFilter(serverCredentials.getServerUrl());					
				}
			}	
			ConnectionConfiguration connectionConfiguration = 
				new ConnectionConfiguration(.
			 = new XMPPConnection(connectionConfiguration);
			try {
				.connect();
				.login(serverCredentials.getAccountName()
serverCredentials.getPassword()
						,serverCredentials.getResource(),
						true);
				//packetFilter = new AndFilter(new PacketTypeFilter(Message.class), 
				//		new FromContainsFilter(XMPPConstants.GOOGLETALK_FROM));
				//packetFilter = new FromContainsFilter(XMPPConstants.GOOGLETALK_FROM);
				 = new ToContainsFilter("@gmail.com");
catch (XMPPException e1) {
				.error("Error occured while connecting to Googletalk server.",e1);
				throw new AxisFault("Error occured while connecting to Googletalk server.");
			}	
		}
		ConnectionListener connectionListener = null;
		connectionListener = new ConnectionListener(){
			public void connectionClosed() {
				.debug("Connection closed normally");
			}
					Exception e1) {
				.error("Connection to "+serverCredentials.getServerUrl()
" closed with error.",e1);
			}
			public void reconnectingIn(int seconds) {
				.error("Connection to "+serverCredentials.getServerUrl() 
						+" failed. Reconnecting in "+seconds+"s");
			}
			public void reconnectionFailed(Exception e) {
				.error("Reconnection to "+serverCredentials.getServerUrl()+" failed.",e);
			}
			public void reconnectionSuccessful() {
				.debug("Reconnection to "+serverCredentials.getServerUrl()+" successful.");
			}
		};
		if( != null && .isConnected()){
			.addConnectionListener(connectionListener);
			.info("Connected to " +serverCredentials.getAccountName()+ "@" 
serverCredentials.getServerUrl()+ "/"serverCredentials.getResource());
		}else{
			.warn(" Not Connected to " +serverCredentials.getAccountName()+ "@" 
serverCredentials.getServerUrl()+ "/"serverCredentials.getResource());
		}
	public XMPPConnection getXmppConnection(){
	    return ;
	}
//	public XMPPConnection getConnection(XMPPServerCredentials credentials) throws AxisFault{
//	    StringBuffer buf = new StringBuffer();
//	    String key = buf.append(credentials.getServerUrl()).append(credentials.getAccountName()).toString();
//	    
//	    XMPPConnectionDetails connDetails = xmppConnections.get(key);
//	    if(connDetails == null){
//	        connDetails = new XMPPConnectionDetails();
//	        connDetails.connection = connect(credentials);
//	        connDetails.userCount = 1;
//	        xmppConnections.put(key, connDetails);
//	    }else{
//	        connDetails.userCount = 1;
//	    }
//	    return connDetails.connection;
//	}
//	
//	public void disconnect(XMPPServerCredentials credentials){
//	    StringBuffer buf = new StringBuffer();
//        String key = buf.append(credentials.getServerUrl()).append(credentials.getAccountName()).toString();
//        
//        XMPPConnectionDetails connDetails = xmppConnections.get(key);
//        if(connDetails != null){
//            connDetails.userCount--;
//        }
//        if(connDetails.userCount == 0){
//            xmppConnections.remove(key);
//            connDetails.connection.disconnect();
//        }
//	} 
//	public void listen(XMPPPacketListener packetListener){
//		xmppConnection.addPacketListener(packetListener,packetFilter);
//	}
	public void stop() {}
	public class XMPPConnectionDetails{
	    XMPPConnection connection;
	    int userCount;
	}
New to GrepCode? Check out our FAQ X