001/**
002 *
003 * Copyright 2003-2006 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.jingleold.nat;
018
019import org.jivesoftware.smack.SmackException;
020import org.jivesoftware.smack.XMPPException;
021
022import org.jivesoftware.smackx.jingleold.JingleSession;
023
024/**
025 * Transport manager for Jingle.
026 *
027 * This class makes easier the use of transport resolvers by presenting a simple
028 * interface for algorithm selection. The transport manager also keeps the match
029 * between the resolution method and the <transport> element present in
030 * Jingle packets.
031 *
032 * As Jingle have many transport methods (official and unofficial methods),
033 * this abstract class helps us to extends the transport support of the API.
034 *
035 * This class must be used with a JingleManager instance in the following way:
036 *
037 * JingleManager jingleManager = new JingleManager(xmppConnection, new BasicTransportManager());
038 *
039 * @author Thiago Camargo
040 */
041public abstract class JingleTransportManager {
042    // This class implements the context of a Strategy pattern...
043
044    /**
045     * Default constructor.
046     */
047    public JingleTransportManager() {
048
049    }
050
051    /**
052     * Get a new Transport Resolver to be used in a Jingle Session.
053     *
054     * @param session the jingle session.
055     * @return the TransportResolver to be used
056     * @throws XMPPException if an XMPP protocol error was received.
057     * @throws SmackException if Smack detected an exceptional situation.
058     * @throws InterruptedException if the calling thread was interrupted.
059     */
060    public TransportResolver getResolver(JingleSession session) throws XMPPException, SmackException, InterruptedException {
061        TransportResolver resolver = createResolver(session);
062        if (resolver == null) {
063            resolver = new BasicResolver();
064        }
065        resolver.initializeAndWait();
066
067        return resolver;
068    }
069
070    /**
071     * Create a Transport Resolver instance according to the implementation.
072     *
073     * @param session the jingle session.
074     * @return the TransportResolver
075     * @throws SmackException if Smack detected an exceptional situation.
076     * @throws InterruptedException if the calling thread was interrupted.
077     */
078    protected abstract TransportResolver createResolver(JingleSession session) throws SmackException, InterruptedException;
079
080}