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