1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_jvmfwk.hxx"
30 
31 #include "osl/file.hxx"
32 #include "osl/thread.h"
33 #include "gnujre.hxx"
34 #include "util.hxx"
35 
36 using namespace rtl;
37 using namespace std;
38 using namespace osl;
39 
40 namespace jfw_plugin
41 {
42 
43 Reference<VendorBase> GnuInfo::createInstance()
44 {
45     return new GnuInfo;
46 }
47 
48 char const* const* GnuInfo::getJavaExePaths(int * size)
49 {
50     static char const * ar[] = {
51         "gij",
52         "bin/gij",
53     };
54     *size = sizeof (ar) / sizeof (char*);
55     return ar;
56 }
57 
58 #if defined(MIPS) && defined(OSL_LITENDIAN)
59 #define GCJ_JFW_PLUGIN_ARCH "mipsel"
60 #else
61 #define GCJ_JFW_PLUGIN_ARCH JFW_PLUGIN_ARCH
62 #endif
63 
64 char const* const* GnuInfo::getRuntimePaths(int * size)
65 {
66     static char const* ar[]= {
67         "/libjvm.so",
68         "/lib/" GCJ_JFW_PLUGIN_ARCH "/client/libjvm.so",
69         "/gcj-4.1.1/libjvm.so",
70         "/libgcj.so.7",
71         "/libgcj.so.6"
72     };
73     *size = sizeof(ar) / sizeof (char*);
74     return ar;
75 }
76 
77 bool GnuInfo::initialize(vector<pair<OUString, OUString> > props)
78 {
79     //get java.vendor, java.version, java.home,
80     //javax.accessibility.assistive_technologies from system properties
81 
82     OUString sVendor;
83     OUString sJavaLibraryPath;
84     typedef vector<pair<OUString, OUString> >::const_iterator it_prop;
85     OUString sVendorProperty(
86         RTL_CONSTASCII_USTRINGPARAM("java.vendor"));
87     OUString sVersionProperty(
88         RTL_CONSTASCII_USTRINGPARAM("java.version"));
89     OUString sJavaHomeProperty(
90         RTL_CONSTASCII_USTRINGPARAM("java.home"));
91     OUString sJavaLibraryPathProperty(
92         RTL_CONSTASCII_USTRINGPARAM("java.library.path"));
93     OUString sGNUHomeProperty(
94         RTL_CONSTASCII_USTRINGPARAM("gnu.classpath.home.url"));
95     OUString sAccessProperty(
96         RTL_CONSTASCII_USTRINGPARAM("javax.accessibility.assistive_technologies"));
97 
98     bool bVersion = false;
99     bool bVendor = false;
100     bool bHome = false;
101     bool bJavaHome = false;
102     bool bJavaLibraryPath = false;
103     bool bAccess = false;
104 
105     typedef vector<pair<OUString, OUString> >::const_iterator it_prop;
106     for (it_prop i = props.begin(); i != props.end(); i++)
107     {
108         if(! bVendor && sVendorProperty.equals(i->first))
109         {
110             m_sVendor = i->second;
111             bVendor = true;
112         }
113         else if (!bVersion && sVersionProperty.equals(i->first))
114         {
115             m_sVersion = i->second;
116             bVersion = true;
117         }
118         else if (!bHome && sGNUHomeProperty.equals(i->first))
119         {
120             m_sHome = i->second;
121             bHome = true;
122         }
123         else if (!bJavaHome && sJavaHomeProperty.equals(i->first))
124         {
125            OUString fileURL;
126            if (osl_getFileURLFromSystemPath(i->second.pData,& fileURL.pData) ==
127                osl_File_E_None)
128            {
129                //make sure that the drive letter have all the same case
130                //otherwise file:///c:/jre and file:///C:/jre produce two
131                //different objects!!!
132                if (makeDriveLetterSame( & fileURL))
133                {
134                    m_sJavaHome = fileURL;
135                    bJavaHome = true;
136                }
137            }
138         }
139         else if (!bJavaLibraryPath && sJavaLibraryPathProperty.equals(i->first))
140         {
141             sal_Int32 nIndex = 0;
142             osl_getFileURLFromSystemPath(i->second.getToken(0, ':', nIndex).pData, &sJavaLibraryPath.pData);
143             bJavaLibraryPath = true;
144         }
145         else if (!bAccess && sAccessProperty.equals(i->first))
146         {
147             if (i->second.getLength() > 0)
148             {
149                 m_bAccessibility = true;
150                 bAccess = true;
151             }
152         }
153         // the javax.accessibility.xxx property may not be set. Therefore we
154         //must search through all properties.
155 
156     }
157     if (!bVersion || !bVendor || !bHome)
158         return false;
159 
160     if (!m_sJavaHome.getLength())
161         m_sJavaHome = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file:///usr/lib"));
162 
163     // init m_sRuntimeLibrary
164     OSL_ASSERT(m_sHome.getLength());
165     //call virtual function to get the possible paths to the runtime library.
166 
167     int size = 0;
168     char const* const* arRtPaths = getRuntimePaths( & size);
169     vector<OUString> libpaths = getVectorFromCharArray(arRtPaths, size);
170 
171     bool bRt = false;
172     typedef vector<OUString>::const_iterator i_path;
173     for(i_path ip = libpaths.begin(); ip != libpaths.end(); ip++)
174     {
175         //Construct an absolute path to the possible runtime
176         OUString usRt= m_sHome + *ip;
177         DirectoryItem item;
178         if(DirectoryItem::get(usRt, item) == File::E_None)
179         {
180             //found runtime lib
181             m_sRuntimeLibrary = usRt;
182             bRt = true;
183             break;
184         }
185     }
186 
187     if (!bRt)
188     {
189         m_sHome = m_sJavaHome;
190         for(i_path ip = libpaths.begin(); ip != libpaths.end(); ip++)
191         {
192             //Construct an absolute path to the possible runtime
193             OUString usRt= m_sHome + *ip;
194             DirectoryItem item;
195             if(DirectoryItem::get(usRt, item) == File::E_None)
196             {
197                 //found runtime lib
198                 m_sRuntimeLibrary = usRt;
199                 bRt = true;
200                 break;
201             }
202         }
203     }
204 
205     // try to find it by the java.library.path property
206     if (!bRt && m_sJavaHome != sJavaLibraryPath)
207     {
208         m_sHome = sJavaLibraryPath;
209         for(i_path ip = libpaths.begin(); ip != libpaths.end(); ip++)
210         {
211             //Construct an absolute path to the possible runtime
212             OUString usRt= m_sHome + *ip;
213             DirectoryItem item;
214             if(DirectoryItem::get(usRt, item) == File::E_None)
215             {
216                 //found runtime lib
217                 m_sRuntimeLibrary = usRt;
218                 bRt = true;
219                 break;
220             }
221         }
222     }
223 
224 #ifdef X86_64
225     //Make one last final legacy attempt on x86_64 in case the distro placed it in lib64 instead
226     if (!bRt && m_sJavaHome != rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file:///usr/lib")))
227     {
228         m_sHome = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file:///usr/lib64"));
229         for(i_path ip = libpaths.begin(); ip != libpaths.end(); ip++)
230         {
231             //Construct an absolute path to the possible runtime
232             OUString usRt= m_sHome + *ip;
233             DirectoryItem item;
234             if(DirectoryItem::get(usRt, item) == File::E_None)
235             {
236                 //found runtime lib
237                 m_sRuntimeLibrary = usRt;
238                 bRt = true;
239                 break;
240             }
241         }
242     }
243 #endif
244 
245     if (!bRt)
246         return false;
247 
248     // init m_sLD_LIBRARY_PATH
249     OSL_ASSERT(m_sHome.getLength());
250     size = 0;
251     char const * const * arLDPaths = getLibraryPaths( & size);
252     vector<OUString> ld_paths = getVectorFromCharArray(arLDPaths, size);
253 
254     char arSep[]= {SAL_PATHSEPARATOR, 0};
255     OUString sPathSep= OUString::createFromAscii(arSep);
256     bool bLdPath = true;
257     int c = 0;
258     for(i_path il = ld_paths.begin(); il != ld_paths.end(); il ++, c++)
259     {
260         OUString usAbsUrl= m_sHome + *il;
261         // convert to system path
262         OUString usSysPath;
263         if(File::getSystemPathFromFileURL(usAbsUrl, usSysPath) == File::E_None)
264         {
265 
266             if(c > 0)
267                 m_sLD_LIBRARY_PATH+= sPathSep;
268             m_sLD_LIBRARY_PATH+= usSysPath;
269         }
270         else
271         {
272             bLdPath = false;
273             break;
274         }
275     }
276     if (bLdPath == false)
277         return false;
278 
279     return true;
280 }
281 
282 int GnuInfo::compareVersions(const rtl::OUString&) const
283 {
284 	return 0;
285 }
286 
287 }
288