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