fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / jvmfwk / source / fwkbase.cxx
blob9c79344496d8ebe7dad91d4d272f54df2f1e789b
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 "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"
33 using namespace osl;
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"
44 namespace jfw
46 bool g_bJavaSet = false;
48 namespace {
50 OString getVendorSettingsPath(OUString const & sURL)
52 if (sURL.isEmpty())
53 return OString();
54 OUString sSystemPathSettings;
55 if (osl_getSystemPathFromFileURL(sURL.pData,
56 & sSystemPathSettings.pData) != osl_File_E_None)
57 throw FrameworkException(
58 JFW_E_ERROR,
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)
68 OUString retVal;
69 bool b = Bootstrap::get()->getFrom(name, retVal);
70 SAL_INFO(
71 "jfw",
72 "Using bootstrap parameter " << name << " = \"" << retVal << "\""
73 << (b ? "" : " (undefined)"));
74 return retVal;
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:
82 sal_Int32 i = 0;
83 return getParam(name).trim().getToken(0, ' ', i);
86 }//blind namespace
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(
108 JFW_E_ERROR,
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));
116 if (res == -1)
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);
126 //Get minVersion
127 OString sExpresion = OString(
128 "/jf:javaSelection/jf:vendorInfos/jf:vendor[@name=\"") +
129 osVendor + OString("\"]/jf:minVersion");
131 CXPathObjectPtr xPathObjectMin;
132 xPathObjectMin =
133 xmlXPathEvalExpression(reinterpret_cast<xmlChar const *>(sExpresion.getStr()),
134 m_xmlPathContextVendorSettings);
135 if (xmlXPathNodeSetIsEmpty(xPathObjectMin->nodesetval))
137 aVersionInfo.sMinVersion.clear();
139 else
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);
150 //Get maxVersion
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();
161 else
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];
182 while (cur != NULL)
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);
197 cur = cur->next;
200 return aVersionInfo;
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];
215 while (cur != NULL)
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);
223 cur = cur->next;
226 return vecVendors;
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);
236 OUString sValue;
237 if (Bootstrap::get()->getFrom(sName, sValue))
239 OString sParam =
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());
246 #endif
248 else
249 break;
251 return vecParams;
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()
266 OString sClassPath;
267 OUString sCP;
268 if (Bootstrap::get()->getFrom(
269 OUString(UNO_JAVA_JFW_CLASSPATH),
270 sCP))
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());
276 #endif
279 OUString sEnvCP;
280 if (Bootstrap::get()->getFrom(
281 OUString(UNO_JAVA_JFW_ENV_CLASSPATH),
282 sEnvCP))
284 char * pCp = getenv("CLASSPATH");
285 if (pCp)
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 : "");
293 #endif
296 return sClassPath;
299 OUString BootParams::getVendorSettings()
301 OUString sVendor;
302 OUString sName(
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);
308 if (s != FILE_OK)
310 //This bootstrap parameter can contain a relative URL
311 OUString sAbsoluteUrl;
312 OUString sBaseDir = getLibraryLocation();
313 if (File::getAbsoluteFileURL(sBaseDir, sVendor, sAbsoluteUrl)
314 != File::E_None)
315 throw FrameworkException(
316 JFW_E_CONFIGURATION,
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(
324 JFW_E_CONFIGURATION,
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());
333 #endif
335 return sVendor;
338 OUString BootParams::getJREHome()
340 OUString sJRE;
341 OUString sEnvJRE;
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);
347 if (bJRE && bEnvJRE)
349 throw FrameworkException(
350 JFW_E_CONFIGURATION,
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."));
357 else if (bEnvJRE)
359 const char * pJRE = getenv("JAVA_HOME");
360 if (pJRE == NULL)
362 throw FrameworkException(
363 JFW_E_CONFIGURATION,
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."));
368 OString osJRE(pJRE);
369 OUString usJRE = OStringToOUString(osJRE, osl_getThreadTextEncoding());
370 if (File::getFileURLFromSystemPath(usJRE, sJRE) != File::E_None)
371 throw FrameworkException(
372 JFW_E_ERROR,
373 OString("[Java framework] Error in function BootParams::getJREHome() "
374 "(fwkbase.cxx)."));
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);
378 #endif
380 else if (getMode() == JFW_MODE_DIRECT
381 && !bEnvJRE
382 && !bJRE)
384 throw FrameworkException(
385 JFW_E_CONFIGURATION,
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());
398 #endif
399 return sJRE;
402 OUString BootParams::getClasspathUrls()
404 OUString sParams;
405 Bootstrap::get()->getFrom(
406 OUString(UNO_JAVA_JFW_CLASSPATH_URLS),
407 sParams);
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());
412 #endif
413 return sParams;
416 JFW_MODE getMode()
418 static bool g_bMode = false;
419 static JFW_MODE g_mode = JFW_MODE_APPLICATION;
421 if (!g_bMode)
423 //check if either of the "direct mode" bootstrap variables is set
424 bool bDirectMode = true;
425 OUString sValue;
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 +
440 OUString::number(1);
441 if (!aBoot->getFrom(sParams, sValue))
443 bDirectMode = false;
450 if (bDirectMode)
451 g_mode = JFW_MODE_DIRECT;
452 else
453 g_mode = JFW_MODE_APPLICATION;
454 g_bMode = true;
457 return g_mode;
460 OUString getApplicationClassPath()
462 OSL_ASSERT(getMode() == JFW_MODE_APPLICATION);
463 OUString retVal;
464 OUString sParams = BootParams::getClasspathUrls();
465 if (sParams.isEmpty())
466 return retVal;
468 OUStringBuffer buf;
469 sal_Int32 index = 0;
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 );
488 while (index >= 0);
489 return buf.makeStringAndClear();
492 OString makeClassPathOption(OUString const & sUserClassPath)
494 //Compose the class path
495 OString sPaths;
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)
534 if (sURL.isEmpty())
535 return OString();
536 OUString sPath;
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()
552 g_bJavaSet = true;
555 bool wasJavaSelectedInSameProcess()
557 //g_setJavaProcId not set means no Java selected
558 if (g_bJavaSet)
559 return true;
560 return false;
566 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */