1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_comphelper.hxx"
26 #include <osl/mutex.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 
30 #include "com/sun/star/beans/XPropertySet.hpp"
31 
32 
33 using namespace ::com::sun::star;
34 using namespace com::sun::star::uno;
35 using namespace com::sun::star::lang;
36 using namespace osl;
37 
38 namespace comphelper
39 {
40 
41 /*
42 	This function preserves only that the xProcessFactory variable will not be create when
43 	the library is loaded.
44 */
localProcessFactory(const Reference<XMultiServiceFactory> & xSMgr,sal_Bool bSet)45 Reference< XMultiServiceFactory > localProcessFactory( const Reference< XMultiServiceFactory >& xSMgr, sal_Bool bSet )
46 {
47 	Guard< Mutex > aGuard( Mutex::getGlobalMutex() );
48 
49 	static Reference< XMultiServiceFactory > xProcessFactory;
50 	if ( bSet )
51 	{
52 		xProcessFactory = xSMgr;
53 	}
54 
55 	return xProcessFactory;
56 }
57 
58 
setProcessServiceFactory(const Reference<XMultiServiceFactory> & xSMgr)59 void setProcessServiceFactory(const Reference< XMultiServiceFactory >& xSMgr)
60 {
61 	localProcessFactory( xSMgr, sal_True );
62 }
63 
getProcessServiceFactory()64 Reference< XMultiServiceFactory > getProcessServiceFactory()
65 {
66 	Reference< XMultiServiceFactory> xReturn;
67 	xReturn = localProcessFactory( xReturn, sal_False );
68 	return xReturn;
69 }
70 
createProcessComponent(const::rtl::OUString & _rServiceSpecifier)71 Reference< XInterface > createProcessComponent( const ::rtl::OUString& _rServiceSpecifier ) SAL_THROW( ( RuntimeException ) )
72 {
73 	Reference< XInterface > xComponent;
74 
75 	Reference< XMultiServiceFactory > xFactory( getProcessServiceFactory() );
76 	if ( xFactory.is() )
77 		xComponent = xFactory->createInstance( _rServiceSpecifier );
78 
79 	return xComponent;
80 }
81 
createProcessComponentWithArguments(const::rtl::OUString & _rServiceSpecifier,const Sequence<Any> & _rArgs)82 Reference< XInterface > createProcessComponentWithArguments( const ::rtl::OUString& _rServiceSpecifier,
83 		const Sequence< Any >& _rArgs ) SAL_THROW( ( RuntimeException ) )
84 {
85 	Reference< XInterface > xComponent;
86 
87 	Reference< XMultiServiceFactory > xFactory( getProcessServiceFactory() );
88 	if ( xFactory.is() )
89 		xComponent = xFactory->createInstanceWithArguments( _rServiceSpecifier, _rArgs );
90 
91 	return xComponent;
92 }
93 
getProcessComponentContext()94 Reference< XComponentContext > getProcessComponentContext()
95 {
96     Reference< XComponentContext > xRet;
97     uno::Reference<beans::XPropertySet> const xProps(
98         comphelper::getProcessServiceFactory(), uno::UNO_QUERY );
99     if (xProps.is()) {
100         try {
101             xRet.set( xProps->getPropertyValue( rtl::OUString(
102                               RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) ),
103                       uno::UNO_QUERY );
104         }
105         catch (beans::UnknownPropertyException const&) {
106         }
107     }
108     return xRet;
109 }
110 
111 } // namespace comphelper
112 
113 extern "C" {
comphelper_getProcessComponentContext()114 uno::XComponentContext * comphelper_getProcessComponentContext()
115 {
116     uno::Reference<uno::XComponentContext> xRet;
117     xRet = ::comphelper::getProcessComponentContext();
118     if (xRet.is())
119         xRet->acquire();
120     return xRet.get();
121 }
122 } // extern "C"
123 
124