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