xref: /AOO41X/main/comphelper/source/processfactory/processfactory.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <osl/mutex.hxx>
31 #include <comphelper/processfactory.hxx>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 
34 #include "com/sun/star/beans/XPropertySet.hpp"
35 
36 
37 using namespace ::com::sun::star;
38 using namespace com::sun::star::uno;
39 using namespace com::sun::star::lang;
40 using namespace osl;
41 
42 namespace comphelper
43 {
44 
45 /*
46 	This function preserves only that the xProcessFactory variable will not be create when
47 	the library is loaded.
48 */
49 Reference< XMultiServiceFactory > localProcessFactory( const Reference< XMultiServiceFactory >& xSMgr, sal_Bool bSet )
50 {
51 	Guard< Mutex > aGuard( Mutex::getGlobalMutex() );
52 
53 	static Reference< XMultiServiceFactory > xProcessFactory;
54 	if ( bSet )
55 	{
56 		xProcessFactory = xSMgr;
57 	}
58 
59 	return xProcessFactory;
60 }
61 
62 
63 void setProcessServiceFactory(const Reference< XMultiServiceFactory >& xSMgr)
64 {
65 	localProcessFactory( xSMgr, sal_True );
66 }
67 
68 Reference< XMultiServiceFactory > getProcessServiceFactory()
69 {
70 	Reference< XMultiServiceFactory> xReturn;
71 	xReturn = localProcessFactory( xReturn, sal_False );
72 	return xReturn;
73 }
74 
75 Reference< XInterface > createProcessComponent( const ::rtl::OUString& _rServiceSpecifier ) SAL_THROW( ( RuntimeException ) )
76 {
77 	Reference< XInterface > xComponent;
78 
79 	Reference< XMultiServiceFactory > xFactory( getProcessServiceFactory() );
80 	if ( xFactory.is() )
81 		xComponent = xFactory->createInstance( _rServiceSpecifier );
82 
83 	return xComponent;
84 }
85 
86 Reference< XInterface > createProcessComponentWithArguments( const ::rtl::OUString& _rServiceSpecifier,
87 		const Sequence< Any >& _rArgs ) SAL_THROW( ( RuntimeException ) )
88 {
89 	Reference< XInterface > xComponent;
90 
91 	Reference< XMultiServiceFactory > xFactory( getProcessServiceFactory() );
92 	if ( xFactory.is() )
93 		xComponent = xFactory->createInstanceWithArguments( _rServiceSpecifier, _rArgs );
94 
95 	return xComponent;
96 }
97 
98 Reference< XComponentContext > getProcessComponentContext()
99 {
100     Reference< XComponentContext > xRet;
101     uno::Reference<beans::XPropertySet> const xProps(
102         comphelper::getProcessServiceFactory(), uno::UNO_QUERY );
103     if (xProps.is()) {
104         try {
105             xRet.set( xProps->getPropertyValue( rtl::OUString(
106                               RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) ),
107                       uno::UNO_QUERY );
108         }
109         catch (beans::UnknownPropertyException const&) {
110         }
111     }
112     return xRet;
113 }
114 
115 } // namespace comphelper
116 
117 extern "C" {
118 uno::XComponentContext * comphelper_getProcessComponentContext()
119 {
120     uno::Reference<uno::XComponentContext> xRet;
121     xRet = ::comphelper::getProcessComponentContext();
122     if (xRet.is())
123         xRet->acquire();
124     return xRet.get();
125 }
126 } // extern "C"
127 
128