xref: /AOO41X/main/comphelper/source/misc/componentcontext.cxx (revision dde7d3faf6dcd9cbeb7b48ba6d0cea5ffcc883d0)
1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /** === begin UNO includes === **/
29cdf0e10cSrcweir #include <com/sun/star/lang/NullPointerException.hpp>
30cdf0e10cSrcweir #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
31cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
32cdf0e10cSrcweir /** === end UNO includes === **/
33cdf0e10cSrcweir 
34cdf0e10cSrcweir //........................................................................
35cdf0e10cSrcweir namespace comphelper
36cdf0e10cSrcweir {
37cdf0e10cSrcweir //........................................................................
38cdf0e10cSrcweir 
39cdf0e10cSrcweir     /** === begin UNO using === **/
40cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
41cdf0e10cSrcweir     using ::com::sun::star::uno::XComponentContext;
42cdf0e10cSrcweir     using ::com::sun::star::lang::NullPointerException;
43cdf0e10cSrcweir     using ::com::sun::star::lang::ServiceNotRegisteredException;
44cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
45cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
46cdf0e10cSrcweir     using ::com::sun::star::uno::XInterface;
47cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
48cdf0e10cSrcweir     using ::com::sun::star::lang::XMultiServiceFactory;
49cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertySet;
50cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
51cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
52cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
53cdf0e10cSrcweir     /** === end UNO using === **/
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     //====================================================================
56cdf0e10cSrcweir 	//= ComponentContext
57cdf0e10cSrcweir 	//====================================================================
58cdf0e10cSrcweir 	//--------------------------------------------------------------------
ComponentContext(const Reference<XComponentContext> & _rxContext)59cdf0e10cSrcweir     ComponentContext::ComponentContext( const Reference< XComponentContext >& _rxContext )
60cdf0e10cSrcweir         :m_xContext( _rxContext )
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         if ( m_xContext.is() )
63cdf0e10cSrcweir             m_xORB = m_xContext->getServiceManager();
64cdf0e10cSrcweir         if ( !m_xORB.is() )
65cdf0e10cSrcweir             throw NullPointerException();
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     //------------------------------------------------------------------------
ComponentContext(const Reference<XMultiServiceFactory> & _rxLegacyFactory)69cdf0e10cSrcweir     ComponentContext::ComponentContext( const Reference< XMultiServiceFactory >& _rxLegacyFactory )
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         if ( !_rxLegacyFactory.is() )
72cdf0e10cSrcweir             throw NullPointerException();
73cdf0e10cSrcweir 
74cdf0e10cSrcweir         try
75cdf0e10cSrcweir         {
76cdf0e10cSrcweir             Reference< XPropertySet > xFactoryProperties( _rxLegacyFactory, UNO_QUERY_THROW );
77cdf0e10cSrcweir             m_xContext = Reference< XComponentContext >(
78cdf0e10cSrcweir                 xFactoryProperties->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) ),
79cdf0e10cSrcweir                 UNO_QUERY );
80cdf0e10cSrcweir         }
81cdf0e10cSrcweir         catch( const RuntimeException& ) { throw; }
82cdf0e10cSrcweir         catch( const Exception& )
83cdf0e10cSrcweir         {
84cdf0e10cSrcweir             throw RuntimeException();
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         if ( m_xContext.is() )
88cdf0e10cSrcweir             m_xORB = m_xContext->getServiceManager();
89cdf0e10cSrcweir         if ( !m_xORB.is() )
90cdf0e10cSrcweir             throw NullPointerException();
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     //------------------------------------------------------------------------
getContextValueByName(const::rtl::OUString & _rName) const94cdf0e10cSrcweir     Any ComponentContext::getContextValueByName( const ::rtl::OUString& _rName ) const
95cdf0e10cSrcweir     {
96cdf0e10cSrcweir         Any aReturn;
97cdf0e10cSrcweir         try
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             aReturn = m_xContext->getValueByName( _rName );
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir         catch( const Exception& )
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir             OSL_ENSURE( sal_False, "ComponentContext::getContextValueByName: caught an exception!" );
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir         return aReturn;
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     //------------------------------------------------------------------------
createComponent(const::rtl::OUString & _rServiceName) const109cdf0e10cSrcweir     Reference< XInterface > ComponentContext::createComponent( const ::rtl::OUString& _rServiceName ) const
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         Reference< XInterface > xComponent(
112cdf0e10cSrcweir             m_xORB->createInstanceWithContext( _rServiceName, m_xContext )
113cdf0e10cSrcweir         );
114cdf0e10cSrcweir         if ( !xComponent.is() )
115cdf0e10cSrcweir             throw ServiceNotRegisteredException( _rServiceName, NULL );
116cdf0e10cSrcweir         return xComponent;
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     //------------------------------------------------------------------------
createComponentWithArguments(const::rtl::OUString & _rServiceName,const Sequence<Any> & _rArguments) const120cdf0e10cSrcweir     Reference< XInterface > ComponentContext::createComponentWithArguments( const ::rtl::OUString& _rServiceName, const Sequence< Any >& _rArguments ) const
121cdf0e10cSrcweir     {
122cdf0e10cSrcweir         Reference< XInterface > xComponent(
123cdf0e10cSrcweir             m_xORB->createInstanceWithArgumentsAndContext( _rServiceName, _rArguments, m_xContext )
124cdf0e10cSrcweir         );
125cdf0e10cSrcweir         if ( !xComponent.is() )
126cdf0e10cSrcweir             throw ServiceNotRegisteredException( _rServiceName, NULL );
127cdf0e10cSrcweir         return xComponent;
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     //------------------------------------------------------------------------
getSingleton(const::rtl::OUString & _rInstanceName) const131cdf0e10cSrcweir     Reference< XInterface > ComponentContext::getSingleton( const ::rtl::OUString& _rInstanceName ) const
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         ::rtl::OUString sKey( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/" ) ) );
134cdf0e10cSrcweir         sKey += _rInstanceName;
135cdf0e10cSrcweir         return Reference< XInterface >( getContextValueByName( sKey ), UNO_QUERY );
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     //------------------------------------------------------------------------
getLegacyServiceFactory() const139cdf0e10cSrcweir     Reference< XMultiServiceFactory > ComponentContext::getLegacyServiceFactory() const
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         return Reference< XMultiServiceFactory >( m_xORB, UNO_QUERY_THROW );
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir //........................................................................
145cdf0e10cSrcweir } // namespace comphelper
146cdf0e10cSrcweir //........................................................................
147cdf0e10cSrcweir 
148