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_scripting.hxx"
30 
31 #ifndef _VCL_MSGBOX_HXX
32 #include <vcl/msgbox.hxx>
33 #endif
34 
35 #include "ScriptExecDialog.hrc"
36 
37 #include <util/scriptingconstants.hxx>
38 
39 #include <cppuhelper/implementationentry.hxx>
40 #include <tools/diagnose_ex.h>
41 
42 #include <com/sun/star/beans/XPropertySet.hpp>
43 #include <com/sun/star/lang/XEventListener.hpp>
44 #include <com/sun/star/lang/EventObject.hpp>
45 
46 #include "ScriptNameResolverImpl.hxx"
47 #include "ScriptRuntimeManager.hxx"
48 #include <util/util.hxx>
49 #include <util/scriptingconstants.hxx>
50 
51 using namespace ::rtl;
52 using namespace ::osl;
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::uno;
55 using namespace ::drafts::com::sun::star::script::framework;
56 
57 namespace scripting_runtimemgr
58 {
59 
60 static OUString s_implName = ::rtl::OUString::createFromAscii(
61  "drafts.com.sun.star.script.framework.runtime.ScriptRuntimeManager" );
62 static OUString s_serviceName = ::rtl::OUString::createFromAscii(
63  "drafts.com.sun.star.script.framework.runtime.ScriptRuntimeManager" );
64 static Sequence< OUString > s_serviceNames = Sequence< OUString >( &s_serviceName, 1 );
65 
66 ::rtl_StandardModuleCount s_moduleCount = MODULE_COUNT_INIT;
67 
68 //*************************************************************************
69 // ScriptRuntimeManager Constructor
70 ScriptRuntimeManager::ScriptRuntimeManager(
71     const Reference< XComponentContext > & xContext ) :
72     m_xContext( xContext, UNO_SET_THROW )
73 {
74     OSL_TRACE( "< ScriptRuntimeManager ctor called >\n" );
75     m_xMgr.set( m_xContext->getServiceManager(), UNO_SET_THROW );
76     s_moduleCount.modCnt.acquire( &s_moduleCount.modCnt );
77     // test
78     //scripting_securitymgr::ScriptSecurityManager ssm(xContext);
79 }
80 
81 //*************************************************************************
82 // ScriptRuntimeManager Destructor
83 ScriptRuntimeManager::~ScriptRuntimeManager()
84 {
85     OSL_TRACE( "< ScriptRuntimeManager dtor called >\n" );
86     s_moduleCount.modCnt.release( &s_moduleCount.modCnt );
87 }
88 
89 //*************************************************************************
90 // Get the proper XScriptInvocation
91 Reference< runtime::XScriptInvocation > SAL_CALL ScriptRuntimeManager::getScriptRuntime(
92 const Reference< XInterface >& scriptInfo )
93 throw( RuntimeException )
94 {
95     OSL_TRACE( "** ==> ScriptRuntimeManager in getScriptRuntime\n" );
96 
97     Reference< runtime::XScriptInvocation > xScriptInvocation;
98 
99     try
100     {
101         Reference< XInterface > xInterface;
102 
103         Reference< storage::XScriptInfo > sinfo =
104             Reference< storage::XScriptInfo >( scriptInfo, UNO_QUERY_THROW );
105 
106         OUStringBuffer* buf( 80 );
107         buf.appendAscii("/singletons/drafts.com.sun.star.script.framework.runtime.theScriptRuntimeFor");
108         buf.append(sinfo->getLanguage());
109 
110         xInterface.set( m_xContext->getValueByName( buf.makeStringAndClear() ), UNO_QUERY_THROW );
111         xScriptInvocation.set( xInterface, UNO_QUERY_THROW );
112     }
113     catch ( Exception & e )
114     {
115         OUString temp = OUSTR( "ScriptRuntimeManager::GetScriptRuntime: " );
116         throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
117     }
118 
119     return xScriptInvocation;
120 }
121 
122 //*************************************************************************
123 // Get the proper XScriptNameResolver
124 Reference< runtime::XScriptNameResolver > SAL_CALL
125 ScriptRuntimeManager::getScriptNameResolver()
126 throw( RuntimeException )
127 {
128     OSL_TRACE( "** ==> ScriptRuntimeManager in getScriptNameResolver\n" );
129     Reference< runtime::XScriptNameResolver > xScriptNameResolver;
130 
131     try
132     {
133         Reference< XInterface > xInterface(
134             m_xMgr->createInstanceWithContext(
135                 OUString::createFromAscii("drafts.com.sun.star.script.framework.runtime.DefaultScriptNameResolver" ),
136                 m_xContext
137             ),
138             UNO_SET_THROW
139         );
140         xScriptNameResolver.set( xInterface, UNO_QUERY_THROW );
141     }
142     catch ( Exception & e )
143     {
144         OUString temp = OUSTR( "ScriptRuntimeManager::GetScriptNameResolver: " );
145         throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
146     }
147     return xScriptNameResolver;
148 }
149 
150 //*************************************************************************
151 // XScriptInvocation implementation
152 Any SAL_CALL ScriptRuntimeManager::invoke(
153     const ::rtl::OUString & scriptURI,
154     const Any& invocationCtx, const Sequence< Any >& aParams,
155     Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
156     throw ( lang::IllegalArgumentException, script::CannotConvertException,
157             reflection::InvocationTargetException, RuntimeException )
158 {
159     OSL_TRACE( "** ==> ScriptRuntimeManager in runtimemgr invoke\n" );
160 
161     Any results;
162     scripting_constants::ScriptingConstantsPool& scriptingConstantsPool =
163                 scripting_constants::ScriptingConstantsPool::instance();
164 
165     // Initialise resolved context with invocation context,
166     // the resolved context (resolvedCtx will be modified by the
167     // resolve method to contain the storage where the script code is
168     // stored
169     Any resolvedCtx = invocationCtx;
170 
171     try
172     {
173         Reference< storage::XScriptInfo > resolvedScript = resolve( scriptURI, resolvedCtx );
174         ENSURE_OR_THROW( resolvedScript.is(), "ScriptRuntimeManager::invoke: No resolvedURI" );
175 
176         Reference< beans::XPropertySet > xPropSetResolvedCtx;
177         if ( sal_False == ( resolvedCtx >>= xPropSetResolvedCtx ) )
178         {
179             throw RuntimeException( OUSTR(
180                 "ScriptRuntimeManager::invoke : unable to get XPropSetScriptingContext from param" ),
181                 Reference< XInterface > () );
182         }
183 
184         Any any = xPropSetResolvedCtx->getPropertyValue(
185             scriptingConstantsPool.RESOLVED_STORAGE_ID );
186         sal_Int32 resolvedSid;
187         if ( sal_False == ( any >>= resolvedSid ) )
188         {
189             throw RuntimeException( OUSTR(
190                 "ScriptRuntimeManager::invoke : unable to get resolved storage id from xPropSetResolvedCtx" ),
191                 Reference< XInterface > () );
192         }
193 
194         OSL_TRACE("Storage sid is: %d\n", resolvedSid);
195 
196         // modifying the XPropertySet on the resolved Context to contain the
197         // full script info
198         Any aResolvedScript;
199         aResolvedScript <<= resolvedScript;
200 
201         xPropSetResolvedCtx->setPropertyValue( scriptingConstantsPool.SCRIPT_INFO,
202                 aResolvedScript );
203 
204         Reference< runtime::XScriptInvocation > xScriptInvocation =
205             getScriptRuntime( resolvedScript );
206         ENSURE_OR_THROW( xScriptInvocation.is(),
207             "ScriptRuntimeManager::invoke: cannot get instance of language specific runtime." );
208 
209         // the scriptURI is currently passed to the language-dept runtime but
210         // is not used (may be useful in the future?). All of the script info
211         // is contained as a property(SCRIPT_INFO) within the resolvedCtx
212         results = xScriptInvocation->invoke( scriptURI, resolvedCtx, aParams,
213                                              aOutParamIndex, aOutParam );
214 
215         // need to dispose of filesystem storage
216         OUString filesysString = OUString::createFromAscii(
217                                         "location=filesystem" );
218         if ( scriptURI.indexOf( filesysString ) != -1 )
219         {
220             Any a = m_xContext->getValueByName(
221                     scriptingConstantsPool.SCRIPTSTORAGEMANAGER_SERVICE );
222             Reference < lang::XEventListener > xEL_ScriptStorageManager( a, UNO_QUERY_THROW );
223             lang::EventObject event(resolvedScript);
224             xEL_ScriptStorageManager->disposing( event );
225         }
226     }
227     catch ( lang::IllegalArgumentException & iae )
228     {
229         OUString temp = OUSTR( "ScriptRuntimeManager::invoke IllegalArgumentException: " );
230         throw lang::IllegalArgumentException( temp.concat( iae.Message ),
231                                               Reference< XInterface > (),
232                                               iae.ArgumentPosition );
233     }
234     catch ( script::CannotConvertException & cce )
235     {
236         OUString temp = OUSTR( "ScriptRuntimeManager::invoke CannotConvertException: " );
237         throw script::CannotConvertException( temp.concat( cce.Message ),
238                                               Reference< XInterface > (),
239                                               cce.DestinationTypeClass, cce.Reason,
240                                               cce.ArgumentIndex );
241     }
242     catch ( reflection::InvocationTargetException & ite )
243     {
244         OUString temp = OUSTR( "ScriptRuntimeManager::invoke InvocationTargetException: " );
245         throw reflection::InvocationTargetException( temp.concat( ite.Message ),
246                 Reference< XInterface > (), ite.TargetException );
247     }
248     catch ( beans::UnknownPropertyException & e )
249     {
250         OUString temp = OUSTR( "ScriptRuntimeManager::invoke UnknownPropertyException: " );
251         throw RuntimeException( temp.concat( e.Message ),
252                                 Reference< XInterface > () );
253     }
254     catch ( lang::WrappedTargetException  & e )
255     {
256         OUString temp = OUSTR( "ScriptRuntimeManager::invoke WrappedTargetException : " );
257         throw RuntimeException( temp.concat( e.Message ),
258                                 Reference< XInterface > () );
259     }
260     catch ( RuntimeException & re )
261     {
262         OUString temp = OUSTR( "ScriptRuntimeManager::invoke RuntimeException: " );
263         throw RuntimeException( temp.concat( re.Message ),
264                                 Reference< XInterface > () );
265     }
266     catch ( Exception & e )
267     {
268         OUString temp = OUSTR( "ScriptRuntimeManager::invoke Exception: " );
269         throw RuntimeException( temp.concat( e.Message ),
270                                 Reference< XInterface > () );
271     }
272 #ifdef _DEBUG
273     catch ( ... )
274     {
275         throw RuntimeException( OUSTR( "ScriptRuntimeManager::invoke UnknownException: " ),
276                                 Reference< XInterface > () );
277     }
278 #endif
279     OSL_TRACE( "** ==> ScriptRuntimeManager returned from invoke: %s\n", ::rtl::OUStringToOString( results.getValueTypeName(),  RTL_TEXTENCODING_ASCII_US ).pData->buffer );
280     return results;
281 }
282 
283 //*************************************************************************
284 // XScriptNameResolver implementation
285 Reference< storage::XScriptInfo > SAL_CALL
286 ScriptRuntimeManager::resolve( const ::rtl::OUString& scriptURI,
287     Any& invocationCtx )
288 throw( lang::IllegalArgumentException, script::CannotConvertException, RuntimeException )
289 {
290     OSL_TRACE( "** ==> ScriptRuntimeManager in resolve\n" );
291     Reference< storage::XScriptInfo > resolvedURI;
292 
293     Reference< runtime::XScriptNameResolver > xScriptNameResolver = getScriptNameResolver();
294     ENSURE_OR_THROW( xScriptNameResolver.is(),
295         "ScriptRuntimeManager::resolve: No ScriptNameResolver" );
296 
297     try
298     {
299         resolvedURI = xScriptNameResolver->resolve( scriptURI, invocationCtx );
300     }
301     catch ( lang::IllegalArgumentException & iae )
302     {
303         OUString temp =
304             OUSTR( "ScriptRuntimeManager::resolve IllegalArgumentException: " );
305         throw lang::IllegalArgumentException( temp.concat( iae.Message ),
306                                               Reference< XInterface > (),
307                                               iae.ArgumentPosition );
308     }
309     catch ( script::CannotConvertException & cce )
310     {
311         OUString temp = OUSTR( "ScriptRuntimeManager::resolve CannotConvertException: " );
312         throw script::CannotConvertException( temp.concat( cce.Message ),
313                                               Reference< XInterface > (),
314                                               cce.DestinationTypeClass, cce.Reason,
315                                               cce.ArgumentIndex );
316     }
317     catch ( RuntimeException & re )
318     {
319         OUString temp = OUSTR( "ScriptRuntimeManager::resolve RuntimeException: " );
320         throw RuntimeException( temp.concat( re.Message ),
321                                 Reference< XInterface > () );
322     }
323 #ifdef _DEBUG
324     catch ( ... )
325     {
326         throw RuntimeException(
327             OUSTR( "ScriptRuntimeManager::resolve UnknownException: " ),
328             Reference< XInterface > () );
329     }
330 #endif
331 
332     return resolvedURI;
333 }
334 
335 //*************************************************************************
336 OUString SAL_CALL ScriptRuntimeManager::getImplementationName( )
337 throw( RuntimeException )
338 {
339     return s_implName;
340 }
341 
342 //*************************************************************************
343 sal_Bool SAL_CALL ScriptRuntimeManager::supportsService( const OUString& serviceName )
344 throw( RuntimeException )
345 {
346     OUString const * pNames = s_serviceNames.getConstArray();
347     for ( sal_Int32 nPos = s_serviceNames.getLength(); nPos--; )
348     {
349         if ( serviceName.equals( pNames[ nPos ] ) )
350         {
351             return sal_True;
352         }
353     }
354     return sal_False;
355 }
356 
357 //*************************************************************************
358 Sequence<OUString> SAL_CALL ScriptRuntimeManager::getSupportedServiceNames( )
359 throw( RuntimeException )
360 {
361     return s_serviceNames;
362 }
363 
364 //*************************************************************************
365 static Reference< XInterface > SAL_CALL srm_create(
366     const Reference< XComponentContext > & xCompC )
367 {
368     return ( cppu::OWeakObject * ) new ScriptRuntimeManager( xCompC );
369 }
370 
371 //*************************************************************************
372 static Sequence<OUString> srm_getSupportedServiceNames( )
373 SAL_THROW( () )
374 {
375     return s_serviceNames;
376 }
377 
378 //*************************************************************************
379 static OUString srm_getImplementationName( )
380 SAL_THROW( () )
381 {
382     return s_implName;
383 }
384 
385 //*************************************************************************
386 Reference< XInterface > SAL_CALL scriptnri_create(
387     Reference< XComponentContext > const & xComponentContext )
388 SAL_THROW( ( Exception ) );
389 
390 //*************************************************************************
391 Sequence< OUString > scriptnri_getSupportedServiceNames() SAL_THROW( () );
392 
393 //*************************************************************************
394 OUString scriptnri_getImplementationName() SAL_THROW( () );
395 
396 //******************** ScriptStorageMangaer defines ***********************
397 Reference< XInterface > SAL_CALL ssm_create(
398     Reference< XComponentContext > const & xComponentContext )
399 SAL_THROW( ( Exception ) );
400 //*************************************************************************
401 Sequence< OUString > ssm_getSupportedServiceNames() SAL_THROW( () );
402 //*************************************************************************
403 OUString ssm_getImplementationName() SAL_THROW( () );
404 //*************************************************************************
405 
406 //************ Script Provider defines ************************************
407 Reference< XInterface > SAL_CALL sp_create( const Reference< XComponentContext > & xCompC );
408 //******************** ScriptProvider defines ***************************
409 Sequence< OUString > sp_getSupportedServiceNames( ) SAL_THROW( () );
410 //*************************************************************************
411 OUString sp_getImplementationName( ) SAL_THROW( () );
412 //*************************************************************************
413 
414 //************ ScriptStorage defines **************************************
415 Reference< XInterface > SAL_CALL ss_create( const Reference< XComponentContext > & xCompC );
416 //******************** ScriptProvider defines ***************************
417 Sequence< OUString > ss_getSupportedServiceNames( ) SAL_THROW( () );
418 //*************************************************************************
419 OUString ss_getImplementationName( ) SAL_THROW( () );
420 //*************************************************************************
421 
422 
423 static struct cppu::ImplementationEntry s_entries [] =
424     {
425         {
426             srm_create, srm_getImplementationName,
427             srm_getSupportedServiceNames, cppu::createSingleComponentFactory,
428             &s_moduleCount.modCnt, 0
429         },
430         {
431             scriptnri_create, scriptnri_getImplementationName,
432             scriptnri_getSupportedServiceNames, cppu::createSingleComponentFactory,
433             &s_moduleCount.modCnt, 0
434         },
435         {
436             ssm_create, ssm_getImplementationName,
437             ssm_getSupportedServiceNames, cppu::createSingleComponentFactory,
438             0, 0
439         },
440         {
441             ss_create, ss_getImplementationName,
442             ss_getSupportedServiceNames, cppu::createSingleComponentFactory,
443             0, 0
444         },
445         {
446             sp_create, sp_getImplementationName,
447             sp_getSupportedServiceNames, cppu::createSingleComponentFactory,
448             0, 0
449         },
450         { 0, 0, 0, 0, 0, 0 }
451     };
452 } // Namespace
453 
454 //#######################################################################################
455 //#### EXPORTED #########################################################################
456 //#######################################################################################
457 
458 /**
459  * Gives the environment this component belongs to.
460  */
461 extern "C"
462 {
463     void SAL_CALL component_getImplementationEnvironment( const sal_Char ** ppEnvTypeName,
464         uno_Environment ** ppEnv )
465     {
466         *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
467     }
468 
469     /**
470      * This function is called to get service factories for an implementation.
471      *
472      * @param pImplName       name of implementation
473      * @param pServiceManager a service manager, need for component creation
474      * @param pRegistryKey    the registry key for this component, need for persistent
475      *                        data
476      * @return a component factory
477      */
478     void * SAL_CALL component_getFactory( const sal_Char * pImplName,
479         lang::XMultiServiceFactory * pServiceManager,
480         registry::XRegistryKey * pRegistryKey )
481     {
482         return ::cppu::component_getFactoryHelper( pImplName, pServiceManager,
483             pRegistryKey, ::scripting_runtimemgr::s_entries );
484     }
485 }
486