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