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