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