001    /*
002     * File:    $HeadURL: https://jvoicexml.svn.sourceforge.net/svnroot/jvoicexml/core/trunk/org.jvoicexml/src/org/jvoicexml/client/jndi/JVoiceXmlStub.java $
003     * Version: $LastChangedRevision: 2430 $
004     * Date:    $Date: 2010-12-21 09:21:06 +0100 (Di, 21 Dez 2010) $
005     * Author:  $LastChangedBy: schnelle $
006     *
007     * JVoiceXML - A free VoiceXML implementation.
008     *
009     * Copyright (C) 2006-2007 JVoiceXML group - http://jvoicexml.sourceforge.net
010     *
011     *  This library is free software; you can redistribute it and/or
012     *  modify it under the terms of the GNU Library General Public
013     *  License as published by the Free Software Foundation; either
014     *  version 2 of the License, or (at your option) any later version.
015     *
016     *  This library is distributed in the hope that it will be useful,
017     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
018     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     *  Library General Public License for more details.
020     *
021     *  You should have received a copy of the GNU Library General Public
022     *  License along with this library; if not, write to the Free Software
023     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024     *
025     */
026    
027    package org.jvoicexml.client.jndi;
028    
029    import java.net.InetAddress;
030    import java.net.URI;
031    import java.net.URISyntaxException;
032    import java.net.UnknownHostException;
033    import java.util.Map;
034    
035    import javax.naming.Context;
036    import javax.naming.NamingException;
037    
038    import org.jvoicexml.ConnectionInformation;
039    import org.jvoicexml.JVoiceXml;
040    import org.jvoicexml.Session;
041    import org.jvoicexml.client.BasicConnectionInformation;
042    import org.jvoicexml.client.TcpUriFactory;
043    import org.jvoicexml.event.ErrorEvent;
044    
045    /**
046     * Stub for <code>JVoiceXml</code>.
047     *
048     * @author Dirk Schnelle-Walka
049     * @version $Revision: 2430 $
050     * @since 0.4
051     * @see org.jvoicexml.JVoiceXml
052     */
053    public final class JVoiceXmlStub
054            extends AbstractStub<RemoteJVoiceXml>
055            implements JVoiceXml {
056        /**
057         * Constructs a new object.
058         */
059        public JVoiceXmlStub() {
060        }
061    
062        /**
063         * Constructs a new object.
064         * @param context The context to use.
065         * @since 0.6
066         */
067        public JVoiceXmlStub(final Context context) {
068            super(context);
069        }
070    
071        /**
072         * {@inheritDoc}
073         */
074        public String getStubName() {
075            return JVoiceXml.class.getSimpleName();
076        }
077    
078        /**
079         * {@inheritDoc}
080         */
081        @Override
082        protected Class<RemoteJVoiceXml> getRemoteClass() {
083            return RemoteJVoiceXml.class;
084        }
085    
086        /**
087         * {@inheritDoc}
088         */
089        @Override
090        protected Class<?> getLocalClass() {
091            return JVoiceXml.class;
092        }
093    
094        /**
095         * {@inheritDoc}
096         */
097        public String getVersion() {
098            final RemoteJVoiceXml jvxml = getSkeleton();
099            try {
100                return jvxml.getVersion();
101            } catch (java.rmi.RemoteException re) {
102                clearSkeleton();
103    
104                re.printStackTrace();
105    
106                return null;
107            }
108        }
109    
110        /**
111         * {@inheritDoc}
112         */
113        public Session createSession(final ConnectionInformation client)
114                throws ErrorEvent {
115            final RemoteJVoiceXml jvxml = getSkeleton();
116    
117            Session session;
118            try {
119                if (client instanceof BasicConnectionInformation) {
120                    final Context context = getContext();
121                    final Map<?, ?> env = context.getEnvironment();
122                    final BasicConnectionInformation basic =
123                        (BasicConnectionInformation) client;
124                    if (basic.getCalledDevice() == null) {
125                        final Object prov = env.get(Context.PROVIDER_URL);
126                        basic.setCalledDevice(new URI(prov.toString()));
127                    }
128                    if (basic.getCallingDevice() == null) {
129                        final InetAddress localhost = InetAddress.getLocalHost();
130                        final URI uri = TcpUriFactory.createUri(localhost);
131                        basic.setCallingDevice(uri);
132                    }
133                    if (basic.getProtocolName() == null) {
134                        basic.setProtocolName("rmi");
135                    }
136                }
137                session = jvxml.createSession(client);
138            } catch (java.rmi.RemoteException re) {
139                clearSkeleton();
140                session = null;
141    
142                final ErrorEvent event = getErrorEvent(re);
143                if (event == null) {
144                    re.printStackTrace();
145                } else {
146                    throw event;
147                }
148            } catch (NamingException e) {
149                clearSkeleton();
150                session = null;
151                e.printStackTrace();
152            } catch (URISyntaxException e) {
153                clearSkeleton();
154                session = null;
155                e.printStackTrace();
156            } catch (UnknownHostException e) {
157                clearSkeleton();
158                session = null;
159                e.printStackTrace();
160            }
161    
162            // Reuse the context on the client.
163            if (session instanceof SessionStub) {
164                final SessionStub sessionStub = (SessionStub) session;
165                final Context context = getContext();
166                sessionStub.setContext(context);
167            }
168    
169            return session;
170        }
171    
172        /**
173         * {@inheritDoc}
174         */
175        public void shutdown() {
176            final RemoteJVoiceXml jvxml = getSkeleton();
177    
178            try {
179                jvxml.shutdown();
180            } catch (java.rmi.RemoteException re) {
181                clearSkeleton();
182    
183                re.printStackTrace();
184            }
185        }
186    }