merge the formfield patch from ooo-build
[ooovba.git] / comphelper / source / misc / componentcontext.cxx
blob40313eff6fe56c292af28f94d6e800e12bb90e57
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: componentcontext.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/componentcontext.hxx>
35 /** === begin UNO includes === **/
36 #include <com/sun/star/lang/NullPointerException.hpp>
37 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 /** === end UNO includes === **/
41 //........................................................................
42 namespace comphelper
44 //........................................................................
46 /** === begin UNO using === **/
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::XComponentContext;
49 using ::com::sun::star::lang::NullPointerException;
50 using ::com::sun::star::lang::ServiceNotRegisteredException;
51 using ::com::sun::star::uno::Exception;
52 using ::com::sun::star::uno::Any;
53 using ::com::sun::star::uno::XInterface;
54 using ::com::sun::star::uno::UNO_QUERY_THROW;
55 using ::com::sun::star::lang::XMultiServiceFactory;
56 using ::com::sun::star::beans::XPropertySet;
57 using ::com::sun::star::uno::UNO_QUERY;
58 using ::com::sun::star::uno::RuntimeException;
59 using ::com::sun::star::uno::Sequence;
60 /** === end UNO using === **/
62 //====================================================================
63 //= ComponentContext
64 //====================================================================
65 //--------------------------------------------------------------------
66 ComponentContext::ComponentContext( const Reference< XComponentContext >& _rxContext )
67 :m_xContext( _rxContext )
69 if ( m_xContext.is() )
70 m_xORB = m_xContext->getServiceManager();
71 if ( !m_xORB.is() )
72 throw NullPointerException();
75 //------------------------------------------------------------------------
76 ComponentContext::ComponentContext( const Reference< XMultiServiceFactory >& _rxLegacyFactory )
78 if ( !_rxLegacyFactory.is() )
79 throw NullPointerException();
81 try
83 Reference< XPropertySet > xFactoryProperties( _rxLegacyFactory, UNO_QUERY_THROW );
84 m_xContext = Reference< XComponentContext >(
85 xFactoryProperties->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) ),
86 UNO_QUERY );
88 catch( const RuntimeException& ) { throw; }
89 catch( const Exception& )
91 throw RuntimeException();
94 if ( m_xContext.is() )
95 m_xORB = m_xContext->getServiceManager();
96 if ( !m_xORB.is() )
97 throw NullPointerException();
100 //------------------------------------------------------------------------
101 Any ComponentContext::getContextValueByName( const ::rtl::OUString& _rName ) const
103 Any aReturn;
106 aReturn = m_xContext->getValueByName( _rName );
108 catch( const Exception& )
110 OSL_ENSURE( sal_False, "ComponentContext::getContextValueByName: caught an exception!" );
112 return aReturn;
115 //------------------------------------------------------------------------
116 Reference< XInterface > ComponentContext::createComponent( const ::rtl::OUString& _rServiceName ) const
118 Reference< XInterface > xComponent(
119 m_xORB->createInstanceWithContext( _rServiceName, m_xContext )
121 if ( !xComponent.is() )
122 throw ServiceNotRegisteredException( _rServiceName, NULL );
123 return xComponent;
126 //------------------------------------------------------------------------
127 Reference< XInterface > ComponentContext::createComponentWithArguments( const ::rtl::OUString& _rServiceName, const Sequence< Any >& _rArguments ) const
129 Reference< XInterface > xComponent(
130 m_xORB->createInstanceWithArgumentsAndContext( _rServiceName, _rArguments, m_xContext )
132 if ( !xComponent.is() )
133 throw ServiceNotRegisteredException( _rServiceName, NULL );
134 return xComponent;
137 //------------------------------------------------------------------------
138 Reference< XInterface > ComponentContext::getSingleton( const ::rtl::OUString& _rInstanceName ) const
140 ::rtl::OUString sKey( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/" ) ) );
141 sKey += _rInstanceName;
142 return Reference< XInterface >( getContextValueByName( sKey ), UNO_QUERY );
145 //------------------------------------------------------------------------
146 Reference< XMultiServiceFactory > ComponentContext::getLegacyServiceFactory() const
148 return Reference< XMultiServiceFactory >( m_xORB, UNO_QUERY_THROW );
151 //........................................................................
152 } // namespace comphelper
153 //........................................................................