136f55ffcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
336f55ffcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
436f55ffcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
536f55ffcSAndrew Rist  * distributed with this work for additional information
636f55ffcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
736f55ffcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
836f55ffcSAndrew Rist  * "License"); you may not use this file except in compliance
936f55ffcSAndrew Rist  * with the License.  You may obtain a copy of the License at
1036f55ffcSAndrew Rist  *
1136f55ffcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1236f55ffcSAndrew Rist  *
1336f55ffcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1436f55ffcSAndrew Rist  * software distributed under the License is distributed on an
1536f55ffcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1636f55ffcSAndrew Rist  * KIND, either express or implied.  See the License for the
1736f55ffcSAndrew Rist  * specific language governing permissions and limitations
1836f55ffcSAndrew Rist  * under the License.
1936f55ffcSAndrew Rist  *
2036f55ffcSAndrew Rist  *************************************************************/
2136f55ffcSAndrew Rist 
2236f55ffcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_jvmfwk.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "osl/file.hxx"
28*e4982c43SAriel Constenla-Haile #include "osl/diagnose.h"
29*e4982c43SAriel Constenla-Haile #include "osl/module.hxx"
30*e4982c43SAriel Constenla-Haile #include "osl/thread.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "vendorbase.hxx"
33cdf0e10cSrcweir #include "util.hxx"
34cdf0e10cSrcweir #include "sunjre.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace std;
37cdf0e10cSrcweir using namespace rtl;
38cdf0e10cSrcweir using namespace osl;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir namespace jfw_plugin
41cdf0e10cSrcweir {
42cdf0e10cSrcweir rtl::Reference<VendorBase> createInstance(createInstance_func pFunc,
43cdf0e10cSrcweir                                           vector<pair<OUString, OUString> > properties);
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir //##############################################################################
52cdf0e10cSrcweir 
MalformedVersionException()53cdf0e10cSrcweir MalformedVersionException::MalformedVersionException()
54cdf0e10cSrcweir {}
MalformedVersionException(const MalformedVersionException &)55cdf0e10cSrcweir MalformedVersionException::MalformedVersionException(
56cdf0e10cSrcweir     const MalformedVersionException & )
57cdf0e10cSrcweir {}
~MalformedVersionException()58cdf0e10cSrcweir MalformedVersionException::~MalformedVersionException()
59cdf0e10cSrcweir {}
60cdf0e10cSrcweir MalformedVersionException &
operator =(const MalformedVersionException &)61cdf0e10cSrcweir MalformedVersionException::operator =(
62cdf0e10cSrcweir     const MalformedVersionException &)
63cdf0e10cSrcweir {
64cdf0e10cSrcweir     return *this;
65cdf0e10cSrcweir }
66cdf0e10cSrcweir //##############################################################################
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 
VendorBase()69cdf0e10cSrcweir VendorBase::VendorBase(): m_bAccessibility(false)
70cdf0e10cSrcweir {
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
getJavaExePaths(int * size)73cdf0e10cSrcweir char const* const * VendorBase::getJavaExePaths(int* size)
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     static char const * ar[] = {
76cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
77cdf0e10cSrcweir         "java.exe",
78cdf0e10cSrcweir         "bin/java.exe"
79cdf0e10cSrcweir #elif UNX
80cdf0e10cSrcweir         "java",
81cdf0e10cSrcweir         "bin/java"
82cdf0e10cSrcweir #endif
83cdf0e10cSrcweir     };
84cdf0e10cSrcweir     *size = sizeof(ar) / sizeof(char*);
85cdf0e10cSrcweir     return ar;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 
createInstance()89cdf0e10cSrcweir rtl::Reference<VendorBase> VendorBase::createInstance()
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     VendorBase *pBase = new VendorBase();
92cdf0e10cSrcweir     return rtl::Reference<VendorBase>(pBase);
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
initialize(vector<pair<OUString,OUString>> props)95cdf0e10cSrcweir bool VendorBase::initialize(vector<pair<OUString, OUString> > props)
96cdf0e10cSrcweir {
97cdf0e10cSrcweir     //get java.vendor, java.version, java.home,
98cdf0e10cSrcweir     //javax.accessibility.assistive_technologies from system properties
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     OUString sVendor;
101cdf0e10cSrcweir     typedef vector<pair<OUString, OUString> >::const_iterator it_prop;
102cdf0e10cSrcweir     OUString sVendorProperty(
103cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("java.vendor"));
104cdf0e10cSrcweir     OUString sVersionProperty(
105cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("java.version"));
106cdf0e10cSrcweir     OUString sHomeProperty(
107cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("java.home"));
108cdf0e10cSrcweir     OUString sAccessProperty(
109cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("javax.accessibility.assistive_technologies"));
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     bool bVersion = false;
112cdf0e10cSrcweir     bool bVendor = false;
113cdf0e10cSrcweir     bool bHome = false;
114cdf0e10cSrcweir     bool bAccess = false;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     typedef vector<pair<OUString, OUString> >::const_iterator it_prop;
117cdf0e10cSrcweir     for (it_prop i = props.begin(); i != props.end(); i++)
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         if(! bVendor && sVendorProperty.equals(i->first))
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir             m_sVendor = i->second;
122cdf0e10cSrcweir             bVendor = true;
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir         else if (!bVersion && sVersionProperty.equals(i->first))
125cdf0e10cSrcweir         {
126cdf0e10cSrcweir             m_sVersion = i->second;
127cdf0e10cSrcweir             bVersion = true;
128cdf0e10cSrcweir         }
129cdf0e10cSrcweir         else if (!bHome && sHomeProperty.equals(i->first))
130cdf0e10cSrcweir         {
131cdf0e10cSrcweir            OUString fileURL;
132cdf0e10cSrcweir            if (osl_getFileURLFromSystemPath(i->second.pData,& fileURL.pData) ==
133cdf0e10cSrcweir                osl_File_E_None)
134cdf0e10cSrcweir            {
135cdf0e10cSrcweir                //make sure that the drive letter have all the same case
136cdf0e10cSrcweir                //otherwise file:///c:/jre and file:///C:/jre produce two
137cdf0e10cSrcweir                //different objects!!!
138cdf0e10cSrcweir                if (makeDriveLetterSame( & fileURL))
139cdf0e10cSrcweir                {
140cdf0e10cSrcweir                    m_sHome = fileURL;
141cdf0e10cSrcweir                    bHome = true;
142cdf0e10cSrcweir                }
143cdf0e10cSrcweir            }
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir         else if (!bAccess && sAccessProperty.equals(i->first))
146cdf0e10cSrcweir         {
147cdf0e10cSrcweir             if (i->second.getLength() > 0)
148cdf0e10cSrcweir             {
149cdf0e10cSrcweir                 m_bAccessibility = true;
150cdf0e10cSrcweir                 bAccess = true;
151cdf0e10cSrcweir             }
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir         // the javax.accessibility.xxx property may not be set. Therefore we
154cdf0e10cSrcweir         //must search through all properties.
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir     if (!bVersion || !bVendor || !bHome)
158cdf0e10cSrcweir         return false;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     // init m_sRuntimeLibrary
161cdf0e10cSrcweir     OSL_ASSERT(m_sHome.getLength());
162cdf0e10cSrcweir     //call virtual function to get the possible paths to the runtime library.
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     int size = 0;
165cdf0e10cSrcweir     char const* const* arRtPaths = getRuntimePaths( & size);
166cdf0e10cSrcweir     vector<OUString> libpaths = getVectorFromCharArray(arRtPaths, size);
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     bool bRt = false;
169cdf0e10cSrcweir     typedef vector<OUString>::const_iterator i_path;
170cdf0e10cSrcweir     for(i_path ip = libpaths.begin(); ip != libpaths.end(); ip++)
171cdf0e10cSrcweir     {
172cdf0e10cSrcweir         //Construct an absolute path to the possible runtime
173cdf0e10cSrcweir         OUString usRt= m_sHome + *ip;
174cdf0e10cSrcweir         DirectoryItem item;
175cdf0e10cSrcweir         if(DirectoryItem::get(usRt, item) == File::E_None)
176cdf0e10cSrcweir         {
177cdf0e10cSrcweir             //found runtime lib
178cdf0e10cSrcweir             m_sRuntimeLibrary = usRt;
179cdf0e10cSrcweir             bRt = true;
180cdf0e10cSrcweir             break;
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir     if (!bRt)
184cdf0e10cSrcweir         return false;
185cdf0e10cSrcweir 
186*e4982c43SAriel Constenla-Haile #if defined(WNT)
187*e4982c43SAriel Constenla-Haile     oslModule moduleRt = 0;
188*e4982c43SAriel Constenla-Haile     rtl::OUString sRuntimeLib;
189*e4982c43SAriel Constenla-Haile     if( File::getSystemPathFromFileURL( m_sRuntimeLibrary, sRuntimeLib ) == File::E_None )
190*e4982c43SAriel Constenla-Haile     {
191*e4982c43SAriel Constenla-Haile         if ( ( moduleRt = osl_loadModule( sRuntimeLib.pData, SAL_LOADMODULE_DEFAULT ) ) == 0 )
192*e4982c43SAriel Constenla-Haile         {
193*e4982c43SAriel Constenla-Haile             OSL_TRACE( "jfw_plugin::VendorBase::initialize - cannot load library %s",
194*e4982c43SAriel Constenla-Haile 			           rtl::OUStringToOString( sRuntimeLib, osl_getThreadTextEncoding() ).getStr() );
195*e4982c43SAriel Constenla-Haile             return false;
196*e4982c43SAriel Constenla-Haile         }
197*e4982c43SAriel Constenla-Haile         else
198*e4982c43SAriel Constenla-Haile         {
199*e4982c43SAriel Constenla-Haile             // do not leave the module loaded!
200*e4982c43SAriel Constenla-Haile             osl_unloadModule( moduleRt );
201*e4982c43SAriel Constenla-Haile         }
202*e4982c43SAriel Constenla-Haile     }
203*e4982c43SAriel Constenla-Haile #endif
204*e4982c43SAriel Constenla-Haile 
205cdf0e10cSrcweir     // init m_sLD_LIBRARY_PATH
206cdf0e10cSrcweir     OSL_ASSERT(m_sHome.getLength());
207cdf0e10cSrcweir     size = 0;
208cdf0e10cSrcweir     char const * const * arLDPaths = getLibraryPaths( & size);
209cdf0e10cSrcweir     vector<OUString> ld_paths = getVectorFromCharArray(arLDPaths, size);
210cdf0e10cSrcweir 
211cdf0e10cSrcweir     char arSep[]= {SAL_PATHSEPARATOR, 0};
212cdf0e10cSrcweir     OUString sPathSep= OUString::createFromAscii(arSep);
213cdf0e10cSrcweir     bool bLdPath = true;
214cdf0e10cSrcweir     int c = 0;
215cdf0e10cSrcweir     for(i_path il = ld_paths.begin(); il != ld_paths.end(); il ++, c++)
216cdf0e10cSrcweir     {
217cdf0e10cSrcweir         OUString usAbsUrl= m_sHome + *il;
218cdf0e10cSrcweir         // convert to system path
219cdf0e10cSrcweir         OUString usSysPath;
220cdf0e10cSrcweir         if(File::getSystemPathFromFileURL(usAbsUrl, usSysPath) == File::E_None)
221cdf0e10cSrcweir         {
222cdf0e10cSrcweir 
223cdf0e10cSrcweir             if(c > 0)
224cdf0e10cSrcweir                 m_sLD_LIBRARY_PATH+= sPathSep;
225cdf0e10cSrcweir             m_sLD_LIBRARY_PATH+= usSysPath;
226cdf0e10cSrcweir         }
227cdf0e10cSrcweir         else
228cdf0e10cSrcweir         {
229cdf0e10cSrcweir             bLdPath = false;
230cdf0e10cSrcweir             break;
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir     if (bLdPath == false)
234cdf0e10cSrcweir         return false;
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     return true;
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
getRuntimePaths(int *)239cdf0e10cSrcweir char const* const* VendorBase::getRuntimePaths(int* /*size*/)
240cdf0e10cSrcweir {
241cdf0e10cSrcweir     return NULL;
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
getLibraryPaths(int *)244cdf0e10cSrcweir char const* const* VendorBase::getLibraryPaths(int* /*size*/)
245cdf0e10cSrcweir {
246cdf0e10cSrcweir     return NULL;
247cdf0e10cSrcweir }
248cdf0e10cSrcweir 
getVendor() const249cdf0e10cSrcweir const OUString & VendorBase::getVendor() const
250cdf0e10cSrcweir {
251cdf0e10cSrcweir     return m_sVendor;
252cdf0e10cSrcweir }
getVersion() const253cdf0e10cSrcweir const OUString & VendorBase::getVersion() const
254cdf0e10cSrcweir {
255cdf0e10cSrcweir     return m_sVersion;
256cdf0e10cSrcweir }
257cdf0e10cSrcweir 
getHome() const258cdf0e10cSrcweir const OUString & VendorBase::getHome() const
259cdf0e10cSrcweir {
260cdf0e10cSrcweir     return m_sHome;
261cdf0e10cSrcweir }
262cdf0e10cSrcweir 
getLibraryPaths() const263cdf0e10cSrcweir const OUString & VendorBase::getLibraryPaths() const
264cdf0e10cSrcweir {
265cdf0e10cSrcweir     return m_sLD_LIBRARY_PATH;
266cdf0e10cSrcweir }
267cdf0e10cSrcweir 
getRuntimeLibrary() const268cdf0e10cSrcweir const OUString & VendorBase::getRuntimeLibrary() const
269cdf0e10cSrcweir {
270cdf0e10cSrcweir     return m_sRuntimeLibrary;
271cdf0e10cSrcweir }
supportsAccessibility() const272cdf0e10cSrcweir bool VendorBase::supportsAccessibility() const
273cdf0e10cSrcweir {
274cdf0e10cSrcweir     return m_bAccessibility;
275cdf0e10cSrcweir }
276cdf0e10cSrcweir 
needsRestart() const277cdf0e10cSrcweir bool VendorBase::needsRestart() const
278cdf0e10cSrcweir {
279cdf0e10cSrcweir     if (getLibraryPaths().getLength() > 0)
280cdf0e10cSrcweir         return true;
281cdf0e10cSrcweir     return false;
282cdf0e10cSrcweir }
283cdf0e10cSrcweir 
compareVersions(const rtl::OUString &) const284cdf0e10cSrcweir int VendorBase::compareVersions(const rtl::OUString& /*sSecond*/) const
285cdf0e10cSrcweir {
286cdf0e10cSrcweir     OSL_ENSURE(0, "[Java framework] VendorBase::compareVersions must be "
287cdf0e10cSrcweir                "overridden in derived class.");
288cdf0e10cSrcweir     return 0;
289cdf0e10cSrcweir }
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 
294cdf0e10cSrcweir }
295