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 "rtl/ustring.hxx"
21 #include "rtl/ustrbuf.hxx"
22 #include "rtl/uri.hxx"
23 #include "osl/thread.hxx"
24 #include "osl/process.h"
25 #include "libxml/xpathInternals.h"
26 #include "osl/file.hxx"
27 #include "osl/module.hxx"
28 #include "framework.hxx"
29 #include "fwkutil.hxx"
30 #include "elements.hxx"
31 #include "fwkbase.hxx"
36 #define UNO_JAVA_JFW_PARAMETER "UNO_JAVA_JFW_PARAMETER_"
37 #define UNO_JAVA_JFW_JREHOME "UNO_JAVA_JFW_JREHOME"
38 #define UNO_JAVA_JFW_ENV_JREHOME "UNO_JAVA_JFW_ENV_JREHOME"
39 #define UNO_JAVA_JFW_CLASSPATH "UNO_JAVA_JFW_CLASSPATH"
40 #define UNO_JAVA_JFW_ENV_CLASSPATH "UNO_JAVA_JFW_ENV_CLASSPATH"
41 #define UNO_JAVA_JFW_CLASSPATH_URLS "UNO_JAVA_JFW_CLASSPATH_URLS"
42 #define UNO_JAVA_JFW_VENDOR_SETTINGS "UNO_JAVA_JFW_VENDOR_SETTINGS"
46 bool g_bJavaSet
= false;
50 OString
getVendorSettingsPath(OUString
const & sURL
)
54 OUString sSystemPathSettings
;
55 if (osl_getSystemPathFromFileURL(sURL
.pData
,
56 & sSystemPathSettings
.pData
) != osl_File_E_None
)
57 throw FrameworkException(
59 OString("[Java framework] Error in function "
60 "getVendorSettingsPath (fwkbase.cxx) "));
61 OString osSystemPathSettings
=
62 OUStringToOString(sSystemPathSettings
,osl_getThreadTextEncoding());
63 return osSystemPathSettings
;
66 OUString
getParam(OUString
const & name
)
69 bool b
= Bootstrap::get()->getFrom(name
, retVal
);
72 "Using bootstrap parameter " << name
<< " = \"" << retVal
<< "\""
73 << (b
? "" : " (undefined)"));
77 OUString
getParamFirstUrl(OUString
const & name
)
79 // Some parameters can consist of multiple URLs (separated by space
80 // characters, although trim() harmlessly also removes other white-space),
81 // of which only the first is used:
83 return getParam(name
).trim().getToken(0, ' ', i
);
89 VendorSettings::VendorSettings():
90 m_xmlDocVendorSettingsFileUrl(BootParams::getVendorSettings())
92 OString
sMsgExc("[Java framework] Error in constructor "
93 "VendorSettings::VendorSettings() (fwkbase.cxx)");
94 //Prepare the xml document and context
95 OString sSettingsPath
= getVendorSettingsPath(m_xmlDocVendorSettingsFileUrl
);
96 if (sSettingsPath
.isEmpty())
98 OString
sMsg("[Java framework] A vendor settings file was not specified."
99 "Check the bootstrap parameter " UNO_JAVA_JFW_VENDOR_SETTINGS
".");
100 OSL_FAIL(sMsg
.getStr());
101 throw FrameworkException(JFW_E_CONFIGURATION
, sMsg
);
103 if (!sSettingsPath
.isEmpty())
105 m_xmlDocVendorSettings
= xmlParseFile(sSettingsPath
.getStr());
106 if (m_xmlDocVendorSettings
== NULL
)
107 throw FrameworkException(
109 OString("[Java framework] Error while parsing file: ")
110 + sSettingsPath
+ OString("."));
112 m_xmlPathContextVendorSettings
= xmlXPathNewContext(m_xmlDocVendorSettings
);
113 int res
= xmlXPathRegisterNs(
114 m_xmlPathContextVendorSettings
, reinterpret_cast<xmlChar
const *>("jf"),
115 reinterpret_cast<xmlChar
const *>(NS_JAVA_FRAMEWORK
));
117 throw FrameworkException(JFW_E_ERROR
, sMsgExc
);
121 VersionInfo
VendorSettings::getVersionInformation(const OUString
& sVendor
)
123 OSL_ASSERT(!sVendor
.isEmpty());
124 VersionInfo aVersionInfo
;
125 OString osVendor
= OUStringToOString(sVendor
, RTL_TEXTENCODING_UTF8
);
127 OString sExpresion
= OString(
128 "/jf:javaSelection/jf:vendorInfos/jf:vendor[@name=\"") +
129 osVendor
+ OString("\"]/jf:minVersion");
131 CXPathObjectPtr xPathObjectMin
;
133 xmlXPathEvalExpression(reinterpret_cast<xmlChar
const *>(sExpresion
.getStr()),
134 m_xmlPathContextVendorSettings
);
135 if (xmlXPathNodeSetIsEmpty(xPathObjectMin
->nodesetval
))
137 aVersionInfo
.sMinVersion
.clear();
141 CXmlCharPtr sVersion
;
142 sVersion
= xmlNodeListGetString(
143 m_xmlDocVendorSettings
,
144 xPathObjectMin
->nodesetval
->nodeTab
[0]->xmlChildrenNode
, 1);
145 OString
osVersion(sVersion
);
146 aVersionInfo
.sMinVersion
= OStringToOUString(
147 osVersion
, RTL_TEXTENCODING_UTF8
);
151 sExpresion
= OString("/jf:javaSelection/jf:vendorInfos/jf:vendor[@name=\"") +
152 osVendor
+ OString("\"]/jf:maxVersion");
153 CXPathObjectPtr xPathObjectMax
;
154 xPathObjectMax
= xmlXPathEvalExpression(
155 reinterpret_cast<xmlChar
const *>(sExpresion
.getStr()),
156 m_xmlPathContextVendorSettings
);
157 if (xmlXPathNodeSetIsEmpty(xPathObjectMax
->nodesetval
))
159 aVersionInfo
.sMaxVersion
.clear();
163 CXmlCharPtr sVersion
;
164 sVersion
= xmlNodeListGetString(
165 m_xmlDocVendorSettings
,
166 xPathObjectMax
->nodesetval
->nodeTab
[0]->xmlChildrenNode
, 1);
167 OString
osVersion(sVersion
);
168 aVersionInfo
.sMaxVersion
= OStringToOUString(
169 osVersion
, RTL_TEXTENCODING_UTF8
);
172 //Get excludeVersions
173 sExpresion
= OString("/jf:javaSelection/jf:vendorInfos/jf:vendor[@name=\"") +
174 osVendor
+ OString("\"]/jf:excludeVersions/jf:version");
175 CXPathObjectPtr xPathObjectVersions
;
176 xPathObjectVersions
=
177 xmlXPathEvalExpression(reinterpret_cast<xmlChar
const *>(sExpresion
.getStr()),
178 m_xmlPathContextVendorSettings
);
179 if (!xmlXPathNodeSetIsEmpty(xPathObjectVersions
->nodesetval
))
181 xmlNode
* cur
= xPathObjectVersions
->nodesetval
->nodeTab
[0];
184 if (cur
->type
== XML_ELEMENT_NODE
)
186 if (xmlStrcmp(cur
->name
, reinterpret_cast<xmlChar
const *>("version")) == 0)
188 CXmlCharPtr sVersion
;
189 sVersion
= xmlNodeListGetString(
190 m_xmlDocVendorSettings
, cur
->xmlChildrenNode
, 1);
191 OString
osVersion(sVersion
);
192 OUString usVersion
= OStringToOUString(
193 osVersion
, RTL_TEXTENCODING_UTF8
);
194 aVersionInfo
.addExcludeVersion(usVersion
);
203 std::vector
<OUString
> VendorSettings::getSupportedVendors()
205 std::vector
<OUString
> vecVendors
;
206 //get the nodeset for the vendor elements
207 jfw::CXPathObjectPtr result
;
208 result
= xmlXPathEvalExpression(
209 reinterpret_cast<xmlChar
const *>("/jf:javaSelection/jf:vendorInfos/jf:vendor"),
210 m_xmlPathContextVendorSettings
);
211 if (!xmlXPathNodeSetIsEmpty(result
->nodesetval
))
213 //get the values of the vendor elements + name attribute
214 xmlNode
* cur
= result
->nodesetval
->nodeTab
[0];
217 //between vendor elements are also text elements
218 if (cur
->type
== XML_ELEMENT_NODE
)
220 jfw::CXmlCharPtr
sAttrVendor(xmlGetProp(cur
, reinterpret_cast<xmlChar
const *>("name")));
221 vecVendors
.push_back(sAttrVendor
);
229 ::std::vector
<OString
> BootParams::getVMParameters()
231 ::std::vector
<OString
> vecParams
;
233 for (sal_Int32 i
= 1; ; i
++)
235 OUString sName
= UNO_JAVA_JFW_PARAMETER
+ OUString::number(i
);
237 if (Bootstrap::get()->getFrom(sName
, sValue
))
240 OUStringToOString(sValue
, osl_getThreadTextEncoding());
241 vecParams
.push_back(sParam
);
242 #if OSL_DEBUG_LEVEL >=2
243 OString sParamName
= OUStringToOString(sName
, osl_getThreadTextEncoding());
244 fprintf(stderr
,"[Java framework] Using bootstrap parameter %s"
245 " = %s.\n", sParamName
.getStr(), sParam
.getStr());
254 OUString
BootParams::getUserData()
256 return getParamFirstUrl("UNO_JAVA_JFW_USER_DATA");
259 OUString
BootParams::getSharedData()
261 return getParamFirstUrl("UNO_JAVA_JFW_SHARED_DATA");
264 OString
BootParams::getClasspath()
268 if (Bootstrap::get()->getFrom(
269 OUString(UNO_JAVA_JFW_CLASSPATH
),
272 sClassPath
= OUStringToOString(sCP
, osl_getThreadTextEncoding());
273 #if OSL_DEBUG_LEVEL >=2
274 fprintf(stderr
,"[Java framework] Using bootstrap parameter "
275 UNO_JAVA_JFW_CLASSPATH
" = %s.\n", sClassPath
.getStr());
280 if (Bootstrap::get()->getFrom(
281 OUString(UNO_JAVA_JFW_ENV_CLASSPATH
),
284 char * pCp
= getenv("CLASSPATH");
287 char szSep
[] = {SAL_PATHSEPARATOR
,0};
288 sClassPath
+= OString(szSep
) + OString(pCp
);
290 #if OSL_DEBUG_LEVEL >=2
291 fprintf(stderr
,"[Java framework] Using bootstrap parameter "
292 UNO_JAVA_JFW_ENV_CLASSPATH
" and class path is:\n %s.\n", pCp
? pCp
: "");
299 OUString
BootParams::getVendorSettings()
303 UNO_JAVA_JFW_VENDOR_SETTINGS
);
304 if (Bootstrap::get()->getFrom(sName
,sVendor
))
306 //check the value of the bootstrap variable
307 jfw::FileStatus s
= checkFileURL(sVendor
);
310 //This bootstrap parameter can contain a relative URL
311 OUString sAbsoluteUrl
;
312 OUString sBaseDir
= getLibraryLocation();
313 if (File::getAbsoluteFileURL(sBaseDir
, sVendor
, sAbsoluteUrl
)
315 throw FrameworkException(
317 OString("[Java framework] Invalid value for bootstrap variable: "
318 UNO_JAVA_JFW_VENDOR_SETTINGS
));
319 sVendor
= sAbsoluteUrl
;
320 s
= checkFileURL(sVendor
);
321 if (s
== jfw::FILE_INVALID
|| s
== jfw::FILE_DOES_NOT_EXIST
)
323 throw FrameworkException(
325 OString("[Java framework] Invalid value for bootstrap variable: "
326 UNO_JAVA_JFW_VENDOR_SETTINGS
));
329 #if OSL_DEBUG_LEVEL >=2
330 OString sValue
= OUStringToOString(sVendor
, osl_getThreadTextEncoding());
331 fprintf(stderr
,"[Java framework] Using bootstrap parameter "
332 UNO_JAVA_JFW_VENDOR_SETTINGS
" = %s.\n", sValue
.getStr());
338 OUString
BootParams::getJREHome()
342 bool bJRE
= Bootstrap::get()->getFrom(
343 OUString(UNO_JAVA_JFW_JREHOME
) ,sJRE
);
344 bool bEnvJRE
= Bootstrap::get()->getFrom(
345 OUString(UNO_JAVA_JFW_ENV_JREHOME
) ,sEnvJRE
);
349 throw FrameworkException(
351 OString("[Java framework] Both bootstrap parameter "
352 UNO_JAVA_JFW_JREHOME
" and "
353 UNO_JAVA_JFW_ENV_JREHOME
" are set. However only one of them can be set."
354 "Check bootstrap parameters: environment variables, command line "
355 "arguments, rc/ini files for executable and java framework library."));
359 const char * pJRE
= getenv("JAVA_HOME");
362 throw FrameworkException(
364 OString("[Java framework] Both bootstrap parameter "
365 UNO_JAVA_JFW_ENV_JREHOME
" is set, but the environment variable "
366 "JAVA_HOME is not set."));
369 OUString usJRE
= OStringToOUString(osJRE
, osl_getThreadTextEncoding());
370 if (File::getFileURLFromSystemPath(usJRE
, sJRE
) != File::E_None
)
371 throw FrameworkException(
373 OString("[Java framework] Error in function BootParams::getJREHome() "
375 #if OSL_DEBUG_LEVEL >=2
376 fprintf(stderr
,"[Java framework] Using bootstrap parameter "
377 UNO_JAVA_JFW_ENV_JREHOME
" with JAVA_HOME = %s.\n", pJRE
);
380 else if (getMode() == JFW_MODE_DIRECT
384 throw FrameworkException(
386 OString("[Java framework] The bootstrap parameter "
387 UNO_JAVA_JFW_ENV_JREHOME
" or " UNO_JAVA_JFW_JREHOME
388 " must be set in direct mode."));
391 #if OSL_DEBUG_LEVEL >=2
392 if (bJRE
== sal_True
)
394 OString sValue
= OUStringToOString(sJRE
, osl_getThreadTextEncoding());
395 fprintf(stderr
,"[Java framework] Using bootstrap parameter "
396 UNO_JAVA_JFW_JREHOME
" = %s.\n", sValue
.getStr());
402 OUString
BootParams::getClasspathUrls()
405 Bootstrap::get()->getFrom(
406 OUString(UNO_JAVA_JFW_CLASSPATH_URLS
),
408 #if OSL_DEBUG_LEVEL >=2
409 OString sValue
= OUStringToOString(sParams
, osl_getThreadTextEncoding());
410 fprintf(stderr
,"[Java framework] Using bootstrap parameter "
411 UNO_JAVA_JFW_CLASSPATH_URLS
" = %s.\n", sValue
.getStr());
418 static bool g_bMode
= false;
419 static JFW_MODE g_mode
= JFW_MODE_APPLICATION
;
423 //check if either of the "direct mode" bootstrap variables is set
424 bool bDirectMode
= true;
426 const rtl::Bootstrap
* aBoot
= Bootstrap::get();
427 OUString
sJREHome(UNO_JAVA_JFW_JREHOME
);
428 if (!aBoot
->getFrom(sJREHome
, sValue
))
430 OUString
sEnvJRE(UNO_JAVA_JFW_ENV_JREHOME
);
431 if (!aBoot
->getFrom(sEnvJRE
, sValue
))
433 OUString
sClasspath(UNO_JAVA_JFW_CLASSPATH
);
434 if (!aBoot
->getFrom(sClasspath
, sValue
))
436 OUString
sEnvClasspath(UNO_JAVA_JFW_ENV_CLASSPATH
);
437 if (!aBoot
->getFrom(sEnvClasspath
, sValue
))
439 OUString sParams
= UNO_JAVA_JFW_PARAMETER
+
441 if (!aBoot
->getFrom(sParams
, sValue
))
451 g_mode
= JFW_MODE_DIRECT
;
453 g_mode
= JFW_MODE_APPLICATION
;
460 OUString
getApplicationClassPath()
462 OSL_ASSERT(getMode() == JFW_MODE_APPLICATION
);
464 OUString sParams
= BootParams::getClasspathUrls();
465 if (sParams
.isEmpty())
470 const char szClassPathSep
[] = {SAL_PATHSEPARATOR
,0};
473 OUString
token( sParams
.getToken( 0, ' ', index
).trim() );
474 if (!token
.isEmpty())
476 OUString systemPathElement
;
477 oslFileError rc
= osl_getSystemPathFromFileURL(
478 token
.pData
, &systemPathElement
.pData
);
479 OSL_ASSERT( rc
== osl_File_E_None
);
480 if (rc
== osl_File_E_None
&& !systemPathElement
.isEmpty())
482 if (buf
.getLength() > 0)
483 buf
.append( szClassPathSep
);
484 buf
.append( systemPathElement
);
489 return buf
.makeStringAndClear();
492 OString
makeClassPathOption(OUString
const & sUserClassPath
)
494 //Compose the class path
496 OUStringBuffer
sBufCP(4096);
498 // append all user selected jars to the class path
499 if (!sUserClassPath
.isEmpty())
500 sBufCP
.append(sUserClassPath
);
502 //append all jar libraries and components to the class path
503 OUString sAppCP
= getApplicationClassPath();
504 if (!sAppCP
.isEmpty())
506 if (!sUserClassPath
.isEmpty())
508 char szSep
[] = {SAL_PATHSEPARATOR
,0};
509 sBufCP
.appendAscii(szSep
);
511 sBufCP
.append(sAppCP
);
514 sPaths
= OUStringToOString(
515 sBufCP
.makeStringAndClear(), osl_getThreadTextEncoding());
517 OString
sOptionClassPath("-Djava.class.path=");
518 sOptionClassPath
+= sPaths
;
519 return sOptionClassPath
;
522 OString
getUserSettingsPath()
524 return getSettingsPath(BootParams::getUserData());
527 OString
getSharedSettingsPath()
529 return getSettingsPath(BootParams::getSharedData());
532 OString
getSettingsPath( const OUString
& sURL
)
537 if (osl_getSystemPathFromFileURL(sURL
.pData
,
538 & sPath
.pData
) != osl_File_E_None
)
539 throw FrameworkException(
540 JFW_E_ERROR
, OString(
541 "[Java framework] Error in function ::getSettingsPath (fwkbase.cxx)."));
542 return OUStringToOString(sPath
,osl_getThreadTextEncoding());
545 OString
getVendorSettingsPath()
547 return getVendorSettingsPath(BootParams::getVendorSettings());
550 void setJavaSelected()
555 bool wasJavaSelectedInSameProcess()
557 //g_setJavaProcId not set means no Java selected
566 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */