xref: /aoo41x/main/jvmfwk/source/fwkutil.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_jvmfwk.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #if defined WNT
32*cdf0e10cSrcweir #if defined _MSC_VER
33*cdf0e10cSrcweir #pragma warning(push, 1)
34*cdf0e10cSrcweir #endif
35*cdf0e10cSrcweir #include <windows.h>
36*cdf0e10cSrcweir #if defined _MSC_VER
37*cdf0e10cSrcweir #pragma warning(pop)
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir #endif
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <string>
42*cdf0e10cSrcweir #include <string.h>
43*cdf0e10cSrcweir #include "osl/mutex.hxx"
44*cdf0e10cSrcweir #include "osl/module.hxx"
45*cdf0e10cSrcweir #include "osl/thread.hxx"
46*cdf0e10cSrcweir #include "rtl/ustring.hxx"
47*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
48*cdf0e10cSrcweir #include "rtl/bootstrap.hxx"
49*cdf0e10cSrcweir #include "osl/file.hxx"
50*cdf0e10cSrcweir #include "osl/process.h"
51*cdf0e10cSrcweir #include "rtl/instance.hxx"
52*cdf0e10cSrcweir #include "rtl/uri.hxx"
53*cdf0e10cSrcweir #include "osl/getglobalmutex.hxx"
54*cdf0e10cSrcweir #include "com/sun/star/lang/IllegalArgumentException.hpp"
55*cdf0e10cSrcweir #include "cppuhelper/bootstrap.hxx"
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #include "framework.hxx"
58*cdf0e10cSrcweir #include "fwkutil.hxx"
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir using namespace rtl;
61*cdf0e10cSrcweir using namespace osl;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir namespace jfw
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir bool isAccessibilitySupportDesired()
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir     OUString sValue;
69*cdf0e10cSrcweir     if ((sal_True == ::rtl::Bootstrap::get(
70*cdf0e10cSrcweir         OUString(RTL_CONSTASCII_USTRINGPARAM("JFW_PLUGIN_DO_NOT_CHECK_ACCESSIBILITY")), sValue))
71*cdf0e10cSrcweir         && sValue.equals(OUString(RTL_CONSTASCII_USTRINGPARAM("1")))
72*cdf0e10cSrcweir         )
73*cdf0e10cSrcweir         return false;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     bool retVal = false;
76*cdf0e10cSrcweir #ifdef WNT
77*cdf0e10cSrcweir     HKEY    hKey = 0;
78*cdf0e10cSrcweir     if (RegOpenKeyEx(HKEY_CURRENT_USER,
79*cdf0e10cSrcweir                      "Software\\OpenOffice.org\\Accessibility\\AtToolSupport",
80*cdf0e10cSrcweir                      0, KEY_READ, &hKey) == ERROR_SUCCESS)
81*cdf0e10cSrcweir     {
82*cdf0e10cSrcweir         DWORD   dwType = 0;
83*cdf0e10cSrcweir         DWORD   dwLen = 16;
84*cdf0e10cSrcweir         unsigned char arData[16];
85*cdf0e10cSrcweir         if( RegQueryValueEx(hKey, "SupportAssistiveTechnology", NULL, &dwType, arData,
86*cdf0e10cSrcweir                             & dwLen)== ERROR_SUCCESS)
87*cdf0e10cSrcweir         {
88*cdf0e10cSrcweir             if (dwType == REG_SZ)
89*cdf0e10cSrcweir             {
90*cdf0e10cSrcweir                 if (strcmp((char*) arData, "true") == 0
91*cdf0e10cSrcweir                     || strcmp((char*) arData, "1") == 0)
92*cdf0e10cSrcweir                     retVal = true;
93*cdf0e10cSrcweir                 else if (strcmp((char*) arData, "false") == 0
94*cdf0e10cSrcweir                          || strcmp((char*) arData, "0") == 0)
95*cdf0e10cSrcweir                     retVal = false;
96*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
97*cdf0e10cSrcweir                 else
98*cdf0e10cSrcweir                     OSL_ASSERT(0);
99*cdf0e10cSrcweir #endif
100*cdf0e10cSrcweir             }
101*cdf0e10cSrcweir             else if (dwType == REG_DWORD)
102*cdf0e10cSrcweir             {
103*cdf0e10cSrcweir                 if (arData[0] == 1)
104*cdf0e10cSrcweir                     retVal = true;
105*cdf0e10cSrcweir                 else if (arData[0] == 0)
106*cdf0e10cSrcweir                     retVal = false;
107*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
108*cdf0e10cSrcweir                 else
109*cdf0e10cSrcweir                     OSL_ASSERT(0);
110*cdf0e10cSrcweir #endif
111*cdf0e10cSrcweir             }
112*cdf0e10cSrcweir         }
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir     RegCloseKey(hKey);
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir #elif UNX
117*cdf0e10cSrcweir     char buf[16];
118*cdf0e10cSrcweir     // use 2 shells to suppress the eventual "gcontool-2 not found" message
119*cdf0e10cSrcweir     // of the shell trying to execute the command
120*cdf0e10cSrcweir     FILE* fp = popen( "/bin/sh 2>/dev/null -c \"gconftool-2 -g /desktop/gnome/interface/accessibility\"", "r" );
121*cdf0e10cSrcweir     if( fp )
122*cdf0e10cSrcweir     {
123*cdf0e10cSrcweir         if( fgets( buf, sizeof(buf), fp ) )
124*cdf0e10cSrcweir         {
125*cdf0e10cSrcweir             int nCompare = strncasecmp( buf, "true", 4 );
126*cdf0e10cSrcweir             retVal = (nCompare == 0 ? true : false);
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir         pclose( fp );
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir #endif
131*cdf0e10cSrcweir     return retVal;
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir rtl::ByteSequence encodeBase16(const rtl::ByteSequence& rawData)
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir     static char EncodingTable[] =
138*cdf0e10cSrcweir         {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
139*cdf0e10cSrcweir     sal_Int32 lenRaw = rawData.getLength();
140*cdf0e10cSrcweir     char* pBuf = new char[lenRaw * 2];
141*cdf0e10cSrcweir     const sal_Int8* arRaw = rawData.getConstArray();
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     char* pCurBuf = pBuf;
144*cdf0e10cSrcweir     for (int i = 0; i < lenRaw; i++)
145*cdf0e10cSrcweir     {
146*cdf0e10cSrcweir         unsigned char curChar = arRaw[i];
147*cdf0e10cSrcweir         curChar >>= 4;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         *pCurBuf = EncodingTable[curChar];
150*cdf0e10cSrcweir         pCurBuf++;
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         curChar = arRaw[i];
153*cdf0e10cSrcweir         curChar &= 0x0F;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         *pCurBuf = EncodingTable[curChar];
156*cdf0e10cSrcweir         pCurBuf++;
157*cdf0e10cSrcweir     }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     rtl::ByteSequence ret((sal_Int8*) pBuf, lenRaw * 2);
160*cdf0e10cSrcweir     delete [] pBuf;
161*cdf0e10cSrcweir     return ret;
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir rtl::ByteSequence decodeBase16(const rtl::ByteSequence& data)
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir     static char decodingTable[] =
167*cdf0e10cSrcweir         {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
168*cdf0e10cSrcweir     sal_Int32 lenData = data.getLength();
169*cdf0e10cSrcweir     sal_Int32 lenBuf = lenData / 2; //always divisable by two
170*cdf0e10cSrcweir     unsigned char* pBuf = new unsigned char[lenBuf];
171*cdf0e10cSrcweir     const sal_Int8* pData = data.getConstArray();
172*cdf0e10cSrcweir     for (sal_Int32 i = 0; i < lenBuf; i++)
173*cdf0e10cSrcweir     {
174*cdf0e10cSrcweir         sal_Int8 curChar = *pData++;
175*cdf0e10cSrcweir         //find the index of the first 4bits
176*cdf0e10cSrcweir 		//  TODO  What happens if text is not valid Hex characters?
177*cdf0e10cSrcweir         unsigned char nibble = 0;
178*cdf0e10cSrcweir         for (unsigned char j = 0; j < 16; j++)
179*cdf0e10cSrcweir         {
180*cdf0e10cSrcweir             if (curChar == decodingTable[j])
181*cdf0e10cSrcweir             {
182*cdf0e10cSrcweir                 nibble = j;
183*cdf0e10cSrcweir                 break;
184*cdf0e10cSrcweir             }
185*cdf0e10cSrcweir         }
186*cdf0e10cSrcweir         nibble <<= 4;
187*cdf0e10cSrcweir         curChar = *pData++;
188*cdf0e10cSrcweir         //find the index for the next 4bits
189*cdf0e10cSrcweir         for (unsigned char j = 0; j < 16; j++)
190*cdf0e10cSrcweir         {
191*cdf0e10cSrcweir             if (curChar == decodingTable[j])
192*cdf0e10cSrcweir             {
193*cdf0e10cSrcweir                 nibble |= j;
194*cdf0e10cSrcweir                 break;
195*cdf0e10cSrcweir             }
196*cdf0e10cSrcweir         }
197*cdf0e10cSrcweir         pBuf[i] = nibble;
198*cdf0e10cSrcweir     }
199*cdf0e10cSrcweir     rtl::ByteSequence ret((sal_Int8*) pBuf, lenBuf );
200*cdf0e10cSrcweir     delete [] pBuf;
201*cdf0e10cSrcweir     return ret;
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir rtl::OUString getDirFromFile(const rtl::OUString& usFilePath)
205*cdf0e10cSrcweir {
206*cdf0e10cSrcweir     sal_Int32 index= usFilePath.lastIndexOf('/');
207*cdf0e10cSrcweir     return rtl::OUString(usFilePath.getStr(), index);
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir rtl::OUString getExecutableDirectory()
211*cdf0e10cSrcweir {
212*cdf0e10cSrcweir     rtl_uString* sExe = NULL;
213*cdf0e10cSrcweir     if (osl_getExecutableFile( & sExe) != osl_Process_E_None)
214*cdf0e10cSrcweir         throw FrameworkException(
215*cdf0e10cSrcweir             JFW_E_ERROR,
216*cdf0e10cSrcweir             "[Java framework] Error in function getExecutableDirectory (fwkutil.cxx)");
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     rtl::OUString ouExe(sExe, SAL_NO_ACQUIRE);
219*cdf0e10cSrcweir     return getDirFromFile(ouExe);
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir rtl::OUString findPlugin(
223*cdf0e10cSrcweir     const rtl::OUString & baseUrl, const rtl::OUString & plugin)
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir     rtl::OUString expandedPlugin;
226*cdf0e10cSrcweir     try
227*cdf0e10cSrcweir     {
228*cdf0e10cSrcweir         expandedPlugin = cppu::bootstrap_expandUri(plugin);
229*cdf0e10cSrcweir     }
230*cdf0e10cSrcweir     catch (com::sun::star::lang::IllegalArgumentException & e)
231*cdf0e10cSrcweir     {
232*cdf0e10cSrcweir         throw FrameworkException(
233*cdf0e10cSrcweir             JFW_E_ERROR,
234*cdf0e10cSrcweir             (rtl::OString(
235*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM(
236*cdf0e10cSrcweir                     "[Java framework] IllegalArgumentException in"
237*cdf0e10cSrcweir                     " findPlugin: "))
238*cdf0e10cSrcweir              + rtl::OUStringToOString(e.Message, osl_getThreadTextEncoding())));
239*cdf0e10cSrcweir     }
240*cdf0e10cSrcweir     rtl::OUString sUrl;
241*cdf0e10cSrcweir     try
242*cdf0e10cSrcweir     {
243*cdf0e10cSrcweir         sUrl = rtl::Uri::convertRelToAbs(baseUrl, expandedPlugin);
244*cdf0e10cSrcweir     }
245*cdf0e10cSrcweir     catch (rtl::MalformedUriException & e)
246*cdf0e10cSrcweir     {
247*cdf0e10cSrcweir         throw FrameworkException(
248*cdf0e10cSrcweir             JFW_E_ERROR,
249*cdf0e10cSrcweir             (rtl::OString(
250*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM(
251*cdf0e10cSrcweir                     "[Java framework] rtl::MalformedUriException in"
252*cdf0e10cSrcweir                     " findPlugin: "))
253*cdf0e10cSrcweir              + rtl::OUStringToOString(
254*cdf0e10cSrcweir                  e.getMessage(), osl_getThreadTextEncoding())));
255*cdf0e10cSrcweir     }
256*cdf0e10cSrcweir     if (checkFileURL(sUrl) == jfw::FILE_OK)
257*cdf0e10cSrcweir     {
258*cdf0e10cSrcweir         return sUrl;
259*cdf0e10cSrcweir     }
260*cdf0e10cSrcweir     rtl::OUString retVal;
261*cdf0e10cSrcweir     rtl::OUString sProgDir = getExecutableDirectory();
262*cdf0e10cSrcweir     sUrl = sProgDir + rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"))
263*cdf0e10cSrcweir         + plugin;
264*cdf0e10cSrcweir     jfw::FileStatus s = checkFileURL(sUrl);
265*cdf0e10cSrcweir     if (s == jfw::FILE_INVALID || s == jfw::FILE_DOES_NOT_EXIST)
266*cdf0e10cSrcweir     {
267*cdf0e10cSrcweir         //If only the name of the library is given, then
268*cdf0e10cSrcweir         //use PATH, LD_LIBRARY_PATH etc. to locate the plugin
269*cdf0e10cSrcweir         if (plugin.indexOf('/') == -1)
270*cdf0e10cSrcweir         {
271*cdf0e10cSrcweir             rtl::OUString url;
272*cdf0e10cSrcweir #ifdef UNX
273*cdf0e10cSrcweir #ifdef MACOSX
274*cdf0e10cSrcweir             rtl::OUString path = rtl::OUString::createFromAscii("DYLD_LIBRARY_PATH");
275*cdf0e10cSrcweir #else
276*cdf0e10cSrcweir             rtl::OUString path = rtl::OUString::createFromAscii("LD_LIBRARY_PATH");
277*cdf0e10cSrcweir #endif
278*cdf0e10cSrcweir             rtl::OUString env_path;
279*cdf0e10cSrcweir             oslProcessError err = osl_getEnvironment(path.pData, &env_path.pData);
280*cdf0e10cSrcweir             if (err != osl_Process_E_None && err != osl_Process_E_NotFound)
281*cdf0e10cSrcweir                 throw FrameworkException(
282*cdf0e10cSrcweir                     JFW_E_ERROR,
283*cdf0e10cSrcweir                     "[Java framework] Error in function findPlugin (fwkutil.cxx).");
284*cdf0e10cSrcweir             if (err == osl_Process_E_NotFound)
285*cdf0e10cSrcweir                 return retVal;
286*cdf0e10cSrcweir             if (osl_searchFileURL(plugin.pData, env_path.pData, &url.pData)
287*cdf0e10cSrcweir                                 == osl_File_E_None)
288*cdf0e10cSrcweir #else
289*cdf0e10cSrcweir             if (osl_searchFileURL(plugin.pData, NULL, &url.pData)
290*cdf0e10cSrcweir                 == osl_File_E_None)
291*cdf0e10cSrcweir #endif
292*cdf0e10cSrcweir                 retVal = url;
293*cdf0e10cSrcweir             else
294*cdf0e10cSrcweir                 throw FrameworkException(
295*cdf0e10cSrcweir                     JFW_E_ERROR,
296*cdf0e10cSrcweir                     "[Java framework] Error in function findPlugin (fwkutil.cxx).");
297*cdf0e10cSrcweir         }
298*cdf0e10cSrcweir     }
299*cdf0e10cSrcweir 	else
300*cdf0e10cSrcweir 	{
301*cdf0e10cSrcweir 		retVal = sUrl;
302*cdf0e10cSrcweir 	}
303*cdf0e10cSrcweir     return retVal;
304*cdf0e10cSrcweir }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir rtl::OUString getLibraryLocation()
307*cdf0e10cSrcweir {
308*cdf0e10cSrcweir     rtl::OString sExcMsg("[Java framework] Error in function getLibraryLocation "
309*cdf0e10cSrcweir                          "(fwkutil.cxx).");
310*cdf0e10cSrcweir     rtl::OUString libraryFileUrl;
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir     if (!osl::Module::getUrlFromAddress(
313*cdf0e10cSrcweir             reinterpret_cast< oslGenericFunction >(getLibraryLocation),
314*cdf0e10cSrcweir             libraryFileUrl))
315*cdf0e10cSrcweir         throw FrameworkException(JFW_E_ERROR, sExcMsg);
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir     return getDirFromFile(libraryFileUrl);
318*cdf0e10cSrcweir }
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir jfw::FileStatus checkFileURL(const rtl::OUString & sURL)
321*cdf0e10cSrcweir {
322*cdf0e10cSrcweir     jfw::FileStatus ret = jfw::FILE_OK;
323*cdf0e10cSrcweir     DirectoryItem item;
324*cdf0e10cSrcweir     File::RC rc_item = DirectoryItem::get(sURL, item);
325*cdf0e10cSrcweir     if (File::E_None == rc_item)
326*cdf0e10cSrcweir     {
327*cdf0e10cSrcweir         osl::FileStatus status(FileStatusMask_Validate);
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir         File::RC rc_stat = item.getFileStatus(status);
330*cdf0e10cSrcweir         if (File::E_None == rc_stat)
331*cdf0e10cSrcweir         {
332*cdf0e10cSrcweir             ret = FILE_OK;
333*cdf0e10cSrcweir         }
334*cdf0e10cSrcweir         else if (File::E_NOENT == rc_stat)
335*cdf0e10cSrcweir         {
336*cdf0e10cSrcweir             ret = FILE_DOES_NOT_EXIST;
337*cdf0e10cSrcweir         }
338*cdf0e10cSrcweir         else
339*cdf0e10cSrcweir         {
340*cdf0e10cSrcweir             ret = FILE_INVALID;
341*cdf0e10cSrcweir         }
342*cdf0e10cSrcweir     }
343*cdf0e10cSrcweir     else if (File::E_NOENT == rc_item)
344*cdf0e10cSrcweir     {
345*cdf0e10cSrcweir         ret = FILE_DOES_NOT_EXIST;
346*cdf0e10cSrcweir     }
347*cdf0e10cSrcweir     else
348*cdf0e10cSrcweir     {
349*cdf0e10cSrcweir         ret = FILE_INVALID;
350*cdf0e10cSrcweir     }
351*cdf0e10cSrcweir     return ret;
352*cdf0e10cSrcweir }
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir }
355