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 : #include <osl/mutex.hxx>
21 : #include <comphelper/processfactory.hxx>
22 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 :
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <com/sun/star/uno/DeploymentException.hpp>
26 :
27 : using namespace ::com::sun::star;
28 : using namespace com::sun::star::uno;
29 : using namespace com::sun::star::lang;
30 : using namespace osl;
31 :
32 : namespace comphelper
33 : {
34 :
35 : /*
36 : This function preserves only that the xProcessFactory variable will not be create when
37 : the library is loaded.
38 : */
39 222710471 : Reference< XMultiServiceFactory > localProcessFactory( const Reference< XMultiServiceFactory >& xSMgr, bool bSet )
40 : {
41 222710471 : Guard< Mutex > aGuard( Mutex::getGlobalMutex() );
42 :
43 222710471 : static Reference< XMultiServiceFactory > xProcessFactory;
44 222710471 : if ( bSet )
45 : {
46 517 : xProcessFactory = xSMgr;
47 : }
48 :
49 222710471 : return xProcessFactory;
50 : }
51 :
52 :
53 517 : void setProcessServiceFactory(const Reference< XMultiServiceFactory >& xSMgr)
54 : {
55 517 : localProcessFactory( xSMgr, true );
56 517 : }
57 :
58 222709954 : Reference< XMultiServiceFactory > getProcessServiceFactory()
59 : {
60 222709954 : Reference< XMultiServiceFactory> xReturn;
61 222709954 : xReturn = localProcessFactory( xReturn, false );
62 222709954 : if ( !xReturn.is() )
63 : {
64 0 : throw DeploymentException( "null process service factory" );
65 : }
66 222709954 : return xReturn;
67 : }
68 :
69 222936567 : Reference< XComponentContext > getComponentContext(
70 : Reference< XMultiServiceFactory > const & factory)
71 : {
72 222936567 : Reference< XComponentContext > xRet;
73 445873134 : uno::Reference<beans::XPropertySet> const xProps( factory, uno::UNO_QUERY );
74 222936567 : if (xProps.is()) {
75 : try {
76 222936567 : xRet.set( xProps->getPropertyValue("DefaultContext"),
77 222936567 : uno::UNO_QUERY );
78 : }
79 0 : catch (beans::UnknownPropertyException & e) {
80 : throw DeploymentException(
81 0 : "unknown service factory DefaultContext property: " + e.Message,
82 0 : Reference<XInterface>(factory, UNO_QUERY) );
83 : }
84 : }
85 222936567 : if ( !xRet.is() )
86 : {
87 : throw DeploymentException(
88 : "no service factory DefaultContext",
89 0 : Reference<XInterface>(factory, UNO_QUERY) );
90 : }
91 445873134 : return xRet;
92 : }
93 :
94 222666465 : Reference< XComponentContext > getProcessComponentContext()
95 : {
96 222666465 : return getComponentContext( getProcessServiceFactory() );
97 : }
98 :
99 : } // namespace comphelper
100 :
101 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|