1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/componentcontext.hxx>
32 /** === begin UNO includes === **/
33 #include <com/sun/star/lang/NullPointerException.hpp>
34 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 /** === end UNO includes === **/
38 //........................................................................
41 //........................................................................
43 /** === begin UNO using === **/
44 using ::com::sun::star::uno::Reference
;
45 using ::com::sun::star::uno::XComponentContext
;
46 using ::com::sun::star::lang::NullPointerException
;
47 using ::com::sun::star::lang::ServiceNotRegisteredException
;
48 using ::com::sun::star::uno::Exception
;
49 using ::com::sun::star::uno::Any
;
50 using ::com::sun::star::uno::XInterface
;
51 using ::com::sun::star::uno::UNO_QUERY_THROW
;
52 using ::com::sun::star::lang::XMultiServiceFactory
;
53 using ::com::sun::star::beans::XPropertySet
;
54 using ::com::sun::star::uno::UNO_QUERY
;
55 using ::com::sun::star::uno::RuntimeException
;
56 using ::com::sun::star::uno::Sequence
;
57 /** === end UNO using === **/
59 //====================================================================
61 //====================================================================
62 //--------------------------------------------------------------------
63 ComponentContext::ComponentContext( const Reference
< XComponentContext
>& _rxContext
)
64 :m_xContext( _rxContext
)
66 if ( m_xContext
.is() )
67 m_xORB
= m_xContext
->getServiceManager();
69 throw NullPointerException();
72 //------------------------------------------------------------------------
73 ComponentContext::ComponentContext( const Reference
< XMultiServiceFactory
>& _rxLegacyFactory
)
75 if ( !_rxLegacyFactory
.is() )
76 throw NullPointerException();
80 Reference
< XPropertySet
> xFactoryProperties( _rxLegacyFactory
, UNO_QUERY_THROW
);
81 m_xContext
= Reference
< XComponentContext
>(
82 xFactoryProperties
->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) ),
85 catch( const RuntimeException
& ) { throw; }
86 catch( const Exception
& )
88 throw RuntimeException();
91 if ( m_xContext
.is() )
92 m_xORB
= m_xContext
->getServiceManager();
94 throw NullPointerException();
97 //------------------------------------------------------------------------
98 Any
ComponentContext::getContextValueByName( const ::rtl::OUString
& _rName
) const
103 aReturn
= m_xContext
->getValueByName( _rName
);
105 catch( const Exception
& )
107 OSL_ENSURE( sal_False
, "ComponentContext::getContextValueByName: caught an exception!" );
112 //------------------------------------------------------------------------
113 Reference
< XInterface
> ComponentContext::createComponent( const ::rtl::OUString
& _rServiceName
) const
115 Reference
< XInterface
> xComponent(
116 m_xORB
->createInstanceWithContext( _rServiceName
, m_xContext
)
118 if ( !xComponent
.is() )
119 throw ServiceNotRegisteredException( _rServiceName
, NULL
);
123 //------------------------------------------------------------------------
124 Reference
< XInterface
> ComponentContext::createComponentWithArguments( const ::rtl::OUString
& _rServiceName
, const Sequence
< Any
>& _rArguments
) const
126 Reference
< XInterface
> xComponent(
127 m_xORB
->createInstanceWithArgumentsAndContext( _rServiceName
, _rArguments
, m_xContext
)
129 if ( !xComponent
.is() )
130 throw ServiceNotRegisteredException( _rServiceName
, NULL
);
134 //------------------------------------------------------------------------
135 Reference
< XInterface
> ComponentContext::getSingleton( const ::rtl::OUString
& _rInstanceName
) const
137 ::rtl::OUString
sKey( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/" ) ) );
138 sKey
+= _rInstanceName
;
139 return Reference
< XInterface
>( getContextValueByName( sKey
), UNO_QUERY
);
142 //------------------------------------------------------------------------
143 Reference
< XMultiServiceFactory
> ComponentContext::getLegacyServiceFactory() const
145 return Reference
< XMultiServiceFactory
>( m_xORB
, UNO_QUERY_THROW
);
148 //........................................................................
149 } // namespace comphelper
150 //........................................................................