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 .
21 #include "framework/ModuleController.hxx"
23 #include "tools/ConfigurationAccess.hxx"
24 #include <comphelper/processfactory.hxx>
25 #include <comphelper/stl_types.hxx>
26 #include <boost/bind.hpp>
27 #include <boost/unordered_map.hpp>
29 #include <tools/diagnose_ex.h>
31 using namespace ::com::sun::star
;
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::drawing::framework
;
34 using ::sd::tools::ConfigurationAccess
;
36 namespace sd
{ namespace framework
{
38 static const sal_uInt32
snFactoryPropertyCount (2);
39 static const sal_uInt32
snStartupPropertyCount (1);
44 class ModuleController::ResourceToFactoryMap
45 : public ::boost::unordered_map
<
49 ::comphelper::UStringEqual
>
52 ResourceToFactoryMap (void) {}
56 class ModuleController::LoadedFactoryContainer
57 : public ::boost::unordered_map
<
59 WeakReference
<XInterface
>,
61 ::comphelper::UStringEqual
>
64 LoadedFactoryContainer (void) {}
71 Reference
<XInterface
> SAL_CALL
ModuleController_createInstance (
72 const Reference
<XComponentContext
>& rxContext
)
74 return Reference
<XInterface
>(ModuleController::CreateInstance(rxContext
), UNO_QUERY
);
80 OUString
ModuleController_getImplementationName (void) throw(RuntimeException
)
82 return OUString("com.sun.star.comp.Draw.framework.module.ModuleController");
88 Sequence
<OUString
> SAL_CALL
ModuleController_getSupportedServiceNames (void)
89 throw (RuntimeException
)
91 static const OUString
sServiceName("com.sun.star.drawing.framework.ModuleController");
92 return Sequence
<OUString
>(&sServiceName
, 1);
98 //===== ModuleController ======================================================
100 Reference
<XModuleController
> ModuleController::CreateInstance (
101 const Reference
<XComponentContext
>& rxContext
)
103 return new ModuleController(rxContext
);
109 ModuleController::ModuleController (const Reference
<XComponentContext
>& rxContext
) throw()
110 : ModuleControllerInterfaceBase(MutexOwner::maMutex
),
112 mpResourceToFactoryMap(new ResourceToFactoryMap()),
113 mpLoadedFactories(new LoadedFactoryContainer())
116 LoadFactories(rxContext
);
122 ModuleController::~ModuleController (void) throw()
129 void SAL_CALL
ModuleController::disposing (void)
131 // Break the cyclic reference back to DrawController object
132 mpLoadedFactories
.reset();
133 mpResourceToFactoryMap
.reset();
134 mxController
.clear();
140 void ModuleController::LoadFactories (const Reference
<XComponentContext
>& rxContext
)
144 ConfigurationAccess
aConfiguration (
146 "/org.openoffice.Office.Impress/",
147 ConfigurationAccess::READ_ONLY
);
148 Reference
<container::XNameAccess
> xFactories (
149 aConfiguration
.GetConfigurationNode("MultiPaneGUI/Framework/ResourceFactories"),
151 ::std::vector
<OUString
> aProperties (snFactoryPropertyCount
);
152 aProperties
[0] = "ServiceName";
153 aProperties
[1] = "ResourceList";
154 ConfigurationAccess::ForAll(
157 ::boost::bind(&ModuleController::ProcessFactory
, this, _2
));
161 DBG_UNHANDLED_EXCEPTION();
168 void ModuleController::ProcessFactory (const ::std::vector
<Any
>& rValues
)
170 OSL_ASSERT(rValues
.size() == snFactoryPropertyCount
);
172 // Get the service name of the factory.
173 OUString sServiceName
;
174 rValues
[0] >>= sServiceName
;
176 // Get all resource URLs that are created by the factory.
177 Reference
<container::XNameAccess
> xResources (rValues
[1], UNO_QUERY
);
178 ::std::vector
<OUString
> aURLs
;
179 tools::ConfigurationAccess::FillList(
184 SAL_INFO("sd.fwk", OSL_THIS_FUNC
<< ": ModuleController::adding factory " <<
185 OUStringToOString(sServiceName
, RTL_TEXTENCODING_UTF8
).getStr());
187 // Add the resource URLs to the map.
188 ::std::vector
<OUString
>::const_iterator iResource
;
189 for (iResource
=aURLs
.begin(); iResource
!=aURLs
.end(); ++iResource
)
191 (*mpResourceToFactoryMap
)[*iResource
] = sServiceName
;
192 SAL_INFO("sd.fwk", OSL_THIS_FUNC
<< ": " <<
193 OUStringToOString(*iResource
, RTL_TEXTENCODING_UTF8
).getStr());
200 void ModuleController::InstantiateStartupServices (void)
204 tools::ConfigurationAccess
aConfiguration (
205 "/org.openoffice.Office.Impress/",
206 tools::ConfigurationAccess::READ_ONLY
);
207 Reference
<container::XNameAccess
> xFactories (
208 aConfiguration
.GetConfigurationNode("MultiPaneGUI/Framework/StartupServices"),
210 ::std::vector
<OUString
> aProperties (snStartupPropertyCount
);
211 aProperties
[0] = "ServiceName";
212 tools::ConfigurationAccess::ForAll(
215 ::boost::bind(&ModuleController::ProcessStartupService
, this, _2
));
219 OSL_TRACE("ERROR in ModuleController::InstantiateStartupServices");
226 void ModuleController::ProcessStartupService (const ::std::vector
<Any
>& rValues
)
228 OSL_ASSERT(rValues
.size() == snStartupPropertyCount
);
232 // Get the service name of the startup service.
233 OUString sServiceName
;
234 rValues
[0] >>= sServiceName
;
236 // Instantiate service.
237 Reference
<uno::XComponentContext
> xContext
=
238 ::comphelper::getProcessComponentContext();
240 // Create the startup service.
241 Sequence
<Any
> aArguments(1);
242 aArguments
[0] <<= mxController
;
243 // Note that when the new object will be destroyed at the end of
244 // this scope when it does not register itself anywhere.
245 // Typically it will add itself as ConfigurationChangeListener
246 // at the configuration controller.
247 xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(sServiceName
, aArguments
, xContext
);
249 SAL_INFO("sd.fwk", OSL_THIS_FUNC
<< ": ModuleController::created startup service " <<
250 OUStringToOString(sServiceName
, RTL_TEXTENCODING_UTF8
).getStr());
254 OSL_TRACE("ERROR in ModuleController::ProcessStartupServices");
261 //----- XModuleController -----------------------------------------------------
263 void SAL_CALL
ModuleController::requestResource (const OUString
& rsResourceURL
)
264 throw (RuntimeException
)
266 ResourceToFactoryMap::const_iterator
iFactory (mpResourceToFactoryMap
->find(rsResourceURL
));
267 if (iFactory
!= mpResourceToFactoryMap
->end())
269 // Check that the factory has already been loaded and not been
270 // destroyed in the meantime.
271 Reference
<XInterface
> xFactory
;
272 LoadedFactoryContainer::const_iterator
iLoadedFactory (
273 mpLoadedFactories
->find(iFactory
->second
));
274 if (iLoadedFactory
!= mpLoadedFactories
->end())
275 xFactory
= Reference
<XInterface
>(iLoadedFactory
->second
, UNO_QUERY
);
276 if ( ! xFactory
.is())
278 // Create a new instance of the factory.
279 Reference
<uno::XComponentContext
> xContext
=
280 ::comphelper::getProcessComponentContext();
282 // Create the factory service.
283 Sequence
<Any
> aArguments(1);
284 aArguments
[0] <<= mxController
;
285 OSL_TRACE("creating resource %s",
286 OUStringToOString(iFactory
->second
, RTL_TEXTENCODING_ASCII_US
).getStr());
289 xFactory
= xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(
294 catch (const Exception
&)
296 OSL_TRACE("caught exception while creating factory.");
299 // Remember that this factory has been instanced.
300 (*mpLoadedFactories
)[iFactory
->second
] = xFactory
;
308 //----- XInitialization -------------------------------------------------------
310 void SAL_CALL
ModuleController::initialize (const Sequence
<Any
>& aArguments
)
311 throw (Exception
, RuntimeException
)
313 if (aArguments
.getLength() > 0)
317 // Get the XController from the first argument.
318 mxController
= Reference
<frame::XController
>(aArguments
[0], UNO_QUERY_THROW
);
320 InstantiateStartupServices();
322 catch (RuntimeException
&)
328 } } // end of namespace sd::framework
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */