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