fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / jvmfwk / source / framework.cxx
blobc138f7ecbb63e4a7537401ef620f59c3650326f2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "boost/scoped_array.hpp"
21 #include "rtl/ustring.hxx"
22 #include "rtl/bootstrap.hxx"
23 #include "osl/thread.hxx"
24 #include "osl/file.hxx"
25 #include "osl/module.hxx"
26 #include "jvmfwk/framework.h"
27 #include "jvmfwk/vendorplugin.h"
28 #include <cassert>
29 #include <vector>
30 #include <functional>
31 #include <algorithm>
32 #include "framework.hxx"
33 #include "fwkutil.hxx"
34 #include "elements.hxx"
35 #include "fwkbase.hxx"
37 namespace {
39 static bool g_bEnabledSwitchedOn = false;
41 static JavaVM * g_pJavaVM = NULL;
43 sal_Bool areEqualJavaInfo(
44 JavaInfo const * pInfoA,JavaInfo const * pInfoB)
46 return jfw_areEqualJavaInfo(pInfoA, pInfoB);
51 #ifdef DISABLE_DYNLOADING
53 extern "C"
54 javaPluginError jfw_plugin_getAllJavaInfos(
55 rtl_uString *sVendor,
56 rtl_uString *sMinVersion,
57 rtl_uString *sMaxVersion,
58 rtl_uString * *arExcludeList,
59 sal_Int32 nLenList,
60 JavaInfo*** parJavaInfo,
61 sal_Int32 *nLenInfoList);
63 extern "C"
64 javaPluginError jfw_plugin_getJavaInfoByPath(
65 rtl_uString *path,
66 rtl_uString *sVendor,
67 rtl_uString *sMinVersion,
68 rtl_uString *sMaxVersion,
69 rtl_uString * *arExcludeList,
70 sal_Int32 nLenList,
71 JavaInfo ** ppInfo);
73 extern "C"
74 javaPluginError jfw_plugin_startJavaVirtualMachine(
75 const JavaInfo *pInfo,
76 const JavaVMOption* arOptions,
77 sal_Int32 cOptions,
78 JavaVM ** ppVm,
79 JNIEnv ** ppEnv);
81 extern "C"
82 javaPluginError jfw_plugin_existJRE(const JavaInfo *pInfo, sal_Bool *exist);
84 #endif
86 javaFrameworkError SAL_CALL jfw_findAllJREs(JavaInfo ***pparInfo, sal_Int32 *pSize)
88 javaFrameworkError retVal = JFW_E_NONE;
89 try
91 osl::MutexGuard guard(jfw::FwkMutex::get());
92 javaFrameworkError errcode = JFW_E_NONE;
93 if (pparInfo == NULL || pSize == NULL)
94 return JFW_E_INVALID_ARG;
96 jfw::VendorSettings aVendorSettings;
97 //Get a list of plugins which provide Java information
98 std::vector<jfw::PluginLibrary> vecPlugins =
99 aVendorSettings.getPluginData();
100 #ifndef DISABLE_DYNLOADING
101 //Create a vector that holds the libraries, which will be later
102 //dynamically loaded;
103 boost::scoped_array<osl::Module> sarModules;
104 sarModules.reset(new osl::Module[vecPlugins.size()]);
105 osl::Module * arModules = sarModules.get();
106 #endif
107 //Add the JavaInfos found by jfw_plugin_getAllJavaInfos to the vector
108 //Make sure that the contents are destroyed if this
109 //function returns with an error
110 std::vector<jfw::CJavaInfo> vecInfo;
111 //Add the JavaInfos found by jfw_plugin_getJavaInfoByPath to this vector
112 //Make sure that the contents are destroyed if this
113 //function returns with an error
114 std::vector<jfw::CJavaInfo> vecInfoManual;
115 typedef std::vector<jfw::CJavaInfo>::iterator it_info;
116 //get the list of paths to jre locations which have been
117 //added manually
118 const jfw::MergedSettings settings;
119 const std::vector<OUString>& vecJRELocations =
120 settings.getJRELocations();
121 //Use every plug-in library to get Java installations.
122 typedef std::vector<jfw::PluginLibrary>::const_iterator ci_pl;
123 int cModule = 0;
124 for (ci_pl i = vecPlugins.begin(); i != vecPlugins.end(); ++i, ++cModule)
126 const jfw::PluginLibrary & library = *i;
127 jfw::VersionInfo versionInfo =
128 aVendorSettings.getVersionInformation(library.sVendor);
129 #ifndef DISABLE_DYNLOADING
130 arModules[cModule].load(library.sPath);
131 osl::Module & pluginLib = arModules[cModule];
133 if (pluginLib.is() == sal_False)
135 OString msg = OUStringToOString(
136 library.sPath, osl_getThreadTextEncoding());
137 fprintf(stderr,"[jvmfwk] Could not load plugin %s\n" \
138 "Modify the javavendors.xml accordingly!\n", msg.getStr());
139 return JFW_E_NO_PLUGIN;
141 jfw_plugin_getAllJavaInfos_ptr getAllJavaFunc =
142 (jfw_plugin_getAllJavaInfos_ptr) pluginLib.getFunctionSymbol(
143 OUString("jfw_plugin_getAllJavaInfos"));
144 #else
145 jfw_plugin_getAllJavaInfos_ptr getAllJavaFunc =
146 jfw_plugin_getAllJavaInfos;
147 #endif
148 OSL_ASSERT(getAllJavaFunc);
149 if (getAllJavaFunc == NULL)
150 return JFW_E_ERROR;
152 //get all installations of one vendor according to minVersion,
153 //maxVersion and excludeVersions
154 sal_Int32 cInfos = 0;
155 JavaInfo** arInfos = NULL;
156 javaPluginError plerr = (*getAllJavaFunc)(
157 library.sVendor.pData,
158 versionInfo.sMinVersion.pData,
159 versionInfo.sMaxVersion.pData,
160 versionInfo.getExcludeVersions(),
161 versionInfo.getExcludeVersionSize(),
162 & arInfos,
163 & cInfos);
165 if (plerr != JFW_PLUGIN_E_NONE)
166 return JFW_E_ERROR;
168 for (int j = 0; j < cInfos; j++)
169 vecInfo.push_back(jfw::CJavaInfo::createWrapper(arInfos[j]));
171 rtl_freeMemory(arInfos);
173 //Check if the current plugin can detect JREs at the location
174 // of the paths added by jfw_setJRELocations or jfw_addJRELocation
175 //get the function from the plugin
176 #ifndef DISABLE_DYNLOADING
177 jfw_plugin_getJavaInfoByPath_ptr jfw_plugin_getJavaInfoByPathFunc =
178 (jfw_plugin_getJavaInfoByPath_ptr) pluginLib.getFunctionSymbol(
179 OUString("jfw_plugin_getJavaInfoByPath"));
180 OSL_ASSERT(jfw_plugin_getJavaInfoByPathFunc);
181 if (jfw_plugin_getJavaInfoByPathFunc == NULL)
182 return JFW_E_ERROR;
183 #else
184 jfw_plugin_getJavaInfoByPath_ptr jfw_plugin_getJavaInfoByPathFunc =
185 jfw_plugin_getJavaInfoByPath;
186 #endif
188 typedef std::vector<OUString>::const_iterator citLoc;
189 //Check every manually added location
190 for (citLoc ii = vecJRELocations.begin();
191 ii != vecJRELocations.end(); ++ii)
193 jfw::CJavaInfo aInfo;
194 plerr = (*jfw_plugin_getJavaInfoByPathFunc)(
195 ii->pData,
196 library.sVendor.pData,
197 versionInfo.sMinVersion.pData,
198 versionInfo.sMaxVersion.pData,
199 versionInfo.getExcludeVersions(),
200 versionInfo.getExcludeVersionSize(),
201 & aInfo.pInfo);
202 if (plerr == JFW_PLUGIN_E_NO_JRE)
203 continue;
204 if (plerr == JFW_PLUGIN_E_FAILED_VERSION)
205 continue;
206 else if (plerr !=JFW_PLUGIN_E_NONE)
207 return JFW_E_ERROR;
209 if (aInfo)
211 //Was this JRE already added?. Different plugins could detect
212 //the same JRE
213 it_info it_duplicate =
214 std::find_if(vecInfoManual.begin(), vecInfoManual.end(),
215 std::bind2nd(std::ptr_fun(areEqualJavaInfo), aInfo));
216 if (it_duplicate == vecInfoManual.end())
217 vecInfoManual.push_back(aInfo);
221 //Make sure vecInfoManual contains only JavaInfos for the vendors for which
222 //there is a javaSelection/plugins/library entry in the javavendors.xml
223 //To obtain the JavaInfos for the manually added JRE locations the function
224 //jfw_getJavaInfoByPath is called which can return a JavaInfo of any vendor.
225 std::vector<jfw::CJavaInfo> vecInfoManual2;
226 for (it_info ivm = vecInfoManual.begin(); ivm != vecInfoManual.end(); ++ivm)
228 for (ci_pl ii = vecPlugins.begin(); ii != vecPlugins.end(); ++ii)
230 if ( ii->sVendor.equals((*ivm)->sVendor))
232 vecInfoManual2.push_back(*ivm);
233 break;
237 //Check which JavaInfo from vector vecInfoManual2 is already
238 //contained in vecInfo. If it already exists then remove it from
239 //vecInfoManual2
240 for (it_info j = vecInfo.begin(); j != vecInfo.end(); ++j)
242 it_info it_duplicate =
243 std::find_if(vecInfoManual2.begin(), vecInfoManual2.end(),
244 std::bind2nd(std::ptr_fun(areEqualJavaInfo), *j));
245 if (it_duplicate != vecInfoManual2.end())
246 vecInfoManual2.erase(it_duplicate);
248 //create an fill the array of JavaInfo*
249 sal_Int32 nSize = vecInfo.size() + vecInfoManual2.size();
250 *pparInfo = (JavaInfo**) rtl_allocateMemory(
251 nSize * sizeof(JavaInfo*));
252 if (*pparInfo == NULL)
253 return JFW_E_ERROR;
255 typedef std::vector<jfw::CJavaInfo>::iterator it;
256 int index = 0;
257 //Add the automatically detected JREs
258 for (it k = vecInfo.begin(); k != vecInfo.end(); ++k)
259 (*pparInfo)[index++] = k->detach();
260 //Add the manually detected JREs
261 for (it l = vecInfoManual2.begin(); l != vecInfoManual2.end(); ++l)
262 (*pparInfo)[index++] = l->detach();
264 *pSize = nSize;
265 return errcode;
267 catch (const jfw::FrameworkException& e)
269 retVal = e.errorCode;
270 fprintf(stderr, "%s\n", e.message.getStr());
271 OSL_FAIL(e.message.getStr());
273 return retVal;
276 javaFrameworkError SAL_CALL jfw_startVM(
277 JavaInfo const * pInfo, JavaVMOption * arOptions, sal_Int32 cOptions,
278 JavaVM ** ppVM, JNIEnv ** ppEnv)
280 javaFrameworkError errcode = JFW_E_NONE;
281 if (cOptions > 0 && arOptions == NULL)
282 return JFW_E_INVALID_ARG;
286 osl::MutexGuard guard(jfw::FwkMutex::get());
288 //We keep this pointer so we can determine if a VM has already
289 //been created.
290 if (g_pJavaVM != NULL)
291 return JFW_E_RUNNING_JVM;
293 if (ppVM == NULL)
294 return JFW_E_INVALID_ARG;
296 std::vector<OString> vmParams;
297 OString sUserClassPath;
298 jfw::CJavaInfo aInfo;
299 if (pInfo == NULL)
301 jfw::JFW_MODE mode = jfw::getMode();
302 if (mode == jfw::JFW_MODE_APPLICATION)
304 const jfw::MergedSettings settings;
305 if (sal_False == settings.getEnabled())
306 return JFW_E_JAVA_DISABLED;
307 aInfo.attach(settings.createJavaInfo());
308 //check if a Java has ever been selected
309 if (aInfo == NULL)
310 return JFW_E_NO_SELECT;
312 #ifdef WNT
313 //Because on Windows there is no system setting that we can use to determine
314 //if Assistive Technology Tool support is needed, we ship a .reg file that the
315 //user can use to create a registry setting. When the user forgets to set
316 //the key before he starts the office then a JRE may be selected without access bridge.
317 //When he later sets the key then we select a JRE with accessibility support but
318 //only if the user has not manually changed the selected JRE in the options dialog.
319 if (jfw::isAccessibilitySupportDesired())
321 // If no JRE has been selected then we do not select one. This function shall then
322 //return JFW_E_NO_SELECT
323 if (aInfo != NULL &&
324 (aInfo->nFeatures & JFW_FEATURE_ACCESSBRIDGE) == 0)
326 //has the user manually selected a JRE?
327 if (settings.getJavaInfoAttrAutoSelect() == true)
329 // if not then the automatism has previously selected a JRE
330 //without accessibility support. We return JFW_E_NO_SELECT
331 //to cause that we search for another JRE. The search code will
332 //then prefer a JRE with accessibility support.
333 return JFW_E_NO_SELECT;
337 #endif
338 //check if the javavendors.xml has changed after a Java was selected
339 OString sVendorUpdate = jfw::getElementUpdated();
341 if (sVendorUpdate != settings.getJavaInfoAttrVendorUpdate())
342 return JFW_E_INVALID_SETTINGS;
344 //check if JAVA is disabled
345 //If Java is enabled, but it was disabled when this process was started
346 // then no preparational work, such as setting the LD_LIBRARY_PATH, was
347 //done. Therefore if a JRE needs it it must not be started.
348 if (g_bEnabledSwitchedOn &&
349 (aInfo->nRequirements & JFW_REQUIRE_NEEDRESTART))
350 return JFW_E_NEED_RESTART;
352 //Check if the selected Java was set in this process. If so it
353 //must not have the requirments flag JFW_REQUIRE_NEEDRESTART
354 if ((aInfo->nRequirements & JFW_REQUIRE_NEEDRESTART)
356 (jfw::wasJavaSelectedInSameProcess() == true))
357 return JFW_E_NEED_RESTART;
359 vmParams = settings.getVmParametersUtf8();
360 sUserClassPath = jfw::makeClassPathOption(settings.getUserClassPath());
361 } // end mode FWK_MODE_OFFICE
362 else if (mode == jfw::JFW_MODE_DIRECT)
364 errcode = jfw_getSelectedJRE(&aInfo.pInfo);
365 if (errcode != JFW_E_NONE)
366 return errcode;
367 //In direct mode the options are specified by bootstrap variables
368 //of the form UNO_JAVA_JFW_PARAMETER_1 .. UNO_JAVA_JFW_PARAMETER_n
369 vmParams = jfw::BootParams::getVMParameters();
370 sUserClassPath =
371 "-Djava.class.path=" + jfw::BootParams::getClasspath();
373 else
374 OSL_ASSERT(0);
375 pInfo = aInfo.pInfo;
377 assert(pInfo != NULL);
379 //get the function jfw_plugin_startJavaVirtualMachine
380 jfw::VendorSettings aVendorSettings;
381 OUString sLibPath = aVendorSettings.getPluginLibrary(pInfo->sVendor);
383 #ifndef DISABLE_DYNLOADING
384 osl::Module modulePlugin(sLibPath);
385 if ( ! modulePlugin)
386 return JFW_E_NO_PLUGIN;
388 OUString sFunctionName("jfw_plugin_startJavaVirtualMachine");
389 jfw_plugin_startJavaVirtualMachine_ptr pFunc =
390 (jfw_plugin_startJavaVirtualMachine_ptr)
391 osl_getFunctionSymbol(modulePlugin, sFunctionName.pData);
392 if (pFunc == NULL)
393 return JFW_E_ERROR;
394 #else
395 jfw_plugin_startJavaVirtualMachine_ptr pFunc =
396 jfw_plugin_startJavaVirtualMachine;
397 #endif
399 // create JavaVMOptions array that is passed to the plugin
400 // it contains the classpath and all options set in the
401 //options dialog
402 boost::scoped_array<JavaVMOption> sarJOptions(
403 new JavaVMOption[cOptions + 2 + vmParams.size()]);
404 JavaVMOption * arOpt = sarJOptions.get();
405 if (! arOpt)
406 return JFW_E_ERROR;
408 //The first argument is the classpath
409 arOpt[0].optionString= (char*) sUserClassPath.getStr();
410 arOpt[0].extraInfo = NULL;
411 // Set a flag that this JVM has been created via the JNI Invocation API
412 // (used, for example, by UNO remote bridges to share a common thread pool
413 // factory among Java and native bridge implementations):
414 arOpt[1].optionString = (char *) "-Dorg.openoffice.native=";
415 arOpt[1].extraInfo = 0;
417 //add the options set by options dialog
418 int index = 2;
419 typedef std::vector<OString>::const_iterator cit;
420 for (cit i = vmParams.begin(); i != vmParams.end(); ++i)
422 arOpt[index].optionString = const_cast<sal_Char*>(i->getStr());
423 arOpt[index].extraInfo = 0;
424 index ++;
426 //add all options of the arOptions argument
427 for (int ii = 0; ii < cOptions; ii++)
429 arOpt[index].optionString = arOptions[ii].optionString;
430 arOpt[index].extraInfo = arOptions[ii].extraInfo;
431 index++;
434 //start Java
435 JavaVM *pVm = NULL;
436 SAL_INFO("jfw", "Starting Java");
437 javaPluginError plerr = (*pFunc)(pInfo, arOpt, index, & pVm, ppEnv);
438 if (plerr == JFW_PLUGIN_E_VM_CREATION_FAILED)
440 errcode = JFW_E_VM_CREATION_FAILED;
442 else if (plerr != JFW_PLUGIN_E_NONE )
444 errcode = JFW_E_ERROR;
446 else
448 g_pJavaVM = pVm;
449 *ppVM = pVm;
451 OSL_ASSERT(plerr != JFW_PLUGIN_E_WRONG_VENDOR);
453 catch (const jfw::FrameworkException& e)
455 errcode = e.errorCode;
456 fprintf(stderr, "%s\n", e.message.getStr());
457 OSL_FAIL(e.message.getStr());
460 return errcode;
463 /** We do not use here jfw_findAllJREs and then check if a JavaInfo
464 meets the requirements, because that means using all plug-ins, which
465 may take quite a while. The implementation uses one plug-in and if
466 it already finds a suitable JRE then it is done and does not need to
467 load another plug-in
469 javaFrameworkError SAL_CALL jfw_findAndSelectJRE(JavaInfo **pInfo)
471 javaFrameworkError errcode = JFW_E_NONE;
474 osl::MutexGuard guard(jfw::FwkMutex::get());
475 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
476 return JFW_E_DIRECT_MODE;
477 sal_uInt64 nFeatureFlags = 0;
478 jfw::CJavaInfo aCurrentInfo;
479 //Determine if accessibility support is needed
480 bool bSupportAccessibility = jfw::isAccessibilitySupportDesired();
481 nFeatureFlags = bSupportAccessibility ?
482 JFW_FEATURE_ACCESSBRIDGE : 0L;
484 //Get a list of services which provide Java information
485 jfw::VendorSettings aVendorSettings;
486 std::vector<jfw::PluginLibrary> vecPlugins =
487 aVendorSettings.getPluginData();
488 #ifndef DISABLE_DYNLOADING
489 //Create a vector that holds the libraries, which will be later
490 //dynamically loaded;
491 boost::scoped_array<osl::Module> sarModules;
492 sarModules.reset(new osl::Module[vecPlugins.size()]);
493 osl::Module * arModules = sarModules.get();
494 #endif
495 //Use every plug-in library to get Java installations. At the first usable
496 //Java the loop will break
497 typedef std::vector<jfw::PluginLibrary>::const_iterator ci_pl;
498 int cModule = 0;
499 for (ci_pl i = vecPlugins.begin(); i != vecPlugins.end(); ++i, ++cModule)
501 const jfw::PluginLibrary & library = *i;
502 jfw::VersionInfo versionInfo =
503 aVendorSettings.getVersionInformation(library.sVendor);
504 #ifndef DISABLE_DYNLOADING
505 arModules[cModule].load(library.sPath);
506 osl::Module & pluginLib = arModules[cModule];
507 if (pluginLib.is() == sal_False)
508 return JFW_E_NO_PLUGIN;
510 jfw_plugin_getAllJavaInfos_ptr getAllJavaFunc =
511 (jfw_plugin_getAllJavaInfos_ptr) pluginLib.getFunctionSymbol(
512 OUString("jfw_plugin_getAllJavaInfos"));
513 #else
514 jfw_plugin_getAllJavaInfos_ptr getAllJavaFunc =
515 jfw_plugin_getAllJavaInfos;
516 #endif
517 OSL_ASSERT(getAllJavaFunc);
518 if (getAllJavaFunc == NULL)
519 continue;
521 //get all installations of one vendor according to minVersion,
522 //maxVersion and excludeVersions
523 sal_Int32 cInfos = 0;
524 JavaInfo** arInfos = NULL;
525 javaPluginError plerr = (*getAllJavaFunc)(
526 library.sVendor.pData,
527 versionInfo.sMinVersion.pData,
528 versionInfo.sMaxVersion.pData,
529 versionInfo.getExcludeVersions(),
530 versionInfo.getExcludeVersionSize(),
531 & arInfos,
532 & cInfos);
534 if (plerr != JFW_PLUGIN_E_NONE)
535 continue;
536 //iterate over all installations to find the best which has
537 //all features
538 if (cInfos == 0)
540 rtl_freeMemory(arInfos);
541 continue;
543 bool bInfoFound = false;
544 for (int ii = 0; ii < cInfos; ii++)
546 JavaInfo* pJInfo = arInfos[ii];
548 //We remember the very first installation in aCurrentInfo
549 if (aCurrentInfo.getLocation().isEmpty())
550 aCurrentInfo = pJInfo;
551 // compare features
552 // If the user does not require any features (nFeatureFlags = 0)
553 // then the first installation is used
554 if ((pJInfo->nFeatures & nFeatureFlags) == nFeatureFlags)
556 //the just found Java implements all required features
557 //currently there is only accessibility!!!
558 aCurrentInfo = pJInfo;
559 bInfoFound = true;
560 break;
563 //The array returned by jfw_plugin_getAllJavaInfos must be freed as well as
564 //its contents
565 for (int j = 0; j < cInfos; j++)
566 jfw_freeJavaInfo(arInfos[j]);
567 rtl_freeMemory(arInfos);
569 if (bInfoFound == true)
570 break;
571 //All Java installations found by the current plug-in lib
572 //do not provide the required features. Try the next plug-in
574 if ((JavaInfo*) aCurrentInfo == NULL)
575 {//The plug-ins did not find a suitable Java. Now try the paths which have been
576 //added manually.
577 //get the list of paths to jre locations which have been added manually
578 const jfw::MergedSettings settings;
579 //node.loadFromSettings();
580 const std::vector<OUString> & vecJRELocations =
581 settings.getJRELocations();
582 //use every plug-in to determine the JavaInfo objects
583 bool bInfoFound = false;
584 for (ci_pl i = vecPlugins.begin(); i != vecPlugins.end(); ++i)
586 const jfw::PluginLibrary & library = *i;
587 jfw::VersionInfo versionInfo =
588 aVendorSettings.getVersionInformation(library.sVendor);
589 #ifndef DISABLE_DYNLOADING
590 osl::Module pluginLib(library.sPath);
591 if (pluginLib.is() == sal_False)
592 return JFW_E_NO_PLUGIN;
593 //Check if the current plugin can detect JREs at the location
594 // of the paths added by jfw_setJRELocations or jfw_addJRELocation
595 //get the function from the plugin
596 jfw_plugin_getJavaInfoByPath_ptr jfw_plugin_getJavaInfoByPathFunc =
597 (jfw_plugin_getJavaInfoByPath_ptr) pluginLib.getFunctionSymbol(
598 OUString("jfw_plugin_getJavaInfoByPath"));
599 #else
600 jfw_plugin_getJavaInfoByPath_ptr jfw_plugin_getJavaInfoByPathFunc =
601 jfw_plugin_getJavaInfoByPath;
602 #endif
603 OSL_ASSERT(jfw_plugin_getJavaInfoByPathFunc);
604 if (jfw_plugin_getJavaInfoByPathFunc == NULL)
605 return JFW_E_ERROR;
607 typedef std::vector<OUString>::const_iterator citLoc;
608 for (citLoc it = vecJRELocations.begin();
609 it != vecJRELocations.end(); ++it)
611 jfw::CJavaInfo aInfo;
612 javaPluginError err = (*jfw_plugin_getJavaInfoByPathFunc)(
613 it->pData,
614 library.sVendor.pData,
615 versionInfo.sMinVersion.pData,
616 versionInfo.sMaxVersion.pData,
617 versionInfo.getExcludeVersions(),
618 versionInfo.getExcludeVersionSize(),
619 & aInfo.pInfo);
620 if (err == JFW_PLUGIN_E_NO_JRE)
621 continue;
622 if (err == JFW_PLUGIN_E_FAILED_VERSION)
623 continue;
624 else if (err !=JFW_PLUGIN_E_NONE)
625 return JFW_E_ERROR;
627 if (aInfo)
629 //We remember the very first installation in aCurrentInfo
630 if (aCurrentInfo.getLocation().isEmpty())
631 aCurrentInfo = aInfo;
632 // compare features
633 // If the user does not require any features (nFeatureFlags = 0)
634 // then the first installation is used
635 if ((aInfo.getFeatures() & nFeatureFlags) == nFeatureFlags)
637 //the just found Java implements all required features
638 //currently there is only accessibility!!!
639 aCurrentInfo = aInfo;
640 bInfoFound = true;
641 break;
644 }//end iterate over paths
645 if (bInfoFound == true)
646 break;
647 }// end iterate plug-ins
649 if ((JavaInfo*) aCurrentInfo)
651 jfw::NodeJava javaNode(jfw::NodeJava::USER);
652 javaNode.setJavaInfo(aCurrentInfo,true);
653 javaNode.write();
654 //remember that this JRE was selected in this process
655 jfw::setJavaSelected();
657 if (pInfo !=NULL)
659 //copy to out param
660 *pInfo = aCurrentInfo.cloneJavaInfo();
663 else
665 errcode = JFW_E_NO_JAVA_FOUND;
668 catch (const jfw::FrameworkException& e)
670 errcode = e.errorCode;
671 fprintf(stderr, "%s\n", e.message.getStr());
672 OSL_FAIL(e.message.getStr());
675 return errcode;
678 sal_Bool SAL_CALL jfw_areEqualJavaInfo(
679 JavaInfo const * pInfoA,JavaInfo const * pInfoB)
681 if (pInfoA == pInfoB)
682 return sal_True;
683 if (pInfoA == NULL || pInfoB == NULL)
684 return sal_False;
685 OUString sVendor(pInfoA->sVendor);
686 OUString sLocation(pInfoA->sLocation);
687 OUString sVersion(pInfoA->sVersion);
688 rtl::ByteSequence sData(pInfoA->arVendorData);
689 if (sVendor.equals(pInfoB->sVendor) == sal_True
690 && sLocation.equals(pInfoB->sLocation) == sal_True
691 && sVersion.equals(pInfoB->sVersion) == sal_True
692 && pInfoA->nFeatures == pInfoB->nFeatures
693 && pInfoA->nRequirements == pInfoB->nRequirements
694 && sData == pInfoB->arVendorData)
696 return sal_True;
698 return sal_False;
702 void SAL_CALL jfw_freeJavaInfo(JavaInfo *pInfo)
704 if (pInfo == NULL)
705 return;
706 rtl_uString_release(pInfo->sVendor);
707 rtl_uString_release(pInfo->sLocation);
708 rtl_uString_release(pInfo->sVersion);
709 rtl_byte_sequence_release(pInfo->arVendorData);
710 rtl_freeMemory(pInfo);
713 javaFrameworkError SAL_CALL jfw_getSelectedJRE(JavaInfo **ppInfo)
715 javaFrameworkError errcode = JFW_E_NONE;
718 osl::MutexGuard guard(jfw::FwkMutex::get());
719 if (ppInfo == NULL)
720 return JFW_E_INVALID_ARG;
722 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
724 OUString sJRE = jfw::BootParams::getJREHome();
726 jfw::CJavaInfo aInfo;
727 if ((errcode = jfw_getJavaInfoByPath(sJRE.pData, & aInfo.pInfo))
728 != JFW_E_NONE)
729 throw jfw::FrameworkException(
730 JFW_E_CONFIGURATION,
731 OString(
732 "[Java framework] The JRE specified by the bootstrap "
733 "variable UNO_JAVA_JFW_JREHOME or UNO_JAVA_JFW_ENV_JREHOME "
734 " could not be recognized. Check the values and make sure that you "
735 "use a plug-in library that can recognize that JRE."));
737 *ppInfo = aInfo.detach();
738 return JFW_E_NONE;
741 const jfw::MergedSettings settings;
742 jfw::CJavaInfo aInfo;
743 aInfo.attach(settings.createJavaInfo());
744 if (! aInfo)
746 *ppInfo = NULL;
747 return JFW_E_NONE;
749 //If the javavendors.xml has changed, then the current selected
750 //Java is not valid anymore
751 // /java/javaInfo/@vendorUpdate != javaSelection/updated (javavendors.xml)
752 OString sUpdated = jfw::getElementUpdated();
754 if (sUpdated.equals(settings.getJavaInfoAttrVendorUpdate()) == sal_False)
755 return JFW_E_INVALID_SETTINGS;
756 *ppInfo = aInfo.detach();
758 catch (const jfw::FrameworkException& e)
760 errcode = e.errorCode;
761 fprintf(stderr, "%s\n", e.message.getStr());
762 OSL_FAIL(e.message.getStr());
764 return errcode;
767 javaFrameworkError SAL_CALL jfw_isVMRunning(sal_Bool *bRunning)
769 osl::MutexGuard guard(jfw::FwkMutex::get());
770 if (bRunning == NULL)
771 return JFW_E_INVALID_ARG;
772 if (g_pJavaVM == NULL)
773 *bRunning = sal_False;
774 else
775 *bRunning = sal_True;
776 return JFW_E_NONE;
779 javaFrameworkError SAL_CALL jfw_getJavaInfoByPath(
780 rtl_uString *pPath, JavaInfo **ppInfo)
782 javaFrameworkError errcode = JFW_E_NONE;
785 osl::MutexGuard guard(jfw::FwkMutex::get());
786 if (pPath == NULL || ppInfo == NULL)
787 return JFW_E_INVALID_ARG;
789 jfw::VendorSettings aVendorSettings;
790 //Get a list of plugins which provide Java information
791 std::vector<jfw::PluginLibrary> vecPlugins =
792 aVendorSettings.getPluginData();
793 #ifndef DISABLE_DYNLOADING
794 //Create a vector that holds the libraries, which will be later
795 //dynamically loaded;
796 boost::scoped_array<osl::Module> sarModules;
797 sarModules.reset(new osl::Module[vecPlugins.size()]);
798 osl::Module * arModules = sarModules.get();
799 #endif
800 typedef std::vector<OUString>::const_iterator CIT_VENDOR;
801 std::vector<OUString> vecVendors =
802 aVendorSettings.getSupportedVendors();
804 //Use every plug-in library to determine if the path represents a
805 //JRE. If a plugin recognized it then the loop will break
806 typedef std::vector<jfw::PluginLibrary>::const_iterator ci_pl;
807 int cModule = 0;
808 for (ci_pl i = vecPlugins.begin(); i != vecPlugins.end();
809 ++i, ++cModule)
811 const jfw::PluginLibrary & library = *i;
812 jfw::VersionInfo versionInfo =
813 aVendorSettings.getVersionInformation(library.sVendor);
815 #ifndef DISABLE_DYNLOADING
816 arModules[cModule].load(library.sPath);
817 osl::Module & pluginLib = arModules[cModule];
818 if (pluginLib.is() == sal_False)
820 OString msg = OUStringToOString(
821 library.sPath, osl_getThreadTextEncoding());
822 fprintf(stderr,"[jvmfwk] Could not load plugin %s\n" \
823 "Modify the javavendors.xml accordingly!\n", msg.getStr());
824 return JFW_E_NO_PLUGIN;
827 jfw_plugin_getJavaInfoByPath_ptr jfw_plugin_getJavaInfoByPathFunc =
828 (jfw_plugin_getJavaInfoByPath_ptr) pluginLib.getFunctionSymbol(
829 OUString("jfw_plugin_getJavaInfoByPath"));
830 #else
831 jfw_plugin_getJavaInfoByPath_ptr jfw_plugin_getJavaInfoByPathFunc =
832 jfw_plugin_getJavaInfoByPath;
833 #endif
834 OSL_ASSERT(jfw_plugin_getJavaInfoByPathFunc);
835 if (jfw_plugin_getJavaInfoByPathFunc == NULL)
836 continue;
838 //ask the plugin if this is a JRE.
839 //If so check if it meets the version requirements.
840 //Only if it does return a JavaInfo
841 JavaInfo* pInfo = NULL;
842 javaPluginError plerr = (*jfw_plugin_getJavaInfoByPathFunc)(
843 pPath,
844 library.sVendor.pData,
845 versionInfo.sMinVersion.pData,
846 versionInfo.sMaxVersion.pData,
847 versionInfo.getExcludeVersions(),
848 versionInfo.getExcludeVersionSize(),
849 & pInfo);
851 if (plerr == JFW_PLUGIN_E_NONE)
853 //check if the vendor of the found JRE is supported
854 if (vecVendors.empty())
856 //vendor does not matter
857 *ppInfo = pInfo;
858 break;
860 else
862 OUString sVendor(pInfo->sVendor);
863 CIT_VENDOR ivendor = std::find(vecVendors.begin(), vecVendors.end(),
864 sVendor);
865 if (ivendor != vecVendors.end())
867 *ppInfo = pInfo;
869 else
871 *ppInfo = NULL;
872 errcode = JFW_E_NOT_RECOGNIZED;
874 break;
877 else if(plerr == JFW_PLUGIN_E_FAILED_VERSION)
878 {//found JRE but it has the wrong version
879 *ppInfo = NULL;
880 errcode = JFW_E_FAILED_VERSION;
881 break;
883 else if (plerr == JFW_PLUGIN_E_NO_JRE)
884 {// plugin does not recognize this path as belonging to JRE
885 continue;
887 OSL_ASSERT(0);
889 if (*ppInfo == NULL && errcode != JFW_E_FAILED_VERSION)
890 errcode = JFW_E_NOT_RECOGNIZED;
892 catch (const jfw::FrameworkException& e)
894 errcode = e.errorCode;
895 fprintf(stderr, "%s\n", e.message.getStr());
896 OSL_FAIL(e.message.getStr());
899 return errcode;
903 javaFrameworkError SAL_CALL jfw_setSelectedJRE(JavaInfo const *pInfo)
905 javaFrameworkError errcode = JFW_E_NONE;
908 osl::MutexGuard guard(jfw::FwkMutex::get());
909 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
910 return JFW_E_DIRECT_MODE;
911 //check if pInfo is the selected JRE
912 JavaInfo *currentInfo = NULL;
913 errcode = jfw_getSelectedJRE( & currentInfo);
914 if (errcode != JFW_E_NONE && errcode != JFW_E_INVALID_SETTINGS)
915 return errcode;
917 if (jfw_areEqualJavaInfo(currentInfo, pInfo) == sal_False)
919 jfw::NodeJava node(jfw::NodeJava::USER);
920 node.setJavaInfo(pInfo, false);
921 node.write();
922 //remember that the JRE was selected in this process
923 jfw::setJavaSelected();
926 catch (const jfw::FrameworkException& e)
928 errcode = e.errorCode;
929 fprintf(stderr, "%s\n", e.message.getStr());
930 OSL_FAIL(e.message.getStr());
932 return errcode;
934 javaFrameworkError SAL_CALL jfw_setEnabled(sal_Bool bEnabled)
936 javaFrameworkError errcode = JFW_E_NONE;
939 osl::MutexGuard guard(jfw::FwkMutex::get());
940 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
941 return JFW_E_DIRECT_MODE;
943 if (g_bEnabledSwitchedOn == false && bEnabled == sal_True)
945 //When the process started then Enabled was false.
946 //This is first time enabled is set to true.
947 //That means, no preparational work has been done, such as setting the
948 //LD_LIBRARY_PATH, etc.
950 //check if Enabled is false;
951 const jfw::MergedSettings settings;
952 if (settings.getEnabled() == sal_False)
953 g_bEnabledSwitchedOn = true;
955 jfw::NodeJava node(jfw::NodeJava::USER);
956 node.setEnabled(bEnabled);
957 node.write();
959 catch (const jfw::FrameworkException& e)
961 errcode = e.errorCode;
962 fprintf(stderr, "%s\n", e.message.getStr());
963 OSL_FAIL(e.message.getStr());
965 return errcode;
968 javaFrameworkError SAL_CALL jfw_getEnabled(sal_Bool *pbEnabled)
970 javaFrameworkError errcode = JFW_E_NONE;
973 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
974 return JFW_E_DIRECT_MODE;
975 osl::MutexGuard guard(jfw::FwkMutex::get());
976 if (pbEnabled == NULL)
977 return JFW_E_INVALID_ARG;
978 jfw::MergedSettings settings;
979 *pbEnabled = settings.getEnabled();
981 catch (const jfw::FrameworkException& e)
983 errcode = e.errorCode;
984 fprintf(stderr, "%s\n", e.message.getStr());
985 OSL_FAIL(e.message.getStr());
987 return errcode;
991 javaFrameworkError SAL_CALL jfw_setVMParameters(
992 rtl_uString * * arOptions, sal_Int32 nLen)
994 javaFrameworkError errcode = JFW_E_NONE;
997 osl::MutexGuard guard(jfw::FwkMutex::get());
998 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
999 return JFW_E_DIRECT_MODE;
1000 jfw::NodeJava node(jfw::NodeJava::USER);
1001 if (arOptions == NULL && nLen != 0)
1002 return JFW_E_INVALID_ARG;
1003 node.setVmParameters(arOptions, nLen);
1004 node.write();
1006 catch (const jfw::FrameworkException& e)
1008 errcode = e.errorCode;
1009 fprintf(stderr, "%s\n", e.message.getStr());
1010 OSL_FAIL(e.message.getStr());
1013 return errcode;
1016 javaFrameworkError SAL_CALL jfw_getVMParameters(
1017 rtl_uString *** parOptions, sal_Int32 * pLen)
1019 javaFrameworkError errcode = JFW_E_NONE;
1022 osl::MutexGuard guard(jfw::FwkMutex::get());
1023 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
1024 return JFW_E_DIRECT_MODE;
1026 if (parOptions == NULL || pLen == NULL)
1027 return JFW_E_INVALID_ARG;
1028 const jfw::MergedSettings settings;
1029 settings.getVmParametersArray(parOptions, pLen);
1031 catch (const jfw::FrameworkException& e)
1033 errcode = e.errorCode;
1034 fprintf(stderr, "%s\n", e.message.getStr());
1035 OSL_FAIL(e.message.getStr());
1037 return errcode;
1040 javaFrameworkError SAL_CALL jfw_setUserClassPath(rtl_uString * pCp)
1042 javaFrameworkError errcode = JFW_E_NONE;
1045 osl::MutexGuard guard(jfw::FwkMutex::get());
1046 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
1047 return JFW_E_DIRECT_MODE;
1048 jfw::NodeJava node(jfw::NodeJava::USER);
1049 if (pCp == NULL)
1050 return JFW_E_INVALID_ARG;
1051 node.setUserClassPath(pCp);
1052 node.write();
1054 catch (const jfw::FrameworkException& e)
1056 errcode = e.errorCode;
1057 fprintf(stderr, "%s\n", e.message.getStr());
1058 OSL_FAIL(e.message.getStr());
1060 return errcode;
1063 javaFrameworkError SAL_CALL jfw_getUserClassPath(rtl_uString ** ppCP)
1065 javaFrameworkError errcode = JFW_E_NONE;
1068 osl::MutexGuard guard(jfw::FwkMutex::get());
1069 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
1070 return JFW_E_DIRECT_MODE;
1071 if (ppCP == NULL)
1072 return JFW_E_INVALID_ARG;
1073 const jfw::MergedSettings settings;
1074 *ppCP = settings.getUserClassPath().pData;
1075 rtl_uString_acquire(*ppCP);
1077 catch (const jfw::FrameworkException& e)
1079 errcode = e.errorCode;
1080 fprintf(stderr, "%s\n", e.message.getStr());
1081 OSL_FAIL(e.message.getStr());
1083 return errcode;
1086 javaFrameworkError SAL_CALL jfw_addJRELocation(rtl_uString * sLocation)
1088 javaFrameworkError errcode = JFW_E_NONE;
1091 osl::MutexGuard guard(jfw::FwkMutex::get());
1092 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
1093 return JFW_E_DIRECT_MODE;
1094 jfw::NodeJava node(jfw::NodeJava::USER);
1095 if (sLocation == NULL)
1096 return JFW_E_INVALID_ARG;
1097 node.load();
1098 node.addJRELocation(sLocation);
1099 node.write();
1101 catch (const jfw::FrameworkException& e)
1103 errcode = e.errorCode;
1104 fprintf(stderr, "%s\n", e.message.getStr());
1105 OSL_FAIL(e.message.getStr());
1108 return errcode;
1112 javaFrameworkError SAL_CALL jfw_setJRELocations(
1113 rtl_uString ** arLocations, sal_Int32 nLen)
1115 javaFrameworkError errcode = JFW_E_NONE;
1118 osl::MutexGuard guard(jfw::FwkMutex::get());
1119 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
1120 return JFW_E_DIRECT_MODE;
1121 jfw::NodeJava node(jfw::NodeJava::USER);
1122 if (arLocations == NULL && nLen != 0)
1123 return JFW_E_INVALID_ARG;
1124 node.setJRELocations(arLocations, nLen);
1125 node.write();
1127 catch (const jfw::FrameworkException& e)
1129 errcode = e.errorCode;
1130 fprintf(stderr, "%s\n", e.message.getStr());
1131 OSL_FAIL(e.message.getStr());
1133 return errcode;
1137 javaFrameworkError SAL_CALL jfw_getJRELocations(
1138 rtl_uString *** parLocations, sal_Int32 *pLen)
1140 javaFrameworkError errcode = JFW_E_NONE;
1143 osl::MutexGuard guard(jfw::FwkMutex::get());
1144 if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
1145 return JFW_E_DIRECT_MODE;
1147 if (parLocations == NULL || pLen == NULL)
1148 return JFW_E_INVALID_ARG;
1149 const jfw::MergedSettings settings;
1150 settings.getJRELocations(parLocations, pLen);
1152 catch (const jfw::FrameworkException& e)
1154 errcode = e.errorCode;
1155 fprintf(stderr, "%s\n", e.message.getStr());
1156 OSL_FAIL(e.message.getStr());
1159 return errcode;
1163 javaFrameworkError jfw_existJRE(const JavaInfo *pInfo, sal_Bool *exist)
1165 //get the function jfw_plugin_existJRE
1166 jfw::VendorSettings aVendorSettings;
1167 jfw::CJavaInfo aInfo;
1168 aInfo = (const ::JavaInfo*) pInfo; //makes a copy of pInfo
1169 #ifndef DISABLE_DYNLOADING
1170 OUString sLibPath = aVendorSettings.getPluginLibrary(aInfo.getVendor());
1171 osl::Module modulePlugin(sLibPath);
1172 if ( ! modulePlugin)
1173 return JFW_E_NO_PLUGIN;
1174 OUString sFunctionName("jfw_plugin_existJRE");
1175 jfw_plugin_existJRE_ptr pFunc =
1176 (jfw_plugin_existJRE_ptr)
1177 osl_getFunctionSymbol(modulePlugin, sFunctionName.pData);
1178 if (pFunc == NULL)
1179 return JFW_E_ERROR;
1180 #else
1181 jfw_plugin_existJRE_ptr pFunc =
1182 jfw_plugin_existJRE;
1183 #endif
1184 javaPluginError plerr = (*pFunc)(pInfo, exist);
1186 javaFrameworkError ret = JFW_E_NONE;
1187 switch (plerr)
1189 case JFW_PLUGIN_E_NONE:
1190 ret = JFW_E_NONE;
1191 break;
1192 case JFW_PLUGIN_E_INVALID_ARG:
1193 ret = JFW_E_INVALID_ARG;
1194 break;
1195 case JFW_PLUGIN_E_ERROR:
1196 ret = JFW_E_ERROR;
1197 break;
1198 default:
1199 ret = JFW_E_ERROR;
1201 return ret;
1204 void SAL_CALL jfw_lock()
1206 jfw::FwkMutex::get().acquire();
1209 void SAL_CALL jfw_unlock()
1211 jfw::FwkMutex::get().release();
1215 namespace jfw
1217 CJavaInfo::CJavaInfo(): pInfo(0)
1221 CJavaInfo::CJavaInfo(const CJavaInfo & info)
1223 pInfo = copyJavaInfo(info.pInfo);
1226 CJavaInfo::CJavaInfo(::JavaInfo * info, _transfer_ownership)
1228 pInfo = info;
1230 CJavaInfo CJavaInfo::createWrapper(::JavaInfo* info)
1232 return CJavaInfo(info, TRANSFER);
1234 void CJavaInfo::attach(::JavaInfo * info)
1236 jfw_freeJavaInfo(pInfo);
1237 pInfo = info;
1239 ::JavaInfo * CJavaInfo::detach()
1241 JavaInfo * tmp = pInfo;
1242 pInfo = NULL;
1243 return tmp;
1246 CJavaInfo::~CJavaInfo()
1248 jfw_freeJavaInfo(pInfo);
1251 CJavaInfo::operator ::JavaInfo* ()
1253 return pInfo;
1256 JavaInfo * CJavaInfo::copyJavaInfo(const JavaInfo * pInfo)
1258 if (pInfo == NULL)
1259 return NULL;
1260 JavaInfo* newInfo =
1261 (JavaInfo*) rtl_allocateMemory(sizeof(JavaInfo));
1262 if (newInfo)
1264 memcpy(newInfo, pInfo, sizeof(JavaInfo));
1265 rtl_uString_acquire(pInfo->sVendor);
1266 rtl_uString_acquire(pInfo->sLocation);
1267 rtl_uString_acquire(pInfo->sVersion);
1268 rtl_byte_sequence_acquire(pInfo->arVendorData);
1270 return newInfo;
1274 JavaInfo* CJavaInfo::cloneJavaInfo() const
1276 if (pInfo == NULL)
1277 return NULL;
1278 return copyJavaInfo(pInfo);
1281 CJavaInfo & CJavaInfo::operator = (const CJavaInfo& info)
1283 if (&info == this)
1284 return *this;
1286 jfw_freeJavaInfo(pInfo);
1287 pInfo = copyJavaInfo(info.pInfo);
1288 return *this;
1290 CJavaInfo & CJavaInfo::operator = (const ::JavaInfo* info)
1292 if (info == pInfo)
1293 return *this;
1295 jfw_freeJavaInfo(pInfo);
1296 pInfo = copyJavaInfo(info);
1297 return *this;
1300 const ::JavaInfo* CJavaInfo::operator ->() const
1302 return pInfo;
1305 CJavaInfo::operator JavaInfo const * () const
1307 return pInfo;
1310 OUString CJavaInfo::getVendor() const
1312 if (pInfo)
1313 return OUString(pInfo->sVendor);
1314 else
1315 return OUString();
1318 OUString CJavaInfo::getLocation() const
1320 if (pInfo)
1321 return OUString(pInfo->sLocation);
1322 else
1323 return OUString();
1326 sal_uInt64 CJavaInfo::getFeatures() const
1328 if (pInfo)
1329 return pInfo->nFeatures;
1330 else
1331 return 0l;
1336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */