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