Branch data 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 <comphelper/componentcontext.hxx>
21 : :
22 : : #include <com/sun/star/lang/NullPointerException.hpp>
23 : : #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
24 : : #include <com/sun/star/beans/XPropertySet.hpp>
25 : :
26 : : //........................................................................
27 : : namespace comphelper
28 : : {
29 : : //........................................................................
30 : :
31 : : /** === begin UNO using === **/
32 : : using ::com::sun::star::uno::Reference;
33 : : using ::com::sun::star::uno::XComponentContext;
34 : : using ::com::sun::star::lang::NullPointerException;
35 : : using ::com::sun::star::lang::ServiceNotRegisteredException;
36 : : using ::com::sun::star::uno::Exception;
37 : : using ::com::sun::star::uno::Any;
38 : : using ::com::sun::star::uno::XInterface;
39 : : using ::com::sun::star::uno::UNO_QUERY_THROW;
40 : : using ::com::sun::star::lang::XMultiServiceFactory;
41 : : using ::com::sun::star::beans::XPropertySet;
42 : : using ::com::sun::star::uno::UNO_QUERY;
43 : : using ::com::sun::star::uno::RuntimeException;
44 : : using ::com::sun::star::uno::Sequence;
45 : : /** === end UNO using === **/
46 : :
47 : : //====================================================================
48 : : //= ComponentContext
49 : : //====================================================================
50 : : //--------------------------------------------------------------------
51 : 1426 : ComponentContext::ComponentContext( const Reference< XComponentContext >& _rxContext )
52 : 1426 : :m_xContext( _rxContext )
53 : : {
54 [ + - ]: 1426 : if ( m_xContext.is() )
55 [ + - ][ + - ]: 1426 : m_xORB = m_xContext->getServiceManager();
[ + - ]
56 [ - + ]: 1426 : if ( !m_xORB.is() )
57 [ # # ]: 0 : throw NullPointerException();
58 : 1426 : }
59 : :
60 : : //------------------------------------------------------------------------
61 : 127202 : ComponentContext::ComponentContext( const Reference< XMultiServiceFactory >& _rxLegacyFactory )
62 : : {
63 [ - + ]: 127202 : if ( !_rxLegacyFactory.is() )
64 [ # # ]: 0 : throw NullPointerException();
65 : :
66 : : try
67 : : {
68 [ + - ]: 127202 : Reference< XPropertySet > xFactoryProperties( _rxLegacyFactory, UNO_QUERY_THROW );
69 : : m_xContext = Reference< XComponentContext >(
70 [ + - ]: 127202 : xFactoryProperties->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) ),
71 [ + - ][ + - ]: 127202 : UNO_QUERY );
[ + - ][ + - ]
72 : : }
73 : 0 : catch( const RuntimeException& ) { throw; }
74 [ # # # ]: 0 : catch( const Exception& )
75 : : {
76 [ # # ]: 0 : throw RuntimeException();
77 : : }
78 : :
79 [ + - ]: 127202 : if ( m_xContext.is() )
80 [ + - ][ + - ]: 127202 : m_xORB = m_xContext->getServiceManager();
[ + - ]
81 [ - + ]: 127202 : if ( !m_xORB.is() )
82 [ # # ]: 0 : throw NullPointerException();
83 : 127202 : }
84 : :
85 : : //------------------------------------------------------------------------
86 : 6200 : Any ComponentContext::getContextValueByName( const ::rtl::OUString& _rName ) const
87 : : {
88 : 6200 : Any aReturn;
89 : : try
90 : : {
91 [ + - ][ + - ]: 6200 : aReturn = m_xContext->getValueByName( _rName );
92 : : }
93 [ # # ]: 0 : catch( const Exception& )
94 : : {
95 : : OSL_FAIL( "ComponentContext::getContextValueByName: caught an exception!" );
96 : : }
97 [ # # ]: 6200 : return aReturn;
98 : : }
99 : :
100 : : //------------------------------------------------------------------------
101 : 3282 : Reference< XInterface > ComponentContext::createComponent( const ::rtl::OUString& _rServiceName ) const
102 : : {
103 : : Reference< XInterface > xComponent(
104 : 3282 : m_xORB->createInstanceWithContext( _rServiceName, m_xContext )
105 : 3282 : );
106 [ - + ]: 3282 : if ( !xComponent.is() )
107 [ # # ][ # # ]: 0 : throw ServiceNotRegisteredException( _rServiceName, NULL );
108 : 3282 : return xComponent;
109 : : }
110 : :
111 : : //------------------------------------------------------------------------
112 : 390 : Reference< XInterface > ComponentContext::createComponentWithArguments( const ::rtl::OUString& _rServiceName, const Sequence< Any >& _rArguments ) const
113 : : {
114 : : Reference< XInterface > xComponent(
115 : 390 : m_xORB->createInstanceWithArgumentsAndContext( _rServiceName, _rArguments, m_xContext )
116 : 390 : );
117 [ - + ]: 390 : if ( !xComponent.is() )
118 [ # # ][ # # ]: 0 : throw ServiceNotRegisteredException( _rServiceName, NULL );
119 : 390 : return xComponent;
120 : : }
121 : :
122 : : //------------------------------------------------------------------------
123 : 6200 : Reference< XInterface > ComponentContext::getSingleton( const ::rtl::OUString& _rInstanceName ) const
124 : : {
125 [ + - ]: 6200 : ::rtl::OUString sKey( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/" ) ) );
126 : 6200 : sKey += _rInstanceName;
127 [ + - ][ + - ]: 6200 : return Reference< XInterface >( getContextValueByName( sKey ), UNO_QUERY );
128 : : }
129 : :
130 : : //------------------------------------------------------------------------
131 : 2666 : Reference< XMultiServiceFactory > ComponentContext::getLegacyServiceFactory() const
132 : : {
133 : 2666 : return Reference< XMultiServiceFactory >( m_xORB, UNO_QUERY_THROW );
134 : : }
135 : :
136 : : //........................................................................
137 : : } // namespace comphelper
138 : : //........................................................................
139 : :
140 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|