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 :
21 : #include "framework/ModuleController.hxx"
22 :
23 : #include "tools/ConfigurationAccess.hxx"
24 : #include <comphelper/processfactory.hxx>
25 : #include <boost/bind.hpp>
26 : #include <boost/unordered_map.hpp>
27 :
28 : #include <tools/diagnose_ex.h>
29 :
30 : using namespace ::com::sun::star;
31 : using namespace ::com::sun::star::uno;
32 : using namespace ::com::sun::star::drawing::framework;
33 : using ::sd::tools::ConfigurationAccess;
34 :
35 : namespace sd { namespace framework {
36 :
37 : static const sal_uInt32 snFactoryPropertyCount (2);
38 : static const sal_uInt32 snStartupPropertyCount (1);
39 :
40 :
41 :
42 :
43 0 : class ModuleController::ResourceToFactoryMap
44 : : public ::boost::unordered_map<
45 : OUString,
46 : OUString,
47 : OUStringHash>
48 : {
49 : public:
50 0 : ResourceToFactoryMap (void) {}
51 : };
52 :
53 :
54 0 : class ModuleController::LoadedFactoryContainer
55 : : public ::boost::unordered_map<
56 : OUString,
57 : WeakReference<XInterface>,
58 : OUStringHash>
59 : {
60 : public:
61 0 : LoadedFactoryContainer (void) {}
62 : };
63 :
64 :
65 :
66 :
67 :
68 0 : Reference<XInterface> SAL_CALL ModuleController_createInstance (
69 : const Reference<XComponentContext>& rxContext)
70 : {
71 0 : return Reference<XInterface>(ModuleController::CreateInstance(rxContext), UNO_QUERY);
72 : }
73 :
74 :
75 :
76 :
77 0 : OUString ModuleController_getImplementationName (void) throw(RuntimeException)
78 : {
79 0 : return OUString("com.sun.star.comp.Draw.framework.module.ModuleController");
80 : }
81 :
82 :
83 :
84 :
85 0 : Sequence<OUString> SAL_CALL ModuleController_getSupportedServiceNames (void)
86 : throw (RuntimeException)
87 : {
88 0 : static const OUString sServiceName("com.sun.star.drawing.framework.ModuleController");
89 0 : return Sequence<OUString>(&sServiceName, 1);
90 : }
91 :
92 :
93 :
94 :
95 : //===== ModuleController ======================================================
96 :
97 0 : Reference<XModuleController> ModuleController::CreateInstance (
98 : const Reference<XComponentContext>& rxContext)
99 : {
100 0 : return new ModuleController(rxContext);
101 : }
102 :
103 :
104 :
105 :
106 0 : ModuleController::ModuleController (const Reference<XComponentContext>& rxContext) throw()
107 : : ModuleControllerInterfaceBase(MutexOwner::maMutex),
108 : mxController(),
109 0 : mpResourceToFactoryMap(new ResourceToFactoryMap()),
110 0 : mpLoadedFactories(new LoadedFactoryContainer())
111 : {
112 : (void)rxContext;
113 0 : LoadFactories(rxContext);
114 0 : }
115 :
116 :
117 :
118 :
119 0 : ModuleController::~ModuleController (void) throw()
120 : {
121 0 : }
122 :
123 :
124 :
125 :
126 0 : void SAL_CALL ModuleController::disposing (void)
127 : {
128 : // Break the cyclic reference back to DrawController object
129 0 : mpLoadedFactories.reset();
130 0 : mpResourceToFactoryMap.reset();
131 0 : mxController.clear();
132 0 : }
133 :
134 :
135 :
136 :
137 0 : void ModuleController::LoadFactories (const Reference<XComponentContext>& rxContext)
138 : {
139 : try
140 : {
141 : ConfigurationAccess aConfiguration (
142 : rxContext,
143 : "/org.openoffice.Office.Impress/",
144 0 : ConfigurationAccess::READ_ONLY);
145 : Reference<container::XNameAccess> xFactories (
146 : aConfiguration.GetConfigurationNode("MultiPaneGUI/Framework/ResourceFactories"),
147 0 : UNO_QUERY);
148 0 : ::std::vector<OUString> aProperties (snFactoryPropertyCount);
149 0 : aProperties[0] = "ServiceName";
150 0 : aProperties[1] = "ResourceList";
151 : ConfigurationAccess::ForAll(
152 : xFactories,
153 : aProperties,
154 0 : ::boost::bind(&ModuleController::ProcessFactory, this, _2));
155 : }
156 0 : catch (Exception&)
157 : {
158 : DBG_UNHANDLED_EXCEPTION();
159 : }
160 0 : }
161 :
162 :
163 :
164 :
165 0 : void ModuleController::ProcessFactory (const ::std::vector<Any>& rValues)
166 : {
167 : OSL_ASSERT(rValues.size() == snFactoryPropertyCount);
168 :
169 : // Get the service name of the factory.
170 0 : OUString sServiceName;
171 0 : rValues[0] >>= sServiceName;
172 :
173 : // Get all resource URLs that are created by the factory.
174 0 : Reference<container::XNameAccess> xResources (rValues[1], UNO_QUERY);
175 0 : ::std::vector<OUString> aURLs;
176 : tools::ConfigurationAccess::FillList(
177 : xResources,
178 : "URL",
179 0 : aURLs);
180 :
181 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ModuleController::adding factory " <<
182 : OUStringToOString(sServiceName, RTL_TEXTENCODING_UTF8).getStr());
183 :
184 : // Add the resource URLs to the map.
185 0 : ::std::vector<OUString>::const_iterator iResource;
186 0 : for (iResource=aURLs.begin(); iResource!=aURLs.end(); ++iResource)
187 : {
188 0 : (*mpResourceToFactoryMap)[*iResource] = sServiceName;
189 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": " <<
190 : OUStringToOString(*iResource, RTL_TEXTENCODING_UTF8).getStr());
191 0 : }
192 0 : }
193 :
194 :
195 :
196 :
197 0 : void ModuleController::InstantiateStartupServices (void)
198 : {
199 : try
200 : {
201 : tools::ConfigurationAccess aConfiguration (
202 : "/org.openoffice.Office.Impress/",
203 0 : tools::ConfigurationAccess::READ_ONLY);
204 : Reference<container::XNameAccess> xFactories (
205 : aConfiguration.GetConfigurationNode("MultiPaneGUI/Framework/StartupServices"),
206 0 : UNO_QUERY);
207 0 : ::std::vector<OUString> aProperties (snStartupPropertyCount);
208 0 : aProperties[0] = "ServiceName";
209 : tools::ConfigurationAccess::ForAll(
210 : xFactories,
211 : aProperties,
212 0 : ::boost::bind(&ModuleController::ProcessStartupService, this, _2));
213 : }
214 0 : catch (Exception&)
215 : {
216 : OSL_TRACE("ERROR in ModuleController::InstantiateStartupServices");
217 : }
218 0 : }
219 :
220 :
221 :
222 :
223 0 : void ModuleController::ProcessStartupService (const ::std::vector<Any>& rValues)
224 : {
225 : OSL_ASSERT(rValues.size() == snStartupPropertyCount);
226 :
227 : try
228 : {
229 : // Get the service name of the startup service.
230 0 : OUString sServiceName;
231 0 : rValues[0] >>= sServiceName;
232 :
233 : // Instantiate service.
234 : Reference<uno::XComponentContext> xContext =
235 0 : ::comphelper::getProcessComponentContext();
236 :
237 : // Create the startup service.
238 0 : Sequence<Any> aArguments(1);
239 0 : aArguments[0] <<= mxController;
240 : // Note that when the new object will be destroyed at the end of
241 : // this scope when it does not register itself anywhere.
242 : // Typically it will add itself as ConfigurationChangeListener
243 : // at the configuration controller.
244 0 : xContext->getServiceManager()->createInstanceWithArgumentsAndContext(sServiceName, aArguments, xContext);
245 :
246 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ModuleController::created startup service " <<
247 0 : OUStringToOString(sServiceName, RTL_TEXTENCODING_UTF8).getStr());
248 : }
249 0 : catch (Exception&)
250 : {
251 : OSL_TRACE("ERROR in ModuleController::ProcessStartupServices");
252 : }
253 0 : }
254 :
255 :
256 :
257 :
258 : //----- XModuleController -----------------------------------------------------
259 :
260 0 : void SAL_CALL ModuleController::requestResource (const OUString& rsResourceURL)
261 : throw (RuntimeException, std::exception)
262 : {
263 0 : ResourceToFactoryMap::const_iterator iFactory (mpResourceToFactoryMap->find(rsResourceURL));
264 0 : if (iFactory != mpResourceToFactoryMap->end())
265 : {
266 : // Check that the factory has already been loaded and not been
267 : // destroyed in the meantime.
268 0 : Reference<XInterface> xFactory;
269 : LoadedFactoryContainer::const_iterator iLoadedFactory (
270 0 : mpLoadedFactories->find(iFactory->second));
271 0 : if (iLoadedFactory != mpLoadedFactories->end())
272 0 : xFactory = Reference<XInterface>(iLoadedFactory->second, UNO_QUERY);
273 0 : if ( ! xFactory.is())
274 : {
275 : // Create a new instance of the factory.
276 : Reference<uno::XComponentContext> xContext =
277 0 : ::comphelper::getProcessComponentContext();
278 :
279 : // Create the factory service.
280 0 : Sequence<Any> aArguments(1);
281 0 : aArguments[0] <<= mxController;
282 : OSL_TRACE("creating resource %s",
283 : OUStringToOString(iFactory->second, RTL_TEXTENCODING_ASCII_US).getStr());
284 : try
285 : {
286 0 : xFactory = xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
287 0 : iFactory->second,
288 : aArguments,
289 0 : xContext);
290 : }
291 0 : catch (const Exception&)
292 : {
293 : OSL_TRACE("caught exception while creating factory.");
294 : }
295 :
296 : // Remember that this factory has been instanced.
297 0 : (*mpLoadedFactories)[iFactory->second] = xFactory;
298 0 : }
299 : }
300 0 : }
301 :
302 :
303 :
304 :
305 : //----- XInitialization -------------------------------------------------------
306 :
307 0 : void SAL_CALL ModuleController::initialize (const Sequence<Any>& aArguments)
308 : throw (Exception, RuntimeException, std::exception)
309 : {
310 0 : if (aArguments.getLength() > 0)
311 : {
312 : try
313 : {
314 : // Get the XController from the first argument.
315 0 : mxController = Reference<frame::XController>(aArguments[0], UNO_QUERY_THROW);
316 :
317 0 : InstantiateStartupServices();
318 : }
319 0 : catch (RuntimeException&)
320 : {}
321 : }
322 0 : }
323 :
324 :
325 0 : } } // end of namespace sd::framework
326 :
327 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|