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