1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_jvmfwk.hxx" 30 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include "sal/main.h" 35 #include "sal/types.h" 36 #include "osl/thread.h" 37 #include "rtl/ustring.hxx" 38 #include "rtl/byteseq.hxx" 39 #include "jvmfwk/framework.h" 40 41 using namespace rtl; 42 43 #define OUSTR(x) OUString(RTL_CONSTASCII_USTRINGPARAM( x )) 44 45 static sal_Bool hasOption(char const * szOption, int argc, char** argv); 46 static rtl::OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData); 47 static bool findAndSelect(JavaInfo**); 48 //static sal_Bool printPaths(const OUString& sPathFile); 49 50 #define HELP_TEXT \ 51 "\njavaldx is necessary to make Java work on some UNIX platforms." \ 52 "It prints a string to std out that consists of directories which " \ 53 "have to be included into the LD_LIBRARY_PATH variable.The setting of " \ 54 "the variable usually occurs in a shell script that runs javaldx.\n" \ 55 "The directories are from the chosen java installation. \n" \ 56 "Options are: \n"\ 57 "--help or -h\n" 58 59 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv) 60 { 61 if( hasOption("--help",argc, argv) || hasOption("-h", argc, argv)) 62 { 63 fprintf(stdout, HELP_TEXT);// default 64 return 0; 65 } 66 javaFrameworkError errcode = JFW_E_NONE; 67 sal_Bool bEnabled = sal_False; 68 errcode = jfw_getEnabled( & bEnabled); 69 if (errcode == JFW_E_NONE && bEnabled == sal_False) 70 { 71 //Do not do any preparation because that may only slow startup time. 72 return 0; 73 } 74 else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE) 75 { 76 fprintf(stderr,"javaldx failed! \n"); 77 return -1; 78 } 79 80 81 JavaInfo * pInfo = NULL; 82 errcode = jfw_getSelectedJRE( & pInfo); 83 84 if (errcode == JFW_E_INVALID_SETTINGS) 85 { 86 fprintf(stderr,"javaldx failed. User must select a JRE from options dialog!"); 87 return -1; 88 } 89 else if (errcode != JFW_E_NONE) 90 { 91 fprintf(stderr,"javaldx failed! \n"); 92 return -1; 93 } 94 95 if (pInfo == NULL) 96 { 97 if (false == findAndSelect(&pInfo)) 98 return -1; 99 } 100 else 101 { 102 //check if the JRE was not uninstalled 103 sal_Bool bExist = sal_False; 104 errcode = jfw_existJRE(pInfo, &bExist); 105 if (errcode == JFW_E_NONE) 106 { 107 if (!bExist && !findAndSelect(&pInfo)) 108 return -1; 109 } 110 else 111 { 112 fprintf(stderr, "javaldx: Could not determine if JRE still exist\n"); 113 return -1; 114 } 115 } 116 117 //Only do something if the sunjavaplugin created this JavaInfo 118 rtl::OUString sVendor1(RTL_CONSTASCII_USTRINGPARAM("Sun Microsystems Inc.")); 119 rtl::OUString sVendor2(RTL_CONSTASCII_USTRINGPARAM("IBM Corporation")); 120 rtl::OUString sVendor3(RTL_CONSTASCII_USTRINGPARAM("Blackdown Java-Linux Team")); 121 rtl::OUString sVendor4(RTL_CONSTASCII_USTRINGPARAM("Apple Inc.")); 122 rtl::OUString sVendor5(RTL_CONSTASCII_USTRINGPARAM("Apple Computer, Inc.")); 123 rtl::OUString sVendor6(RTL_CONSTASCII_USTRINGPARAM("BEA Systems, Inc.")); 124 rtl::OUString sVendor7(RTL_CONSTASCII_USTRINGPARAM("Free Software Foundation, Inc.")); 125 rtl::OUString sVendor8(RTL_CONSTASCII_USTRINGPARAM("The FreeBSD Foundation")); 126 if ( ! (sVendor1.equals(pInfo->sVendor) == sal_True 127 || sVendor2.equals(pInfo->sVendor) == sal_True 128 || sVendor3.equals(pInfo->sVendor) == sal_True 129 || sVendor4.equals(pInfo->sVendor) == sal_True 130 || sVendor5.equals(pInfo->sVendor) == sal_True 131 || sVendor6.equals(pInfo->sVendor) == sal_True 132 || sVendor7.equals(pInfo->sVendor) == sal_True 133 || sVendor8.equals(pInfo->sVendor) == sal_True)) 134 return 0; 135 136 rtl::OString sPaths = getLD_LIBRARY_PATH(pInfo->arVendorData); 137 fprintf(stdout, "%s\n", sPaths.getStr()); 138 jfw_freeJavaInfo(pInfo); 139 140 return 0; 141 } 142 143 rtl::OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData) 144 { 145 const sal_Unicode* chars = (sal_Unicode*) vendorData.getConstArray(); 146 sal_Int32 len = vendorData.getLength(); 147 rtl::OUString sData(chars, len / 2); 148 //the runtime lib is on the first line 149 sal_Int32 index = 0; 150 rtl::OUString aToken = sData.getToken( 1, '\n', index); 151 152 rtl::OString paths = 153 rtl::OUStringToOString(aToken, osl_getThreadTextEncoding()); 154 return paths; 155 } 156 157 static sal_Bool hasOption(char const * szOption, int argc, char** argv) 158 { 159 sal_Bool retVal= sal_False; 160 for(sal_Int16 i= 1; i < argc; i++) 161 { 162 if( ! strcmp(argv[i], szOption)) 163 { 164 retVal= sal_True; 165 break; 166 } 167 } 168 return retVal; 169 } 170 171 static bool findAndSelect(JavaInfo ** ppInfo) 172 { 173 javaFrameworkError errcode = jfw_findAndSelectJRE(ppInfo); 174 if (errcode == JFW_E_NO_JAVA_FOUND) 175 { 176 fprintf(stderr,"javaldx: Could not find a Java Runtime Environment! \n"); 177 return false; 178 } 179 else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE) 180 { 181 fprintf(stderr,"javaldx failed!\n"); 182 return false; 183 } 184 return true; 185 } 186 187 188 189