1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <dispatch/servicehandler.hxx>
24 #include <com/sun/star/frame/DispatchResultState.hpp>
25 #include <com/sun/star/task/XJobExecutor.hpp>
27 #include <vcl/svapp.hxx>
31 #define PROTOCOL_VALUE "service:"
32 #define PROTOCOL_LENGTH 8
34 // XInterface, XTypeProvider, XServiceInfo
36 DEFINE_XSERVICEINFO_MULTISERVICE(ServiceHandler
,
38 SERVICENAME_PROTOCOLHANDLER
,
39 IMPLEMENTATIONNAME_SERVICEHANDLER
)
41 DEFINE_INIT_SERVICE(ServiceHandler
,
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!
53 @descr This initializes a new instance of ths class with needed information for work.
56 reference to uno servicemanager for creation of new services
58 ServiceHandler::ServiceHandler( const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xFactory
)
59 : m_xFactory ( xFactory
)
66 ServiceHandler::~ServiceHandler()
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.
79 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
)
83 css::uno::Reference
< css::frame::XDispatch
> xDispatcher
;
84 if (aURL
.Complete
.startsWith(PROTOCOL_VALUE
))
90 @short do the same like dispatch() but for multiple requests at the same time
92 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
)
94 sal_Int32 nCount
= lDescriptor
.getLength();
95 css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > lDispatcher( nCount
);
96 for( sal_Int32 i
=0; i
<nCount
; ++i
)
98 lDispatcher
[i
] = this->queryDispatch(
99 lDescriptor
[i
].FeatureURL
,
100 lDescriptor
[i
].FrameName
,
101 lDescriptor
[i
].SearchFlags
);
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.
112 uno URL which should be executed
114 list of optional arguments for this request
116 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
)
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 css::uno::Reference
< css::frame::XNotifyingDispatch
> xSelfHold(static_cast< ::cppu::OWeakObject
* >(this), css::uno::UNO_QUERY
);
122 implts_dispatch(aURL
,lArguments
);
123 // No notification for status listener!
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.
132 uno URL which should be executed
134 list of optional arguments for this request
136 optional listener for state events
138 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
)
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 css::uno::Reference
< css::frame::XNotifyingDispatch
> xThis(static_cast< ::cppu::OWeakObject
* >(this), css::uno::UNO_QUERY
);
147 css::uno::Reference
< css::uno::XInterface
> xService
= implts_dispatch(aURL
,lArguments
);
150 css::frame::DispatchResultEvent aEvent
;
152 aEvent
.State
= css::frame::DispatchResultState::SUCCESS
;
154 aEvent
.State
= css::frame::DispatchResultState::FAILURE
;
155 aEvent
.Result
<<= xService
; // may NULL for state=FAILED!
156 aEvent
.Source
= xThis
;
158 xListener
->dispatchFinished( aEvent
);
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.
169 uno URL which should be executed
171 list of optional arguments for this request
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.
177 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
)
180 if (!m_xFactory
.is())
181 return css::uno::Reference
< css::uno::XInterface
>();
183 // extract service name and may optional given parameters from given URL
184 // and use it to create and start the component
185 OUString sServiceAndArguments
= aURL
.Complete
.copy(PROTOCOL_LENGTH
);
186 OUString sServiceName
;
189 sal_Int32 nArgStart
= sServiceAndArguments
.indexOf('?',0);
192 sServiceName
= sServiceAndArguments
.copy(0,nArgStart
);
193 ++nArgStart
; // ignore '?'!
194 sArguments
= sServiceAndArguments
.copy(nArgStart
);
198 sServiceName
= sServiceAndArguments
;
201 if (sServiceName
.isEmpty())
202 return css::uno::Reference
< css::uno::XInterface
>();
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() ...
208 css::uno::Reference
< css::uno::XInterface
> xService
;
211 // => a) a service starts running inside his own ctor and we create it only
212 xService
= m_xFactory
->createInstance(sServiceName
);
213 // or b) he implements the right interface and starts there (may with optional parameters)
214 css::uno::Reference
< css::task::XJobExecutor
> xExecuteable(xService
, css::uno::UNO_QUERY
);
215 if (xExecuteable
.is())
216 xExecuteable
->trigger(sArguments
);
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 catch(const css::uno::Exception
& e
)
224 "fwk.dispatch", "ignored UNO Exception \"" << e
.Message
<< '"');
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.
238 reference to a valid listener for state events
240 URL about listener will be informed, if something occurred
242 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
)
248 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
)
254 } // namespace framework
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */