1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScriptRuntimeManager.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_scripting.hxx"
34 #ifndef _VCL_MSGBOX_HXX
35 #include <vcl/msgbox.hxx>
38 #include "ScriptExecDialog.hrc"
40 #include <util/scriptingconstants.hxx>
42 #include <cppuhelper/implementationentry.hxx>
44 #include <com/sun/star/beans/XPropertySet.hpp>
45 #include <com/sun/star/lang/XEventListener.hpp>
46 #include <com/sun/star/lang/EventObject.hpp>
48 #include "ScriptNameResolverImpl.hxx"
49 #include "ScriptRuntimeManager.hxx"
50 #include <util/util.hxx>
51 #include <util/scriptingconstants.hxx>
53 using namespace ::rtl
;
54 using namespace ::osl
;
55 using namespace ::com::sun::star
;
56 using namespace ::com::sun::star::uno
;
57 using namespace ::drafts::com::sun::star::script::framework
;
59 namespace scripting_runtimemgr
62 static OUString s_implName
= ::rtl::OUString::createFromAscii(
63 "drafts.com.sun.star.script.framework.runtime.ScriptRuntimeManager" );
64 static OUString s_serviceName
= ::rtl::OUString::createFromAscii(
65 "drafts.com.sun.star.script.framework.runtime.ScriptRuntimeManager" );
66 static Sequence
< OUString
> s_serviceNames
= Sequence
< OUString
>( &s_serviceName
, 1 );
68 ::rtl_StandardModuleCount s_moduleCount
= MODULE_COUNT_INIT
;
70 //*************************************************************************
71 // ScriptRuntimeManager Constructor
72 ScriptRuntimeManager::ScriptRuntimeManager(
73 const Reference
< XComponentContext
> & xContext
) :
74 m_xContext( xContext
)
76 OSL_TRACE( "< ScriptRuntimeManager ctor called >\n" );
77 validateXRef( m_xContext
,
78 "ScriptRuntimeManager::ScriptRuntimeManager: invalid context" );
79 m_xMgr
= m_xContext
->getServiceManager();
81 "ScriptRuntimeManager::ScriptRuntimeManager: cannot get ServiceManager" );
82 s_moduleCount
.modCnt
.acquire( &s_moduleCount
.modCnt
);
84 //scripting_securitymgr::ScriptSecurityManager ssm(xContext);
87 //*************************************************************************
88 // ScriptRuntimeManager Destructor
89 ScriptRuntimeManager::~ScriptRuntimeManager()
91 OSL_TRACE( "< ScriptRuntimeManager dtor called >\n" );
92 s_moduleCount
.modCnt
.release( &s_moduleCount
.modCnt
);
95 //*************************************************************************
96 // Get the proper XScriptInvocation
97 Reference
< runtime::XScriptInvocation
> SAL_CALL
ScriptRuntimeManager::getScriptRuntime(
98 const Reference
< XInterface
>& scriptInfo
)
99 throw( RuntimeException
)
101 OSL_TRACE( "** ==> ScriptRuntimeManager in getScriptRuntime\n" );
103 Reference
< runtime::XScriptInvocation
> xScriptInvocation
;
107 Reference
< XInterface
> xInterface
;
109 Reference
< storage::XScriptInfo
> sinfo
=
110 Reference
< storage::XScriptInfo
>( scriptInfo
, UNO_QUERY_THROW
);
112 OUStringBuffer
*buf
= new OUStringBuffer(80);
113 buf
->appendAscii("/singletons/drafts.com.sun.star.script.framework.runtime.theScriptRuntimeFor");
114 buf
->append(sinfo
->getLanguage());
116 Any a
= m_xContext
->getValueByName(buf
->makeStringAndClear());
118 if ( sal_False
== ( a
>>= xInterface
) )
120 throw RuntimeException(
121 sinfo
->getLanguage().concat( OUSTR( " runtime support is not installed for this language" ) ),
122 Reference
< XInterface
>() );
124 validateXRef( xInterface
,
125 "ScriptRuntimeManager::GetScriptRuntime: cannot get appropriate ScriptRuntime Service"
127 xScriptInvocation
= Reference
< runtime::XScriptInvocation
>( xInterface
, UNO_QUERY_THROW
);
129 catch ( Exception
& e
)
131 OUString temp
= OUSTR( "ScriptRuntimeManager::GetScriptRuntime: " );
132 throw RuntimeException( temp
.concat( e
.Message
), Reference
< XInterface
>() );
135 return xScriptInvocation
;
138 //*************************************************************************
139 // Get the proper XScriptNameResolver
140 Reference
< runtime::XScriptNameResolver
> SAL_CALL
141 ScriptRuntimeManager::getScriptNameResolver()
142 throw( RuntimeException
)
144 OSL_TRACE( "** ==> ScriptRuntimeManager in getScriptNameResolver\n" );
145 Reference
< runtime::XScriptNameResolver
> xScriptNameResolver
;
149 Reference
< XInterface
> xInterface
= m_xMgr
->createInstanceWithContext(
150 OUString::createFromAscii(
151 "drafts.com.sun.star.script.framework.runtime.DefaultScriptNameResolver" ),
153 validateXRef( xInterface
,
154 "ScriptRuntimeManager::GetScriptRuntime: cannot get instance of DefaultScriptNameResolver" );
155 xScriptNameResolver
= Reference
< runtime::XScriptNameResolver
>( xInterface
, UNO_QUERY_THROW
);
157 catch ( Exception
& e
)
159 OUString temp
= OUSTR( "ScriptRuntimeManager::GetScriptNameResolver: " );
160 throw RuntimeException( temp
.concat( e
.Message
), Reference
< XInterface
>() );
162 return xScriptNameResolver
;
165 //*************************************************************************
166 // XScriptInvocation implementation
167 Any SAL_CALL
ScriptRuntimeManager::invoke(
168 const ::rtl::OUString
& scriptURI
,
169 const Any
& invocationCtx
, const Sequence
< Any
>& aParams
,
170 Sequence
< sal_Int16
>& aOutParamIndex
, Sequence
< Any
>& aOutParam
)
171 throw ( lang::IllegalArgumentException
, script::CannotConvertException
,
172 reflection::InvocationTargetException
, RuntimeException
)
174 OSL_TRACE( "** ==> ScriptRuntimeManager in runtimemgr invoke\n" );
177 scripting_constants::ScriptingConstantsPool
& scriptingConstantsPool
=
178 scripting_constants::ScriptingConstantsPool::instance();
180 // Initialise resolved context with invocation context,
181 // the resolved context (resolvedCtx will be modified by the
182 // resolve method to contain the storage where the script code is
184 Any resolvedCtx
= invocationCtx
;
188 Reference
< storage::XScriptInfo
> resolvedScript
= resolve( scriptURI
,
190 validateXRef( resolvedScript
, "ScriptRuntimeManager::invoke: No resolvedURI" );
192 Reference
< beans::XPropertySet
> xPropSetResolvedCtx
;
193 if ( sal_False
== ( resolvedCtx
>>= xPropSetResolvedCtx
) )
195 throw RuntimeException( OUSTR(
196 "ScriptRuntimeManager::invoke : unable to get XPropSetScriptingContext from param" ),
197 Reference
< XInterface
> () );
200 Any any
= xPropSetResolvedCtx
->getPropertyValue(
201 scriptingConstantsPool
.RESOLVED_STORAGE_ID
);
202 sal_Int32 resolvedSid
;
203 if ( sal_False
== ( any
>>= resolvedSid
) )
205 throw RuntimeException( OUSTR(
206 "ScriptRuntimeManager::invoke : unable to get resolved storage id from xPropSetResolvedCtx" ),
207 Reference
< XInterface
> () );
210 OSL_TRACE("Storage sid is: %d\n", resolvedSid
);
212 // modifying the XPropertySet on the resolved Context to contain the
215 aResolvedScript
<<= resolvedScript
;
217 xPropSetResolvedCtx
->setPropertyValue( scriptingConstantsPool
.SCRIPT_INFO
,
220 Reference
< runtime::XScriptInvocation
> xScriptInvocation
=
221 getScriptRuntime( resolvedScript
);
222 validateXRef( xScriptInvocation
,
223 "ScriptRuntimeManager::invoke: cannot get instance of language specific runtime." );
225 // the scriptURI is currently passed to the language-dept runtime but
226 // is not used (may be useful in the future?). All of the script info
227 // is contained as a property(SCRIPT_INFO) within the resolvedCtx
228 results
= xScriptInvocation
->invoke( scriptURI
, resolvedCtx
, aParams
,
229 aOutParamIndex
, aOutParam
);
231 // need to dispose of filesystem storage
232 OUString filesysString
= OUString::createFromAscii(
233 "location=filesystem" );
234 if ( scriptURI
.indexOf( filesysString
) != -1 )
236 Any a
= m_xContext
->getValueByName(
237 scriptingConstantsPool
.SCRIPTSTORAGEMANAGER_SERVICE
);
238 Reference
< lang::XEventListener
> xEL_ScriptStorageManager
;
239 if ( sal_False
== ( a
>>= xEL_ScriptStorageManager
) )
241 throw RuntimeException( OUSTR( "ScriptRuntimeManager::invoke: can't get ScriptStorageManager XEventListener interface when trying to dispose of filesystem storage" ),
242 Reference
< XInterface
> () );
244 validateXRef( xEL_ScriptStorageManager
, "Cannot get XEventListener from ScriptStorageManager" );
245 lang::EventObject
event(resolvedScript
);
246 xEL_ScriptStorageManager
->disposing( event
);
249 catch ( lang::IllegalArgumentException
& iae
)
251 OUString temp
= OUSTR( "ScriptRuntimeManager::invoke IllegalArgumentException: " );
252 throw lang::IllegalArgumentException( temp
.concat( iae
.Message
),
253 Reference
< XInterface
> (),
254 iae
.ArgumentPosition
);
256 catch ( script::CannotConvertException
& cce
)
258 OUString temp
= OUSTR( "ScriptRuntimeManager::invoke CannotConvertException: " );
259 throw script::CannotConvertException( temp
.concat( cce
.Message
),
260 Reference
< XInterface
> (),
261 cce
.DestinationTypeClass
, cce
.Reason
,
264 catch ( reflection::InvocationTargetException
& ite
)
266 OUString temp
= OUSTR( "ScriptRuntimeManager::invoke InvocationTargetException: " );
267 throw reflection::InvocationTargetException( temp
.concat( ite
.Message
),
268 Reference
< XInterface
> (), ite
.TargetException
);
270 catch ( beans::UnknownPropertyException
& e
)
272 OUString temp
= OUSTR( "ScriptRuntimeManager::invoke UnknownPropertyException: " );
273 throw RuntimeException( temp
.concat( e
.Message
),
274 Reference
< XInterface
> () );
276 catch ( lang::WrappedTargetException
& e
)
278 OUString temp
= OUSTR( "ScriptRuntimeManager::invoke WrappedTargetException : " );
279 throw RuntimeException( temp
.concat( e
.Message
),
280 Reference
< XInterface
> () );
282 catch ( RuntimeException
& re
)
284 OUString temp
= OUSTR( "ScriptRuntimeManager::invoke RuntimeException: " );
285 throw RuntimeException( temp
.concat( re
.Message
),
286 Reference
< XInterface
> () );
288 catch ( Exception
& e
)
290 OUString temp
= OUSTR( "ScriptRuntimeManager::invoke Exception: " );
291 throw RuntimeException( temp
.concat( e
.Message
),
292 Reference
< XInterface
> () );
297 throw RuntimeException( OUSTR( "ScriptRuntimeManager::invoke UnknownException: " ),
298 Reference
< XInterface
> () );
301 OSL_TRACE( "** ==> ScriptRuntimeManager returned from invoke: %s\n", ::rtl::OUStringToOString( results
.getValueTypeName(), RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
305 //*************************************************************************
306 // XScriptNameResolver implementation
307 Reference
< storage::XScriptInfo
> SAL_CALL
308 ScriptRuntimeManager::resolve( const ::rtl::OUString
& scriptURI
,
310 throw( lang::IllegalArgumentException
, script::CannotConvertException
, RuntimeException
)
312 OSL_TRACE( "** ==> ScriptRuntimeManager in resolve\n" );
313 Reference
< storage::XScriptInfo
> resolvedURI
;
315 Reference
< runtime::XScriptNameResolver
> xScriptNameResolver
= getScriptNameResolver();
316 validateXRef( xScriptNameResolver
,
317 "ScriptRuntimeManager::resolve: No ScriptNameResolver" );
321 resolvedURI
= xScriptNameResolver
->resolve( scriptURI
, invocationCtx
);
323 catch ( lang::IllegalArgumentException
& iae
)
326 OUSTR( "ScriptRuntimeManager::resolve IllegalArgumentException: " );
327 throw lang::IllegalArgumentException( temp
.concat( iae
.Message
),
328 Reference
< XInterface
> (),
329 iae
.ArgumentPosition
);
331 catch ( script::CannotConvertException
& cce
)
333 OUString temp
= OUSTR( "ScriptRuntimeManager::resolve CannotConvertException: " );
334 throw script::CannotConvertException( temp
.concat( cce
.Message
),
335 Reference
< XInterface
> (),
336 cce
.DestinationTypeClass
, cce
.Reason
,
339 catch ( RuntimeException
& re
)
341 OUString temp
= OUSTR( "ScriptRuntimeManager::resolve RuntimeException: " );
342 throw RuntimeException( temp
.concat( re
.Message
),
343 Reference
< XInterface
> () );
348 throw RuntimeException(
349 OUSTR( "ScriptRuntimeManager::resolve UnknownException: " ),
350 Reference
< XInterface
> () );
357 //*************************************************************************
358 OUString SAL_CALL
ScriptRuntimeManager::getImplementationName( )
359 throw( RuntimeException
)
364 //*************************************************************************
365 sal_Bool SAL_CALL
ScriptRuntimeManager::supportsService( const OUString
& serviceName
)
366 throw( RuntimeException
)
368 OUString
const * pNames
= s_serviceNames
.getConstArray();
369 for ( sal_Int32 nPos
= s_serviceNames
.getLength(); nPos
--; )
371 if ( serviceName
.equals( pNames
[ nPos
] ) )
379 //*************************************************************************
380 Sequence
<OUString
> SAL_CALL
ScriptRuntimeManager::getSupportedServiceNames( )
381 throw( RuntimeException
)
383 return s_serviceNames
;
386 //*************************************************************************
387 static Reference
< XInterface
> SAL_CALL
srm_create(
388 const Reference
< XComponentContext
> & xCompC
)
390 return ( cppu::OWeakObject
* ) new ScriptRuntimeManager( xCompC
);
393 //*************************************************************************
394 static Sequence
<OUString
> srm_getSupportedServiceNames( )
397 return s_serviceNames
;
400 //*************************************************************************
401 static OUString
srm_getImplementationName( )
407 //*************************************************************************
408 Reference
< XInterface
> SAL_CALL
scriptnri_create(
409 Reference
< XComponentContext
> const & xComponentContext
)
410 SAL_THROW( ( Exception
) );
412 //*************************************************************************
413 Sequence
< OUString
> scriptnri_getSupportedServiceNames() SAL_THROW( () );
415 //*************************************************************************
416 OUString
scriptnri_getImplementationName() SAL_THROW( () );
418 //******************** ScriptStorageMangaer defines ***********************
419 Reference
< XInterface
> SAL_CALL
ssm_create(
420 Reference
< XComponentContext
> const & xComponentContext
)
421 SAL_THROW( ( Exception
) );
422 //*************************************************************************
423 Sequence
< OUString
> ssm_getSupportedServiceNames() SAL_THROW( () );
424 //*************************************************************************
425 OUString
ssm_getImplementationName() SAL_THROW( () );
426 //*************************************************************************
428 //************ Script Provider defines ************************************
429 Reference
< XInterface
> SAL_CALL
sp_create( const Reference
< XComponentContext
> & xCompC
);
430 //******************** ScriptProvider defines ***************************
431 Sequence
< OUString
> sp_getSupportedServiceNames( ) SAL_THROW( () );
432 //*************************************************************************
433 OUString
sp_getImplementationName( ) SAL_THROW( () );
434 //*************************************************************************
436 //************ ScriptStorage defines **************************************
437 Reference
< XInterface
> SAL_CALL
ss_create( const Reference
< XComponentContext
> & xCompC
);
438 //******************** ScriptProvider defines ***************************
439 Sequence
< OUString
> ss_getSupportedServiceNames( ) SAL_THROW( () );
440 //*************************************************************************
441 OUString
ss_getImplementationName( ) SAL_THROW( () );
442 //*************************************************************************
445 static struct cppu::ImplementationEntry s_entries
[] =
448 srm_create
, srm_getImplementationName
,
449 srm_getSupportedServiceNames
, cppu::createSingleComponentFactory
,
450 &s_moduleCount
.modCnt
, 0
453 scriptnri_create
, scriptnri_getImplementationName
,
454 scriptnri_getSupportedServiceNames
, cppu::createSingleComponentFactory
,
455 &s_moduleCount
.modCnt
, 0
458 ssm_create
, ssm_getImplementationName
,
459 ssm_getSupportedServiceNames
, cppu::createSingleComponentFactory
,
463 ss_create
, ss_getImplementationName
,
464 ss_getSupportedServiceNames
, cppu::createSingleComponentFactory
,
468 sp_create
, sp_getImplementationName
,
469 sp_getSupportedServiceNames
, cppu::createSingleComponentFactory
,
476 //#######################################################################################
477 //#### EXPORTED #########################################################################
478 //#######################################################################################
481 * Gives the environment this component belongs to.
485 void SAL_CALL
component_getImplementationEnvironment( const sal_Char
** ppEnvTypeName
,
486 uno_Environment
** ppEnv
)
488 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
492 * This function creates an implementation section in the registry and another subkey
494 * for each supported service.
495 * @param pServiceManager the service manager
496 * @param pRegistryKey the registry key
498 sal_Bool SAL_CALL
component_writeInfo( lang::XMultiServiceFactory
* pServiceManager
,
499 registry::XRegistryKey
* pRegistryKey
)
501 if (::cppu::component_writeInfoHelper( pServiceManager
, pRegistryKey
,
502 ::scripting_runtimemgr::s_entries
))
506 // register RuntimeManager singleton
508 registry::XRegistryKey
* pKey
=
509 reinterpret_cast< registry::XRegistryKey
* >(pRegistryKey
);
511 Reference
< registry::XRegistryKey
> xKey(
514 OUSTR("drafts.com.sun.star.script.framework.runtime.ScriptRuntimeManager/UNO/SINGLETONS/drafts.com.sun.star.script.framework.runtime.theScriptRuntimeManager")));
515 xKey
->setStringValue( OUSTR("drafts.com.sun.star.script.framework.runtime.ScriptRuntimeManager") );
517 // ScriptStorage Mangaer singleton
519 xKey
= pKey
->createKey(
520 OUSTR("drafts.com.sun.star.script.framework.storage.ScriptStorageManager/UNO/SINGLETONS/drafts.com.sun.star.script.framework.storage.theScriptStorageManager"));
521 xKey
->setStringValue( OUSTR("drafts.com.sun.star.script.framework.storage.ScriptStorageManager") );
522 // Singleton entries are not handled by the setup process
523 // below is the only alternative at the momement which
524 // is to programmatically do this.
526 // "Java" Runtime singleton entry
528 xKey
= pKey
->createKey(
529 OUSTR("com.sun.star.scripting.runtime.java.ScriptRuntimeForJava$_ScriptRuntimeForJava/UNO/SINGLETONS/drafts.com.sun.star.script.framework.runtime.theScriptRuntimeForJava"));
530 xKey
->setStringValue( OUSTR("drafts.com.sun.star.script.framework.runtime.ScriptRuntimeForJava") );
532 // "JavaScript" Runtime singleton entry
534 xKey
= pKey
->createKey(
535 OUSTR("com.sun.star.scripting.runtime.javascript.ScriptRuntimeForJavaScript$_ScriptRuntimeForJavaScript/UNO/SINGLETONS/drafts.com.sun.star.script.framework.runtime.theScriptRuntimeForJavaScript"));
536 xKey
->setStringValue( OUSTR("drafts.com.sun.star.script.framework.runtime.ScriptRuntimeForJavaScript") );
538 // "BeanShell" Runtime singleton entry
540 xKey
= pKey
->createKey(
541 OUSTR("com.sun.star.scripting.runtime.beanshell.ScriptRuntimeForBeanShell$_ScriptRuntimeForBeanShell/UNO/SINGLETONS/drafts.com.sun.star.script.framework.runtime.theScriptRuntimeForBeanShell"));
542 xKey
->setStringValue( OUSTR("drafts.com.sun.star.script.framework.runtime.ScriptRuntimeForBeanShell") );
546 catch (Exception
& exc
)
554 * This function is called to get service factories for an implementation.
556 * @param pImplName name of implementation
557 * @param pServiceManager a service manager, need for component creation
558 * @param pRegistryKey the registry key for this component, need for persistent
560 * @return a component factory
562 void * SAL_CALL
component_getFactory( const sal_Char
* pImplName
,
563 lang::XMultiServiceFactory
* pServiceManager
,
564 registry::XRegistryKey
* pRegistryKey
)
566 return ::cppu::component_getFactoryHelper( pImplName
, pServiceManager
,
567 pRegistryKey
, ::scripting_runtimemgr::s_entries
);