1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/componentcontext.hxx>
31 
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 === **/
37 
38 //........................................................................
39 namespace comphelper
40 {
41 //........................................................................
42 
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 === **/
58 
59     //====================================================================
60 	//= ComponentContext
61 	//====================================================================
62 	//--------------------------------------------------------------------
63     ComponentContext::ComponentContext( const Reference< XComponentContext >& _rxContext )
64         :m_xContext( _rxContext )
65     {
66         if ( m_xContext.is() )
67             m_xORB = m_xContext->getServiceManager();
68         if ( !m_xORB.is() )
69             throw NullPointerException();
70     }
71 
72     //------------------------------------------------------------------------
73     ComponentContext::ComponentContext( const Reference< XMultiServiceFactory >& _rxLegacyFactory )
74     {
75         if ( !_rxLegacyFactory.is() )
76             throw NullPointerException();
77 
78         try
79         {
80             Reference< XPropertySet > xFactoryProperties( _rxLegacyFactory, UNO_QUERY_THROW );
81             m_xContext = Reference< XComponentContext >(
82                 xFactoryProperties->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) ),
83                 UNO_QUERY );
84         }
85         catch( const RuntimeException& ) { throw; }
86         catch( const Exception& )
87         {
88             throw RuntimeException();
89         }
90 
91         if ( m_xContext.is() )
92             m_xORB = m_xContext->getServiceManager();
93         if ( !m_xORB.is() )
94             throw NullPointerException();
95     }
96 
97     //------------------------------------------------------------------------
98     Any ComponentContext::getContextValueByName( const ::rtl::OUString& _rName ) const
99     {
100         Any aReturn;
101         try
102         {
103             aReturn = m_xContext->getValueByName( _rName );
104         }
105         catch( const Exception& )
106         {
107             OSL_ENSURE( sal_False, "ComponentContext::getContextValueByName: caught an exception!" );
108         }
109         return aReturn;
110     }
111 
112     //------------------------------------------------------------------------
113     Reference< XInterface > ComponentContext::createComponent( const ::rtl::OUString& _rServiceName ) const
114     {
115         Reference< XInterface > xComponent(
116             m_xORB->createInstanceWithContext( _rServiceName, m_xContext )
117         );
118         if ( !xComponent.is() )
119             throw ServiceNotRegisteredException( _rServiceName, NULL );
120         return xComponent;
121     }
122 
123     //------------------------------------------------------------------------
124     Reference< XInterface > ComponentContext::createComponentWithArguments( const ::rtl::OUString& _rServiceName, const Sequence< Any >& _rArguments ) const
125     {
126         Reference< XInterface > xComponent(
127             m_xORB->createInstanceWithArgumentsAndContext( _rServiceName, _rArguments, m_xContext )
128         );
129         if ( !xComponent.is() )
130             throw ServiceNotRegisteredException( _rServiceName, NULL );
131         return xComponent;
132     }
133 
134     //------------------------------------------------------------------------
135     Reference< XInterface > ComponentContext::getSingleton( const ::rtl::OUString& _rInstanceName ) const
136     {
137         ::rtl::OUString sKey( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/" ) ) );
138         sKey += _rInstanceName;
139         return Reference< XInterface >( getContextValueByName( sKey ), UNO_QUERY );
140     }
141 
142     //------------------------------------------------------------------------
143     Reference< XMultiServiceFactory > ComponentContext::getLegacyServiceFactory() const
144     {
145         return Reference< XMultiServiceFactory >( m_xORB, UNO_QUERY_THROW );
146     }
147 
148 //........................................................................
149 } // namespace comphelper
150 //........................................................................
151 
152