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