aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/argdelegation/ArgDelegationPeerImpl.java
blob: b76111c4628a0c00bd8ba9e4d867556167522363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed 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 net.java.sip.communicator.impl.argdelegation;

import java.util.*;

import net.java.sip.communicator.service.argdelegation.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.util.*;
import net.java.sip.communicator.util.launchutils.*;

import org.osgi.framework.*;

/**
 * Implements the <tt>UriDelegationPeer</tt> interface from our argument handler
 * utility. We use this handler to relay arguments to URI handlers that have
 * been registered from other services such as the SIP provider for example.
 *
 * @author Emil Ivov
 */
public class ArgDelegationPeerImpl
    implements ArgDelegationPeer,
               ServiceListener
{
    /**
     * The <tt>Logger</tt> used by the <tt>ArgDelegationPeerImpl</tt> class and
     * its instances for logging output.
     */
    private static final Logger logger
        = Logger.getLogger(ArgDelegationPeerImpl.class);

    /**
     * The list of uriHandlers that we are currently aware of.
     */
    private final Map<String, UriHandler> uriHandlers
        = new Hashtable<String, UriHandler>();

    /**
     * Creates an instance of this peer and scans <tt>bundleContext</tt> for all
     * existing <tt>UriHandler</tt>
     *
     * @param bundleContext a reference to a currently valid instance of a
     * bundle context.
     */
    public ArgDelegationPeerImpl(BundleContext bundleContext)
    {
        Collection<ServiceReference<UriHandler>> uriHandlerRefs
            = ServiceUtils.getServiceReferences(
                    bundleContext,
                    UriHandler.class);

        if (!uriHandlerRefs.isEmpty())
        {
            synchronized (uriHandlers)
            {
                for (ServiceReference<UriHandler> uriHandlerRef
                        : uriHandlerRefs)
                {
                    UriHandler uriHandler
                        = bundleContext.getService(uriHandlerRef);

                    for (String protocol : uriHandler.getProtocol())
                    {
                        uriHandlers.put(protocol, uriHandler);
                    }
                }
            }
        }
    }

    /**
     * Listens for <tt>UriHandlers</tt> that are registered in the bundle
     * context after we had started so that we could add them to the list
     * of currently known handlers.
     *
     * @param event the event containing the newly (un)registered service.
     */
    public void serviceChanged(ServiceEvent event)
    {
        BundleContext bc
            = event.getServiceReference().getBundle().getBundleContext();

        /*
         * TODO When the Update button of the plug-in manager is invoked for the
         * IRC protocol provider plug-in, bc is of value null and thus causes a
         * NullPointerException. Determine whether it is a problem (in general)
         * to not process ServiceEvent.UNREGISTERING in such a case.
         */
        if (bc == null)
            return;

        Object service = bc.getService(event.getServiceReference());

        //we are only interested in UriHandler-s
        if (!(service instanceof UriHandler))
            return;

        UriHandler uriHandler = (UriHandler) service;

        synchronized (uriHandlers)
        {
            switch (event.getType())
            {
            case ServiceEvent.MODIFIED:
            case ServiceEvent.REGISTERED:
                for (String protocol : uriHandler.getProtocol())
                {
                    uriHandlers.put(protocol, uriHandler);
                }
                break;

            case ServiceEvent.UNREGISTERING:
                for (String protocol : uriHandler.getProtocol())
                {
                    if(uriHandlers.get(protocol) == uriHandler)
                        uriHandlers.remove(protocol);
                }

                break;
            }
        }
    }

    /**
     * Relays <tt>uirArg</tt> to the corresponding handler or shows an error
     * message in case no handler has been registered for the corresponding
     * protocol.
     *
     * @param uriArg the uri that we've been passed and that we'd like to
     * delegate to the corresponding provider.
     */
    public void handleUri(String uriArg)
    {
        if (logger.isTraceEnabled())
            logger.trace("Handling URI: " + uriArg);
        //first parse the uri and determine the scheme/protocol
        //the parsing is currently a bit oversimplified so we'd probably need
        //to revisit it at some point.
        int colonIndex = uriArg.indexOf(":");

        if( colonIndex == -1)
        {
            //no scheme, we don't know how to handle the URI
            ArgDelegationActivator.getUIService().getPopupDialog()
                .showMessagePopupDialog(
                        "Could not determine how to handle: " + uriArg
                            + ".\nNo protocol scheme found.",
                        "Error handling URI",
                        PopupDialog.ERROR_MESSAGE);
            return;
        }

        String scheme = uriArg.substring(0, colonIndex);

        UriHandler handler;
        synchronized (uriHandlers) {
            handler = uriHandlers.get(scheme);
        }

        //if handler is null we need to tell the user.
        if(handler == null)
        {
            if (logger.isTraceEnabled())
                logger.trace("Couldn't open " + uriArg
                         + "No handler found for protocol"+ scheme);
            ArgDelegationActivator.getUIService().getPopupDialog()
                .showMessagePopupDialog(
                        "\"" + scheme + "\" URIs are currently not supported.",
                        "Error handling URI",
                        PopupDialog.ERROR_MESSAGE);
            return;
        }

        //we're all set. let's do the handling now.
        try
        {
            handler.handleUri(uriArg);
        }
        //catch every possible exception
        catch(Throwable thr)
        {
            // ThreadDeath should always be re-thrown.
            if (thr instanceof ThreadDeath)
                throw (ThreadDeath) thr;

            ArgDelegationActivator.getUIService().getPopupDialog()
                .showMessagePopupDialog(
                        "Error handling " + uriArg,
                        "Error handling URI",
                        PopupDialog.ERROR_MESSAGE);
            logger.error("Failed to handle \""+ uriArg +"\"", thr);
        }
    }

    /**
     * This method would simply bring the application on focus as it is called
     * when the user has tried to launch a second instance of SIP Communicator
     * while a first one was already running.  Future implementations may also
     * show an error/information message to the user notifying them that a
     * second instance is not to be launched.
     */
    public void handleConcurrentInvocationRequest()
    {
        ArgDelegationActivator.getUIService().setVisible(true);
    }
}