xref: /AOO41X/main/jvmfwk/plugins/sunmajor/javaenvsetup/javaldx.cxx (revision a937066d3a6a4955ebe972d362067dfafb27ac14)
136f55ffcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
336f55ffcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
436f55ffcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
536f55ffcSAndrew Rist  * distributed with this work for additional information
636f55ffcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
736f55ffcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
836f55ffcSAndrew Rist  * "License"); you may not use this file except in compliance
936f55ffcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1136f55ffcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1336f55ffcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1436f55ffcSAndrew Rist  * software distributed under the License is distributed on an
1536f55ffcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1636f55ffcSAndrew Rist  * KIND, either express or implied.  See the License for the
1736f55ffcSAndrew Rist  * specific language governing permissions and limitations
1836f55ffcSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2036f55ffcSAndrew Rist  *************************************************************/
2136f55ffcSAndrew Rist 
2236f55ffcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_jvmfwk.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include <stdlib.h>
29cdf0e10cSrcweir #include <string.h>
30cdf0e10cSrcweir #include "sal/main.h"
31cdf0e10cSrcweir #include "sal/types.h"
32cdf0e10cSrcweir #include "osl/thread.h"
33cdf0e10cSrcweir #include "rtl/ustring.hxx"
34cdf0e10cSrcweir #include "rtl/byteseq.hxx"
35cdf0e10cSrcweir #include "jvmfwk/framework.h"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace rtl;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #define OUSTR(x) OUString(RTL_CONSTASCII_USTRINGPARAM( x ))
40cdf0e10cSrcweir 
41cdf0e10cSrcweir static sal_Bool hasOption(char const * szOption, int argc, char** argv);
42cdf0e10cSrcweir static rtl::OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData);
43cdf0e10cSrcweir static bool findAndSelect(JavaInfo**);
44cdf0e10cSrcweir //static sal_Bool printPaths(const OUString& sPathFile);
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #define HELP_TEXT    \
47cdf0e10cSrcweir "\njavaldx is necessary to make Java work on some UNIX platforms." \
48cdf0e10cSrcweir "It prints a string to std out that consists of directories which " \
49cdf0e10cSrcweir "have to be included into the LD_LIBRARY_PATH variable.The setting of " \
50cdf0e10cSrcweir "the variable usually occurs in a shell script that runs javaldx.\n" \
51cdf0e10cSrcweir "The directories are from the chosen java installation. \n" \
52cdf0e10cSrcweir "Options are: \n"\
53cdf0e10cSrcweir "--help or -h\n"
54cdf0e10cSrcweir 
55cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     if( hasOption("--help",argc, argv) || hasOption("-h", argc, argv))
58cdf0e10cSrcweir     {
59cdf0e10cSrcweir         fprintf(stdout, HELP_TEXT);// default
60cdf0e10cSrcweir         return 0;
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir     javaFrameworkError errcode = JFW_E_NONE;
63cdf0e10cSrcweir     sal_Bool bEnabled = sal_False;
64cdf0e10cSrcweir     errcode = jfw_getEnabled( & bEnabled);
65cdf0e10cSrcweir     if (errcode == JFW_E_NONE && bEnabled == sal_False)
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir             //Do not do any preparation because that may only slow startup time.
68cdf0e10cSrcweir         return 0;
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir     else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE)
71cdf0e10cSrcweir     {
72cdf0e10cSrcweir         fprintf(stderr,"javaldx failed!\n");
73cdf0e10cSrcweir         return -1;
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     JavaInfo * pInfo = NULL;
78cdf0e10cSrcweir     errcode = jfw_getSelectedJRE( & pInfo);
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     if (errcode == JFW_E_INVALID_SETTINGS)
81cdf0e10cSrcweir     {
82*a937066dSAriel Constenla-Haile         fprintf(stderr,"javaldx: invalid settings. User must select a JRE from options dialog!\n");
83cdf0e10cSrcweir         return -1;
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir     else if (errcode != JFW_E_NONE)
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir         fprintf(stderr,"javaldx failed!\n");
88cdf0e10cSrcweir         return -1;
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     if (pInfo == NULL)
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         if (false == findAndSelect(&pInfo))
94cdf0e10cSrcweir             return -1;
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir     else
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         //check if the JRE was not uninstalled
99cdf0e10cSrcweir         sal_Bool bExist = sal_False;
100cdf0e10cSrcweir         errcode = jfw_existJRE(pInfo, &bExist);
101cdf0e10cSrcweir         if (errcode == JFW_E_NONE)
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir             if (!bExist && !findAndSelect(&pInfo))
104cdf0e10cSrcweir                 return -1;
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir         else
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             fprintf(stderr, "javaldx: Could not determine if JRE still exist\n");
109cdf0e10cSrcweir             return -1;
110cdf0e10cSrcweir         }
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     //Only do something if the sunjavaplugin created this JavaInfo
114696f238bSAriel Constenla-Haile     rtl::OUString sVendor0(RTL_CONSTASCII_USTRINGPARAM("Oracle Corporation"));
115cdf0e10cSrcweir     rtl::OUString sVendor1(RTL_CONSTASCII_USTRINGPARAM("Sun Microsystems Inc."));
116cdf0e10cSrcweir     rtl::OUString sVendor2(RTL_CONSTASCII_USTRINGPARAM("IBM Corporation"));
117cdf0e10cSrcweir     rtl::OUString sVendor3(RTL_CONSTASCII_USTRINGPARAM("Blackdown Java-Linux Team"));
118cdf0e10cSrcweir     rtl::OUString sVendor4(RTL_CONSTASCII_USTRINGPARAM("Apple Inc."));
119cdf0e10cSrcweir     rtl::OUString sVendor5(RTL_CONSTASCII_USTRINGPARAM("Apple Computer, Inc."));
120cdf0e10cSrcweir     rtl::OUString sVendor6(RTL_CONSTASCII_USTRINGPARAM("BEA Systems, Inc."));
121cdf0e10cSrcweir     rtl::OUString sVendor7(RTL_CONSTASCII_USTRINGPARAM("Free Software Foundation, Inc."));
122cdf0e10cSrcweir     rtl::OUString sVendor8(RTL_CONSTASCII_USTRINGPARAM("The FreeBSD Foundation"));
123696f238bSAriel Constenla-Haile     if ( ! (sVendor0.equals(pInfo->sVendor) == sal_True
124696f238bSAriel Constenla-Haile             || sVendor1.equals(pInfo->sVendor) == sal_True
125cdf0e10cSrcweir             || sVendor2.equals(pInfo->sVendor) == sal_True
126cdf0e10cSrcweir             || sVendor3.equals(pInfo->sVendor) == sal_True
127cdf0e10cSrcweir             || sVendor4.equals(pInfo->sVendor) == sal_True
128cdf0e10cSrcweir             || sVendor5.equals(pInfo->sVendor) == sal_True
129cdf0e10cSrcweir             || sVendor6.equals(pInfo->sVendor) == sal_True
130cdf0e10cSrcweir             || sVendor7.equals(pInfo->sVendor) == sal_True
131cdf0e10cSrcweir             || sVendor8.equals(pInfo->sVendor) == sal_True))
132cdf0e10cSrcweir         return 0;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     rtl::OString sPaths = getLD_LIBRARY_PATH(pInfo->arVendorData);
135cdf0e10cSrcweir     fprintf(stdout, "%s\n", sPaths.getStr());
136cdf0e10cSrcweir     jfw_freeJavaInfo(pInfo);
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     return 0;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir rtl::OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData)
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     const sal_Unicode* chars = (sal_Unicode*) vendorData.getConstArray();
144cdf0e10cSrcweir     sal_Int32 len = vendorData.getLength();
145cdf0e10cSrcweir     rtl::OUString sData(chars, len / 2);
146cdf0e10cSrcweir     //the runtime lib is on the first line
147cdf0e10cSrcweir     sal_Int32 index = 0;
148cdf0e10cSrcweir     rtl::OUString aToken = sData.getToken( 1, '\n', index);
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     rtl::OString paths =
151cdf0e10cSrcweir         rtl::OUStringToOString(aToken, osl_getThreadTextEncoding());
152cdf0e10cSrcweir     return paths;
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir static sal_Bool hasOption(char const * szOption, int argc, char** argv)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir     sal_Bool retVal= sal_False;
158cdf0e10cSrcweir     for(sal_Int16 i= 1; i < argc; i++)
159cdf0e10cSrcweir     {
160cdf0e10cSrcweir         if( ! strcmp(argv[i], szOption))
161cdf0e10cSrcweir         {
162cdf0e10cSrcweir             retVal= sal_True;
163cdf0e10cSrcweir             break;
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir     return retVal;
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir static bool findAndSelect(JavaInfo ** ppInfo)
170cdf0e10cSrcweir {
171cdf0e10cSrcweir     javaFrameworkError errcode = jfw_findAndSelectJRE(ppInfo);
172cdf0e10cSrcweir     if (errcode == JFW_E_NO_JAVA_FOUND)
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         fprintf(stderr,"javaldx: Could not find a Java Runtime Environment!\n");
175cdf0e10cSrcweir         return false;
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir     else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE)
178cdf0e10cSrcweir     {
179cdf0e10cSrcweir         fprintf(stderr,"javaldx failed!\n");
180cdf0e10cSrcweir         return false;
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir     return true;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 
187