Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <dispatch/servicehandler.hxx>
21 : #include <general.h>
22 : #include <services.h>
23 :
24 : #include <com/sun/star/frame/DispatchResultState.hpp>
25 : #include <com/sun/star/task/XJobExecutor.hpp>
26 :
27 : #include <vcl/svapp.hxx>
28 :
29 : namespace framework{
30 :
31 : #define PROTOCOL_VALUE "service:"
32 : #define PROTOCOL_LENGTH 8
33 :
34 : // XInterface, XTypeProvider, XServiceInfo
35 :
36 84 : DEFINE_XSERVICEINFO_MULTISERVICE(ServiceHandler ,
37 : ::cppu::OWeakObject ,
38 : SERVICENAME_PROTOCOLHANDLER ,
39 : IMPLEMENTATIONNAME_SERVICEHANDLER)
40 :
41 2 : DEFINE_INIT_SERVICE(ServiceHandler,
42 : {
43 : /*Attention
44 : I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
45 : to create a new instance of this class by our own supported service factory.
46 : see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further information!
47 : */
48 : }
49 : )
50 :
51 : /**
52 : @short standard ctor
53 : @descr This initializes a new instance of ths class with needed information for work.
54 :
55 : @param xFactory
56 : reference to uno servicemanager for creation of new services
57 : */
58 2 : ServiceHandler::ServiceHandler( const css::uno::Reference< css::lang::XMultiServiceFactory >& xFactory )
59 2 : : m_xFactory ( xFactory )
60 : {
61 2 : }
62 :
63 : /**
64 : @short standard dtor
65 : */
66 4 : ServiceHandler::~ServiceHandler()
67 : {
68 4 : }
69 :
70 : /**
71 : @short decide if this dispatch implementation can be used for requested URL or not
72 : @descr A protocol handler is registered for an URL pattern inside configuration and will
73 : be asked by the generic dispatch mechanism inside framework, if he can handle this
74 : special URL which match his registration. He can agree by returning of a valid dispatch
75 : instance or disagree by returning <NULL/>.
76 : We don't create new dispatch instances here really - we return THIS as result to handle it
77 : at the same implementation.
78 : */
79 3 : css::uno::Reference< css::frame::XDispatch > SAL_CALL ServiceHandler::queryDispatch( const css::util::URL& aURL ,
80 : const OUString& /*sTarget*/ ,
81 : sal_Int32 /*nFlags*/ ) throw( css::uno::RuntimeException, std::exception )
82 : {
83 3 : css::uno::Reference< css::frame::XDispatch > xDispatcher;
84 3 : if (aURL.Complete.startsWith(PROTOCOL_VALUE))
85 3 : xDispatcher = this;
86 3 : return xDispatcher;
87 : }
88 :
89 : /**
90 : @short do the same like dispatch() but for multiple requests at the same time
91 : */
92 1 : css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL ServiceHandler::queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor ) throw( css::uno::RuntimeException, std::exception )
93 : {
94 1 : sal_Int32 nCount = lDescriptor.getLength();
95 1 : css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatcher( nCount );
96 3 : for( sal_Int32 i=0; i<nCount; ++i )
97 : {
98 8 : lDispatcher[i] = this->queryDispatch(
99 2 : lDescriptor[i].FeatureURL,
100 2 : lDescriptor[i].FrameName,
101 4 : lDescriptor[i].SearchFlags);
102 : }
103 1 : return lDispatcher;
104 : }
105 :
106 : /**
107 : @short dispatch URL with arguments
108 : @descr We use threadsafe internal method to do so. It returns a state value - but we ignore it.
109 : Because we don't support status listener notifications here.
110 :
111 : @param aURL
112 : uno URL which should be executed
113 : @param lArguments
114 : list of optional arguments for this request
115 : */
116 0 : void SAL_CALL ServiceHandler::dispatch( const css::util::URL& aURL ,
117 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments ) throw( css::uno::RuntimeException, std::exception )
118 : {
119 : // dispatch() is an [oneway] call ... and may our user release his reference to us immediately.
120 : // So we should hold us self alive till this call ends.
121 0 : css::uno::Reference< css::frame::XNotifyingDispatch > xSelfHold(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
122 0 : implts_dispatch(aURL,lArguments);
123 : // No notification for status listener!
124 0 : }
125 :
126 : /**
127 : @short dispatch with guaranteed notifications about success
128 : @descr We use threadsafe internal method to do so. Return state of this function will be used
129 : for notification if an optional listener is given.
130 :
131 : @param aURL
132 : uno URL which should be executed
133 : @param lArguments
134 : list of optional arguments for this request
135 : @param xListener
136 : optional listener for state events
137 : */
138 0 : void SAL_CALL ServiceHandler::dispatchWithNotification( const css::util::URL& aURL ,
139 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments,
140 : const css::uno::Reference< css::frame::XDispatchResultListener >& xListener ) throw( css::uno::RuntimeException, std::exception )
141 : {
142 : // This class was designed to die by reference. And if user release his reference to us immediately after calling this method
143 : // we can run into some problems. So we hold us self alive till this method ends.
144 : // Another reason: We can use this reference as source of sending event at the end too.
145 0 : css::uno::Reference< css::frame::XNotifyingDispatch > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
146 :
147 0 : css::uno::Reference< css::uno::XInterface > xService = implts_dispatch(aURL,lArguments);
148 0 : if (xListener.is())
149 : {
150 0 : css::frame::DispatchResultEvent aEvent;
151 0 : if (xService.is())
152 0 : aEvent.State = css::frame::DispatchResultState::SUCCESS;
153 : else
154 0 : aEvent.State = css::frame::DispatchResultState::FAILURE;
155 0 : aEvent.Result <<= xService; // may NULL for state=FAILED!
156 0 : aEvent.Source = xThis;
157 :
158 0 : xListener->dispatchFinished( aEvent );
159 0 : }
160 0 : }
161 :
162 : /**
163 : @short threadsafe helper for dispatch calls
164 : @descr We support two interfaces for the same process - dispatch URLs. That the reason for this internal
165 : function. It implements the real dispatch operation and returns a state value which inform caller
166 : about success. He can notify listener then by using this return value.
167 :
168 : @param aURL
169 : uno URL which should be executed
170 : @param lArguments
171 : list of optional arguments for this request
172 :
173 : @return <NULL/> if requested service couldn't be created successullfy;
174 : a valid reference otherwise. This return value can be used to indicate,
175 : if dispatch was successfully or not.
176 : */
177 0 : css::uno::Reference< css::uno::XInterface > ServiceHandler::implts_dispatch( const css::util::URL& aURL ,
178 : const css::uno::Sequence< css::beans::PropertyValue >& /*lArguments*/ ) throw( css::uno::RuntimeException )
179 : {
180 0 : if (!m_xFactory.is())
181 0 : return css::uno::Reference< css::uno::XInterface >();
182 :
183 : // extract service name and may optional given parameters from given URL
184 : // and use it to create and start the component
185 0 : OUString sServiceAndArguments = aURL.Complete.copy(PROTOCOL_LENGTH);
186 0 : OUString sServiceName;
187 0 : OUString sArguments;
188 :
189 0 : sal_Int32 nArgStart = sServiceAndArguments.indexOf('?',0);
190 0 : if (nArgStart!=-1)
191 : {
192 0 : sServiceName = sServiceAndArguments.copy(0,nArgStart);
193 0 : ++nArgStart; // ignore '?'!
194 0 : sArguments = sServiceAndArguments.copy(nArgStart);
195 : }
196 : else
197 : {
198 0 : sServiceName = sServiceAndArguments;
199 : }
200 :
201 0 : if (sServiceName.isEmpty())
202 0 : return css::uno::Reference< css::uno::XInterface >();
203 :
204 : // If a service doesn't support an optional job executor interface - he can't get
205 : // any given parameters!
206 : // Because we can't know if we must call createInstanceWithArguments() or XJobExecutor::trigger() ...
207 :
208 0 : css::uno::Reference< css::uno::XInterface > xService;
209 : try
210 : {
211 : // => a) a service starts running inside his own ctor and we create it only
212 0 : xService = m_xFactory->createInstance(sServiceName);
213 : // or b) he implements the right interface and starts there (may with optional parameters)
214 0 : css::uno::Reference< css::task::XJobExecutor > xExecuteable(xService, css::uno::UNO_QUERY);
215 0 : if (xExecuteable.is())
216 0 : xExecuteable->trigger(sArguments);
217 : }
218 : // ignore all errors - inclusive runtime errors!
219 : // E.g. a script based service (written in phyton) could not be executed
220 : // because it contains syntax errors, which was detected at runtime ...
221 0 : catch(const css::uno::Exception& e)
222 : {
223 : SAL_WARN(
224 : "fwk.dispatch", "ignored UNO Exception \"" << e.Message << '"');
225 0 : xService.clear();
226 : }
227 :
228 0 : return xService;
229 : }
230 :
231 : /**
232 : @short add/remove listener for state events
233 : @descr We use an internal container to hold such registered listener. This container lives if we live.
234 : And if call pass registration as non breakable transaction - we can accept the request without
235 : any explicit lock. Because we share our mutex with this container.
236 :
237 : @param xListener
238 : reference to a valid listener for state events
239 : @param aURL
240 : URL about listener will be informed, if something occurred
241 : */
242 0 : void SAL_CALL ServiceHandler::addStatusListener( const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/ ,
243 : const css::util::URL& /*aURL*/ ) throw( css::uno::RuntimeException, std::exception )
244 : {
245 : // not supported yet
246 0 : }
247 :
248 0 : void SAL_CALL ServiceHandler::removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/ ,
249 : const css::util::URL& /*aURL*/ ) throw( css::uno::RuntimeException, std::exception )
250 : {
251 : // not supported yet
252 0 : }
253 :
254 : } // namespace framework
255 :
256 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|