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 
25 #ifndef _FRAMEWORK_SCRIPT_SCRIPTRUNTIMEMANAGER_HXX_
26 #define _FRAMEWORK_SCRIPT_SCRIPTRUNTIMEMANAGER_HXX_
27 
28 #include <osl/mutex.hxx>
29 
30 #include <cppuhelper/implbase3.hxx>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 #include <com/sun/star/uno/RuntimeException.hpp>
35 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
36 #include <com/sun/star/script/CannotConvertException.hpp>
37 #include <com/sun/star/reflection/InvocationTargetException.hpp>
38 
39 #include <drafts/com/sun/star/script/framework/runtime/XScriptInvocation.hpp>
40 #include <drafts/com/sun/star/script/framework/runtime/XScriptNameResolver.hpp>
41 
42 namespace scripting_runtimemgr
43 {
44 // for simplification
45 #define css ::com::sun::star
46 #define dcsssf ::drafts::com::sun::star::script::framework
47 
48 /**
49  * Class responsible for managing the various ScriptRuntime implementations.
50  */
51 class ScriptRuntimeManager : public
52     ::cppu::WeakImplHelper3< dcsssf::runtime::XScriptInvocation, css::lang::XServiceInfo,
53     dcsssf::runtime::XScriptNameResolver >
54 {
55 public:
56     explicit ScriptRuntimeManager(
57         const css::uno::Reference< css::uno::XComponentContext > & xContext );
58     ~ScriptRuntimeManager();
59 
60 
61     // XServiceInfo implementation
62     virtual ::rtl::OUString SAL_CALL getImplementationName()
63         throw( css::uno::RuntimeException );
64     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName )
65         throw( css::uno::RuntimeException );
66     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames()
67         throw( css::uno::RuntimeException );
68 
69     /**
70      * implements XScriptInvocation, invokes the script named in scriptURI
71      * (resolving it first, if necessary), with the args passed.
72      *
73      * @param scriptURI the URI, which may not be fully qualified, for the
74      *  script to be invoked
75      *
76      *************************************************************
77       Invoke
78         @param scriptUri script uri describing the script
79         @param invocationCtx
80              the invocation context contains the documentStorageID and document reference
81              for use in script name resolving
82 
83         @param aParams all parameters; pure, out params are undefined in sequence,
84             i.e., the value has to be ignored by the callee
85         @param aOutParamIndex out indices
86         @param aOutParam out parameters
87 
88         @returns
89             the value returned from the function being invoked
90 
91         @throws IllegalArgumentException
92             if there is no matching script name
93 
94         @throws CannotConvertException
95             if args do not match or cannot be converted the those
96             of the invokee
97 
98         @throws InvocationTargetException
99             if the running script throws an exception this information is captured and
100             rethrown as this exception type.
101 
102     */
103     virtual css::uno::Any SAL_CALL invoke(
104         const ::rtl::OUString & scriptUri,
105         const css::uno::Any& invocationCtx,
106         const css::uno::Sequence< css::uno::Any >& aParams,
107         css::uno::Sequence< sal_Int16 >& aOutParamIndex,
108         css::uno::Sequence< css::uno::Any >& aOutParam )
109         throw ( css::lang::IllegalArgumentException,
110                 css::script::CannotConvertException,
111                 css::reflection::InvocationTargetException,
112                 css::uno::RuntimeException );
113 
114     /**
115      * implements  XScriptNameResolver, attempts to resolve the script URI
116      * passed in
117      *
118      * @param scriptURI the URI to be resolved
119      * @param invocationCtx  the invocation context contains the
120      * documentStorageID and document reference for use in script name
121      * resolving. On full name resolution it sets the resolvedScriptStorageID to
122      * the actual storage location of the fully resolved script. May or may not * be the
123      same as the documentStorageID.
124      * @return the resolved URI
125      */
126     virtual css::uno::Reference< dcsssf::storage::XScriptInfo > SAL_CALL resolve(
127         const ::rtl::OUString& scriptUri,
128         css::uno::Any& invocationCtx )
129         throw( css::lang::IllegalArgumentException, css::script::CannotConvertException,
130            css::uno::RuntimeException );
131 
132 private:
133     css::uno::Reference< dcsssf::runtime::XScriptInvocation > SAL_CALL getScriptRuntime(
134         const css::uno::Reference< css::uno::XInterface > & scriptInfo )
135         throw( css::uno::RuntimeException );
136     css::uno::Reference< dcsssf::runtime::XScriptNameResolver > SAL_CALL getScriptNameResolver()
137         throw( css::uno::RuntimeException );
138 
139     css::uno::Reference< css::uno::XComponentContext > m_xContext;
140     css::uno::Reference< css::lang::XMultiComponentFactory > m_xMgr;
141     ::osl::Mutex m_mutex;
142 };
143 } // scripting_runtimemgr
144 
145 #endif //_FRAMEWORK_SCRIPT_SCRIPTRUNTIMEMANAGER_HXX_
146