//Use all plug-in libraries to get Java installations.
std::vector<std::unique_ptr<JavaInfo>> arInfos;
std::vector<rtl::Reference<jfw_plugin::VendorBase>> infos;
javaPluginError plerr = jfw_plugin_getAllJavaInfos( true,
aVendorSettings,
& arInfos,
infos);
if (plerr != javaPluginError::NONE) return JFW_E_ERROR;
for (auto & j: arInfos)
vecInfo.push_back(std::move(j));
// direct mode disregards Java settings, so only retrieve // JREs from settings when application mode is used if (jfw::getMode() == jfw::JFW_MODE_APPLICATION)
{ //get the list of paths to jre locations which have been //added manually const jfw::MergedSettings settings; const std::vector<OUString>& vecJRELocations =
settings.getJRELocations(); //Check if any plugin can detect JREs at the location // of the paths added by jfw_addJRELocation //Check every manually added location for (autoconst & ii: vecJRELocations)
{
std::unique_ptr<JavaInfo> aInfo;
plerr = jfw_plugin_getJavaInfoByPath(
ii,
aVendorSettings,
&aInfo); if (plerr == javaPluginError::NoJre) continue; if (plerr == javaPluginError::FailedVersion) continue; if (plerr == javaPluginError::WrongArch) continue; elseif (plerr != javaPluginError::NONE) return JFW_E_ERROR;
// Was this JRE already added? if (std::none_of(
vecInfo.begin(), vecInfo.end(),
[&aInfo](std::unique_ptr<JavaInfo> const & info) { return areEqualJavaInfo(
info.get(), aInfo.get());
}))
{
vecInfo.push_back(std::move(aInfo));
}
}
}
//We keep this pointer so we can determine if a VM has already //been created. if (g_pJavaVM != nullptr) return JFW_E_RUNNING_JVM;
std::vector<OString> vmParams;
OString sUserClassPath;
std::unique_ptr<JavaInfo> aInfo; if (pInfo == nullptr)
{
jfw::JFW_MODE mode = jfw::getMode(); if (mode == jfw::JFW_MODE_APPLICATION)
{ const jfw::MergedSettings settings; if (!settings.getEnabled()) return JFW_E_JAVA_DISABLED;
aInfo = settings.createJavaInfo(); //check if a Java has ever been selected if (!aInfo) return JFW_E_NO_SELECT;
//check if the javavendors.xml has changed after a Java was selected
OString sVendorUpdate = jfw::getElementUpdated();
if (sVendorUpdate != settings.getJavaInfoAttrVendorUpdate()) return JFW_E_INVALID_SETTINGS;
//check if JAVA is disabled //If Java is enabled, but it was disabled when this process was started // then no preparational work, such as setting the LD_LIBRARY_PATH, was //done. Therefore if a JRE needs it, it must not be started. if (g_bEnabledSwitchedOn &&
(aInfo->nRequirements & JFW_REQUIRE_NEEDRESTART)) return JFW_E_NEED_RESTART;
//Check if the selected Java was set in this process. If so it //must not have the requirements flag JFW_REQUIRE_NEEDRESTART if ((aInfo->nRequirements & JFW_REQUIRE_NEEDRESTART)
&& jfw::wasJavaSelectedInSameProcess()) return JFW_E_NEED_RESTART;
vmParams = settings.getVmParametersUtf8(); // Expand user classpath (might contain bootstrap vars) const OUString& sUserPath(settings.getUserClassPath());
std::vector paths = jfw_convertUserPathList(sUserPath);
OUString sUserPathExpanded; for (auto& path : paths)
{ if (!sUserPathExpanded.isEmpty())
sUserPathExpanded += OUStringChar(SAL_PATHSEPARATOR); if (path.startsWith("$"))
{
OUString sURL = path;
rtl::Bootstrap::expandMacros(sURL);
osl::FileBase::getSystemPathFromFileURL(sURL, path);
}
sUserPathExpanded += path;
}
sUserClassPath = jfw::makeClassPathOption(sUserPathExpanded);
} // end mode FWK_MODE_OFFICE elseif (mode == jfw::JFW_MODE_DIRECT)
{
errcode = jfw_getSelectedJRE(&aInfo); if (errcode != JFW_E_NONE) return errcode; //In direct mode the options are specified by bootstrap variables //of the form UNO_JAVA_JFW_PARAMETER_1 .. UNO_JAVA_JFW_PARAMETER_n
vmParams = jfw::BootParams::getVMParameters(); autoconst cp = jfw::BootParams::getClasspath(); if (!cp.isEmpty())
{
sUserClassPath = "-Djava.class.path=" + cp;
}
} else
OSL_ASSERT(false);
pInfo = aInfo.get();
}
assert(pInfo != nullptr);
#ifdef _WIN32 // Alternative JREs (AdoptOpenJDK, Azul Zulu) are missing the bin/ folder in // java.library.path. Somehow setting java.library.path accordingly doesn't work, // but the PATH gets picked up, so add it there. // Without this hack, some features don't work in alternative JREs.
OUString sPATH;
osl_getEnvironment(OUString("PATH").pData, &sPATH.pData);
OUString sJRELocation;
osl::FileBase::getSystemPathFromFileURL(pInfo->sLocation + "/bin", sJRELocation); if (sPATH.isEmpty())
sPATH = sJRELocation; else
sPATH = sJRELocation + OUStringChar(SAL_PATHSEPARATOR) + sPATH;
osl_setEnvironment(OUString("PATH").pData, sPATH.pData); #endif// _WIN32
// create JavaVMOptions array that is passed to the plugin // it contains the classpath and all options set in the //options dialog
std::unique_ptr<JavaVMOption[]> sarJOptions( new JavaVMOption[
arOptions.size() + (sUserClassPath.isEmpty() ? 2 : 3) + vmParams.size()]);
JavaVMOption * arOpt = sarJOptions.get(); if (! arOpt) return JFW_E_ERROR;
//The first argument is the classpath int index = 0; if (!sUserClassPath.isEmpty()) {
arOpt[index].optionString= const_cast<char*>(sUserClassPath.getStr());
arOpt[index].extraInfo = nullptr;
++index;
} // Set a flag that this JVM has been created via the JNI Invocation API // (used, for example, by UNO remote bridges to share a common thread pool // factory among Java and native bridge implementations):
arOpt[index].optionString = const_cast<char *>("-Dorg.openoffice.native=");
arOpt[index].extraInfo = nullptr;
++index;
//add the options set by options dialog for (autoconst & vmParam : vmParams)
{
arOpt[index].optionString = const_cast<char*>(vmParam.getStr());
arOpt[index].extraInfo = nullptr;
index ++;
} //add all options of the arOptions argument
std::vector<OString> convertedOptions; for (autoconst & ii: arOptions)
{
OString conv = OUStringToOString(ii, osl_getThreadTextEncoding());
convertedOptions.push_back(conv); // keep conv.getStr() alive until after the call to // jfw_plugin_startJavaVirtualMachine below
arOpt[index].optionString = const_cast<char *>(conv.getStr());
arOpt[index].extraInfo = nullptr;
index++;
}
/** We do not use here jfw_findAllJREs and then check if a JavaInfo meetstherequirements,becausethatmeansusingallplug-ins,which maytakequiteawhile.TheimplementationfirstinspectsJAVA_HOMEand PATHenvironmentvariables.IfnosuitableJavaInfoisfoundthere,it inspectsallJavaInfosfoundbythejfw_plugin_get*functions.
*/
javaFrameworkError jfw_findAndSelectJRE(std::unique_ptr<JavaInfo> *pInfo)
{
javaFrameworkError errcode = JFW_E_NONE; try
{
osl::MutexGuard guard(jfw::FwkMutex()); if (jfw::getMode() == jfw::JFW_MODE_DIRECT) return JFW_E_DIRECT_MODE;
std::unique_ptr<JavaInfo> aCurrentInfo;
// 'bInfoFound' indicates whether a Java installation has been found bool bInfoFound = false;
// get list of vendors for Java installations
jfw::VendorSettings aVendorSettings;
// first inspect Java installation that the JAVA_HOME // environment variable points to (if it is set) if (jfw_plugin_getJavaInfoFromJavaHome(
aVendorSettings, &aCurrentInfo, infos)
== javaPluginError::NONE)
{
bInfoFound = true;
}
// if no Java installation was detected by using JAVA_HOME, // query PATH for Java installations if (!bInfoFound)
{
std::vector<std::unique_ptr<JavaInfo>> vecJavaInfosFromPath; if (jfw_plugin_getJavaInfosFromPath(
aVendorSettings, vecJavaInfosFromPath, infos)
== javaPluginError::NONE)
{
assert(!vecJavaInfosFromPath.empty());
aCurrentInfo = std::move(vecJavaInfosFromPath[0]);
bInfoFound = true;
}
}
// if no suitable Java installation has been found yet: // first use jfw_plugin_getAllJavaInfos to find a suitable Java installation, // then try paths that have been added manually if (!bInfoFound)
{ //get all installations
std::vector<std::unique_ptr<JavaInfo>> arInfos;
javaPluginError plerr = jfw_plugin_getAllJavaInfos( false,
aVendorSettings,
& arInfos,
infos);
if (!aCurrentInfo)
{//The plug-ins did not find a suitable Java. Now try the paths which have been //added manually. //get the list of paths to jre locations which have been added manually const jfw::MergedSettings settings; //node.loadFromSettings(); const std::vector<OUString> & vecJRELocations =
settings.getJRELocations(); //use all plug-ins to determine the JavaInfo objects for (autoconst & JRELocation : vecJRELocations)
{
std::unique_ptr<JavaInfo> aInfo;
javaPluginError err = jfw_plugin_getJavaInfoByPath(
JRELocation,
aVendorSettings,
&aInfo); if (err == javaPluginError::NoJre) continue; if (err == javaPluginError::FailedVersion) continue; elseif (err !=javaPluginError::NONE) return JFW_E_ERROR;
if (aInfo)
{
aCurrentInfo = std::move(aInfo); break;
}
}//end iterate over paths
}
} if (aCurrentInfo)
{
jfw::NodeJava javaNode(jfw::NodeJava::USER);
javaNode.setJavaInfo(aCurrentInfo.get(),true);
javaNode.write(); //remember that this JRE was selected in this process
jfw::setJavaSelected();
if (jfw::getMode() == jfw::JFW_MODE_DIRECT)
{ if ((errcode = jfw_getJavaInfoByPath(
jfw::BootParams::getJREHome(), ppInfo))
!= JFW_E_NONE) throw jfw::FrameworkException(
JFW_E_CONFIGURATION, "[Java framework] The JRE specified by the bootstrap " "variable UNO_JAVA_JFW_JREHOME or UNO_JAVA_JFW_ENV_JREHOME " " could not be recognized. Check the values and make sure that you " "use a plug-in library that can recognize that JRE."_ostr);
return JFW_E_NONE;
}
const jfw::MergedSettings settings;
*ppInfo = settings.createJavaInfo(); if (!*ppInfo)
{ return JFW_E_NONE;
} //If the javavendors.xml has changed, then the current selected //Java is not valid anymore // /java/javaInfo/@vendorUpdate != javaSelection/updated (javavendors.xml)
OString sUpdated = jfw::getElementUpdated();
//ask all plugins if this is a JRE. //If so check if it meets the version requirements. //Only if it does return a JavaInfo
javaPluginError plerr = jfw_plugin_getJavaInfoByPath(
pPath,
aVendorSettings,
ppInfo);
if(plerr == javaPluginError::FailedVersion)
{//found JRE but it has the wrong version
ppInfo->reset();
errcode = JFW_E_FAILED_VERSION;
}
OSL_ASSERT(plerr == javaPluginError::NONE || plerr == javaPluginError::NoJre); if (!*ppInfo && errcode != JFW_E_FAILED_VERSION)
errcode = JFW_E_NOT_RECOGNIZED;
} catch (const jfw::FrameworkException& e)
{
errcode = e.errorCode;
SAL_WARN( "jfw", e.message );
}
return errcode;
}
javaFrameworkError jfw_setSelectedJRE(JavaInfo const *pInfo)
{
javaFrameworkError errcode = JFW_E_NONE; try
{
osl::MutexGuard guard(jfw::FwkMutex()); if (jfw::getMode() == jfw::JFW_MODE_DIRECT) return JFW_E_DIRECT_MODE; //check if pInfo is the selected JRE
std::unique_ptr<JavaInfo> currentInfo;
errcode = jfw_getSelectedJRE( & currentInfo); if (errcode != JFW_E_NONE && errcode != JFW_E_INVALID_SETTINGS) return errcode;
if (!jfw_areEqualJavaInfo(currentInfo.get(), pInfo))
{
jfw::NodeJava node(jfw::NodeJava::USER);
node.setJavaInfo(pInfo, false);
node.write(); //remember that the JRE was selected in this process
jfw::setJavaSelected();
}
} catch (const jfw::FrameworkException& e)
{
errcode = e.errorCode;
SAL_WARN( "jfw", e.message );
} return errcode;
}
javaFrameworkError jfw_setEnabled(bool bEnabled)
{
javaFrameworkError errcode = JFW_E_NONE; try
{
osl::MutexGuard guard(jfw::FwkMutex()); if (jfw::getMode() == jfw::JFW_MODE_DIRECT) return JFW_E_DIRECT_MODE;
if (!g_bEnabledSwitchedOn && bEnabled)
{ //When the process started then Enabled was false. //This is first time enabled is set to true. //That means, no preparational work has been done, such as setting the //LD_LIBRARY_PATH, etc.
javaFrameworkError ret = JFW_E_NONE; switch (plerr)
{ case javaPluginError::NONE:
ret = JFW_E_NONE; break; case javaPluginError::Error:
ret = JFW_E_ERROR; break; default:
ret = JFW_E_ERROR;
} return ret;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.