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 
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     if ( ! (sVendor0.equals(pInfo->sVendor) == sal_True
124             || sVendor1.equals(pInfo->sVendor) == sal_True
125             || sVendor2.equals(pInfo->sVendor) == sal_True
126             || sVendor3.equals(pInfo->sVendor) == sal_True
127             || sVendor4.equals(pInfo->sVendor) == sal_True
128             || sVendor5.equals(pInfo->sVendor) == sal_True
129             || sVendor6.equals(pInfo->sVendor) == sal_True
130             || sVendor7.equals(pInfo->sVendor) == sal_True
131             || sVendor8.equals(pInfo->sVendor) == sal_True))
132         return 0;
133 
134     rtl::OString sPaths = getLD_LIBRARY_PATH(pInfo->arVendorData);
135     fprintf(stdout, "%s\n", sPaths.getStr());
136     jfw_freeJavaInfo(pInfo);
137 
138     return 0;
139 }
140 
141 rtl::OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData)
142 {
143     const sal_Unicode* chars = (sal_Unicode*) vendorData.getConstArray();
144     sal_Int32 len = vendorData.getLength();
145     rtl::OUString sData(chars, len / 2);
146     //the runtime lib is on the first line
147     sal_Int32 index = 0;
148     rtl::OUString aToken = sData.getToken( 1, '\n', index);
149 
150     rtl::OString paths =
151         rtl::OUStringToOString(aToken, osl_getThreadTextEncoding());
152     return paths;
153 }
154 
155 static sal_Bool hasOption(char const * szOption, int argc, char** argv)
156 {
157     sal_Bool retVal= sal_False;
158     for(sal_Int16 i= 1; i < argc; i++)
159     {
160         if( ! strcmp(argv[i], szOption))
161         {
162             retVal= sal_True;
163             break;
164         }
165     }
166     return retVal;
167 }
168 
169 static bool findAndSelect(JavaInfo ** ppInfo)
170 {
171     javaFrameworkError errcode = jfw_findAndSelectJRE(ppInfo);
172     if (errcode == JFW_E_NO_JAVA_FOUND)
173     {
174         fprintf(stderr,"javaldx: Could not find a Java Runtime Environment!\n");
175         return false;
176     }
177     else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE)
178     {
179         fprintf(stderr,"javaldx failed!\n");
180         return false;
181     }
182     return true;
183 }
184 
185 
186 
187