1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ModuleController.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_sd.hxx"
33 #include "framework/ModuleController.hxx"
35 #include "tools/ConfigurationAccess.hxx"
36 #include <comphelper/processfactory.hxx>
37 #include <comphelper/stl_types.hxx>
38 #include <boost/bind.hpp>
41 using namespace ::com::sun::star
;
42 using namespace ::com::sun::star::uno
;
43 using namespace ::com::sun::star::drawing::framework
;
44 using ::rtl::OUString
;
45 using ::sd::tools::ConfigurationAccess
;
50 namespace sd
{ namespace framework
{
52 static const sal_uInt32
snFactoryPropertyCount (2);
53 static const sal_uInt32
snStartupPropertyCount (1);
58 class ModuleController::ResourceToFactoryMap
59 : public ::std::hash_map
<
62 ::comphelper::UStringHash
,
63 ::comphelper::UStringEqual
>
66 ResourceToFactoryMap (void) {}
70 class ModuleController::LoadedFactoryContainer
71 : public ::std::hash_map
<
73 WeakReference
<XInterface
>,
74 ::comphelper::UStringHash
,
75 ::comphelper::UStringEqual
>
78 LoadedFactoryContainer (void) {}
85 Reference
<XInterface
> SAL_CALL
ModuleController_createInstance (
86 const Reference
<XComponentContext
>& rxContext
)
88 return Reference
<XInterface
>(ModuleController::CreateInstance(rxContext
), UNO_QUERY
);
94 ::rtl::OUString
ModuleController_getImplementationName (void) throw(RuntimeException
)
96 return ::rtl::OUString(
97 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Draw.framework.module.ModuleController"));
103 Sequence
<rtl::OUString
> SAL_CALL
ModuleController_getSupportedServiceNames (void)
104 throw (RuntimeException
)
106 static const ::rtl::OUString
sServiceName(
107 ::rtl::OUString::createFromAscii("com.sun.star.drawing.framework.ModuleController"));
108 return Sequence
<rtl::OUString
>(&sServiceName
, 1);
114 //===== ModuleController ======================================================
116 Reference
<XModuleController
> ModuleController::CreateInstance (
117 const Reference
<XComponentContext
>& rxContext
)
119 return new ModuleController(rxContext
);
125 ModuleController::ModuleController (const Reference
<XComponentContext
>& rxContext
) throw()
126 : ModuleControllerInterfaceBase(MutexOwner::maMutex
),
128 mpResourceToFactoryMap(new ResourceToFactoryMap()),
129 mpLoadedFactories(new LoadedFactoryContainer())
132 LoadFactories(rxContext
);
138 ModuleController::~ModuleController (void) throw()
145 void SAL_CALL
ModuleController::disposing (void)
152 void ModuleController::LoadFactories (const Reference
<XComponentContext
>& rxContext
)
156 ConfigurationAccess
aConfiguration (
158 OUString::createFromAscii("/org.openoffice.Office.Impress/"),
159 ConfigurationAccess::READ_ONLY
);
160 Reference
<container::XNameAccess
> xFactories (
161 aConfiguration
.GetConfigurationNode(
162 OUString::createFromAscii("MultiPaneGUI/Framework/ResourceFactories")),
164 ::std::vector
<rtl::OUString
> aProperties (snFactoryPropertyCount
);
165 aProperties
[0] = OUString::createFromAscii("ServiceName");
166 aProperties
[1] = OUString::createFromAscii("ResourceList");
167 ConfigurationAccess::ForAll(
170 ::boost::bind(&ModuleController::ProcessFactory
, this, _2
));
174 OSL_TRACE("ERROR in ModuleController::LoadFactories");
181 void ModuleController::ProcessFactory (const ::std::vector
<Any
>& rValues
)
183 OSL_ASSERT(rValues
.size() == snFactoryPropertyCount
);
185 // Get the service name of the factory.
186 rtl::OUString sServiceName
;
187 rValues
[0] >>= sServiceName
;
189 // Get all resource URLs that are created by the factory.
190 Reference
<container::XNameAccess
> xResources (rValues
[1], UNO_QUERY
);
191 ::std::vector
<rtl::OUString
> aURLs
;
192 tools::ConfigurationAccess::FillList(
194 OUString::createFromAscii("URL"),
197 #if defined VERBOSE && VERBOSE>0
198 OSL_TRACE("ModuleController::adding factory %s",
199 OUStringToOString(sServiceName
, RTL_TEXTENCODING_UTF8
).getStr());
202 // Add the resource URLs to the map.
203 ::std::vector
<rtl::OUString
>::const_iterator iResource
;
204 for (iResource
=aURLs
.begin(); iResource
!=aURLs
.end(); ++iResource
)
206 (*mpResourceToFactoryMap
)[*iResource
] = sServiceName
;
207 #if defined VERBOSE && VERBOSE>1
209 OUStringToOString(*iResource
, RTL_TEXTENCODING_UTF8
).getStr());
217 void ModuleController::InstantiateStartupServices (void)
221 tools::ConfigurationAccess
aConfiguration (
222 OUString::createFromAscii("/org.openoffice.Office.Impress/"),
223 tools::ConfigurationAccess::READ_ONLY
);
224 Reference
<container::XNameAccess
> xFactories (
225 aConfiguration
.GetConfigurationNode(
226 OUString::createFromAscii("MultiPaneGUI/Framework/StartupServices")),
228 ::std::vector
<rtl::OUString
> aProperties (snStartupPropertyCount
);
229 aProperties
[0] = OUString::createFromAscii("ServiceName");
230 tools::ConfigurationAccess::ForAll(
233 ::boost::bind(&ModuleController::ProcessStartupService
, this, _2
));
237 OSL_TRACE("ERROR in ModuleController::InstantiateStartupServices");
244 void ModuleController::ProcessStartupService (const ::std::vector
<Any
>& rValues
)
246 OSL_ASSERT(rValues
.size() == snStartupPropertyCount
);
250 // Get the service name of the startup service.
251 rtl::OUString sServiceName
;
252 rValues
[0] >>= sServiceName
;
254 // Instantiate service.
255 Reference
<lang::XMultiServiceFactory
> xGlobalFactory (
256 ::comphelper::getProcessServiceFactory(), UNO_QUERY
);
257 if (xGlobalFactory
.is())
259 // Create the startup service.
260 Sequence
<Any
> aArguments(1);
261 aArguments
[0] <<= mxController
;
262 // Note that when the new object will be destroyed at the end of
263 // this scope when it does not register itself anywhere.
264 // Typically it will add itself as ConfigurationChangeListener
265 // at the configuration controller.
266 xGlobalFactory
->createInstanceWithArguments(sServiceName
, aArguments
);
268 #if defined VERBOSE && VERBOSE>0
269 OSL_TRACE("ModuleController::created startup service %s",
270 OUStringToOString(sServiceName
, RTL_TEXTENCODING_UTF8
).getStr());
276 OSL_TRACE("ERROR in ModuleController::ProcessStartupServices");
283 //----- XModuleController -----------------------------------------------------
285 void SAL_CALL
ModuleController::requestResource (const OUString
& rsResourceURL
)
286 throw (RuntimeException
)
288 ResourceToFactoryMap::const_iterator
iFactory (mpResourceToFactoryMap
->find(rsResourceURL
));
289 if (iFactory
!= mpResourceToFactoryMap
->end())
291 // Check that the factory has already been loaded and not been
292 // destroyed in the meantime.
293 Reference
<XInterface
> xFactory
;
294 LoadedFactoryContainer::const_iterator
iLoadedFactory (
295 mpLoadedFactories
->find(iFactory
->second
));
296 if (iLoadedFactory
!= mpLoadedFactories
->end())
297 xFactory
= Reference
<XInterface
>(iLoadedFactory
->second
, UNO_QUERY
);
298 if ( ! xFactory
.is())
300 // Create a new instance of the factory.
301 Reference
<lang::XMultiServiceFactory
> xGlobalFactory (
302 ::comphelper::getProcessServiceFactory(), UNO_QUERY
);
303 if (xGlobalFactory
.is())
305 // Create the factory service.
306 Sequence
<Any
> aArguments(1);
307 aArguments
[0] <<= mxController
;
308 xFactory
= xGlobalFactory
->createInstanceWithArguments(
312 // Remember that this factory has been instanced.
313 (*mpLoadedFactories
)[iFactory
->second
] = xFactory
;
322 //----- XInitialization -------------------------------------------------------
324 void SAL_CALL
ModuleController::initialize (const Sequence
<Any
>& aArguments
)
325 throw (Exception
, RuntimeException
)
327 if (aArguments
.getLength() > 0)
331 // Get the XController from the first argument.
332 mxController
= Reference
<frame::XController
>(aArguments
[0], UNO_QUERY_THROW
);
334 InstantiateStartupServices();
336 catch (RuntimeException
&)
342 } } // end of namespace sd::framework