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