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_plugin.hxx"
26 
27 #include "osl/file.hxx"
28 #include "osl/diagnose.h"
29 #include "osl/module.hxx"
30 #include "osl/thread.hxx"
31 
32 #include "vendorbase.hxx"
33 #include "util.hxx"
34 #include "sunjre.hxx"
35 
36 using namespace std;
37 using namespace rtl;
38 using namespace osl;
39 
40 namespace jfw_plugin
41 {
42 rtl::Reference<VendorBase> createInstance(createInstance_func pFunc,
43                                           vector<pair<OUString, OUString> > properties);
44 
45 
46 
47 
48 
49 
50 
51 //##############################################################################
52 
MalformedVersionException()53 MalformedVersionException::MalformedVersionException()
54 {}
MalformedVersionException(const MalformedVersionException &)55 MalformedVersionException::MalformedVersionException(
56     const MalformedVersionException & )
57 {}
~MalformedVersionException()58 MalformedVersionException::~MalformedVersionException()
59 {}
60 MalformedVersionException &
operator =(const MalformedVersionException &)61 MalformedVersionException::operator =(
62     const MalformedVersionException &)
63 {
64     return *this;
65 }
66 //##############################################################################
67 
68 
VendorBase()69 VendorBase::VendorBase(): m_bAccessibility(false)
70 {
71 }
72 
getJavaExePaths(int * size)73 char const* const * VendorBase::getJavaExePaths(int* size)
74 {
75     static char const * ar[] = {
76 #if defined(WNT) || defined(OS2)
77         "java.exe",
78         "bin/java.exe"
79 #elif UNX
80         "java",
81         "bin/java"
82 #endif
83     };
84     *size = sizeof(ar) / sizeof(char*);
85     return ar;
86 }
87 
88 
createInstance()89 rtl::Reference<VendorBase> VendorBase::createInstance()
90 {
91     VendorBase *pBase = new VendorBase();
92     return rtl::Reference<VendorBase>(pBase);
93 }
94 
initialize(vector<pair<OUString,OUString>> props)95 bool VendorBase::initialize(vector<pair<OUString, OUString> > props)
96 {
97     //get java.vendor, java.version, java.home,
98     //javax.accessibility.assistive_technologies from system properties
99 
100     OUString sVendor;
101     typedef vector<pair<OUString, OUString> >::const_iterator it_prop;
102     OUString sVendorProperty(
103         RTL_CONSTASCII_USTRINGPARAM("java.vendor"));
104     OUString sVersionProperty(
105         RTL_CONSTASCII_USTRINGPARAM("java.version"));
106     OUString sHomeProperty(
107         RTL_CONSTASCII_USTRINGPARAM("java.home"));
108     OUString sAccessProperty(
109         RTL_CONSTASCII_USTRINGPARAM("javax.accessibility.assistive_technologies"));
110 
111     bool bVersion = false;
112     bool bVendor = false;
113     bool bHome = false;
114     bool bAccess = false;
115 
116     typedef vector<pair<OUString, OUString> >::const_iterator it_prop;
117     for (it_prop i = props.begin(); i != props.end(); i++)
118     {
119         if(! bVendor && sVendorProperty.equals(i->first))
120         {
121             m_sVendor = i->second;
122             bVendor = true;
123         }
124         else if (!bVersion && sVersionProperty.equals(i->first))
125         {
126             m_sVersion = i->second;
127             bVersion = true;
128         }
129         else if (!bHome && sHomeProperty.equals(i->first))
130         {
131            OUString fileURL;
132            if (osl_getFileURLFromSystemPath(i->second.pData,& fileURL.pData) ==
133                osl_File_E_None)
134            {
135                //make sure that the drive letter have all the same case
136                //otherwise file:///c:/jre and file:///C:/jre produce two
137                //different objects!!!
138                if (makeDriveLetterSame( & fileURL))
139                {
140                    m_sHome = fileURL;
141                    bHome = true;
142                }
143            }
144         }
145         else if (!bAccess && sAccessProperty.equals(i->first))
146         {
147             if (i->second.getLength() > 0)
148             {
149                 m_bAccessibility = true;
150                 bAccess = true;
151             }
152         }
153         // the javax.accessibility.xxx property may not be set. Therefore we
154         //must search through all properties.
155 
156     }
157     if (!bVersion || !bVendor || !bHome)
158         return false;
159 
160     // init m_sRuntimeLibrary
161     OSL_ASSERT(m_sHome.getLength());
162     //call virtual function to get the possible paths to the runtime library.
163 
164     int size = 0;
165     char const* const* arRtPaths = getRuntimePaths( & size);
166     vector<OUString> libpaths = getVectorFromCharArray(arRtPaths, size);
167 
168     bool bRt = false;
169     typedef vector<OUString>::const_iterator i_path;
170     for(i_path ip = libpaths.begin(); ip != libpaths.end(); ip++)
171     {
172         //Construct an absolute path to the possible runtime
173         OUString usRt= m_sHome + *ip;
174         DirectoryItem item;
175         if(DirectoryItem::get(usRt, item) == File::E_None)
176         {
177             //found runtime lib
178             m_sRuntimeLibrary = usRt;
179             bRt = true;
180             break;
181         }
182     }
183     if (!bRt)
184         return false;
185 
186 #if defined(WNT)
187     oslModule moduleRt = 0;
188     rtl::OUString sRuntimeLib;
189     if( File::getSystemPathFromFileURL( m_sRuntimeLibrary, sRuntimeLib ) == File::E_None )
190     {
191         if ( ( moduleRt = osl_loadModule( sRuntimeLib.pData, SAL_LOADMODULE_DEFAULT ) ) == 0 )
192         {
193             OSL_TRACE( "jfw_plugin::VendorBase::initialize - cannot load library %s",
194 			           rtl::OUStringToOString( sRuntimeLib, osl_getThreadTextEncoding() ).getStr() );
195             return false;
196         }
197         else
198         {
199             // do not leave the module loaded!
200             osl_unloadModule( moduleRt );
201         }
202     }
203 #endif
204 
205     // init m_sLD_LIBRARY_PATH
206     OSL_ASSERT(m_sHome.getLength());
207     size = 0;
208     char const * const * arLDPaths = getLibraryPaths( & size);
209     vector<OUString> ld_paths = getVectorFromCharArray(arLDPaths, size);
210 
211     char arSep[]= {SAL_PATHSEPARATOR, 0};
212     OUString sPathSep= OUString::createFromAscii(arSep);
213     bool bLdPath = true;
214     int c = 0;
215     for(i_path il = ld_paths.begin(); il != ld_paths.end(); il ++, c++)
216     {
217         OUString usAbsUrl= m_sHome + *il;
218         // convert to system path
219         OUString usSysPath;
220         if(File::getSystemPathFromFileURL(usAbsUrl, usSysPath) == File::E_None)
221         {
222 
223             if(c > 0)
224                 m_sLD_LIBRARY_PATH+= sPathSep;
225             m_sLD_LIBRARY_PATH+= usSysPath;
226         }
227         else
228         {
229             bLdPath = false;
230             break;
231         }
232     }
233     if (bLdPath == false)
234         return false;
235 
236     return true;
237 }
238 
getRuntimePaths(int *)239 char const* const* VendorBase::getRuntimePaths(int* /*size*/)
240 {
241     return NULL;
242 }
243 
getLibraryPaths(int *)244 char const* const* VendorBase::getLibraryPaths(int* /*size*/)
245 {
246     return NULL;
247 }
248 
getVendor() const249 const OUString & VendorBase::getVendor() const
250 {
251     return m_sVendor;
252 }
getVersion() const253 const OUString & VendorBase::getVersion() const
254 {
255     return m_sVersion;
256 }
257 
getHome() const258 const OUString & VendorBase::getHome() const
259 {
260     return m_sHome;
261 }
262 
getLibraryPaths() const263 const OUString & VendorBase::getLibraryPaths() const
264 {
265     return m_sLD_LIBRARY_PATH;
266 }
267 
getRuntimeLibrary() const268 const OUString & VendorBase::getRuntimeLibrary() const
269 {
270     return m_sRuntimeLibrary;
271 }
supportsAccessibility() const272 bool VendorBase::supportsAccessibility() const
273 {
274     return m_bAccessibility;
275 }
276 
needsRestart() const277 bool VendorBase::needsRestart() const
278 {
279     if (getLibraryPaths().getLength() > 0)
280         return true;
281     return false;
282 }
283 
compareVersions(const rtl::OUString &) const284 int VendorBase::compareVersions(const rtl::OUString& /*sSecond*/) const
285 {
286     OSL_ENSURE(0, "[Java framework] VendorBase::compareVersions must be "
287                "overridden in derived class.");
288     return 0;
289 }
290 
291 
292 
293 
294 }
295