1*2c696243SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2c696243SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2c696243SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2c696243SAndrew Rist  * distributed with this work for additional information
6*2c696243SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2c696243SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2c696243SAndrew Rist  * "License"); you may not use this file except in compliance
9*2c696243SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2c696243SAndrew Rist  *
11*2c696243SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2c696243SAndrew Rist  *
13*2c696243SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2c696243SAndrew Rist  * software distributed under the License is distributed on an
15*2c696243SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2c696243SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2c696243SAndrew Rist  * specific language governing permissions and limitations
18*2c696243SAndrew Rist  * under the License.
19*2c696243SAndrew Rist  *
20*2c696243SAndrew Rist  *************************************************************/
21*2c696243SAndrew Rist 
22*2c696243SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_scripting.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <vector>
28cdf0e10cSrcweir #include <stdlib.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
31cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
32cdf0e10cSrcweir #include <com/sun/star/security/AccessControlException.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <util/util.hxx>
35cdf0e10cSrcweir #include <util/scriptingconstants.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <drafts/com/sun/star/script/framework/storage/XScriptStorageManager.hpp>
38cdf0e10cSrcweir #include <drafts/com/sun/star/script/framework/security/XScriptSecurity.hpp>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include "ScriptNameResolverImpl.hxx"
41cdf0e10cSrcweir #include "ScriptRuntimeManager.hxx"
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using namespace ::rtl;
44cdf0e10cSrcweir using namespace ::com::sun::star;
45cdf0e10cSrcweir using namespace ::com::sun::star::uno;
46cdf0e10cSrcweir using namespace ::drafts::com::sun::star::script::framework;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir namespace scripting_runtimemgr
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir const sal_Char* const LANGUAGE_TO_RESOLVE_ON[] = { "All" }; // should be configurable
52cdf0e10cSrcweir OUString nrs_implName = OUString::createFromAscii(
53cdf0e10cSrcweir     "drafts.com.sun.star.script.framework.runtime.DefaultScriptNameResolver" );
54cdf0e10cSrcweir OUString nrs_serviceName = OUString::createFromAscii(
55cdf0e10cSrcweir     "drafts.com.sun.star.script.framework.runtime.DefaultScriptNameResolver" );
56cdf0e10cSrcweir Sequence< OUString > nrs_serviceNames = Sequence< OUString >( &nrs_serviceName, 1 );
57cdf0e10cSrcweir 
58cdf0e10cSrcweir const char* const SCRIPTSTORAGEMANAGER_SERVICE =
59cdf0e10cSrcweir     "/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager";
60cdf0e10cSrcweir 
61cdf0e10cSrcweir extern ::rtl_StandardModuleCount s_moduleCount;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir // define storages to search
64cdf0e10cSrcweir static ::std::vector< sal_Int32 >* m_pSearchIDs = NULL;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir //*************************************************************************
ScriptNameResolverImpl(const Reference<XComponentContext> & xContext)67cdf0e10cSrcweir ScriptNameResolverImpl::ScriptNameResolverImpl(
68cdf0e10cSrcweir     const Reference< XComponentContext > & xContext ) :
69cdf0e10cSrcweir     m_xContext( xContext, UNO_SET_THROW )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir     OSL_TRACE( "< ScriptNameResolverImpl ctor called >\n" );
72cdf0e10cSrcweir     validateXRef( m_xContext, "ScriptNameResolverImpl::ScriptNameResolverImpl: invalid context" );
73cdf0e10cSrcweir     m_xMultiComFac.set( m_xContext->getServiceManager(), UNO_SET_THROW );
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     if( !m_pSearchIDs )
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         osl::Guard< osl::Mutex > aGuard( m_mutex );
78cdf0e10cSrcweir         if( !m_pSearchIDs )
79cdf0e10cSrcweir         {
80cdf0e10cSrcweir             scripting_constants::ScriptingConstantsPool& scriptingConstantsPool =
81cdf0e10cSrcweir                 scripting_constants::ScriptingConstantsPool::instance();
82cdf0e10cSrcweir             m_pSearchIDs = new ::std::vector< sal_Int32 >();
83cdf0e10cSrcweir             m_pSearchIDs->push_back( scriptingConstantsPool.DOC_STORAGE_ID_NOT_SET );
84cdf0e10cSrcweir             m_pSearchIDs->push_back( scriptingConstantsPool.USER_STORAGE_ID );
85cdf0e10cSrcweir             m_pSearchIDs->push_back( scriptingConstantsPool.SHARED_STORAGE_ID );
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     s_moduleCount.modCnt.acquire( &s_moduleCount.modCnt );
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir //*************************************************************************
~ScriptNameResolverImpl()93cdf0e10cSrcweir ScriptNameResolverImpl::~ScriptNameResolverImpl()
94cdf0e10cSrcweir {
95cdf0e10cSrcweir     OSL_TRACE( "< ScriptNameResolverImpl dtor called >\n" );
96cdf0e10cSrcweir     s_moduleCount.modCnt.release( &s_moduleCount.modCnt );
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir //*************************************************************************
resolve(const::rtl::OUString & scriptURI,Any & invocationCtx)100cdf0e10cSrcweir Reference< storage::XScriptInfo > ScriptNameResolverImpl::resolve(
101cdf0e10cSrcweir const ::rtl::OUString & scriptURI, Any& invocationCtx )
102cdf0e10cSrcweir throw ( lang::IllegalArgumentException, script::CannotConvertException, RuntimeException )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     Reference< storage::XScriptInfo > resolvedName;
106cdf0e10cSrcweir     Reference< beans::XPropertySet > xPropSetScriptingContext;
107cdf0e10cSrcweir     scripting_constants::ScriptingConstantsPool& scriptingConstantsPool =
108cdf0e10cSrcweir             scripting_constants::ScriptingConstantsPool::instance();
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     OSL_TRACE( "ScriptNameResolverImpl::resolve: in resolve - start" );
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     if ( sal_False == ( invocationCtx >>= xPropSetScriptingContext ) )
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         throw RuntimeException( OUSTR(
115cdf0e10cSrcweir             "ScriptNameResolverImpl::resolve : unable to get XScriptingContext from param" ),
116cdf0e10cSrcweir             Reference< XInterface > () );
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     Any any;
120cdf0e10cSrcweir     OUString docUri;
121cdf0e10cSrcweir     sal_Int32 filesysScriptStorageID = -1;
122cdf0e10cSrcweir     Reference < storage::XScriptStorageManager > xScriptStorageMgr;
123cdf0e10cSrcweir     sal_Int32 docSid;
124cdf0e10cSrcweir     try
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         any = xPropSetScriptingContext->getPropertyValue(
127cdf0e10cSrcweir             scriptingConstantsPool.DOC_URI );
128cdf0e10cSrcweir         OSL_TRACE( "ScriptNameResolverImpl::resolve: in resolve - got anyUri" );
129cdf0e10cSrcweir         if ( sal_False == ( any >>= docUri ) )
130cdf0e10cSrcweir         {
131cdf0e10cSrcweir             throw RuntimeException( OUSTR(
132cdf0e10cSrcweir             "ScriptNameResolverImpl::resolve : unable to get doc Uri from xPropSetScriptingContext" ),
133cdf0e10cSrcweir                 Reference< XInterface > () );
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir         any = xPropSetScriptingContext->getPropertyValue(
136cdf0e10cSrcweir             scriptingConstantsPool.DOC_STORAGE_ID );
137cdf0e10cSrcweir         if ( sal_False == ( any >>= docSid ) )
138cdf0e10cSrcweir         {
139cdf0e10cSrcweir             throw RuntimeException( OUSTR(
140cdf0e10cSrcweir                 "ScriptNameResolverImpl::resolve : unable to get doc storage id from xPropSetScriptingContext" ),
141cdf0e10cSrcweir                 Reference< XInterface > () );
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir     catch ( Exception & e )
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         OUString temp = OUSTR(
147cdf0e10cSrcweir             "ScriptNameResolverImpl::resolve : problem with getPropertyValue" );
148cdf0e10cSrcweir         throw RuntimeException( temp.concat( e.Message ),
149cdf0e10cSrcweir                                 Reference< XInterface > () );
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir #ifdef _DEBUG
152cdf0e10cSrcweir     catch ( ... )
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         throw RuntimeException( OUSTR(
155cdf0e10cSrcweir             "ScriptNameResolverImpl::resolve Unknown Exception caught - RuntimeException rethrown" ),
156cdf0e10cSrcweir             Reference< XInterface > () );
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir #endif
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     ::rtl::OString docUriO(
162cdf0e10cSrcweir         ::rtl::OUStringToOString( docUri , RTL_TEXTENCODING_ASCII_US ) );
163cdf0e10cSrcweir     OSL_TRACE(
164cdf0e10cSrcweir         "ScriptNameResolverImpl::resolve: *** >>> DOC URI: %s, doc sid is %d\n",
165cdf0e10cSrcweir         docUriO.pData->buffer, docSid );
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     OSL_TRACE( "ScriptNameResolverImpl::resolve Starting..." );
169cdf0e10cSrcweir     OUString docString = OUString::createFromAscii( "location=document" );
170cdf0e10cSrcweir     OUString userString = OUString::createFromAscii( "location=user" );
171cdf0e10cSrcweir     OUString shareString = OUString::createFromAscii( "location=share" );
172cdf0e10cSrcweir     OUString filesysString = OUString::createFromAscii( "location=filesystem" );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir     // initialise vector with doc, user and share
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     // m_pSearchIDs is initialised as follows,
177cdf0e10cSrcweir     // m_pSearchIDs [ 0 ] empty
178cdf0e10cSrcweir     // m_pSearchIDs [ 1 ] user storage id
179cdf0e10cSrcweir     // m_pSearchIDs [ 2 ] share "      "
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     ::std::vector< sal_Int32 > m_vSearchIDs = *m_pSearchIDs;
182cdf0e10cSrcweir     m_vSearchIDs[ 0 ] = docSid;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir     if ( scriptURI.indexOf( docString ) != -1 )
185cdf0e10cSrcweir     {
186cdf0e10cSrcweir         OSL_TRACE("Full resolution available, search document");
187cdf0e10cSrcweir         // search in document
188cdf0e10cSrcweir         m_vSearchIDs.resize( 1 );
189cdf0e10cSrcweir     }
190cdf0e10cSrcweir     else if ( scriptURI.indexOf( userString ) != -1 )
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         OSL_TRACE("Full resolution available, search user");
193cdf0e10cSrcweir         // search in user
194cdf0e10cSrcweir         m_vSearchIDs[ 0 ] = ( *m_pSearchIDs )[ 1 ];
195cdf0e10cSrcweir         m_vSearchIDs.resize( 1 );
196cdf0e10cSrcweir     }
197cdf0e10cSrcweir     else if ( scriptURI.indexOf( shareString ) != -1 )
198cdf0e10cSrcweir     {
199cdf0e10cSrcweir         OSL_TRACE("Full resolution available, search share");
200cdf0e10cSrcweir         // search in share
201cdf0e10cSrcweir         m_vSearchIDs[ 0 ] = ( *m_pSearchIDs )[ 2 ];
202cdf0e10cSrcweir         m_vSearchIDs.resize( 1 );
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir     else if ( scriptURI.indexOf( filesysString ) != -1 )
205cdf0e10cSrcweir     {
206cdf0e10cSrcweir         OSL_TRACE("Full resolution available, create & search filesystem");
207cdf0e10cSrcweir         OUString filesysURL;
208cdf0e10cSrcweir         try
209cdf0e10cSrcweir         {
210cdf0e10cSrcweir             filesysURL = getFilesysURL( scriptURI );
211cdf0e10cSrcweir         }
212cdf0e10cSrcweir         catch ( lang::IllegalArgumentException & e )
213cdf0e10cSrcweir         {
214cdf0e10cSrcweir             OUString temp = OUSTR( "ScriptNameResolverImpl::resolve: " );
215cdf0e10cSrcweir             throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
216cdf0e10cSrcweir         }
217cdf0e10cSrcweir         Reference< XInterface > xInterface(
218cdf0e10cSrcweir             m_xMultiComFac->createInstanceWithContext(
219cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ),
220cdf0e10cSrcweir                 m_xContext
221cdf0e10cSrcweir             ),
222cdf0e10cSrcweir             UNO_SET_THROW
223cdf0e10cSrcweir         );
224cdf0e10cSrcweir         Reference < ucb::XSimpleFileAccess > xSimpleFileAccess = Reference <
225cdf0e10cSrcweir                     ucb::XSimpleFileAccess > ( xInterface, UNO_QUERY_THROW );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir         // do we need to encode this? hope not.
228cdf0e10cSrcweir         OSL_TRACE( ">>>> About to create storage for %s",
229cdf0e10cSrcweir                 ::rtl::OUStringToOString( filesysURL,
230cdf0e10cSrcweir                     RTL_TEXTENCODING_ASCII_US ).pData->buffer );
231cdf0e10cSrcweir         // ask storage manager to create storage
232cdf0e10cSrcweir         try
233cdf0e10cSrcweir         {
234cdf0e10cSrcweir             // need to get the ScriptStorageManager
235cdf0e10cSrcweir             xScriptStorageMgr.set( m_xContext->getValueByName(
236cdf0e10cSrcweir                     scriptingConstantsPool.SCRIPTSTORAGEMANAGER_SERVICE ), UNO_QUERY_THROW );
237cdf0e10cSrcweir             filesysScriptStorageID =
238cdf0e10cSrcweir                     xScriptStorageMgr->createScriptStorageWithURI(
239cdf0e10cSrcweir                         xSimpleFileAccess, filesysURL );
240cdf0e10cSrcweir                 OSL_TRACE( ">>>> Created storage %d - for %s ",
241cdf0e10cSrcweir                     filesysScriptStorageID, ::rtl::OUStringToOString(
242cdf0e10cSrcweir                         filesysURL, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
243cdf0e10cSrcweir         }
244cdf0e10cSrcweir         catch ( RuntimeException & e )
245cdf0e10cSrcweir         {
246cdf0e10cSrcweir             OUString temp = OUSTR( "ScriptNameResolverImpl::resolve: " );
247cdf0e10cSrcweir             throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
248cdf0e10cSrcweir         }
249cdf0e10cSrcweir         m_vSearchIDs[ 0 ] = filesysScriptStorageID;
250cdf0e10cSrcweir         m_vSearchIDs.resize( 1 );
251cdf0e10cSrcweir     }
252cdf0e10cSrcweir     else
253cdf0e10cSrcweir     {
254cdf0e10cSrcweir         OSL_TRACE("Only partial uri available, search doc, user & share");
255cdf0e10cSrcweir         // is this illegal or do we search in a default way
256cdf0e10cSrcweir         // if we get to here a uri has been passed in that has:
257cdf0e10cSrcweir         // a) not got a location specified
258cdf0e10cSrcweir         // b) an illegal location
259cdf0e10cSrcweir 
260cdf0e10cSrcweir         // detect illegal location
261cdf0e10cSrcweir         if (  scriptURI.indexOf( OUString::createFromAscii( "location=" ) ) != -1 )
262cdf0e10cSrcweir         {
263cdf0e10cSrcweir             OSL_TRACE(
264cdf0e10cSrcweir                 "ScriptNameResolver::resolve, throwing IllegalArgException" );
265cdf0e10cSrcweir             throw lang::IllegalArgumentException(
266cdf0e10cSrcweir                 OUSTR( "invalid URI: " ).concat( scriptURI ),
267cdf0e10cSrcweir                 Reference < XInterface > (), 1 );
268cdf0e10cSrcweir 
269cdf0e10cSrcweir         }
270cdf0e10cSrcweir         // leave vSearchIDs take care of the search...
271cdf0e10cSrcweir     }
272cdf0e10cSrcweir 
273cdf0e10cSrcweir     ::std::vector< sal_Int32 >::const_iterator iter;
274cdf0e10cSrcweir     ::std::vector< sal_Int32 >::const_iterator iterEnd = m_vSearchIDs.end();
275cdf0e10cSrcweir 
276cdf0e10cSrcweir     for ( iter = m_vSearchIDs.begin() ; iter != iterEnd; ++iter )
277cdf0e10cSrcweir     {
278cdf0e10cSrcweir         try
279cdf0e10cSrcweir         {
280cdf0e10cSrcweir             OSL_TRACE( "** about to resolve from storage using id %d from vector of size %d",
281cdf0e10cSrcweir                 *iter, m_vSearchIDs.size() );
282cdf0e10cSrcweir             if ( ( resolvedName = resolveURIFromStorageID( *iter, docUri, scriptURI ) ).is() )
283cdf0e10cSrcweir             {
284cdf0e10cSrcweir                 OSL_TRACE( "found match in uri from storage %d", *iter );
285cdf0e10cSrcweir                 xPropSetScriptingContext->setPropertyValue(
286cdf0e10cSrcweir                 scriptingConstantsPool.RESOLVED_STORAGE_ID, makeAny(*iter) );
287cdf0e10cSrcweir                 break;
288cdf0e10cSrcweir             }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir         }
291cdf0e10cSrcweir         catch ( css::security::AccessControlException  & e )
292cdf0e10cSrcweir         {
293cdf0e10cSrcweir             // no execute permission
294cdf0e10cSrcweir             OSL_TRACE( "ScriptNameResolverImpl::resolve : AccessControlException " );
295cdf0e10cSrcweir             continue;
296cdf0e10cSrcweir         }
297cdf0e10cSrcweir         catch ( beans::UnknownPropertyException & e )
298cdf0e10cSrcweir         {
299cdf0e10cSrcweir             OUString temp = OUSTR(
300cdf0e10cSrcweir                 "ScriptNameResolverImpl::resolve : UnknownPropertyException" );
301cdf0e10cSrcweir             throw RuntimeException( temp.concat( e.Message ),
302cdf0e10cSrcweir                 Reference< XInterface > () );
303cdf0e10cSrcweir         }
304cdf0e10cSrcweir         catch ( beans::PropertyVetoException  & e )
305cdf0e10cSrcweir         {
306cdf0e10cSrcweir             OUString temp = OUSTR(
307cdf0e10cSrcweir                 "ScriptNameResolverImpl::resolve : PropertyVetoException " );
308cdf0e10cSrcweir             throw RuntimeException( temp.concat( e.Message ),
309cdf0e10cSrcweir                 Reference< XInterface > () );
310cdf0e10cSrcweir         }
311cdf0e10cSrcweir         catch ( lang::IllegalArgumentException  & e )
312cdf0e10cSrcweir         {
313cdf0e10cSrcweir             OUString temp = OUSTR(
314cdf0e10cSrcweir                 "ScriptNameResolverImpl::resolve : IllegalArgumentException " );
315cdf0e10cSrcweir             throw lang::IllegalArgumentException( temp.concat( e.Message ),
316cdf0e10cSrcweir                 Reference< XInterface > (), e.ArgumentPosition );
317cdf0e10cSrcweir         }
318cdf0e10cSrcweir         catch ( lang::WrappedTargetException & e )
319cdf0e10cSrcweir         {
320cdf0e10cSrcweir 	    OUString temp = OUSTR(
321cdf0e10cSrcweir                 "ScriptNameResolverImpl::resolve : WrappedTargetException " );
322cdf0e10cSrcweir             throw RuntimeException( temp.concat( e.Message ),
323cdf0e10cSrcweir                 Reference< XInterface > () );
324cdf0e10cSrcweir         }
325cdf0e10cSrcweir         catch ( Exception & e )
326cdf0e10cSrcweir         {
327cdf0e10cSrcweir             OSL_TRACE(
328cdf0e10cSrcweir                 "Exception thrown by storage %d, failed to match uri: %s",
329cdf0e10cSrcweir                  *iter,
330cdf0e10cSrcweir                  ::rtl::OUStringToOString( e.Message,
331cdf0e10cSrcweir                  RTL_TEXTENCODING_ASCII_US ).pData->buffer );
332cdf0e10cSrcweir 	    OUString temp = OUSTR(
333cdf0e10cSrcweir                 "ScriptNameResolverImpl::resolve : unknown exception" );
334cdf0e10cSrcweir             throw RuntimeException( temp.concat( e.Message ),
335cdf0e10cSrcweir                 Reference< XInterface > () );
336cdf0e10cSrcweir         }
337cdf0e10cSrcweir #ifdef _DEBUG
338cdf0e10cSrcweir         catch ( ... )
339cdf0e10cSrcweir         {
340cdf0e10cSrcweir             OSL_TRACE(
341cdf0e10cSrcweir                 "unknown exception thrown by storage %d, failed to match uri",
342cdf0e10cSrcweir                 *iter );
343cdf0e10cSrcweir 	    OUString temp = OUSTR(
344cdf0e10cSrcweir                 "ScriptNameResolverImpl::resolve Unknown exception caught - RuntimeException rethrown" );
345cdf0e10cSrcweir             throw RuntimeException( temp,
346cdf0e10cSrcweir                 Reference< XInterface > () );
347cdf0e10cSrcweir         }
348cdf0e10cSrcweir #endif
349cdf0e10cSrcweir 
350cdf0e10cSrcweir     }
351cdf0e10cSrcweir     if ( !resolvedName.is() )
352cdf0e10cSrcweir     {
353cdf0e10cSrcweir         if( filesysScriptStorageID >  2 )
354cdf0e10cSrcweir         {
355cdf0e10cSrcweir             // get the filesys storage and dispose of it
356cdf0e10cSrcweir             Reference< XInterface > xScriptStorage( xScriptStorageMgr->getScriptStorage( filesysScriptStorageID ), UNO_SET_THROW );
357cdf0e10cSrcweir             Reference< storage::XScriptInfoAccess > xScriptInfoAccess = Reference<
358cdf0e10cSrcweir                 storage::XScriptInfoAccess > ( xScriptStorage, UNO_QUERY_THROW );
359cdf0e10cSrcweir             Sequence< Reference< storage::XScriptInfo > > results =
360cdf0e10cSrcweir                 xScriptInfoAccess->getAllImplementations( );
361cdf0e10cSrcweir             Reference < lang::XEventListener > xEL_ScriptStorageMgr(( xScriptStorageMgr ,UNO_QUERY_THROW );
362cdf0e10cSrcweir             lang::EventObject event( results[ 0 ] );
363cdf0e10cSrcweir             xEL_ScriptStorageMgr->disposing( event );
364cdf0e10cSrcweir         }
365cdf0e10cSrcweir         throw lang::IllegalArgumentException( OUSTR(
366cdf0e10cSrcweir             "ScriptNameResolverImpl::resolve: no script found for uri=" ).concat( scriptURI ),
367cdf0e10cSrcweir             Reference< XInterface > (), 0 );
368cdf0e10cSrcweir     }
369cdf0e10cSrcweir     return resolvedName;
370cdf0e10cSrcweir }
371cdf0e10cSrcweir 
372cdf0e10cSrcweir //*************************************************************************
373cdf0e10cSrcweir OUString SAL_CALL
374cdf0e10cSrcweir ScriptNameResolverImpl::getImplementationName( )
375cdf0e10cSrcweir throw( RuntimeException )
376cdf0e10cSrcweir {
377cdf0e10cSrcweir     return nrs_implName;
378cdf0e10cSrcweir }
379cdf0e10cSrcweir 
380cdf0e10cSrcweir //*************************************************************************
381cdf0e10cSrcweir sal_Bool SAL_CALL
382cdf0e10cSrcweir ScriptNameResolverImpl::supportsService( const OUString& serviceName )
383cdf0e10cSrcweir throw( RuntimeException )
384cdf0e10cSrcweir {
385cdf0e10cSrcweir     OUString const * pNames = nrs_serviceNames.getConstArray();
386cdf0e10cSrcweir     for ( sal_Int32 nPos = nrs_serviceNames.getLength(); nPos--; )
387cdf0e10cSrcweir     {
388cdf0e10cSrcweir         if ( serviceName.equals( pNames[ nPos ] ) )
389cdf0e10cSrcweir         {
390cdf0e10cSrcweir             return sal_True;
391cdf0e10cSrcweir         }
392cdf0e10cSrcweir     }
393cdf0e10cSrcweir     return sal_False;
394cdf0e10cSrcweir }
395cdf0e10cSrcweir 
396cdf0e10cSrcweir //*************************************************************************
397cdf0e10cSrcweir 
398cdf0e10cSrcweir Reference< storage::XScriptInfo >
399cdf0e10cSrcweir ScriptNameResolverImpl::resolveURIFromStorageID
400cdf0e10cSrcweir ( sal_Int32 sid, const ::rtl::OUString & docURI,
401cdf0e10cSrcweir   const ::rtl::OUString& scriptURI )
402cdf0e10cSrcweir SAL_THROW ( ( lang::IllegalArgumentException, css::security::AccessControlException, RuntimeException ) )
403cdf0e10cSrcweir {
404cdf0e10cSrcweir     Reference< storage::XScriptInfo > resolvedScriptInfo;
405cdf0e10cSrcweir     scripting_constants::ScriptingConstantsPool& scriptingConstantsPool =
406cdf0e10cSrcweir         scripting_constants::ScriptingConstantsPool::instance();
407cdf0e10cSrcweir     if ( sid == scriptingConstantsPool.DOC_STORAGE_ID_NOT_SET )
408cdf0e10cSrcweir     {
409cdf0e10cSrcweir         OSL_TRACE( "@@@@ **** ScriptNameResolverImpl::resolve DOC_STORAGE_ID_NOT_SET" );
410cdf0e10cSrcweir         return resolvedScriptInfo;
411cdf0e10cSrcweir     }
412cdf0e10cSrcweir     try
413cdf0e10cSrcweir     {
414cdf0e10cSrcweir         OUString permissionURI = docURI;
415cdf0e10cSrcweir         OUString filesysString = OUString::createFromAscii( "location=filesystem" );
416cdf0e10cSrcweir         if ( scriptURI.indexOf( filesysString ) != -1 )
417cdf0e10cSrcweir         {
418cdf0e10cSrcweir             // in the case of filesys scripts we're checking whether the
419cdf0e10cSrcweir             // location of the script, rather than the location of the document,
420cdf0e10cSrcweir             // has execute permission
421cdf0e10cSrcweir             try
422cdf0e10cSrcweir             {
423cdf0e10cSrcweir                 permissionURI = getFilesysURL( scriptURI );
424cdf0e10cSrcweir             }
425cdf0e10cSrcweir             catch ( lang::IllegalArgumentException & e )
426cdf0e10cSrcweir             {
427cdf0e10cSrcweir                 OUString temp = OUSTR( "ScriptNameResolverImpl::resolveFromURI: " );
428cdf0e10cSrcweir                 throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
429cdf0e10cSrcweir             }
430cdf0e10cSrcweir         }
431cdf0e10cSrcweir         Reference< storage::XScriptInfoAccess > storage( getStorageInstance( sid, permissionURI ), UNO_SET_THROW );
432cdf0e10cSrcweir         Sequence< Reference< storage::XScriptInfo > > results =
433cdf0e10cSrcweir             storage->getImplementations( scriptURI );
434cdf0e10cSrcweir 
435cdf0e10cSrcweir         const sal_Int32 length = results.getLength();
436cdf0e10cSrcweir 
437cdf0e10cSrcweir         if ( !length )
438cdf0e10cSrcweir         {
439cdf0e10cSrcweir             return resolvedScriptInfo;
440cdf0e10cSrcweir         }
441cdf0e10cSrcweir 
442cdf0e10cSrcweir         OSL_TRACE( "ScriptNameResolverImpl::resolve Got some results..." );
443cdf0e10cSrcweir         // if we get results, just return first in list,
444cdf0e10cSrcweir         // storage has already matched language, function name etc. if
445cdf0e10cSrcweir         // that information was in the uri
446cdf0e10cSrcweir         resolvedScriptInfo = results[ 0 ];
447cdf0e10cSrcweir     }
448cdf0e10cSrcweir     catch ( css::security::AccessControlException & ace )
449cdf0e10cSrcweir     {
450cdf0e10cSrcweir         OUString temp = OUSTR(
451cdf0e10cSrcweir             "ScriptRuntimeManager::resolveURIFromStorageID AccessControlException: " );
452cdf0e10cSrcweir         throw css::security::AccessControlException( temp.concat( ace.Message ),
453cdf0e10cSrcweir                                               Reference< XInterface > (),
454cdf0e10cSrcweir                                                 ace.LackingPermission );
455cdf0e10cSrcweir     }
456cdf0e10cSrcweir     catch ( lang::IllegalArgumentException & iae )
457cdf0e10cSrcweir     {
458cdf0e10cSrcweir         OUString temp = OUSTR(
459cdf0e10cSrcweir             "ScriptRuntimeManager::resolveURIFromStorageID IllegalArgumentException: " );
460cdf0e10cSrcweir         throw lang::IllegalArgumentException( temp.concat( iae.Message ),
461cdf0e10cSrcweir                                               Reference< XInterface > (),
462cdf0e10cSrcweir                                               iae.ArgumentPosition );
463cdf0e10cSrcweir     }
464cdf0e10cSrcweir     catch ( RuntimeException & re )
465cdf0e10cSrcweir     {
466cdf0e10cSrcweir         OUString temp = OUSTR(
467cdf0e10cSrcweir             "ScriptRuntimeManager::resolveURIFromStorageID RuntimeException: " );
468cdf0e10cSrcweir         throw RuntimeException( temp.concat( re.Message ),
469cdf0e10cSrcweir                                 Reference< XInterface > () );
470cdf0e10cSrcweir     }
471cdf0e10cSrcweir     catch ( Exception & e )
472cdf0e10cSrcweir     {
473cdf0e10cSrcweir         OUString temp = OUSTR(
474cdf0e10cSrcweir             "ScriptNameResolverImpl::resolveURIFromStorageID : Exception caught - RuntimeException rethrown" );
475cdf0e10cSrcweir         throw RuntimeException( temp.concat( e.Message ),
476cdf0e10cSrcweir                                 Reference< XInterface > () );
477cdf0e10cSrcweir     }
478cdf0e10cSrcweir #ifdef _DEBUG
479cdf0e10cSrcweir     catch ( ... )
480cdf0e10cSrcweir     {
481cdf0e10cSrcweir         throw RuntimeException( OUSTR(
482cdf0e10cSrcweir             "ScriptNameResolverImpl::resolveURIFromStorageID Unknown exception caught - RuntimeException rethrown" ),
483cdf0e10cSrcweir             Reference< XInterface > () );
484cdf0e10cSrcweir     }
485cdf0e10cSrcweir #endif
486cdf0e10cSrcweir     return resolvedScriptInfo;
487cdf0e10cSrcweir }
488cdf0e10cSrcweir //*************************************************************************
489cdf0e10cSrcweir 
490cdf0e10cSrcweir Reference< storage::XScriptInfoAccess >
491cdf0e10cSrcweir 
492cdf0e10cSrcweir ScriptNameResolverImpl::getStorageInstance( sal_Int32 sid,
493cdf0e10cSrcweir const ::rtl::OUString & permissionURI ) SAL_THROW ( ( RuntimeException, css::security::AccessControlException, lang::IllegalArgumentException ) )
494cdf0e10cSrcweir {
495cdf0e10cSrcweir     Reference< storage::XScriptInfoAccess > xScriptInfoAccess;
496cdf0e10cSrcweir     try
497cdf0e10cSrcweir     {
498cdf0e10cSrcweir         Reference< XInterface > xInterface( m_xContext->getValueByName(
499cdf0e10cSrcweir                     OUString::createFromAscii( SCRIPTSTORAGEMANAGER_SERVICE ) ), UNO_QUERY_THROW );
500cdf0e10cSrcweir         // check that we have permissions for this storage
501cdf0e10cSrcweir         Reference< dcsssf::security::XScriptSecurity > xScriptSecurity( xInterface, UNO_QUERY_THROW );
502cdf0e10cSrcweir         scripting_constants::ScriptingConstantsPool& scriptingConstantsPool =
503cdf0e10cSrcweir                 scripting_constants::ScriptingConstantsPool::instance();
504cdf0e10cSrcweir         // if we dealing with a document storage (ie. not user or share
505cdf0e10cSrcweir         // we need to check the permission
506cdf0e10cSrcweir         if( ( sid != scriptingConstantsPool.USER_STORAGE_ID ) &&
507cdf0e10cSrcweir             ( sid != scriptingConstantsPool.SHARED_STORAGE_ID ) )
508cdf0e10cSrcweir         {
509cdf0e10cSrcweir             xScriptSecurity->checkPermission( permissionURI,
510cdf0e10cSrcweir                 OUString::createFromAscii( "execute" ) );
511cdf0e10cSrcweir             // if we get here, the checkPermission hasn't thrown an
512cdf0e10cSrcweir             // AccessControlException, ie. permission has been granted
513cdf0e10cSrcweir             OSL_TRACE( "ScriptNameResolverImpl::getStorageInstance: got execute permission for ID=%d", sid );
514cdf0e10cSrcweir         }
515cdf0e10cSrcweir         Reference< storage::XScriptStorageManager > xScriptStorageManager( xInterface, UNO_QUERY_THROW );
516cdf0e10cSrcweir         Reference< XInterface > xScriptStorage( ScriptStorageManager->getScriptStorage( sid ), UNO_SET_THROW );
517cdf0e10cSrcweir         xScriptInfoAccess.set( xScriptStorage, UNO_QUERY_THROW );
518cdf0e10cSrcweir     }
519cdf0e10cSrcweir     catch ( lang::IllegalArgumentException & e )
520cdf0e10cSrcweir     {
521cdf0e10cSrcweir         OUString temp = OUSTR( "ScriptNameResolverImpl::getStorageInstance: " );
522cdf0e10cSrcweir         throw lang::IllegalArgumentException( temp.concat( e.Message ),
523cdf0e10cSrcweir             Reference< XInterface >(), e.ArgumentPosition );
524cdf0e10cSrcweir     }
525cdf0e10cSrcweir     catch ( css::security::AccessControlException & e )
526cdf0e10cSrcweir     {
527cdf0e10cSrcweir         OUString temp = OUSTR( "ScriptNameResolverImpl::getStorageInstance: AccessControlException " );
528cdf0e10cSrcweir         throw css::security::AccessControlException( temp.concat( e.Message ), Reference< XInterface >(), e.LackingPermission );
529cdf0e10cSrcweir     }
530cdf0e10cSrcweir     catch ( RuntimeException & re )
531cdf0e10cSrcweir     {
532cdf0e10cSrcweir         OUString temp = OUSTR( "ScriptNameResolverImpl::getStorageInstance: " );
533cdf0e10cSrcweir         throw RuntimeException( temp.concat( re.Message ), Reference< XInterface >() );
534cdf0e10cSrcweir     }
535cdf0e10cSrcweir     catch ( Exception & e )
536cdf0e10cSrcweir     {
537cdf0e10cSrcweir         OUString temp = OUSTR( "ScriptNameResolverImpl::getStorageInstance: " );
538cdf0e10cSrcweir         throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
539cdf0e10cSrcweir     }
540cdf0e10cSrcweir     return xScriptInfoAccess;
541cdf0e10cSrcweir }
542cdf0e10cSrcweir //*************************************************************************
543cdf0e10cSrcweir OUString
544cdf0e10cSrcweir ScriptNameResolverImpl::getFilesysURL( const OUString & scriptURI )
545cdf0e10cSrcweir throw( lang::IllegalArgumentException )
546cdf0e10cSrcweir {
547cdf0e10cSrcweir         OUString filePath;
548cdf0e10cSrcweir         OUString fileName;
549cdf0e10cSrcweir         OUString filesysString = OUString::createFromAscii( "location=filesystem" );
550cdf0e10cSrcweir         sal_Int32 locationPos = scriptURI.indexOf( filesysString );
551cdf0e10cSrcweir         // expect location=filesys:file:///foo/bar/myscript.bsh etc
552cdf0e10cSrcweir         // except the file url at this point is encoded
553cdf0e10cSrcweir         // so we should be ok searching for the '&'
554cdf0e10cSrcweir         sal_Int32 filesysStrLen = filesysString.getLength() + 1;
555cdf0e10cSrcweir         sal_Int32 endOfLocn = scriptURI.indexOf( '&', locationPos );
556cdf0e10cSrcweir         if (endOfLocn == -1 )
557cdf0e10cSrcweir         {
558cdf0e10cSrcweir                 filePath = scriptURI.copy( locationPos + filesysString.getLength() + 1 );
559cdf0e10cSrcweir         }
560cdf0e10cSrcweir         else
561cdf0e10cSrcweir         {
562cdf0e10cSrcweir                 filePath = scriptURI.copy( locationPos + filesysStrLen,
563cdf0e10cSrcweir                                 endOfLocn - locationPos - filesysStrLen );
564cdf0e10cSrcweir         }
565cdf0e10cSrcweir         //file name shoul also be encoded so again ok to search for '&'
566cdf0e10cSrcweir         OUString functionKey = OUString::createFromAscii( "function=" );
567cdf0e10cSrcweir         sal_Int32 functionKeyLength = functionKey.getLength();
568cdf0e10cSrcweir         sal_Int32 functionNamePos = scriptURI.indexOf( functionKey );
569cdf0e10cSrcweir         if ( functionNamePos > 0 )
570cdf0e10cSrcweir         {
571cdf0e10cSrcweir             sal_Int32 endOfFn = scriptURI.indexOf( '&', functionNamePos );
572cdf0e10cSrcweir             if ( endOfFn == -1 )
573cdf0e10cSrcweir             {
574cdf0e10cSrcweir                 fileName = scriptURI.copy( functionNamePos + functionKeyLength );
575cdf0e10cSrcweir             }
576cdf0e10cSrcweir             else
577cdf0e10cSrcweir             {
578cdf0e10cSrcweir                 fileName = scriptURI.copy( functionNamePos + functionKeyLength,
579cdf0e10cSrcweir                                 endOfFn - functionNamePos - functionKeyLength );
580cdf0e10cSrcweir             }
581cdf0e10cSrcweir         }
582cdf0e10cSrcweir         else
583cdf0e10cSrcweir         {
584cdf0e10cSrcweir             // we need to throw
585cdf0e10cSrcweir             OUString temp = OUSTR( "ScriptNameResolverImpl::getFilesysURL: error getting the filesysURL" );
586cdf0e10cSrcweir             throw lang::IllegalArgumentException( temp, Reference< XInterface >(), 0 );
587cdf0e10cSrcweir         }
588cdf0e10cSrcweir         filePath+=fileName;
589cdf0e10cSrcweir         OSL_TRACE( "ScriptNameResolverImpl::getFilesysURL: filesys URL = %s",
590cdf0e10cSrcweir                  ::rtl::OUStringToOString( filePath,
591cdf0e10cSrcweir                  RTL_TEXTENCODING_ASCII_US ).pData->buffer );
592cdf0e10cSrcweir         return filePath;
593cdf0e10cSrcweir }
594cdf0e10cSrcweir //*************************************************************************
595cdf0e10cSrcweir Sequence<OUString> SAL_CALL
596cdf0e10cSrcweir ScriptNameResolverImpl::getSupportedServiceNames( )
597cdf0e10cSrcweir throw( RuntimeException )
598cdf0e10cSrcweir {
599cdf0e10cSrcweir     return nrs_serviceNames;
600cdf0e10cSrcweir }
601cdf0e10cSrcweir 
602cdf0e10cSrcweir //*************************************************************************
603cdf0e10cSrcweir Reference< XInterface > SAL_CALL scriptnri_create(
604cdf0e10cSrcweir     Reference< XComponentContext > const & xComponentContext )
605cdf0e10cSrcweir SAL_THROW( ( Exception ) )
606cdf0e10cSrcweir {
607cdf0e10cSrcweir     return ( cppu::OWeakObject * ) new ScriptNameResolverImpl( xComponentContext );
608cdf0e10cSrcweir }
609cdf0e10cSrcweir 
610cdf0e10cSrcweir //*************************************************************************
611cdf0e10cSrcweir Sequence< OUString > scriptnri_getSupportedServiceNames() SAL_THROW( () )
612cdf0e10cSrcweir {
613cdf0e10cSrcweir     return nrs_serviceNames;
614cdf0e10cSrcweir }
615cdf0e10cSrcweir 
616cdf0e10cSrcweir //*************************************************************************
617cdf0e10cSrcweir OUString scriptnri_getImplementationName() SAL_THROW( () )
618cdf0e10cSrcweir {
619cdf0e10cSrcweir     return nrs_implName;
620cdf0e10cSrcweir }
621cdf0e10cSrcweir } // namespace scripting_runtimemgr
622