xref: /AOO41X/main/jvmfwk/plugins/sunmajor/javaenvsetup/javaldx.cxx (revision 36f55ffc0c229af2ff5a030b2f51d4c800cb77c9)
1*36f55ffcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*36f55ffcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*36f55ffcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*36f55ffcSAndrew Rist  * distributed with this work for additional information
6*36f55ffcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*36f55ffcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*36f55ffcSAndrew Rist  * "License"); you may not use this file except in compliance
9*36f55ffcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*36f55ffcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*36f55ffcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*36f55ffcSAndrew Rist  * software distributed under the License is distributed on an
15*36f55ffcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*36f55ffcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*36f55ffcSAndrew Rist  * specific language governing permissions and limitations
18*36f55ffcSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*36f55ffcSAndrew Rist  *************************************************************/
21*36f55ffcSAndrew Rist 
22*36f55ffcSAndrew 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     {
82cdf0e10cSrcweir         fprintf(stderr,"javaldx failed. User must select a JRE from options dialog!");
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
114cdf0e10cSrcweir     rtl::OUString sVendor1(RTL_CONSTASCII_USTRINGPARAM("Sun Microsystems Inc."));
115cdf0e10cSrcweir     rtl::OUString sVendor2(RTL_CONSTASCII_USTRINGPARAM("IBM Corporation"));
116cdf0e10cSrcweir     rtl::OUString sVendor3(RTL_CONSTASCII_USTRINGPARAM("Blackdown Java-Linux Team"));
117cdf0e10cSrcweir     rtl::OUString sVendor4(RTL_CONSTASCII_USTRINGPARAM("Apple Inc."));
118cdf0e10cSrcweir     rtl::OUString sVendor5(RTL_CONSTASCII_USTRINGPARAM("Apple Computer, Inc."));
119cdf0e10cSrcweir     rtl::OUString sVendor6(RTL_CONSTASCII_USTRINGPARAM("BEA Systems, Inc."));
120cdf0e10cSrcweir     rtl::OUString sVendor7(RTL_CONSTASCII_USTRINGPARAM("Free Software Foundation, Inc."));
121cdf0e10cSrcweir     rtl::OUString sVendor8(RTL_CONSTASCII_USTRINGPARAM("The FreeBSD Foundation"));
122cdf0e10cSrcweir     if ( ! (sVendor1.equals(pInfo->sVendor) == sal_True
123cdf0e10cSrcweir             || sVendor2.equals(pInfo->sVendor) == sal_True
124cdf0e10cSrcweir             || sVendor3.equals(pInfo->sVendor) == sal_True
125cdf0e10cSrcweir             || sVendor4.equals(pInfo->sVendor) == sal_True
126cdf0e10cSrcweir             || sVendor5.equals(pInfo->sVendor) == sal_True
127cdf0e10cSrcweir             || sVendor6.equals(pInfo->sVendor) == sal_True
128cdf0e10cSrcweir             || sVendor7.equals(pInfo->sVendor) == sal_True
129cdf0e10cSrcweir             || sVendor8.equals(pInfo->sVendor) == sal_True))
130cdf0e10cSrcweir         return 0;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     rtl::OString sPaths = getLD_LIBRARY_PATH(pInfo->arVendorData);
133cdf0e10cSrcweir     fprintf(stdout, "%s\n", sPaths.getStr());
134cdf0e10cSrcweir     jfw_freeJavaInfo(pInfo);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     return 0;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir rtl::OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData)
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     const sal_Unicode* chars = (sal_Unicode*) vendorData.getConstArray();
142cdf0e10cSrcweir     sal_Int32 len = vendorData.getLength();
143cdf0e10cSrcweir     rtl::OUString sData(chars, len / 2);
144cdf0e10cSrcweir     //the runtime lib is on the first line
145cdf0e10cSrcweir     sal_Int32 index = 0;
146cdf0e10cSrcweir     rtl::OUString aToken = sData.getToken( 1, '\n', index);
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     rtl::OString paths =
149cdf0e10cSrcweir         rtl::OUStringToOString(aToken, osl_getThreadTextEncoding());
150cdf0e10cSrcweir     return paths;
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir static sal_Bool hasOption(char const * szOption, int argc, char** argv)
154cdf0e10cSrcweir {
155cdf0e10cSrcweir     sal_Bool retVal= sal_False;
156cdf0e10cSrcweir     for(sal_Int16 i= 1; i < argc; i++)
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         if( ! strcmp(argv[i], szOption))
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             retVal= sal_True;
161cdf0e10cSrcweir             break;
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir     return retVal;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir static bool findAndSelect(JavaInfo ** ppInfo)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir     javaFrameworkError errcode = jfw_findAndSelectJRE(ppInfo);
170cdf0e10cSrcweir     if (errcode == JFW_E_NO_JAVA_FOUND)
171cdf0e10cSrcweir     {
172cdf0e10cSrcweir         fprintf(stderr,"javaldx: Could not find a Java Runtime Environment! \n");
173cdf0e10cSrcweir         return false;
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir     else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE)
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         fprintf(stderr,"javaldx failed!\n");
178cdf0e10cSrcweir         return false;
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir     return true;
181cdf0e10cSrcweir }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 
185