lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / jvmfwk / plugins / sunmajor / javaenvsetup / javaldx.cxx
blob2a1a6b13baf25ec6090e4da7cae76594ae55df09
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 <sal/config.h>
22 #include <memory>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <iostream>
28 #include <sal/main.h>
29 #include <sal/types.h>
30 #include <osl/thread.h>
31 #include <rtl/ustring.hxx>
32 #include <rtl/byteseq.hxx>
33 #include <jvmfwk/framework.hxx>
36 static bool hasOption(char const * szOption, int argc, char** argv);
37 static OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData);
38 static bool findAndSelect(std::unique_ptr<JavaInfo>*);
40 #define HELP_TEXT \
41 "\njavaldx is necessary to make Java work on some UNIX platforms." \
42 "It prints a string to std out that consists of directories which " \
43 "have to be included into the LD_LIBRARY_PATH variable.The setting of " \
44 "the variable usually occurs in a shell script that runs javaldx.\n" \
45 "The directories are from the chosen java installation. \n" \
46 "Options are: \n"\
47 "--help or -h\n"
49 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
51 try
53 if( hasOption("--help",argc, argv) || hasOption("-h", argc, argv))
55 fprintf(stdout, HELP_TEXT);// default
56 return 0;
58 bool bEnabled = false;
59 javaFrameworkError errcode = jfw_getEnabled( & bEnabled);
60 if (errcode == JFW_E_NONE && !bEnabled)
62 //Do not do any preparation because that may only slow startup time.
63 return 0;
65 else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE)
67 fprintf(stderr,"javaldx failed!\n");
68 return -1;
71 std::unique_ptr<JavaInfo> aInfo;
72 errcode = jfw_getSelectedJRE(&aInfo);
74 if (errcode != JFW_E_NONE && errcode != JFW_E_INVALID_SETTINGS)
76 fprintf(stderr,"javaldx failed!\n");
77 return -1;
80 if (!aInfo)
82 if (!findAndSelect(&aInfo))
83 return -1;
85 else
87 //check if the JRE was not uninstalled
88 bool bExist = false;
89 errcode = jfw_existJRE(aInfo.get(), &bExist);
90 if (errcode == JFW_E_NONE)
92 if (!bExist && !findAndSelect(&aInfo))
93 return -1;
95 else
97 fprintf(stderr, "javaldx: Could not determine if JRE still exist\n");
98 return -1;
102 OString sPaths = getLD_LIBRARY_PATH(aInfo->arVendorData);
103 fprintf(stdout, "%s\n", sPaths.getStr());
105 catch (const std::exception& e)
107 std::cerr << "javaldx failed! " << e.what() << std::endl;
108 return -1;
111 return 0;
114 OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData)
116 const sal_Unicode* chars = reinterpret_cast<sal_Unicode const *>(vendorData.getConstArray());
117 sal_Int32 len = vendorData.getLength();
118 OUString sData(chars, len / 2);
119 //the runtime lib is on the first line
120 sal_Int32 index = 0;
121 OUString aToken = sData.getToken( 1, '\n', index);
123 OString paths =
124 OUStringToOString(aToken, osl_getThreadTextEncoding());
125 return paths;
128 static bool hasOption(char const * szOption, int argc, char** argv)
130 bool retVal= false;
131 for(int i= 1; i < argc; i++)
133 if( ! strcmp(argv[i], szOption))
135 retVal= true;
136 break;
139 return retVal;
142 static bool findAndSelect(std::unique_ptr<JavaInfo> * ppInfo)
144 javaFrameworkError errcode = jfw_findAndSelectJRE(ppInfo);
145 if (errcode == JFW_E_NO_JAVA_FOUND)
147 fprintf(stderr,"javaldx: Could not find a Java Runtime Environment!\n");
148 return false;
150 else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE)
152 fprintf(stderr,"javaldx failed!\n");
153 return false;
155 return true;
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */