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