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