update credits
[LibreOffice.git] / comphelper / source / misc / componentcontext.cxx
blobdded964ed549177cdd0c72d17b0a6d46f4b82d32
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/componentcontext.hxx>
21 #include <comphelper/processfactory.hxx>
22 #include <com/sun/star/lang/NullPointerException.hpp>
23 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
26 //........................................................................
27 namespace comphelper
29 //........................................................................
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::XComponentContext;
33 using ::com::sun::star::lang::NullPointerException;
34 using ::com::sun::star::lang::ServiceNotRegisteredException;
35 using ::com::sun::star::uno::Exception;
36 using ::com::sun::star::uno::Any;
37 using ::com::sun::star::uno::XInterface;
38 using ::com::sun::star::uno::UNO_QUERY_THROW;
39 using ::com::sun::star::lang::XMultiServiceFactory;
40 using ::com::sun::star::beans::XPropertySet;
41 using ::com::sun::star::uno::UNO_QUERY;
42 using ::com::sun::star::uno::RuntimeException;
43 using ::com::sun::star::uno::Sequence;
45 //====================================================================
46 //= ComponentContext
47 //====================================================================
48 //--------------------------------------------------------------------
49 ComponentContext::ComponentContext( const Reference< XComponentContext >& _rxContext )
50 :m_xContext( _rxContext )
52 if ( m_xContext.is() )
53 m_xORB = m_xContext->getServiceManager();
56 //------------------------------------------------------------------------
57 ComponentContext::ComponentContext( const Reference< XMultiServiceFactory >& _rxLegacyFactory )
59 if ( !_rxLegacyFactory.is() )
60 throw NullPointerException();
62 m_xContext = comphelper::getComponentContext( _rxLegacyFactory );
63 m_xORB = m_xContext->getServiceManager();
66 //------------------------------------------------------------------------
67 Any ComponentContext::getContextValueByName( const OUString& _rName ) const
69 Any aReturn;
70 try
72 aReturn = m_xContext->getValueByName( _rName );
74 catch( const Exception& )
76 OSL_FAIL( "ComponentContext::getContextValueByName: caught an exception!" );
78 return aReturn;
81 //------------------------------------------------------------------------
82 Reference< XInterface > ComponentContext::createComponent( const OUString& _rServiceName ) const
84 Reference< XInterface > xComponent(
85 m_xORB->createInstanceWithContext( _rServiceName, m_xContext )
87 if ( !xComponent.is() )
88 throw ServiceNotRegisteredException( _rServiceName, NULL );
89 return xComponent;
92 //------------------------------------------------------------------------
93 Reference< XInterface > ComponentContext::createComponentWithArguments( const OUString& _rServiceName, const Sequence< Any >& _rArguments ) const
95 Reference< XInterface > xComponent(
96 m_xORB->createInstanceWithArgumentsAndContext( _rServiceName, _rArguments, m_xContext )
98 if ( !xComponent.is() )
99 throw ServiceNotRegisteredException( _rServiceName, NULL );
100 return xComponent;
103 //------------------------------------------------------------------------
104 Reference< XInterface > ComponentContext::getSingleton( const OUString& _rInstanceName ) const
106 OUString sKey( "/singletons/" );
107 sKey += _rInstanceName;
108 return Reference< XInterface >( getContextValueByName( sKey ), UNO_QUERY );
111 //------------------------------------------------------------------------
112 Reference< XMultiServiceFactory > ComponentContext::getLegacyServiceFactory() const
114 return Reference< XMultiServiceFactory >( m_xORB, UNO_QUERY_THROW );
117 //........................................................................
118 } // namespace comphelper
119 //........................................................................
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */