xref: /trunk/main/cppuhelper/source/stdidlclass.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_cppuhelper.hxx"
30 
31 #include <osl/mutex.hxx>
32 
33 #include <cppuhelper/weakref.hxx>
34 #include <cppuhelper/weak.hxx>
35 #include <cppuhelper/stdidlclass.hxx>
36 
37 #include <com/sun/star/reflection/XIdlClassProvider.hpp>
38 #include <com/sun/star/reflection/XIdlReflection.hpp>
39 #include <com/sun/star/uno/XComponentContext.hpp>
40 #include <com/sun/star/uno/DeploymentException.hpp>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 
43 using namespace com::sun::star;
44 using namespace com::sun::star::uno;
45 using namespace com::sun::star::lang;
46 using namespace com::sun::star::reflection;
47 using namespace rtl;
48 
49 namespace cppu {
50 
51 /*---------------------------------------------------------
52 *   This helper class implements XIdlClass. Is used by
53 *   createStdIdlClass()
54 *---------------------------------------------------------*/
55 class OStdIdlClass :
56             public OWeakObject,
57             public XIdlClass,
58             public XIdlClassProvider
59 {
60 public:
61     OStdIdlClass(
62                     const Reference < XMultiServiceFactory > &rSMgr ,
63                     const OUString & sImplementationName ,
64                     const Reference < XIdlClass > & rSuperClass,
65                     const Sequence < OUString > &seq
66                 ) SAL_THROW( () );
67 
68     // XInterface
69     Any                 SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType )
70         throw(::com::sun::star::uno::RuntimeException);
71 
72     void                SAL_CALL acquire() throw()   { OWeakObject::acquire(); }
73     void                SAL_CALL release() throw()   { OWeakObject::release(); }
74 
75     // XIdlClassProvider
76     Sequence< Reference < XIdlClass > > SAL_CALL getIdlClasses(void)
77         throw (RuntimeException);
78 
79     // XIdlClass
80     virtual Sequence< Reference< XIdlClass > > SAL_CALL getClasses(  ) throw(RuntimeException)
81                                         { return Sequence < Reference < XIdlClass > > (); }
82     virtual Reference< XIdlClass > SAL_CALL getClass( const ::rtl::OUString& ) throw(RuntimeException)
83                                         { return Reference < XIdlClass > (); }
84     virtual sal_Bool SAL_CALL equals( const Reference< XIdlClass >& Type ) throw(RuntimeException)
85                                         { return getName() == Type->getName(); }
86     virtual sal_Bool SAL_CALL isAssignableFrom( const Reference< XIdlClass >& xType ) throw(RuntimeException)
87                                         { return equals( xType ); }
88     virtual TypeClass SAL_CALL getTypeClass(  ) throw(RuntimeException)
89                                         { return TypeClass_UNKNOWN; }
90     virtual OUString SAL_CALL getName(  ) throw(RuntimeException)
91                                         { return m_sImplementationName; }
92     virtual Uik SAL_CALL getUik(  ) throw(RuntimeException)
93                                         { return Uik(); }
94     virtual Sequence< Reference< XIdlClass > > SAL_CALL getSuperclasses(  ) throw(RuntimeException)
95                                         { return m_seqSuperClasses; }
96     virtual Sequence< Reference< XIdlClass > > SAL_CALL getInterfaces(  ) throw(RuntimeException);
97 
98     virtual Reference< XIdlClass > SAL_CALL getComponentType(  ) throw(RuntimeException)
99                                         { return Reference < XIdlClass > (); }
100     virtual Reference< XIdlField > SAL_CALL getField( const ::rtl::OUString& ) throw(RuntimeException)
101                                         { return Reference < XIdlField > (); }
102     virtual Sequence< Reference< XIdlField > > SAL_CALL getFields(  ) throw(RuntimeException)
103                                         { return Sequence< Reference < XIdlField > > (); }
104     virtual Reference< XIdlMethod > SAL_CALL getMethod( const ::rtl::OUString& ) throw(RuntimeException)
105                                         { return Reference < XIdlMethod > (); }
106     virtual Sequence< Reference< XIdlMethod > > SAL_CALL getMethods(  ) throw(RuntimeException)
107                                         { return Sequence < Reference < XIdlMethod > > (); }
108     virtual Reference< XIdlArray > SAL_CALL getArray(  ) throw(RuntimeException)
109                                         { return Reference < XIdlArray > (); }
110     virtual void SAL_CALL createObject( Any& ) throw(RuntimeException) {}
111 
112 private:
113     OUString                                m_sImplementationName;
114     Sequence < OUString >                   m_seqSupportedInterface;
115     Sequence < Reference < XIdlClass > >    m_seqSuperClasses;
116     Reference < XMultiServiceFactory >      m_rSMgr;
117 
118     Reference< XIdlReflection > m_xCorefl;
119     Reference< XIdlReflection > const & get_corefl() SAL_THROW( (RuntimeException) );
120 };
121 
122 Reference< XIdlReflection > const & OStdIdlClass::get_corefl()
123     SAL_THROW( (RuntimeException) )
124 {
125     if (! m_xCorefl.is())
126     {
127         if( m_rSMgr.is() )
128         {
129             Reference< beans::XPropertySet > xProps( m_rSMgr, UNO_QUERY );
130             OSL_ASSERT( xProps.is() );
131             if (xProps.is())
132             {
133                 Reference< XComponentContext > xContext;
134                 xProps->getPropertyValue(
135                     OUString( RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) ) >>= xContext;
136                 OSL_ASSERT( xContext.is() );
137                 if (xContext.is())
138                 {
139                     Reference < XIdlReflection > x;
140                     xContext->getValueByName(
141                         OUString( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.reflection.theCoreReflection") ) ) >>= x;
142                     OSL_ENSURE( x.is(), "### CoreReflection singleton not accessible!?" );
143 
144                     if (x.is())
145                     {
146                         ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
147                         if (! m_xCorefl.is())
148                         {
149                             m_xCorefl = x;
150                         }
151                     }
152                 }
153             }
154         }
155         if (! m_xCorefl.is())
156         {
157             throw DeploymentException(
158                 OUString( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.reflection.theCoreReflection singleton not accessible") ),
159                 Reference< XInterface >() );
160         }
161     }
162     return m_xCorefl;
163 }
164 
165 OStdIdlClass::OStdIdlClass(
166                     const Reference < XMultiServiceFactory > &rSMgr ,
167                     const OUString & sImplementationName ,
168                     const Reference < XIdlClass > & rSuperClass,
169                     const Sequence < OUString > &seq
170                           ) SAL_THROW( () ) :
171                 m_sImplementationName( sImplementationName ) ,
172                 m_seqSupportedInterface( seq ),
173                 m_rSMgr( rSMgr )
174 {
175     if( rSuperClass.is() )
176         m_seqSuperClasses = Sequence< Reference < XIdlClass > >( &rSuperClass, 1 );
177 
178 }
179 
180 Any SAL_CALL OStdIdlClass::queryInterface( const Type & rType )
181      throw(::com::sun::star::uno::RuntimeException)
182 {
183     Any aRet( ::cppu::queryInterface(
184         rType, static_cast< XIdlClass * >( this ), static_cast< XIdlClassProvider * >( this ) ) );
185 
186     return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
187 }
188 
189 
190 Sequence< Reference< XIdlClass > > SAL_CALL OStdIdlClass::getInterfaces(  ) throw(RuntimeException)
191 {
192     int nMax = m_seqSupportedInterface.getLength();
193 
194     Reference< XIdlReflection > const & rCoreRefl = get_corefl();
195     if( rCoreRefl.is() )
196     {
197         Sequence< Reference< XIdlClass > > seqClasses( nMax );
198 
199         for( int n = 0 ; n < nMax ; n++ )
200         {
201             seqClasses.getArray()[n] = rCoreRefl->forName( m_seqSupportedInterface.getArray()[n] );
202         }
203 
204         return seqClasses;
205     }
206     return Sequence< Reference< XIdlClass > > () ;
207 }
208 
209 
210 // XIdlClassProvider
211 Sequence< Reference < XIdlClass > > SAL_CALL OStdIdlClass::getIdlClasses(void)
212     throw (RuntimeException)
213 {
214     // weak reference to cache the standard class
215     static WeakReference< XIdlClass >   weakRef;
216 
217     // try to make weakref hard
218     Reference < XIdlClass > r = weakRef;
219 
220     if( ! r.is() ) {
221         // xidlclass has not been initialized before or has been destroyed already.
222         r = ::cppu::createStandardClass(
223                                         m_rSMgr ,
224                                         OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.cppuhelper.OStdIdlClass") ) ,
225                                         Reference < XIdlClass > () ,
226                                         SAL_STATIC_CAST( XIdlClassProvider * , this ) ,
227                                         SAL_STATIC_CAST( XIdlClass * , this )
228                                         );
229 
230         // store reference for later use
231         weakRef = r;
232     }
233 
234     return Sequence < Reference < XIdlClass > > ( &r , 1 );
235 }
236 
237 
238 
239 
240 // external constructor
241 XIdlClass *  SAL_CALL createStandardClassWithSequence(
242                     const Reference < XMultiServiceFactory > &rSMgr ,
243                     const OUString & sImplementationName ,
244                     const Reference < XIdlClass > & rSuperClass,
245                     const Sequence < OUString > &seqInterfaceNames )
246     SAL_THROW( () )
247 {
248     return SAL_STATIC_CAST(
249                         XIdlClass * ,
250                         new OStdIdlClass(
251                                             rSMgr ,
252                                             sImplementationName,
253                                             rSuperClass,
254                                             seqInterfaceNames
255                                           )
256                        );
257 }
258 
259 } //end namespace cppu
260