Linux x86 build fix
[LibreOffice.git] / jvmfwk / plugins / sunmajor / javaenvsetup / javaldx.cxx
blobf179bdec76441105ac830deda2c910fee16bb937
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 .
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "sal/main.h"
25 #include "sal/types.h"
26 #include "osl/thread.h"
27 #include "rtl/ustring.hxx"
28 #include "rtl/byteseq.hxx"
29 #include "jvmfwk/framework.h"
32 static bool hasOption(char const * szOption, int argc, char** argv);
33 static OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData);
34 static bool findAndSelect(JavaInfo**);
36 #define HELP_TEXT \
37 "\njavaldx is necessary to make Java work on some UNIX platforms." \
38 "It prints a string to std out that consists of directories which " \
39 "have to be included into the LD_LIBRARY_PATH variable.The setting of " \
40 "the variable usually occurs in a shell script that runs javaldx.\n" \
41 "The directories are from the chosen java installation. \n" \
42 "Options are: \n"\
43 "--help or -h\n"
45 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
47 try
49 if( hasOption("--help",argc, argv) || hasOption("-h", argc, argv))
51 fprintf(stdout, HELP_TEXT);// default
52 return 0;
54 javaFrameworkError errcode = JFW_E_NONE;
55 sal_Bool bEnabled = sal_False;
56 errcode = jfw_getEnabled( & bEnabled);
57 if (errcode == JFW_E_NONE && bEnabled == sal_False)
59 //Do not do any preparation because that may only slow startup time.
60 return 0;
62 else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE)
64 fprintf(stderr,"javaldx failed!\n");
65 return -1;
68 JavaInfo * pInfo = NULL;
69 errcode = jfw_getSelectedJRE( & pInfo);
71 if (errcode != JFW_E_NONE && errcode != JFW_E_INVALID_SETTINGS)
73 fprintf(stderr,"javaldx failed!\n");
74 return -1;
77 if (pInfo == NULL)
79 if (!findAndSelect(&pInfo))
80 return -1;
82 else
84 //check if the JRE was not uninstalled
85 sal_Bool bExist = sal_False;
86 errcode = jfw_existJRE(pInfo, &bExist);
87 if (errcode == JFW_E_NONE)
89 if (!bExist && !findAndSelect(&pInfo))
90 return -1;
92 else
94 fprintf(stderr, "javaldx: Could not determine if JRE still exist\n");
95 return -1;
99 OString sPaths = getLD_LIBRARY_PATH(pInfo->arVendorData);
100 fprintf(stdout, "%s\n", sPaths.getStr());
101 jfw_freeJavaInfo(pInfo);
103 catch (const std::exception&)
105 fprintf(stderr,"javaldx failed!\n");
106 return -1;
109 return 0;
112 OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData)
114 const sal_Unicode* chars = reinterpret_cast<sal_Unicode const *>(vendorData.getConstArray());
115 sal_Int32 len = vendorData.getLength();
116 OUString sData(chars, len / 2);
117 //the runtime lib is on the first line
118 sal_Int32 index = 0;
119 OUString aToken = sData.getToken( 1, '\n', index);
121 OString paths =
122 OUStringToOString(aToken, osl_getThreadTextEncoding());
123 return paths;
126 static bool hasOption(char const * szOption, int argc, char** argv)
128 bool retVal= false;
129 for(sal_Int16 i= 1; i < argc; i++)
131 if( ! strcmp(argv[i], szOption))
133 retVal= true;
134 break;
137 return retVal;
140 static bool findAndSelect(JavaInfo ** ppInfo)
142 javaFrameworkError errcode = jfw_findAndSelectJRE(ppInfo);
143 if (errcode == JFW_E_NO_JAVA_FOUND)
145 fprintf(stderr,"javaldx: Could not find a Java Runtime Environment!\n");
146 return false;
148 else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE)
150 fprintf(stderr,"javaldx failed!\n");
151 return false;
153 return true;
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */