15b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
35b190011SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
45b190011SAndrew Rist * or more contributor license agreements. See the NOTICE file
55b190011SAndrew Rist * distributed with this work for additional information
65b190011SAndrew Rist * regarding copyright ownership. The ASF licenses this file
75b190011SAndrew Rist * to you under the Apache License, Version 2.0 (the
85b190011SAndrew Rist * "License"); you may not use this file except in compliance
95b190011SAndrew Rist * with the License. You may obtain a copy of the License at
105b190011SAndrew Rist *
115b190011SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
125b190011SAndrew Rist *
135b190011SAndrew Rist * Unless required by applicable law or agreed to in writing,
145b190011SAndrew Rist * software distributed under the License is distributed on an
155b190011SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165b190011SAndrew Rist * KIND, either express or implied. See the License for the
175b190011SAndrew Rist * specific language governing permissions and limitations
185b190011SAndrew Rist * under the License.
195b190011SAndrew Rist *
205b190011SAndrew Rist *************************************************************/
215b190011SAndrew Rist
225b190011SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "precompiled_sd.hxx"
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include "framework/ModuleController.hxx"
27cdf0e10cSrcweir
28cdf0e10cSrcweir #include "tools/ConfigurationAccess.hxx"
29cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
30cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
31cdf0e10cSrcweir #include <boost/bind.hpp>
32cdf0e10cSrcweir #include <hash_map>
33cdf0e10cSrcweir
34cdf0e10cSrcweir #include <tools/diagnose_ex.h>
35cdf0e10cSrcweir
36cdf0e10cSrcweir using namespace ::com::sun::star;
37cdf0e10cSrcweir using namespace ::com::sun::star::uno;
38cdf0e10cSrcweir using namespace ::com::sun::star::drawing::framework;
39cdf0e10cSrcweir using ::rtl::OUString;
40cdf0e10cSrcweir using ::sd::tools::ConfigurationAccess;
41cdf0e10cSrcweir
42cdf0e10cSrcweir #undef VERBOSE
43cdf0e10cSrcweir //#define VERBOSE 2
44cdf0e10cSrcweir
45cdf0e10cSrcweir namespace sd { namespace framework {
46cdf0e10cSrcweir
47cdf0e10cSrcweir static const sal_uInt32 snFactoryPropertyCount (2);
48cdf0e10cSrcweir static const sal_uInt32 snStartupPropertyCount (1);
49cdf0e10cSrcweir
50cdf0e10cSrcweir
51cdf0e10cSrcweir
52cdf0e10cSrcweir
53cdf0e10cSrcweir class ModuleController::ResourceToFactoryMap
54cdf0e10cSrcweir : public ::std::hash_map<
55cdf0e10cSrcweir rtl::OUString,
56cdf0e10cSrcweir rtl::OUString,
57cdf0e10cSrcweir ::comphelper::UStringHash,
58cdf0e10cSrcweir ::comphelper::UStringEqual>
59cdf0e10cSrcweir {
60cdf0e10cSrcweir public:
ResourceToFactoryMap(void)61cdf0e10cSrcweir ResourceToFactoryMap (void) {}
62cdf0e10cSrcweir };
63cdf0e10cSrcweir
64cdf0e10cSrcweir
65cdf0e10cSrcweir class ModuleController::LoadedFactoryContainer
66cdf0e10cSrcweir : public ::std::hash_map<
67cdf0e10cSrcweir rtl::OUString,
68cdf0e10cSrcweir WeakReference<XInterface>,
69cdf0e10cSrcweir ::comphelper::UStringHash,
70cdf0e10cSrcweir ::comphelper::UStringEqual>
71cdf0e10cSrcweir {
72cdf0e10cSrcweir public:
LoadedFactoryContainer(void)73cdf0e10cSrcweir LoadedFactoryContainer (void) {}
74cdf0e10cSrcweir };
75cdf0e10cSrcweir
76cdf0e10cSrcweir
77cdf0e10cSrcweir
78cdf0e10cSrcweir
79cdf0e10cSrcweir
ModuleController_createInstance(const Reference<XComponentContext> & rxContext)80cdf0e10cSrcweir Reference<XInterface> SAL_CALL ModuleController_createInstance (
81cdf0e10cSrcweir const Reference<XComponentContext>& rxContext)
82cdf0e10cSrcweir {
83cdf0e10cSrcweir return Reference<XInterface>(ModuleController::CreateInstance(rxContext), UNO_QUERY);
84cdf0e10cSrcweir }
85cdf0e10cSrcweir
86cdf0e10cSrcweir
87cdf0e10cSrcweir
88cdf0e10cSrcweir
ModuleController_getImplementationName(void)89cdf0e10cSrcweir ::rtl::OUString ModuleController_getImplementationName (void) throw(RuntimeException)
90cdf0e10cSrcweir {
91cdf0e10cSrcweir return ::rtl::OUString(
92cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Draw.framework.module.ModuleController"));
93cdf0e10cSrcweir }
94cdf0e10cSrcweir
95cdf0e10cSrcweir
96cdf0e10cSrcweir
97cdf0e10cSrcweir
ModuleController_getSupportedServiceNames(void)98cdf0e10cSrcweir Sequence<rtl::OUString> SAL_CALL ModuleController_getSupportedServiceNames (void)
99cdf0e10cSrcweir throw (RuntimeException)
100cdf0e10cSrcweir {
101cdf0e10cSrcweir static const ::rtl::OUString sServiceName(
102cdf0e10cSrcweir ::rtl::OUString::createFromAscii("com.sun.star.drawing.framework.ModuleController"));
103cdf0e10cSrcweir return Sequence<rtl::OUString>(&sServiceName, 1);
104cdf0e10cSrcweir }
105cdf0e10cSrcweir
106cdf0e10cSrcweir
107cdf0e10cSrcweir
108cdf0e10cSrcweir
109cdf0e10cSrcweir //===== ModuleController ======================================================
110cdf0e10cSrcweir
CreateInstance(const Reference<XComponentContext> & rxContext)111cdf0e10cSrcweir Reference<XModuleController> ModuleController::CreateInstance (
112cdf0e10cSrcweir const Reference<XComponentContext>& rxContext)
113cdf0e10cSrcweir {
114cdf0e10cSrcweir return new ModuleController(rxContext);
115cdf0e10cSrcweir }
116cdf0e10cSrcweir
117cdf0e10cSrcweir
118cdf0e10cSrcweir
119cdf0e10cSrcweir
ModuleController(const Reference<XComponentContext> & rxContext)120cdf0e10cSrcweir ModuleController::ModuleController (const Reference<XComponentContext>& rxContext) throw()
121cdf0e10cSrcweir : ModuleControllerInterfaceBase(MutexOwner::maMutex),
122cdf0e10cSrcweir mxController(),
123cdf0e10cSrcweir mpResourceToFactoryMap(new ResourceToFactoryMap()),
124cdf0e10cSrcweir mpLoadedFactories(new LoadedFactoryContainer())
125cdf0e10cSrcweir {
126cdf0e10cSrcweir (void)rxContext;
127cdf0e10cSrcweir LoadFactories(rxContext);
128cdf0e10cSrcweir }
129cdf0e10cSrcweir
130cdf0e10cSrcweir
131cdf0e10cSrcweir
132cdf0e10cSrcweir
~ModuleController(void)133cdf0e10cSrcweir ModuleController::~ModuleController (void) throw()
134cdf0e10cSrcweir {
135cdf0e10cSrcweir }
136cdf0e10cSrcweir
137cdf0e10cSrcweir
138cdf0e10cSrcweir
139cdf0e10cSrcweir
disposing(void)140cdf0e10cSrcweir void SAL_CALL ModuleController::disposing (void)
141cdf0e10cSrcweir {
142cdf0e10cSrcweir // Break the cyclic reference back to DrawController object
143cdf0e10cSrcweir mpLoadedFactories.reset();
144cdf0e10cSrcweir mpResourceToFactoryMap.reset();
145cdf0e10cSrcweir mxController.clear();
146cdf0e10cSrcweir }
147cdf0e10cSrcweir
148cdf0e10cSrcweir
149cdf0e10cSrcweir
150cdf0e10cSrcweir
LoadFactories(const Reference<XComponentContext> & rxContext)151cdf0e10cSrcweir void ModuleController::LoadFactories (const Reference<XComponentContext>& rxContext)
152cdf0e10cSrcweir {
153cdf0e10cSrcweir try
154cdf0e10cSrcweir {
155cdf0e10cSrcweir ConfigurationAccess aConfiguration (
156cdf0e10cSrcweir rxContext,
157cdf0e10cSrcweir OUString::createFromAscii("/org.openoffice.Office.Impress/"),
158cdf0e10cSrcweir ConfigurationAccess::READ_ONLY);
159cdf0e10cSrcweir Reference<container::XNameAccess> xFactories (
160cdf0e10cSrcweir aConfiguration.GetConfigurationNode(
161cdf0e10cSrcweir OUString::createFromAscii("MultiPaneGUI/Framework/ResourceFactories")),
162cdf0e10cSrcweir UNO_QUERY);
163cdf0e10cSrcweir ::std::vector<rtl::OUString> aProperties (snFactoryPropertyCount);
164cdf0e10cSrcweir aProperties[0] = OUString::createFromAscii("ServiceName");
165cdf0e10cSrcweir aProperties[1] = OUString::createFromAscii("ResourceList");
166cdf0e10cSrcweir ConfigurationAccess::ForAll(
167cdf0e10cSrcweir xFactories,
168cdf0e10cSrcweir aProperties,
169cdf0e10cSrcweir ::boost::bind(&ModuleController::ProcessFactory, this, _2));
170cdf0e10cSrcweir }
171cdf0e10cSrcweir catch (Exception&)
172cdf0e10cSrcweir {
173cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
174cdf0e10cSrcweir }
175cdf0e10cSrcweir }
176cdf0e10cSrcweir
177cdf0e10cSrcweir
178cdf0e10cSrcweir
179cdf0e10cSrcweir
ProcessFactory(const::std::vector<Any> & rValues)180cdf0e10cSrcweir void ModuleController::ProcessFactory (const ::std::vector<Any>& rValues)
181cdf0e10cSrcweir {
182cdf0e10cSrcweir OSL_ASSERT(rValues.size() == snFactoryPropertyCount);
183cdf0e10cSrcweir
184cdf0e10cSrcweir // Get the service name of the factory.
185cdf0e10cSrcweir rtl::OUString sServiceName;
186cdf0e10cSrcweir rValues[0] >>= sServiceName;
187cdf0e10cSrcweir
188cdf0e10cSrcweir // Get all resource URLs that are created by the factory.
189cdf0e10cSrcweir Reference<container::XNameAccess> xResources (rValues[1], UNO_QUERY);
190cdf0e10cSrcweir ::std::vector<rtl::OUString> aURLs;
191cdf0e10cSrcweir tools::ConfigurationAccess::FillList(
192cdf0e10cSrcweir xResources,
193cdf0e10cSrcweir OUString::createFromAscii("URL"),
194cdf0e10cSrcweir aURLs);
195cdf0e10cSrcweir
196cdf0e10cSrcweir #if defined VERBOSE && VERBOSE>0
197cdf0e10cSrcweir OSL_TRACE("ModuleController::adding factory %s",
198cdf0e10cSrcweir OUStringToOString(sServiceName, RTL_TEXTENCODING_UTF8).getStr());
199cdf0e10cSrcweir #endif
200cdf0e10cSrcweir
201cdf0e10cSrcweir // Add the resource URLs to the map.
202cdf0e10cSrcweir ::std::vector<rtl::OUString>::const_iterator iResource;
203cdf0e10cSrcweir for (iResource=aURLs.begin(); iResource!=aURLs.end(); ++iResource)
204cdf0e10cSrcweir {
205cdf0e10cSrcweir (*mpResourceToFactoryMap)[*iResource] = sServiceName;
206cdf0e10cSrcweir #if defined VERBOSE && VERBOSE>1
207cdf0e10cSrcweir OSL_TRACE(" %s",
208cdf0e10cSrcweir OUStringToOString(*iResource, RTL_TEXTENCODING_UTF8).getStr());
209cdf0e10cSrcweir #endif
210cdf0e10cSrcweir }
211cdf0e10cSrcweir }
212cdf0e10cSrcweir
213cdf0e10cSrcweir
214cdf0e10cSrcweir
215cdf0e10cSrcweir
InstantiateStartupServices(void)216cdf0e10cSrcweir void ModuleController::InstantiateStartupServices (void)
217cdf0e10cSrcweir {
218cdf0e10cSrcweir try
219cdf0e10cSrcweir {
220cdf0e10cSrcweir tools::ConfigurationAccess aConfiguration (
221cdf0e10cSrcweir OUString::createFromAscii("/org.openoffice.Office.Impress/"),
222cdf0e10cSrcweir tools::ConfigurationAccess::READ_ONLY);
223cdf0e10cSrcweir Reference<container::XNameAccess> xFactories (
224cdf0e10cSrcweir aConfiguration.GetConfigurationNode(
225cdf0e10cSrcweir OUString::createFromAscii("MultiPaneGUI/Framework/StartupServices")),
226cdf0e10cSrcweir UNO_QUERY);
227cdf0e10cSrcweir ::std::vector<rtl::OUString> aProperties (snStartupPropertyCount);
228cdf0e10cSrcweir aProperties[0] = OUString::createFromAscii("ServiceName");
229cdf0e10cSrcweir tools::ConfigurationAccess::ForAll(
230cdf0e10cSrcweir xFactories,
231cdf0e10cSrcweir aProperties,
232cdf0e10cSrcweir ::boost::bind(&ModuleController::ProcessStartupService, this, _2));
233cdf0e10cSrcweir }
234cdf0e10cSrcweir catch (Exception&)
235cdf0e10cSrcweir {
236cdf0e10cSrcweir OSL_TRACE("ERROR in ModuleController::InstantiateStartupServices");
237cdf0e10cSrcweir }
238cdf0e10cSrcweir }
239cdf0e10cSrcweir
240cdf0e10cSrcweir
241cdf0e10cSrcweir
242cdf0e10cSrcweir
ProcessStartupService(const::std::vector<Any> & rValues)243cdf0e10cSrcweir void ModuleController::ProcessStartupService (const ::std::vector<Any>& rValues)
244cdf0e10cSrcweir {
245cdf0e10cSrcweir OSL_ASSERT(rValues.size() == snStartupPropertyCount);
246cdf0e10cSrcweir
247cdf0e10cSrcweir try
248cdf0e10cSrcweir {
249cdf0e10cSrcweir // Get the service name of the startup service.
250cdf0e10cSrcweir rtl::OUString sServiceName;
251cdf0e10cSrcweir rValues[0] >>= sServiceName;
252cdf0e10cSrcweir
253cdf0e10cSrcweir // Instantiate service.
254cdf0e10cSrcweir Reference<lang::XMultiServiceFactory> xGlobalFactory (
255cdf0e10cSrcweir ::comphelper::getProcessServiceFactory(), UNO_QUERY);
256cdf0e10cSrcweir if (xGlobalFactory.is())
257cdf0e10cSrcweir {
258cdf0e10cSrcweir // Create the startup service.
259cdf0e10cSrcweir Sequence<Any> aArguments(1);
260cdf0e10cSrcweir aArguments[0] <<= mxController;
261cdf0e10cSrcweir // Note that when the new object will be destroyed at the end of
262cdf0e10cSrcweir // this scope when it does not register itself anywhere.
263cdf0e10cSrcweir // Typically it will add itself as ConfigurationChangeListener
264cdf0e10cSrcweir // at the configuration controller.
265cdf0e10cSrcweir xGlobalFactory->createInstanceWithArguments(sServiceName, aArguments);
266cdf0e10cSrcweir
267cdf0e10cSrcweir #if defined VERBOSE && VERBOSE>0
268cdf0e10cSrcweir OSL_TRACE("ModuleController::created startup service %s",
269cdf0e10cSrcweir OUStringToOString(sServiceName, RTL_TEXTENCODING_UTF8).getStr());
270cdf0e10cSrcweir #endif
271cdf0e10cSrcweir }
272cdf0e10cSrcweir }
273cdf0e10cSrcweir catch (Exception&)
274cdf0e10cSrcweir {
275cdf0e10cSrcweir OSL_TRACE("ERROR in ModuleController::ProcessStartupServices");
276cdf0e10cSrcweir }
277cdf0e10cSrcweir }
278cdf0e10cSrcweir
279cdf0e10cSrcweir
280cdf0e10cSrcweir
281cdf0e10cSrcweir
282cdf0e10cSrcweir //----- XModuleController -----------------------------------------------------
283cdf0e10cSrcweir
requestResource(const OUString & rsResourceURL)284cdf0e10cSrcweir void SAL_CALL ModuleController::requestResource (const OUString& rsResourceURL)
285cdf0e10cSrcweir throw (RuntimeException)
286cdf0e10cSrcweir {
287cdf0e10cSrcweir ResourceToFactoryMap::const_iterator iFactory (mpResourceToFactoryMap->find(rsResourceURL));
288cdf0e10cSrcweir if (iFactory != mpResourceToFactoryMap->end())
289cdf0e10cSrcweir {
290cdf0e10cSrcweir // Check that the factory has already been loaded and not been
291cdf0e10cSrcweir // destroyed in the meantime.
292cdf0e10cSrcweir Reference<XInterface> xFactory;
293cdf0e10cSrcweir LoadedFactoryContainer::const_iterator iLoadedFactory (
294cdf0e10cSrcweir mpLoadedFactories->find(iFactory->second));
295cdf0e10cSrcweir if (iLoadedFactory != mpLoadedFactories->end())
296cdf0e10cSrcweir xFactory = Reference<XInterface>(iLoadedFactory->second, UNO_QUERY);
297cdf0e10cSrcweir if ( ! xFactory.is())
298cdf0e10cSrcweir {
299cdf0e10cSrcweir // Create a new instance of the factory.
300cdf0e10cSrcweir Reference<lang::XMultiServiceFactory> xGlobalFactory (
301cdf0e10cSrcweir ::comphelper::getProcessServiceFactory(), UNO_QUERY);
302cdf0e10cSrcweir if (xGlobalFactory.is())
303cdf0e10cSrcweir {
304cdf0e10cSrcweir // Create the factory service.
305cdf0e10cSrcweir Sequence<Any> aArguments(1);
306cdf0e10cSrcweir aArguments[0] <<= mxController;
307*02c50d82SAndre Fischer OSL_TRACE("creating resource %s",
308*02c50d82SAndre Fischer OUStringToOString(iFactory->second, RTL_TEXTENCODING_ASCII_US).getStr());
309*02c50d82SAndre Fischer try
310*02c50d82SAndre Fischer {
311*02c50d82SAndre Fischer xFactory = xGlobalFactory->createInstanceWithArguments(
312*02c50d82SAndre Fischer iFactory->second,
313*02c50d82SAndre Fischer aArguments);
314*02c50d82SAndre Fischer }
315*02c50d82SAndre Fischer catch(Exception&e)
316*02c50d82SAndre Fischer {
317*02c50d82SAndre Fischer OSL_TRACE("caught exception while creating factory.");
318*02c50d82SAndre Fischer }
319cdf0e10cSrcweir
320cdf0e10cSrcweir // Remember that this factory has been instanced.
321cdf0e10cSrcweir (*mpLoadedFactories)[iFactory->second] = xFactory;
322cdf0e10cSrcweir }
323cdf0e10cSrcweir }
324cdf0e10cSrcweir }
325cdf0e10cSrcweir }
326cdf0e10cSrcweir
327cdf0e10cSrcweir
328cdf0e10cSrcweir
329cdf0e10cSrcweir
330cdf0e10cSrcweir //----- XInitialization -------------------------------------------------------
331cdf0e10cSrcweir
initialize(const Sequence<Any> & aArguments)332cdf0e10cSrcweir void SAL_CALL ModuleController::initialize (const Sequence<Any>& aArguments)
333cdf0e10cSrcweir throw (Exception, RuntimeException)
334cdf0e10cSrcweir {
335cdf0e10cSrcweir if (aArguments.getLength() > 0)
336cdf0e10cSrcweir {
337cdf0e10cSrcweir try
338cdf0e10cSrcweir {
339cdf0e10cSrcweir // Get the XController from the first argument.
340cdf0e10cSrcweir mxController = Reference<frame::XController>(aArguments[0], UNO_QUERY_THROW);
341cdf0e10cSrcweir
342cdf0e10cSrcweir InstantiateStartupServices();
343cdf0e10cSrcweir }
344cdf0e10cSrcweir catch (RuntimeException&)
345cdf0e10cSrcweir {}
346cdf0e10cSrcweir }
347cdf0e10cSrcweir }
348cdf0e10cSrcweir
349cdf0e10cSrcweir
350cdf0e10cSrcweir } } // end of namespace sd::framework
351