1*67c7d1c1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*67c7d1c1SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*67c7d1c1SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*67c7d1c1SAndrew Rist  * distributed with this work for additional information
6*67c7d1c1SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*67c7d1c1SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*67c7d1c1SAndrew Rist  * "License"); you may not use this file except in compliance
9*67c7d1c1SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*67c7d1c1SAndrew Rist  *
11*67c7d1c1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*67c7d1c1SAndrew Rist  *
13*67c7d1c1SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*67c7d1c1SAndrew Rist  * software distributed under the License is distributed on an
15*67c7d1c1SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*67c7d1c1SAndrew Rist  * KIND, either express or implied.  See the License for the
17*67c7d1c1SAndrew Rist  * specific language governing permissions and limitations
18*67c7d1c1SAndrew Rist  * under the License.
19*67c7d1c1SAndrew Rist  *
20*67c7d1c1SAndrew Rist  *************************************************************/
21*67c7d1c1SAndrew Rist 
22*67c7d1c1SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <pyuno/pyuno.hxx>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <osl/process.h>
27cdf0e10cSrcweir #include <osl/file.h>
28cdf0e10cSrcweir #include <osl/thread.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
31cdf0e10cSrcweir #include <rtl/strbuf.hxx>
32cdf0e10cSrcweir #include <rtl/bootstrap.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
35cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using rtl::OUString;
38cdf0e10cSrcweir using rtl::OUStringBuffer;
39cdf0e10cSrcweir using rtl::OString;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using pyuno::PyRef;
42cdf0e10cSrcweir using pyuno::Runtime;
43cdf0e10cSrcweir using pyuno::PyThreadAttach;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using com::sun::star::registry::XRegistryKey;
46cdf0e10cSrcweir using com::sun::star::uno::Reference;
47cdf0e10cSrcweir using com::sun::star::uno::XInterface;
48cdf0e10cSrcweir using com::sun::star::uno::Sequence;
49cdf0e10cSrcweir using com::sun::star::uno::XComponentContext;
50cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir namespace pyuno_loader
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 
raiseRuntimeExceptionWhenNeeded()55cdf0e10cSrcweir static void raiseRuntimeExceptionWhenNeeded() throw ( RuntimeException )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     if( PyErr_Occurred() )
58cdf0e10cSrcweir     {
59cdf0e10cSrcweir         PyRef excType, excValue, excTraceback;
60cdf0e10cSrcweir         PyErr_Fetch( (PyObject **)&excType, (PyObject**)&excValue,(PyObject**)&excTraceback);
61cdf0e10cSrcweir         Runtime runtime;
62cdf0e10cSrcweir         com::sun::star::uno::Any a = runtime.extractUnoException( excType, excValue, excTraceback );
63cdf0e10cSrcweir         OUStringBuffer buf;
64cdf0e10cSrcweir         buf.appendAscii( "python-loader:" );
65cdf0e10cSrcweir         if( a.hasValue() )
66cdf0e10cSrcweir             buf.append( ((com::sun::star::uno::Exception *)a.getValue())->Message );
67cdf0e10cSrcweir         throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
getLoaderModule()71cdf0e10cSrcweir static PyRef getLoaderModule() throw( RuntimeException )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     PyRef module(
74cdf0e10cSrcweir         PyImport_ImportModule( const_cast< char * >("pythonloader") ),
75cdf0e10cSrcweir         SAL_NO_ACQUIRE );
76cdf0e10cSrcweir     raiseRuntimeExceptionWhenNeeded();
77cdf0e10cSrcweir     if( !module.is() )
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         throw RuntimeException(
80cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "pythonloader: Couldn't load pythonloader module" ) ),
81cdf0e10cSrcweir             Reference< XInterface > () );
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir     return PyRef( PyModule_GetDict( module.get() ));
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
getObjectFromLoaderModule(const char * func)86cdf0e10cSrcweir static PyRef getObjectFromLoaderModule( const char * func )
87cdf0e10cSrcweir     throw ( RuntimeException )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir     PyRef object( PyDict_GetItemString(getLoaderModule().get(), (char*)func ) );
90cdf0e10cSrcweir     if( !object.is() )
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         OUStringBuffer buf;
93cdf0e10cSrcweir         buf.appendAscii( "pythonloader: couldn't find core element pythonloader." );
94cdf0e10cSrcweir         buf.appendAscii( func );
95cdf0e10cSrcweir         throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >());
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir     return object;
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
getImplementationName()100cdf0e10cSrcweir OUString getImplementationName()
101cdf0e10cSrcweir {
102cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.comp.pyuno.Loader" ) );
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
getSupportedServiceNames()105cdf0e10cSrcweir Sequence< OUString > getSupportedServiceNames()
106cdf0e10cSrcweir {
107cdf0e10cSrcweir     OUString serviceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.Python" ) );
108cdf0e10cSrcweir     return Sequence< OUString > ( &serviceName, 1 );
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
setPythonHome(const OUString & pythonHome)111cdf0e10cSrcweir static void setPythonHome ( const OUString & pythonHome )
112cdf0e10cSrcweir {
113cdf0e10cSrcweir     OUString systemPythonHome;
114cdf0e10cSrcweir     osl_getSystemPathFromFileURL( pythonHome.pData, &(systemPythonHome.pData) );
115cdf0e10cSrcweir     OString o = rtl::OUStringToOString( systemPythonHome, osl_getThreadTextEncoding() );
116cdf0e10cSrcweir     rtl_string_acquire(o.pData); // leak this string (thats the api!)
117cdf0e10cSrcweir     Py_SetPythonHome( o.pData->buffer);
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
prependPythonPath(const OUString & pythonPathBootstrap)120cdf0e10cSrcweir static void prependPythonPath( const OUString & pythonPathBootstrap )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     rtl::OUStringBuffer bufPYTHONPATH( 256 );
123cdf0e10cSrcweir     sal_Int32 nIndex = 0;
124cdf0e10cSrcweir     while( 1 )
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         sal_Int32 nNew = pythonPathBootstrap.indexOf( ' ', nIndex );
127cdf0e10cSrcweir         OUString fileUrl;
128cdf0e10cSrcweir         if( nNew == -1 )
129cdf0e10cSrcweir         {
130cdf0e10cSrcweir             fileUrl = OUString( &( pythonPathBootstrap[nIndex] ) );
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir         else
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             fileUrl = OUString( &(pythonPathBootstrap[nIndex]) , nNew - nIndex );
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir         OUString systemPath;
137cdf0e10cSrcweir         osl_getSystemPathFromFileURL( fileUrl.pData, &(systemPath.pData) );
138cdf0e10cSrcweir         bufPYTHONPATH.append( systemPath );
139cdf0e10cSrcweir         bufPYTHONPATH.append( static_cast<sal_Unicode>(SAL_PATHSEPARATOR) );
140cdf0e10cSrcweir         if( nNew == -1 )
141cdf0e10cSrcweir             break;
142cdf0e10cSrcweir         nIndex = nNew + 1;
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir     const char * oldEnv = getenv( "PYTHONPATH");
145cdf0e10cSrcweir     if( oldEnv )
146cdf0e10cSrcweir         bufPYTHONPATH.append( rtl::OUString(oldEnv, strlen(oldEnv), osl_getThreadTextEncoding()) );
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     rtl::OUString envVar(RTL_CONSTASCII_USTRINGPARAM("PYTHONPATH"));
149cdf0e10cSrcweir     rtl::OUString envValue(bufPYTHONPATH.makeStringAndClear());
150cdf0e10cSrcweir     osl_setEnvironment(envVar.pData, envValue.pData);
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
CreateInstance(const Reference<XComponentContext> & ctx)153cdf0e10cSrcweir Reference< XInterface > CreateInstance( const Reference< XComponentContext > & ctx )
154cdf0e10cSrcweir {
155cdf0e10cSrcweir     Reference< XInterface > ret;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     if( ! Py_IsInitialized() )
158cdf0e10cSrcweir     {
159cdf0e10cSrcweir         OUString pythonPath;
160cdf0e10cSrcweir         OUString pythonHome;
161cdf0e10cSrcweir         OUString path( RTL_CONSTASCII_USTRINGPARAM( "$OOO_BASE_DIR/program/" SAL_CONFIGFILE("pythonloader.uno" )));
162cdf0e10cSrcweir         rtl::Bootstrap::expandMacros(path); //TODO: detect failure
163cdf0e10cSrcweir         rtl::Bootstrap bootstrap(path);
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         // look for pythonhome
166cdf0e10cSrcweir         bootstrap.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "PYUNO_LOADER_PYTHONHOME") ), pythonHome );
167cdf0e10cSrcweir         bootstrap.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "PYUNO_LOADER_PYTHONPATH" ) ) , pythonPath );
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         // pythonhome+pythonpath must be set before Py_Initialize(), otherwise there appear warning on the console
170cdf0e10cSrcweir         // sadly, there is no api for setting the pythonpath, we have to use the environment variable
171cdf0e10cSrcweir         if( pythonHome.getLength() )
172cdf0e10cSrcweir             setPythonHome( pythonHome );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         if( pythonPath.getLength() )
175cdf0e10cSrcweir             prependPythonPath( pythonPath );
176cdf0e10cSrcweir 
177cdf0e10cSrcweir         // initialize python
178cdf0e10cSrcweir         Py_Initialize();
179cdf0e10cSrcweir         PyEval_InitThreads();
180cdf0e10cSrcweir 
181cdf0e10cSrcweir         PyThreadState *tstate = PyThreadState_Get();
182cdf0e10cSrcweir         PyEval_ReleaseThread( tstate );
183cdf0e10cSrcweir     }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     PyThreadAttach attach( PyInterpreterState_Head() );
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         if( ! Runtime::isInitialized() )
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir             Runtime::initialize( ctx );
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir         Runtime runtime;
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         PyRef pyCtx = runtime.any2PyObject(
194cdf0e10cSrcweir             com::sun::star::uno::makeAny( ctx ) );
195cdf0e10cSrcweir 
196cdf0e10cSrcweir         PyRef clazz = getObjectFromLoaderModule( "Loader" );
197cdf0e10cSrcweir         PyRef args ( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
198cdf0e10cSrcweir         PyTuple_SetItem( args.get(), 0 , pyCtx.getAcquired() );
199cdf0e10cSrcweir         PyRef pyInstance( PyObject_CallObject( clazz.get() , args.get() ), SAL_NO_ACQUIRE );
200cdf0e10cSrcweir         runtime.pyObject2Any( pyInstance ) >>= ret;
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir     return ret;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 
208cdf0e10cSrcweir static struct cppu::ImplementationEntry g_entries[] =
209cdf0e10cSrcweir {
210cdf0e10cSrcweir 	{
211cdf0e10cSrcweir 		pyuno_loader::CreateInstance, pyuno_loader::getImplementationName,
212cdf0e10cSrcweir 		pyuno_loader::getSupportedServiceNames, cppu::createSingleComponentFactory,
213cdf0e10cSrcweir 		0 , 0
214cdf0e10cSrcweir 	},
215cdf0e10cSrcweir 	{ 0, 0, 0, 0, 0, 0 }
216cdf0e10cSrcweir };
217cdf0e10cSrcweir 
218cdf0e10cSrcweir extern "C"
219cdf0e10cSrcweir {
220cdf0e10cSrcweir 
221cdf0e10cSrcweir //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)222cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment(
223cdf0e10cSrcweir 	const sal_Char ** ppEnvTypeName, uno_Environment ** )
224cdf0e10cSrcweir {
225cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
226cdf0e10cSrcweir }
227cdf0e10cSrcweir //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)228cdf0e10cSrcweir void * SAL_CALL component_getFactory(
229cdf0e10cSrcweir 	const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
230cdf0e10cSrcweir {
231cdf0e10cSrcweir 	return cppu::component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir }
235cdf0e10cSrcweir 
236