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
10*36f55ffcSAndrew Rist  *
11*36f55ffcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*36f55ffcSAndrew Rist  *
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.
19*36f55ffcSAndrew Rist  *
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 "osl/file.hxx"
28cdf0e10cSrcweir #include "osl/thread.h"
29cdf0e10cSrcweir #include "gnujre.hxx"
30cdf0e10cSrcweir #include "util.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir using namespace rtl;
33cdf0e10cSrcweir using namespace std;
34cdf0e10cSrcweir using namespace osl;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir namespace jfw_plugin
37cdf0e10cSrcweir {
38cdf0e10cSrcweir 
createInstance()39cdf0e10cSrcweir Reference<VendorBase> GnuInfo::createInstance()
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     return new GnuInfo;
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
getJavaExePaths(int * size)44cdf0e10cSrcweir char const* const* GnuInfo::getJavaExePaths(int * size)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     static char const * ar[] = {
47cdf0e10cSrcweir         "gij",
48cdf0e10cSrcweir         "bin/gij",
49cdf0e10cSrcweir     };
50cdf0e10cSrcweir     *size = sizeof (ar) / sizeof (char*);
51cdf0e10cSrcweir     return ar;
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir #if defined(MIPS) && defined(OSL_LITENDIAN)
55cdf0e10cSrcweir #define GCJ_JFW_PLUGIN_ARCH "mipsel"
56cdf0e10cSrcweir #else
57cdf0e10cSrcweir #define GCJ_JFW_PLUGIN_ARCH JFW_PLUGIN_ARCH
58cdf0e10cSrcweir #endif
59cdf0e10cSrcweir 
getRuntimePaths(int * size)60cdf0e10cSrcweir char const* const* GnuInfo::getRuntimePaths(int * size)
61cdf0e10cSrcweir {
62cdf0e10cSrcweir     static char const* ar[]= {
63cdf0e10cSrcweir         "/libjvm.so",
64cdf0e10cSrcweir         "/lib/" GCJ_JFW_PLUGIN_ARCH "/client/libjvm.so",
65cdf0e10cSrcweir         "/gcj-4.1.1/libjvm.so",
66cdf0e10cSrcweir         "/libgcj.so.7",
67cdf0e10cSrcweir         "/libgcj.so.6"
68cdf0e10cSrcweir     };
69cdf0e10cSrcweir     *size = sizeof(ar) / sizeof (char*);
70cdf0e10cSrcweir     return ar;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
initialize(vector<pair<OUString,OUString>> props)73cdf0e10cSrcweir bool GnuInfo::initialize(vector<pair<OUString, OUString> > props)
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     //get java.vendor, java.version, java.home,
76cdf0e10cSrcweir     //javax.accessibility.assistive_technologies from system properties
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     OUString sVendor;
79cdf0e10cSrcweir     OUString sJavaLibraryPath;
80cdf0e10cSrcweir     typedef vector<pair<OUString, OUString> >::const_iterator it_prop;
81cdf0e10cSrcweir     OUString sVendorProperty(
82cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("java.vendor"));
83cdf0e10cSrcweir     OUString sVersionProperty(
84cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("java.version"));
85cdf0e10cSrcweir     OUString sJavaHomeProperty(
86cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("java.home"));
87cdf0e10cSrcweir     OUString sJavaLibraryPathProperty(
88cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("java.library.path"));
89cdf0e10cSrcweir     OUString sGNUHomeProperty(
90cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("gnu.classpath.home.url"));
91cdf0e10cSrcweir     OUString sAccessProperty(
92cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("javax.accessibility.assistive_technologies"));
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     bool bVersion = false;
95cdf0e10cSrcweir     bool bVendor = false;
96cdf0e10cSrcweir     bool bHome = false;
97cdf0e10cSrcweir     bool bJavaHome = false;
98cdf0e10cSrcweir     bool bJavaLibraryPath = false;
99cdf0e10cSrcweir     bool bAccess = false;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     typedef vector<pair<OUString, OUString> >::const_iterator it_prop;
102cdf0e10cSrcweir     for (it_prop i = props.begin(); i != props.end(); i++)
103cdf0e10cSrcweir     {
104cdf0e10cSrcweir         if(! bVendor && sVendorProperty.equals(i->first))
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             m_sVendor = i->second;
107cdf0e10cSrcweir             bVendor = true;
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir         else if (!bVersion && sVersionProperty.equals(i->first))
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             m_sVersion = i->second;
112cdf0e10cSrcweir             bVersion = true;
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         else if (!bHome && sGNUHomeProperty.equals(i->first))
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             m_sHome = i->second;
117cdf0e10cSrcweir             bHome = true;
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir         else if (!bJavaHome && sJavaHomeProperty.equals(i->first))
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir            OUString fileURL;
122cdf0e10cSrcweir            if (osl_getFileURLFromSystemPath(i->second.pData,& fileURL.pData) ==
123cdf0e10cSrcweir                osl_File_E_None)
124cdf0e10cSrcweir            {
125cdf0e10cSrcweir                //make sure that the drive letter have all the same case
126cdf0e10cSrcweir                //otherwise file:///c:/jre and file:///C:/jre produce two
127cdf0e10cSrcweir                //different objects!!!
128cdf0e10cSrcweir                if (makeDriveLetterSame( & fileURL))
129cdf0e10cSrcweir                {
130cdf0e10cSrcweir                    m_sJavaHome = fileURL;
131cdf0e10cSrcweir                    bJavaHome = true;
132cdf0e10cSrcweir                }
133cdf0e10cSrcweir            }
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir         else if (!bJavaLibraryPath && sJavaLibraryPathProperty.equals(i->first))
136cdf0e10cSrcweir         {
137cdf0e10cSrcweir             sal_Int32 nIndex = 0;
138cdf0e10cSrcweir             osl_getFileURLFromSystemPath(i->second.getToken(0, ':', nIndex).pData, &sJavaLibraryPath.pData);
139cdf0e10cSrcweir             bJavaLibraryPath = true;
140cdf0e10cSrcweir         }
141cdf0e10cSrcweir         else if (!bAccess && sAccessProperty.equals(i->first))
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             if (i->second.getLength() > 0)
144cdf0e10cSrcweir             {
145cdf0e10cSrcweir                 m_bAccessibility = true;
146cdf0e10cSrcweir                 bAccess = true;
147cdf0e10cSrcweir             }
148cdf0e10cSrcweir         }
149cdf0e10cSrcweir         // the javax.accessibility.xxx property may not be set. Therefore we
150cdf0e10cSrcweir         //must search through all properties.
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir     if (!bVersion || !bVendor || !bHome)
154cdf0e10cSrcweir         return false;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     if (!m_sJavaHome.getLength())
157cdf0e10cSrcweir         m_sJavaHome = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file:///usr/lib"));
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     // init m_sRuntimeLibrary
160cdf0e10cSrcweir     OSL_ASSERT(m_sHome.getLength());
161cdf0e10cSrcweir     //call virtual function to get the possible paths to the runtime library.
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     int size = 0;
164cdf0e10cSrcweir     char const* const* arRtPaths = getRuntimePaths( & size);
165cdf0e10cSrcweir     vector<OUString> libpaths = getVectorFromCharArray(arRtPaths, size);
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     bool bRt = false;
168cdf0e10cSrcweir     typedef vector<OUString>::const_iterator i_path;
169cdf0e10cSrcweir     for(i_path ip = libpaths.begin(); ip != libpaths.end(); ip++)
170cdf0e10cSrcweir     {
171cdf0e10cSrcweir         //Construct an absolute path to the possible runtime
172cdf0e10cSrcweir         OUString usRt= m_sHome + *ip;
173cdf0e10cSrcweir         DirectoryItem item;
174cdf0e10cSrcweir         if(DirectoryItem::get(usRt, item) == File::E_None)
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             //found runtime lib
177cdf0e10cSrcweir             m_sRuntimeLibrary = usRt;
178cdf0e10cSrcweir             bRt = true;
179cdf0e10cSrcweir             break;
180cdf0e10cSrcweir         }
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     if (!bRt)
184cdf0e10cSrcweir     {
185cdf0e10cSrcweir         m_sHome = m_sJavaHome;
186cdf0e10cSrcweir         for(i_path ip = libpaths.begin(); ip != libpaths.end(); ip++)
187cdf0e10cSrcweir         {
188cdf0e10cSrcweir             //Construct an absolute path to the possible runtime
189cdf0e10cSrcweir             OUString usRt= m_sHome + *ip;
190cdf0e10cSrcweir             DirectoryItem item;
191cdf0e10cSrcweir             if(DirectoryItem::get(usRt, item) == File::E_None)
192cdf0e10cSrcweir             {
193cdf0e10cSrcweir                 //found runtime lib
194cdf0e10cSrcweir                 m_sRuntimeLibrary = usRt;
195cdf0e10cSrcweir                 bRt = true;
196cdf0e10cSrcweir                 break;
197cdf0e10cSrcweir             }
198cdf0e10cSrcweir         }
199cdf0e10cSrcweir     }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     // try to find it by the java.library.path property
202cdf0e10cSrcweir     if (!bRt && m_sJavaHome != sJavaLibraryPath)
203cdf0e10cSrcweir     {
204cdf0e10cSrcweir         m_sHome = sJavaLibraryPath;
205cdf0e10cSrcweir         for(i_path ip = libpaths.begin(); ip != libpaths.end(); ip++)
206cdf0e10cSrcweir         {
207cdf0e10cSrcweir             //Construct an absolute path to the possible runtime
208cdf0e10cSrcweir             OUString usRt= m_sHome + *ip;
209cdf0e10cSrcweir             DirectoryItem item;
210cdf0e10cSrcweir             if(DirectoryItem::get(usRt, item) == File::E_None)
211cdf0e10cSrcweir             {
212cdf0e10cSrcweir                 //found runtime lib
213cdf0e10cSrcweir                 m_sRuntimeLibrary = usRt;
214cdf0e10cSrcweir                 bRt = true;
215cdf0e10cSrcweir                 break;
216cdf0e10cSrcweir             }
217cdf0e10cSrcweir         }
218cdf0e10cSrcweir     }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir #ifdef X86_64
221cdf0e10cSrcweir     //Make one last final legacy attempt on x86_64 in case the distro placed it in lib64 instead
222cdf0e10cSrcweir     if (!bRt && m_sJavaHome != rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file:///usr/lib")))
223cdf0e10cSrcweir     {
224cdf0e10cSrcweir         m_sHome = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file:///usr/lib64"));
225cdf0e10cSrcweir         for(i_path ip = libpaths.begin(); ip != libpaths.end(); ip++)
226cdf0e10cSrcweir         {
227cdf0e10cSrcweir             //Construct an absolute path to the possible runtime
228cdf0e10cSrcweir             OUString usRt= m_sHome + *ip;
229cdf0e10cSrcweir             DirectoryItem item;
230cdf0e10cSrcweir             if(DirectoryItem::get(usRt, item) == File::E_None)
231cdf0e10cSrcweir             {
232cdf0e10cSrcweir                 //found runtime lib
233cdf0e10cSrcweir                 m_sRuntimeLibrary = usRt;
234cdf0e10cSrcweir                 bRt = true;
235cdf0e10cSrcweir                 break;
236cdf0e10cSrcweir             }
237cdf0e10cSrcweir         }
238cdf0e10cSrcweir     }
239cdf0e10cSrcweir #endif
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     if (!bRt)
242cdf0e10cSrcweir         return false;
243cdf0e10cSrcweir 
244cdf0e10cSrcweir     // init m_sLD_LIBRARY_PATH
245cdf0e10cSrcweir     OSL_ASSERT(m_sHome.getLength());
246cdf0e10cSrcweir     size = 0;
247cdf0e10cSrcweir     char const * const * arLDPaths = getLibraryPaths( & size);
248cdf0e10cSrcweir     vector<OUString> ld_paths = getVectorFromCharArray(arLDPaths, size);
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     char arSep[]= {SAL_PATHSEPARATOR, 0};
251cdf0e10cSrcweir     OUString sPathSep= OUString::createFromAscii(arSep);
252cdf0e10cSrcweir     bool bLdPath = true;
253cdf0e10cSrcweir     int c = 0;
254cdf0e10cSrcweir     for(i_path il = ld_paths.begin(); il != ld_paths.end(); il ++, c++)
255cdf0e10cSrcweir     {
256cdf0e10cSrcweir         OUString usAbsUrl= m_sHome + *il;
257cdf0e10cSrcweir         // convert to system path
258cdf0e10cSrcweir         OUString usSysPath;
259cdf0e10cSrcweir         if(File::getSystemPathFromFileURL(usAbsUrl, usSysPath) == File::E_None)
260cdf0e10cSrcweir         {
261cdf0e10cSrcweir 
262cdf0e10cSrcweir             if(c > 0)
263cdf0e10cSrcweir                 m_sLD_LIBRARY_PATH+= sPathSep;
264cdf0e10cSrcweir             m_sLD_LIBRARY_PATH+= usSysPath;
265cdf0e10cSrcweir         }
266cdf0e10cSrcweir         else
267cdf0e10cSrcweir         {
268cdf0e10cSrcweir             bLdPath = false;
269cdf0e10cSrcweir             break;
270cdf0e10cSrcweir         }
271cdf0e10cSrcweir     }
272cdf0e10cSrcweir     if (bLdPath == false)
273cdf0e10cSrcweir         return false;
274cdf0e10cSrcweir 
275cdf0e10cSrcweir     return true;
276cdf0e10cSrcweir }
277cdf0e10cSrcweir 
compareVersions(const rtl::OUString &) const278cdf0e10cSrcweir int GnuInfo::compareVersions(const rtl::OUString&) const
279cdf0e10cSrcweir {
280cdf0e10cSrcweir 	return 0;
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
283cdf0e10cSrcweir }
284