1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
32 #include "framework.hxx"
33 #include "fwkutil.hxx"
34 #include "elements.hxx"
35 #include "fwkbase.hxx"
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
54 javaPluginError
jfw_plugin_getAllJavaInfos(
56 rtl_uString
*sMinVersion
,
57 rtl_uString
*sMaxVersion
,
58 rtl_uString
* *arExcludeList
,
60 JavaInfo
*** parJavaInfo
,
61 sal_Int32
*nLenInfoList
);
64 javaPluginError
jfw_plugin_getJavaInfoByPath(
67 rtl_uString
*sMinVersion
,
68 rtl_uString
*sMaxVersion
,
69 rtl_uString
* *arExcludeList
,
74 javaPluginError
jfw_plugin_startJavaVirtualMachine(
75 const JavaInfo
*pInfo
,
76 const JavaVMOption
* arOptions
,
82 javaPluginError
jfw_plugin_existJRE(const JavaInfo
*pInfo
, sal_Bool
*exist
);
86 javaFrameworkError SAL_CALL
jfw_findAllJREs(JavaInfo
***pparInfo
, sal_Int32
*pSize
)
88 javaFrameworkError retVal
= JFW_E_NONE
;
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();
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
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
;
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"));
145 jfw_plugin_getAllJavaInfos_ptr getAllJavaFunc
=
146 jfw_plugin_getAllJavaInfos
;
148 OSL_ASSERT(getAllJavaFunc
);
149 if (getAllJavaFunc
== NULL
)
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(),
165 if (plerr
!= JFW_PLUGIN_E_NONE
)
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
)
184 jfw_plugin_getJavaInfoByPath_ptr jfw_plugin_getJavaInfoByPathFunc
=
185 jfw_plugin_getJavaInfoByPath
;
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
)(
196 library
.sVendor
.pData
,
197 versionInfo
.sMinVersion
.pData
,
198 versionInfo
.sMaxVersion
.pData
,
199 versionInfo
.getExcludeVersions(),
200 versionInfo
.getExcludeVersionSize(),
202 if (plerr
== JFW_PLUGIN_E_NO_JRE
)
204 if (plerr
== JFW_PLUGIN_E_FAILED_VERSION
)
206 else if (plerr
!=JFW_PLUGIN_E_NONE
)
211 //Was this JRE already added?. Different plugins could detect
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
);
237 //Check which JavaInfo from vector vecInfoManual2 is already
238 //contained in vecInfo. If it already exists then remove it from
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
)
255 typedef std::vector
<jfw::CJavaInfo
>::iterator it
;
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();
267 catch (const jfw::FrameworkException
& e
)
269 retVal
= e
.errorCode
;
270 fprintf(stderr
, "%s\n", e
.message
.getStr());
271 OSL_FAIL(e
.message
.getStr());
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
290 if (g_pJavaVM
!= NULL
)
291 return JFW_E_RUNNING_JVM
;
294 return JFW_E_INVALID_ARG
;
296 std::vector
<OString
> vmParams
;
297 OString sUserClassPath
;
298 jfw::CJavaInfo aInfo
;
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
310 return JFW_E_NO_SELECT
;
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
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
;
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
)
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();
371 "-Djava.class.path=" + jfw::BootParams::getClasspath();
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
);
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
);
395 jfw_plugin_startJavaVirtualMachine_ptr pFunc
=
396 jfw_plugin_startJavaVirtualMachine
;
399 // create JavaVMOptions array that is passed to the plugin
400 // it contains the classpath and all options set in the
402 boost::scoped_array
<JavaVMOption
> sarJOptions(
403 new JavaVMOption
[cOptions
+ 2 + vmParams
.size()]);
404 JavaVMOption
* arOpt
= sarJOptions
.get();
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
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;
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
;
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
;
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());
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
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();
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
;
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"));
514 jfw_plugin_getAllJavaInfos_ptr getAllJavaFunc
=
515 jfw_plugin_getAllJavaInfos
;
517 OSL_ASSERT(getAllJavaFunc
);
518 if (getAllJavaFunc
== NULL
)
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(),
534 if (plerr
!= JFW_PLUGIN_E_NONE
)
536 //iterate over all installations to find the best which has
540 rtl_freeMemory(arInfos
);
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
;
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
;
563 //The array returned by jfw_plugin_getAllJavaInfos must be freed as well as
565 for (int j
= 0; j
< cInfos
; j
++)
566 jfw_freeJavaInfo(arInfos
[j
]);
567 rtl_freeMemory(arInfos
);
569 if (bInfoFound
== true)
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
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"));
600 jfw_plugin_getJavaInfoByPath_ptr jfw_plugin_getJavaInfoByPathFunc
=
601 jfw_plugin_getJavaInfoByPath
;
603 OSL_ASSERT(jfw_plugin_getJavaInfoByPathFunc
);
604 if (jfw_plugin_getJavaInfoByPathFunc
== NULL
)
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
)(
614 library
.sVendor
.pData
,
615 versionInfo
.sMinVersion
.pData
,
616 versionInfo
.sMaxVersion
.pData
,
617 versionInfo
.getExcludeVersions(),
618 versionInfo
.getExcludeVersionSize(),
620 if (err
== JFW_PLUGIN_E_NO_JRE
)
622 if (err
== JFW_PLUGIN_E_FAILED_VERSION
)
624 else if (err
!=JFW_PLUGIN_E_NONE
)
629 //We remember the very first installation in aCurrentInfo
630 if (aCurrentInfo
.getLocation().isEmpty())
631 aCurrentInfo
= aInfo
;
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
;
644 }//end iterate over paths
645 if (bInfoFound
== true)
647 }// end iterate plug-ins
649 if ((JavaInfo
*) aCurrentInfo
)
651 jfw::NodeJava
javaNode(jfw::NodeJava::USER
);
652 javaNode
.setJavaInfo(aCurrentInfo
,true);
654 //remember that this JRE was selected in this process
655 jfw::setJavaSelected();
660 *pInfo
= aCurrentInfo
.cloneJavaInfo();
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());
678 sal_Bool SAL_CALL
jfw_areEqualJavaInfo(
679 JavaInfo
const * pInfoA
,JavaInfo
const * pInfoB
)
681 if (pInfoA
== pInfoB
)
683 if (pInfoA
== NULL
|| pInfoB
== NULL
)
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
)
702 void SAL_CALL
jfw_freeJavaInfo(JavaInfo
*pInfo
)
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());
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
))
729 throw jfw::FrameworkException(
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();
741 const jfw::MergedSettings settings
;
742 jfw::CJavaInfo aInfo
;
743 aInfo
.attach(settings
.createJavaInfo());
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());
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
;
775 *bRunning
= sal_True
;
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();
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
;
808 for (ci_pl i
= vecPlugins
.begin(); i
!= vecPlugins
.end();
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"));
831 jfw_plugin_getJavaInfoByPath_ptr jfw_plugin_getJavaInfoByPathFunc
=
832 jfw_plugin_getJavaInfoByPath
;
834 OSL_ASSERT(jfw_plugin_getJavaInfoByPathFunc
);
835 if (jfw_plugin_getJavaInfoByPathFunc
== NULL
)
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
)(
844 library
.sVendor
.pData
,
845 versionInfo
.sMinVersion
.pData
,
846 versionInfo
.sMaxVersion
.pData
,
847 versionInfo
.getExcludeVersions(),
848 versionInfo
.getExcludeVersionSize(),
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
862 OUString
sVendor(pInfo
->sVendor
);
863 CIT_VENDOR ivendor
= std::find(vecVendors
.begin(), vecVendors
.end(),
865 if (ivendor
!= vecVendors
.end())
872 errcode
= JFW_E_NOT_RECOGNIZED
;
877 else if(plerr
== JFW_PLUGIN_E_FAILED_VERSION
)
878 {//found JRE but it has the wrong version
880 errcode
= JFW_E_FAILED_VERSION
;
883 else if (plerr
== JFW_PLUGIN_E_NO_JRE
)
884 {// plugin does not recognize this path as belonging to JRE
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());
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
)
917 if (jfw_areEqualJavaInfo(currentInfo
, pInfo
) == sal_False
)
919 jfw::NodeJava
node(jfw::NodeJava::USER
);
920 node
.setJavaInfo(pInfo
, false);
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());
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
);
959 catch (const jfw::FrameworkException
& e
)
961 errcode
= e
.errorCode
;
962 fprintf(stderr
, "%s\n", e
.message
.getStr());
963 OSL_FAIL(e
.message
.getStr());
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());
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
);
1006 catch (const jfw::FrameworkException
& e
)
1008 errcode
= e
.errorCode
;
1009 fprintf(stderr
, "%s\n", e
.message
.getStr());
1010 OSL_FAIL(e
.message
.getStr());
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());
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
);
1050 return JFW_E_INVALID_ARG
;
1051 node
.setUserClassPath(pCp
);
1054 catch (const jfw::FrameworkException
& e
)
1056 errcode
= e
.errorCode
;
1057 fprintf(stderr
, "%s\n", e
.message
.getStr());
1058 OSL_FAIL(e
.message
.getStr());
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
;
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());
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
;
1098 node
.addJRELocation(sLocation
);
1101 catch (const jfw::FrameworkException
& e
)
1103 errcode
= e
.errorCode
;
1104 fprintf(stderr
, "%s\n", e
.message
.getStr());
1105 OSL_FAIL(e
.message
.getStr());
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
);
1127 catch (const jfw::FrameworkException
& e
)
1129 errcode
= e
.errorCode
;
1130 fprintf(stderr
, "%s\n", e
.message
.getStr());
1131 OSL_FAIL(e
.message
.getStr());
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());
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
);
1181 jfw_plugin_existJRE_ptr pFunc
=
1182 jfw_plugin_existJRE
;
1184 javaPluginError plerr
= (*pFunc
)(pInfo
, exist
);
1186 javaFrameworkError ret
= JFW_E_NONE
;
1189 case JFW_PLUGIN_E_NONE
:
1192 case JFW_PLUGIN_E_INVALID_ARG
:
1193 ret
= JFW_E_INVALID_ARG
;
1195 case JFW_PLUGIN_E_ERROR
:
1204 void SAL_CALL
jfw_lock()
1206 jfw::FwkMutex::get().acquire();
1209 void SAL_CALL
jfw_unlock()
1211 jfw::FwkMutex::get().release();
1217 CJavaInfo::CJavaInfo(): pInfo(0)
1221 CJavaInfo::CJavaInfo(const CJavaInfo
& info
)
1223 pInfo
= copyJavaInfo(info
.pInfo
);
1226 CJavaInfo::CJavaInfo(::JavaInfo
* info
, _transfer_ownership
)
1230 CJavaInfo
CJavaInfo::createWrapper(::JavaInfo
* info
)
1232 return CJavaInfo(info
, TRANSFER
);
1234 void CJavaInfo::attach(::JavaInfo
* info
)
1236 jfw_freeJavaInfo(pInfo
);
1239 ::JavaInfo
* CJavaInfo::detach()
1241 JavaInfo
* tmp
= pInfo
;
1246 CJavaInfo::~CJavaInfo()
1248 jfw_freeJavaInfo(pInfo
);
1251 CJavaInfo::operator ::JavaInfo
* ()
1256 JavaInfo
* CJavaInfo::copyJavaInfo(const JavaInfo
* pInfo
)
1261 (JavaInfo
*) rtl_allocateMemory(sizeof(JavaInfo
));
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
);
1274 JavaInfo
* CJavaInfo::cloneJavaInfo() const
1278 return copyJavaInfo(pInfo
);
1281 CJavaInfo
& CJavaInfo::operator = (const CJavaInfo
& info
)
1286 jfw_freeJavaInfo(pInfo
);
1287 pInfo
= copyJavaInfo(info
.pInfo
);
1290 CJavaInfo
& CJavaInfo::operator = (const ::JavaInfo
* info
)
1295 jfw_freeJavaInfo(pInfo
);
1296 pInfo
= copyJavaInfo(info
);
1300 const ::JavaInfo
* CJavaInfo::operator ->() const
1305 CJavaInfo::operator JavaInfo
const * () const
1310 OUString
CJavaInfo::getVendor() const
1313 return OUString(pInfo
->sVendor
);
1318 OUString
CJavaInfo::getLocation() const
1321 return OUString(pInfo
->sLocation
);
1326 sal_uInt64
CJavaInfo::getFeatures() const
1329 return pInfo
->nFeatures
;
1336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */