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