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 10cdf0e10cSrcweir * 11*36f55ffcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 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. 19cdf0e10cSrcweir * 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 #if OSL_DEBUG_LEVEL > 0 27cdf0e10cSrcweir #include <stdio.h> 28cdf0e10cSrcweir #endif 29cdf0e10cSrcweir #include <string.h> 30cdf0e10cSrcweir 31cdf0e10cSrcweir #include "boost/scoped_array.hpp" 32cdf0e10cSrcweir #include "osl/diagnose.h" 33cdf0e10cSrcweir #include "rtl/ustring.hxx" 34cdf0e10cSrcweir #include "rtl/ustrbuf.hxx" 35cdf0e10cSrcweir #include "osl/module.hxx" 36cdf0e10cSrcweir #include "osl/mutex.hxx" 37cdf0e10cSrcweir #include "osl/thread.hxx" 38cdf0e10cSrcweir #include "osl/file.hxx" 39cdf0e10cSrcweir #include "rtl/instance.hxx" 40cdf0e10cSrcweir #include "osl/getglobalmutex.hxx" 41cdf0e10cSrcweir #include <setjmp.h> 42cdf0e10cSrcweir #include <signal.h> 43cdf0e10cSrcweir #include <stack> 44cdf0e10cSrcweir 45cdf0e10cSrcweir #include "jni.h" 46cdf0e10cSrcweir #include "rtl/byteseq.hxx" 47cdf0e10cSrcweir #include "jvmfwk/vendorplugin.h" 48cdf0e10cSrcweir #include "util.hxx" 49cdf0e10cSrcweir #include "sunversion.hxx" 50cdf0e10cSrcweir #include "vendorlist.hxx" 51cdf0e10cSrcweir #include "diagnostics.h" 52cdf0e10cSrcweir 53cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) ) 54cdf0e10cSrcweir #define SUN_MICRO "Sun Microsystems Inc." 55cdf0e10cSrcweir 56cdf0e10cSrcweir using namespace osl; 57cdf0e10cSrcweir using namespace rtl; 58cdf0e10cSrcweir using namespace std; 59cdf0e10cSrcweir using namespace jfw_plugin; 60cdf0e10cSrcweir 61cdf0e10cSrcweir namespace { 62cdf0e10cSrcweir 63cdf0e10cSrcweir struct PluginMutex: public ::rtl::Static<osl::Mutex, PluginMutex> {}; 64cdf0e10cSrcweir 65cdf0e10cSrcweir #if defined UNX 66cdf0e10cSrcweir OString getPluginJarPath( 67cdf0e10cSrcweir const OUString & sVendor, 68cdf0e10cSrcweir const OUString& sLocation, 69cdf0e10cSrcweir const OUString& sVersion) 70cdf0e10cSrcweir { 71cdf0e10cSrcweir OString ret; 72cdf0e10cSrcweir OUString sName1(RTL_CONSTASCII_USTRINGPARAM("javaplugin.jar")); 73cdf0e10cSrcweir OUString sName2(RTL_CONSTASCII_USTRINGPARAM("plugin.jar")); 74cdf0e10cSrcweir OUString sPath; 75cdf0e10cSrcweir if (sVendor.equals(OUString(RTL_CONSTASCII_USTRINGPARAM(SUN_MICRO)))) 76cdf0e10cSrcweir { 77cdf0e10cSrcweir SunVersion ver142("1.4.2-ea"); 78cdf0e10cSrcweir SunVersion ver150("1.5.0-ea"); 79cdf0e10cSrcweir SunVersion ver(sVersion); 80cdf0e10cSrcweir OSL_ASSERT(ver142 && ver150 && ver); 81cdf0e10cSrcweir 82cdf0e10cSrcweir OUString sName; 83cdf0e10cSrcweir if (ver < ver142) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir sName = sName1; 86cdf0e10cSrcweir } 87cdf0e10cSrcweir else if (ver < ver150) 88cdf0e10cSrcweir {//this will cause ea, beta etc. to have plugin.jar in path. 89cdf0e10cSrcweir //but this does not harm. 1.5.0-beta < 1.5.0 90cdf0e10cSrcweir sName = sName2; 91cdf0e10cSrcweir } 92cdf0e10cSrcweir if (sName.getLength()) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir sName = sLocation + OUSTR("/lib/") + sName; 95cdf0e10cSrcweir OSL_VERIFY( 96cdf0e10cSrcweir osl_getSystemPathFromFileURL(sName.pData, & sPath.pData) 97cdf0e10cSrcweir == osl_File_E_None); 98cdf0e10cSrcweir } 99cdf0e10cSrcweir } 100cdf0e10cSrcweir else 101cdf0e10cSrcweir { 102cdf0e10cSrcweir char sep[] = {SAL_PATHSEPARATOR, 0}; 103cdf0e10cSrcweir OUString sName(sLocation + OUSTR("/lib/") + sName1); 104cdf0e10cSrcweir OUString sPath1; 105cdf0e10cSrcweir OUString sPath2; 106cdf0e10cSrcweir if (osl_getSystemPathFromFileURL(sName.pData, & sPath1.pData) 107cdf0e10cSrcweir == osl_File_E_None) 108cdf0e10cSrcweir { 109cdf0e10cSrcweir sName = sLocation + OUSTR("/lib/") + sName2; 110cdf0e10cSrcweir if (osl_getSystemPathFromFileURL(sName.pData, & sPath2.pData) 111cdf0e10cSrcweir == osl_File_E_None) 112cdf0e10cSrcweir { 113cdf0e10cSrcweir sPath = sPath1 + OUString::createFromAscii(sep) + sPath2; 114cdf0e10cSrcweir } 115cdf0e10cSrcweir } 116cdf0e10cSrcweir OSL_ASSERT(sPath.getLength()); 117cdf0e10cSrcweir } 118cdf0e10cSrcweir ret = rtl::OUStringToOString(sPath, osl_getThreadTextEncoding()); 119cdf0e10cSrcweir 120cdf0e10cSrcweir return ret; 121cdf0e10cSrcweir } 122cdf0e10cSrcweir #endif // UNX 123cdf0e10cSrcweir 124cdf0e10cSrcweir 125cdf0e10cSrcweir JavaInfo* createJavaInfo(const rtl::Reference<VendorBase> & info) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir JavaInfo* pInfo = (JavaInfo*) rtl_allocateMemory(sizeof(JavaInfo)); 128cdf0e10cSrcweir if (pInfo == NULL) 129cdf0e10cSrcweir return NULL; 130cdf0e10cSrcweir rtl::OUString sVendor = info->getVendor(); 131cdf0e10cSrcweir pInfo->sVendor = sVendor.pData; 132cdf0e10cSrcweir rtl_uString_acquire(sVendor.pData); 133cdf0e10cSrcweir rtl::OUString sHome = info->getHome(); 134cdf0e10cSrcweir pInfo->sLocation = sHome.pData; 135cdf0e10cSrcweir rtl_uString_acquire(pInfo->sLocation); 136cdf0e10cSrcweir rtl::OUString sVersion = info->getVersion(); 137cdf0e10cSrcweir pInfo->sVersion = sVersion.pData; 138cdf0e10cSrcweir rtl_uString_acquire(pInfo->sVersion); 139cdf0e10cSrcweir pInfo->nFeatures = info->supportsAccessibility() ? 1 : 0; 140cdf0e10cSrcweir pInfo->nRequirements = info->needsRestart() ? JFW_REQUIRE_NEEDRESTART : 0; 141cdf0e10cSrcweir rtl::OUStringBuffer buf(1024); 142cdf0e10cSrcweir buf.append(info->getRuntimeLibrary()); 143cdf0e10cSrcweir if (info->getLibraryPaths().getLength() > 0) 144cdf0e10cSrcweir { 145cdf0e10cSrcweir buf.appendAscii("\n"); 146cdf0e10cSrcweir buf.append(info->getLibraryPaths()); 147cdf0e10cSrcweir buf.appendAscii("\n"); 148cdf0e10cSrcweir } 149cdf0e10cSrcweir 150cdf0e10cSrcweir rtl::OUString sVendorData = buf.makeStringAndClear(); 151cdf0e10cSrcweir rtl::ByteSequence byteSeq( (sal_Int8*) sVendorData.pData->buffer, 152cdf0e10cSrcweir sVendorData.getLength() * sizeof(sal_Unicode)); 153cdf0e10cSrcweir pInfo->arVendorData = byteSeq.get(); 154cdf0e10cSrcweir rtl_byte_sequence_acquire(pInfo->arVendorData); 155cdf0e10cSrcweir 156cdf0e10cSrcweir return pInfo; 157cdf0e10cSrcweir } 158cdf0e10cSrcweir 159cdf0e10cSrcweir rtl::OUString getRuntimeLib(const rtl::ByteSequence & data) 160cdf0e10cSrcweir { 161cdf0e10cSrcweir const sal_Unicode* chars = (sal_Unicode*) data.getConstArray(); 162cdf0e10cSrcweir sal_Int32 len = data.getLength(); 163cdf0e10cSrcweir rtl::OUString sData(chars, len / 2); 164cdf0e10cSrcweir //the runtime lib is on the first line 165cdf0e10cSrcweir sal_Int32 index = 0; 166cdf0e10cSrcweir rtl::OUString aToken = sData.getToken( 0, '\n', index); 167cdf0e10cSrcweir 168cdf0e10cSrcweir return aToken; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir jmp_buf jmp_jvm_abort; 172cdf0e10cSrcweir sig_atomic_t g_bInGetJavaVM = 0; 173cdf0e10cSrcweir 174cdf0e10cSrcweir extern "C" void JNICALL abort_handler() 175cdf0e10cSrcweir { 176cdf0e10cSrcweir // If we are within JNI_CreateJavaVM then we jump back into getJavaVM 177cdf0e10cSrcweir if( g_bInGetJavaVM != 0 ) 178cdf0e10cSrcweir { 179cdf0e10cSrcweir fprintf( stderr, "JavaVM: JNI_CreateJavaVM called _exit, caught by abort_handler in javavm.cxx\n"); 180cdf0e10cSrcweir longjmp( jmp_jvm_abort, 0); 181cdf0e10cSrcweir } 182cdf0e10cSrcweir } 183cdf0e10cSrcweir 184cdf0e10cSrcweir } 185cdf0e10cSrcweir 186cdf0e10cSrcweir extern "C" 187cdf0e10cSrcweir javaPluginError jfw_plugin_getAllJavaInfos( 188cdf0e10cSrcweir rtl_uString *sVendor, 189cdf0e10cSrcweir rtl_uString *sMinVersion, 190cdf0e10cSrcweir rtl_uString *sMaxVersion, 191cdf0e10cSrcweir rtl_uString * *arExcludeList, 192cdf0e10cSrcweir sal_Int32 nLenList, 193cdf0e10cSrcweir JavaInfo*** parJavaInfo, 194cdf0e10cSrcweir sal_Int32 *nLenInfoList) 195cdf0e10cSrcweir { 196cdf0e10cSrcweir OSL_ASSERT(sVendor); 197cdf0e10cSrcweir OSL_ASSERT(sMinVersion); 198cdf0e10cSrcweir OSL_ASSERT(sMaxVersion); 199cdf0e10cSrcweir OSL_ASSERT(parJavaInfo); 200cdf0e10cSrcweir OSL_ASSERT(parJavaInfo); 201cdf0e10cSrcweir OSL_ASSERT(nLenInfoList); 202cdf0e10cSrcweir if (!sVendor || !sMinVersion || !sMaxVersion || !parJavaInfo || !nLenInfoList) 203cdf0e10cSrcweir return JFW_PLUGIN_E_INVALID_ARG; 204cdf0e10cSrcweir 205cdf0e10cSrcweir //nLenlist contains the number of element in arExcludeList. 206cdf0e10cSrcweir //If no exclude list is provided then nLenList must be 0 207cdf0e10cSrcweir OSL_ASSERT( ! (arExcludeList == NULL && nLenList > 0)); 208cdf0e10cSrcweir if (arExcludeList == NULL && nLenList > 0) 209cdf0e10cSrcweir return JFW_PLUGIN_E_INVALID_ARG; 210cdf0e10cSrcweir 211cdf0e10cSrcweir OUString ouVendor(sVendor); 212cdf0e10cSrcweir OUString ouMinVer(sMinVersion); 213cdf0e10cSrcweir OUString ouMaxVer(sMaxVersion); 214cdf0e10cSrcweir 215cdf0e10cSrcweir OSL_ASSERT(ouVendor.getLength() > 0); 216cdf0e10cSrcweir if (ouVendor.getLength() == 0) 217cdf0e10cSrcweir return JFW_PLUGIN_E_INVALID_ARG; 218cdf0e10cSrcweir 219cdf0e10cSrcweir JavaInfo** arInfo = NULL; 220cdf0e10cSrcweir 221cdf0e10cSrcweir //Find all JREs 222cdf0e10cSrcweir vector<rtl::Reference<VendorBase> > vecInfos = 223cdf0e10cSrcweir getAllJREInfos(); 224cdf0e10cSrcweir vector<rtl::Reference<VendorBase> > vecVerifiedInfos; 225cdf0e10cSrcweir 226cdf0e10cSrcweir typedef vector<rtl::Reference<VendorBase> >::iterator it; 227cdf0e10cSrcweir for (it i= vecInfos.begin(); i != vecInfos.end(); i++) 228cdf0e10cSrcweir { 229cdf0e10cSrcweir const rtl::Reference<VendorBase>& cur = *i; 230cdf0e10cSrcweir 231cdf0e10cSrcweir if (ouVendor.equals(cur->getVendor()) == sal_False) 232cdf0e10cSrcweir continue; 233cdf0e10cSrcweir 234cdf0e10cSrcweir if (ouMinVer.getLength() > 0) 235cdf0e10cSrcweir { 236cdf0e10cSrcweir try 237cdf0e10cSrcweir { 238cdf0e10cSrcweir if (cur->compareVersions(sMinVersion) == -1) 239cdf0e10cSrcweir continue; 240cdf0e10cSrcweir } 241cdf0e10cSrcweir catch (MalformedVersionException&) 242cdf0e10cSrcweir { 243cdf0e10cSrcweir //The minVersion was not recognized as valid for this vendor. 244cdf0e10cSrcweir JFW_ENSURE( 245cdf0e10cSrcweir 0,OUSTR("[Java framework]sunjavaplugin does not know version: ") 246cdf0e10cSrcweir + ouMinVer + OUSTR(" for vendor: ") + cur->getVendor() 247cdf0e10cSrcweir + OUSTR(" .Check minimum Version.") ); 248cdf0e10cSrcweir return JFW_PLUGIN_E_WRONG_VERSION_FORMAT; 249cdf0e10cSrcweir } 250cdf0e10cSrcweir } 251cdf0e10cSrcweir 252cdf0e10cSrcweir if (ouMaxVer.getLength() > 0) 253cdf0e10cSrcweir { 254cdf0e10cSrcweir try 255cdf0e10cSrcweir { 256cdf0e10cSrcweir if (cur->compareVersions(sMaxVersion) == 1) 257cdf0e10cSrcweir continue; 258cdf0e10cSrcweir } 259cdf0e10cSrcweir catch (MalformedVersionException&) 260cdf0e10cSrcweir { 261cdf0e10cSrcweir //The maxVersion was not recognized as valid for this vendor. 262cdf0e10cSrcweir JFW_ENSURE( 263cdf0e10cSrcweir 0,OUSTR("[Java framework]sunjavaplugin does not know version: ") 264cdf0e10cSrcweir + ouMaxVer + OUSTR(" for vendor: ") + cur->getVendor() 265cdf0e10cSrcweir + OUSTR(" .Check maximum Version.") ); 266cdf0e10cSrcweir return JFW_PLUGIN_E_WRONG_VERSION_FORMAT; 267cdf0e10cSrcweir } 268cdf0e10cSrcweir } 269cdf0e10cSrcweir 270cdf0e10cSrcweir if (arExcludeList > 0) 271cdf0e10cSrcweir { 272cdf0e10cSrcweir bool bExclude = false; 273cdf0e10cSrcweir for (int j = 0; j < nLenList; j++) 274cdf0e10cSrcweir { 275cdf0e10cSrcweir rtl::OUString sExVer(arExcludeList[j]); 276cdf0e10cSrcweir try 277cdf0e10cSrcweir { 278cdf0e10cSrcweir if (cur->compareVersions(sExVer) == 0) 279cdf0e10cSrcweir { 280cdf0e10cSrcweir bExclude = true; 281cdf0e10cSrcweir break; 282cdf0e10cSrcweir } 283cdf0e10cSrcweir } 284cdf0e10cSrcweir catch (MalformedVersionException&) 285cdf0e10cSrcweir { 286cdf0e10cSrcweir //The excluded version was not recognized as valid for this vendor. 287cdf0e10cSrcweir JFW_ENSURE( 288cdf0e10cSrcweir 0,OUSTR("[Java framework]sunjavaplugin does not know version: ") 289cdf0e10cSrcweir + sExVer + OUSTR(" for vendor: ") + cur->getVendor() 290cdf0e10cSrcweir + OUSTR(" .Check excluded versions.") ); 291cdf0e10cSrcweir return JFW_PLUGIN_E_WRONG_VERSION_FORMAT; 292cdf0e10cSrcweir } 293cdf0e10cSrcweir } 294cdf0e10cSrcweir if (bExclude == true) 295cdf0e10cSrcweir continue; 296cdf0e10cSrcweir } 297cdf0e10cSrcweir vecVerifiedInfos.push_back(*i); 298cdf0e10cSrcweir } 299cdf0e10cSrcweir //Now vecVerifiedInfos contains all those JREs which meet the version requirements 300cdf0e10cSrcweir //Transfer them into the array that is passed out. 301cdf0e10cSrcweir arInfo = (JavaInfo**) rtl_allocateMemory(vecVerifiedInfos.size() * sizeof (JavaInfo*)); 302cdf0e10cSrcweir int j = 0; 303cdf0e10cSrcweir typedef vector<rtl::Reference<VendorBase> >::const_iterator cit; 304cdf0e10cSrcweir for (cit ii = vecVerifiedInfos.begin(); ii != vecVerifiedInfos.end(); ii++, j++) 305cdf0e10cSrcweir { 306cdf0e10cSrcweir arInfo[j] = createJavaInfo(*ii); 307cdf0e10cSrcweir } 308cdf0e10cSrcweir *nLenInfoList = vecVerifiedInfos.size(); 309cdf0e10cSrcweir 310cdf0e10cSrcweir 311cdf0e10cSrcweir *parJavaInfo = arInfo; 312cdf0e10cSrcweir return JFW_PLUGIN_E_NONE; 313cdf0e10cSrcweir } 314cdf0e10cSrcweir 315cdf0e10cSrcweir extern "C" 316cdf0e10cSrcweir javaPluginError jfw_plugin_getJavaInfoByPath( 317cdf0e10cSrcweir rtl_uString *path, 318cdf0e10cSrcweir rtl_uString *sVendor, 319cdf0e10cSrcweir rtl_uString *sMinVersion, 320cdf0e10cSrcweir rtl_uString *sMaxVersion, 321cdf0e10cSrcweir rtl_uString * *arExcludeList, 322cdf0e10cSrcweir sal_Int32 nLenList, 323cdf0e10cSrcweir JavaInfo ** ppInfo) 324cdf0e10cSrcweir { 325cdf0e10cSrcweir javaPluginError errcode = JFW_PLUGIN_E_NONE; 326cdf0e10cSrcweir 327cdf0e10cSrcweir OSL_ASSERT(path); 328cdf0e10cSrcweir OSL_ASSERT(sVendor); 329cdf0e10cSrcweir OSL_ASSERT(sMinVersion); 330cdf0e10cSrcweir OSL_ASSERT(sMaxVersion); 331cdf0e10cSrcweir if (!path || !sVendor || !sMinVersion || !sMaxVersion || !ppInfo) 332cdf0e10cSrcweir return JFW_PLUGIN_E_INVALID_ARG; 333cdf0e10cSrcweir OUString ouPath(path); 334cdf0e10cSrcweir OSL_ASSERT(ouPath.getLength() > 0); 335cdf0e10cSrcweir if (ouPath.getLength() == 0) 336cdf0e10cSrcweir return JFW_PLUGIN_E_INVALID_ARG; 337cdf0e10cSrcweir 338cdf0e10cSrcweir //nLenlist contains the number of element in arExcludeList. 339cdf0e10cSrcweir //If no exclude list is provided then nLenList must be 0 340cdf0e10cSrcweir OSL_ASSERT( ! (arExcludeList == NULL && nLenList > 0)); 341cdf0e10cSrcweir if (arExcludeList == NULL && nLenList > 0) 342cdf0e10cSrcweir return JFW_PLUGIN_E_INVALID_ARG; 343cdf0e10cSrcweir 344cdf0e10cSrcweir OUString ouVendor(sVendor); 345cdf0e10cSrcweir OUString ouMinVer(sMinVersion); 346cdf0e10cSrcweir OUString ouMaxVer(sMaxVersion); 347cdf0e10cSrcweir 348cdf0e10cSrcweir OSL_ASSERT(ouVendor.getLength() > 0); 349cdf0e10cSrcweir if (ouVendor.getLength() == 0) 350cdf0e10cSrcweir return JFW_PLUGIN_E_INVALID_ARG; 351cdf0e10cSrcweir 352cdf0e10cSrcweir rtl::Reference<VendorBase> aVendorInfo = getJREInfoByPath(ouPath); 353cdf0e10cSrcweir if (aVendorInfo.is() == sal_False) 354cdf0e10cSrcweir return JFW_PLUGIN_E_NO_JRE; 355cdf0e10cSrcweir 356cdf0e10cSrcweir //Check if the detected JRE matches the version requirements 357cdf0e10cSrcweir if (ouVendor.equals(aVendorInfo->getVendor()) == sal_False) 358cdf0e10cSrcweir return JFW_PLUGIN_E_NO_JRE; 359cdf0e10cSrcweir 360cdf0e10cSrcweir if (ouMinVer.getLength() > 0) 361cdf0e10cSrcweir { 362cdf0e10cSrcweir int nRes = 0; 363cdf0e10cSrcweir try 364cdf0e10cSrcweir { 365cdf0e10cSrcweir nRes = aVendorInfo->compareVersions(ouMinVer); 366cdf0e10cSrcweir } 367cdf0e10cSrcweir catch (MalformedVersionException&) 368cdf0e10cSrcweir { 369cdf0e10cSrcweir //The minVersion was not recognized as valid for this vendor. 370cdf0e10cSrcweir JFW_ENSURE( 371cdf0e10cSrcweir 0,OUSTR("[Java framework]sunjavaplugin does not know version: ") 372cdf0e10cSrcweir + ouMinVer + OUSTR(" for vendor: ") + aVendorInfo->getVendor() 373cdf0e10cSrcweir + OUSTR(" .Check minimum Version.") ); 374cdf0e10cSrcweir return JFW_PLUGIN_E_WRONG_VERSION_FORMAT; 375cdf0e10cSrcweir } 376cdf0e10cSrcweir if (nRes < 0) 377cdf0e10cSrcweir return JFW_PLUGIN_E_FAILED_VERSION; 378cdf0e10cSrcweir } 379cdf0e10cSrcweir 380cdf0e10cSrcweir if (ouMaxVer.getLength() > 0) 381cdf0e10cSrcweir { 382cdf0e10cSrcweir int nRes = 0; 383cdf0e10cSrcweir try 384cdf0e10cSrcweir { 385cdf0e10cSrcweir nRes = aVendorInfo->compareVersions(ouMaxVer); 386cdf0e10cSrcweir } 387cdf0e10cSrcweir catch (MalformedVersionException&) 388cdf0e10cSrcweir { 389cdf0e10cSrcweir //The maxVersion was not recognized as valid for this vendor. 390cdf0e10cSrcweir JFW_ENSURE( 391cdf0e10cSrcweir 0,OUSTR("[Java framework]sunjavaplugin does not know version: ") 392cdf0e10cSrcweir + ouMaxVer + OUSTR(" for vendor: ") + aVendorInfo->getVendor() 393cdf0e10cSrcweir + OUSTR(" .Check maximum Version.") ); 394cdf0e10cSrcweir return JFW_PLUGIN_E_WRONG_VERSION_FORMAT; 395cdf0e10cSrcweir } 396cdf0e10cSrcweir if (nRes > 0) 397cdf0e10cSrcweir return JFW_PLUGIN_E_FAILED_VERSION; 398cdf0e10cSrcweir } 399cdf0e10cSrcweir 400cdf0e10cSrcweir if (arExcludeList > 0) 401cdf0e10cSrcweir { 402cdf0e10cSrcweir for (int i = 0; i < nLenList; i++) 403cdf0e10cSrcweir { 404cdf0e10cSrcweir rtl::OUString sExVer(arExcludeList[i]); 405cdf0e10cSrcweir int nRes = 0; 406cdf0e10cSrcweir try 407cdf0e10cSrcweir { 408cdf0e10cSrcweir nRes = aVendorInfo->compareVersions(sExVer); 409cdf0e10cSrcweir } 410cdf0e10cSrcweir catch (MalformedVersionException&) 411cdf0e10cSrcweir { 412cdf0e10cSrcweir //The excluded version was not recognized as valid for this vendor. 413cdf0e10cSrcweir JFW_ENSURE( 414cdf0e10cSrcweir 0,OUSTR("[Java framework]sunjavaplugin does not know version: ") 415cdf0e10cSrcweir + sExVer + OUSTR(" for vendor: ") + aVendorInfo->getVendor() 416cdf0e10cSrcweir + OUSTR(" .Check excluded versions.") ); 417cdf0e10cSrcweir return JFW_PLUGIN_E_WRONG_VERSION_FORMAT; 418cdf0e10cSrcweir } 419cdf0e10cSrcweir if (nRes == 0) 420cdf0e10cSrcweir return JFW_PLUGIN_E_FAILED_VERSION; 421cdf0e10cSrcweir } 422cdf0e10cSrcweir } 423cdf0e10cSrcweir *ppInfo = createJavaInfo(aVendorInfo); 424cdf0e10cSrcweir 425cdf0e10cSrcweir return errcode; 426cdf0e10cSrcweir } 427cdf0e10cSrcweir 428cdf0e10cSrcweir /** starts a Java Virtual Machine. 429cdf0e10cSrcweir <p> 430cdf0e10cSrcweir The function shall ensure, that the VM does not abort the process 431cdf0e10cSrcweir during instantiation. 432cdf0e10cSrcweir </p> 433cdf0e10cSrcweir */ 434cdf0e10cSrcweir extern "C" 435cdf0e10cSrcweir javaPluginError jfw_plugin_startJavaVirtualMachine( 436cdf0e10cSrcweir const JavaInfo *pInfo, 437cdf0e10cSrcweir const JavaVMOption* arOptions, 438cdf0e10cSrcweir sal_Int32 cOptions, 439cdf0e10cSrcweir JavaVM ** ppVm, 440cdf0e10cSrcweir JNIEnv ** ppEnv) 441cdf0e10cSrcweir { 442cdf0e10cSrcweir // unless guard is volatile the following warning occurs on gcc: 443cdf0e10cSrcweir // warning: variable 't' might be clobbered by `longjmp' or `vfork' 444cdf0e10cSrcweir volatile osl::MutexGuard guard(PluginMutex::get()); 445cdf0e10cSrcweir // unless errcode is volatile the following warning occurs on gcc: 446cdf0e10cSrcweir // warning: variable 'errcode' might be clobbered by `longjmp' or `vfork' 447cdf0e10cSrcweir volatile javaPluginError errcode = JFW_PLUGIN_E_NONE; 448cdf0e10cSrcweir if ( pInfo == NULL || ppVm == NULL || ppEnv == NULL) 449cdf0e10cSrcweir return JFW_PLUGIN_E_INVALID_ARG; 450cdf0e10cSrcweir //Check if the Vendor (pInfo->sVendor) is supported by this plugin 451cdf0e10cSrcweir if ( ! isVendorSupported(pInfo->sVendor)) 452cdf0e10cSrcweir return JFW_PLUGIN_E_WRONG_VENDOR; 453cdf0e10cSrcweir rtl::OUString sRuntimeLib = getRuntimeLib(pInfo->arVendorData); 454cdf0e10cSrcweir JFW_TRACE2(OUSTR("[Java framework] Using Java runtime library: ") 455cdf0e10cSrcweir + sRuntimeLib + OUSTR(".\n")); 456cdf0e10cSrcweir // On linux we load jvm with RTLD_GLOBAL. This is necessary for debugging, because 457cdf0e10cSrcweir // libjdwp.so need a symbol (fork1) from libjvm which it only gets if the jvm is loaded 458cdf0e10cSrcweir // witd RTLD_GLOBAL. On Solaris libjdwp.so is correctly linked with libjvm.so 459cdf0e10cSrcweir oslModule moduleRt = 0; 460cdf0e10cSrcweir #if defined(LINUX) 461cdf0e10cSrcweir if ((moduleRt = osl_loadModule(sRuntimeLib.pData, 462cdf0e10cSrcweir SAL_LOADMODULE_GLOBAL | SAL_LOADMODULE_NOW)) == 0 ) 463cdf0e10cSrcweir #else 464cdf0e10cSrcweir if ((moduleRt = osl_loadModule(sRuntimeLib.pData, SAL_LOADMODULE_DEFAULT)) == 0) 465cdf0e10cSrcweir #endif 466cdf0e10cSrcweir { 467cdf0e10cSrcweir JFW_ENSURE(0, OUSTR("[Java framework]sunjavaplugin" SAL_DLLEXTENSION 468cdf0e10cSrcweir " could not load Java runtime library: \n") 469cdf0e10cSrcweir + sRuntimeLib + OUSTR("\n")); 470cdf0e10cSrcweir JFW_TRACE0(OUSTR("[Java framework]sunjavaplugin" SAL_DLLEXTENSION 471cdf0e10cSrcweir " could not load Java runtime library: \n") 472cdf0e10cSrcweir + sRuntimeLib + OUSTR("\n")); 473cdf0e10cSrcweir return JFW_PLUGIN_E_VM_CREATION_FAILED; 474cdf0e10cSrcweir } 475cdf0e10cSrcweir 476cdf0e10cSrcweir #ifdef UNX 477cdf0e10cSrcweir //Setting the JAVA_HOME is needed for awt 478cdf0e10cSrcweir rtl::OUString javaHome(RTL_CONSTASCII_USTRINGPARAM("JAVA_HOME=")); 479cdf0e10cSrcweir rtl::OUString sPathLocation; 480cdf0e10cSrcweir osl_getSystemPathFromFileURL(pInfo->sLocation, & sPathLocation.pData); 481cdf0e10cSrcweir javaHome += sPathLocation; 482cdf0e10cSrcweir rtl::OString osJavaHome = rtl::OUStringToOString( 483cdf0e10cSrcweir javaHome, osl_getThreadTextEncoding()); 484cdf0e10cSrcweir putenv(strdup(osJavaHome.getStr())); 485cdf0e10cSrcweir #endif 486cdf0e10cSrcweir 487cdf0e10cSrcweir typedef jint JNICALL JNI_InitArgs_Type(void *); 488cdf0e10cSrcweir typedef jint JNICALL JNI_CreateVM_Type(JavaVM **, JNIEnv **, void *); 489cdf0e10cSrcweir rtl::OUString sSymbolCreateJava( 490cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("JNI_CreateJavaVM")); 491cdf0e10cSrcweir 492cdf0e10cSrcweir JNI_CreateVM_Type * pCreateJavaVM = (JNI_CreateVM_Type *) osl_getFunctionSymbol( 493cdf0e10cSrcweir moduleRt, sSymbolCreateJava.pData); 494cdf0e10cSrcweir if (!pCreateJavaVM) 495cdf0e10cSrcweir { 496cdf0e10cSrcweir OSL_ASSERT(0); 497cdf0e10cSrcweir rtl::OString sLib = rtl::OUStringToOString( 498cdf0e10cSrcweir sRuntimeLib, osl_getThreadTextEncoding()); 499cdf0e10cSrcweir rtl::OString sSymbol = rtl::OUStringToOString( 500cdf0e10cSrcweir sSymbolCreateJava, osl_getThreadTextEncoding()); 501cdf0e10cSrcweir fprintf(stderr,"[Java framework]sunjavaplugin"SAL_DLLEXTENSION 502cdf0e10cSrcweir "Java runtime library: %s does not export symbol %s !\n", 503cdf0e10cSrcweir sLib.getStr(), sSymbol.getStr()); 504cdf0e10cSrcweir return JFW_PLUGIN_E_VM_CREATION_FAILED; 505cdf0e10cSrcweir } 506cdf0e10cSrcweir 507cdf0e10cSrcweir // Some testing with Java 1.4 showed that JavaVMOption.optionString has to 508cdf0e10cSrcweir // be encoded with the system encoding (i.e., osl_getThreadTextEncoding): 509cdf0e10cSrcweir JavaVMInitArgs vm_args; 510cdf0e10cSrcweir 511cdf0e10cSrcweir boost::scoped_array<JavaVMOption> sarOptions( 512cdf0e10cSrcweir new JavaVMOption[cOptions + 1]); 513cdf0e10cSrcweir JavaVMOption * options = sarOptions.get(); 514cdf0e10cSrcweir 515cdf0e10cSrcweir // We set an abort handler which is called when the VM calls _exit during 516cdf0e10cSrcweir // JNI_CreateJavaVM. This happens when the LD_LIBRARY_PATH does not contain 517cdf0e10cSrcweir // all some directories of the Java installation. This is necessary for 518cdf0e10cSrcweir // all versions below 1.5.1 519cdf0e10cSrcweir options[0].optionString= (char *) "abort"; 520cdf0e10cSrcweir options[0].extraInfo= (void* )(sal_IntPtr)abort_handler; 521cdf0e10cSrcweir rtl::OString sClassPathProp("-Djava.class.path="); 522cdf0e10cSrcweir rtl::OString sClassPathOption; 523cdf0e10cSrcweir for (int i = 0; i < cOptions; i++) 524cdf0e10cSrcweir { 525cdf0e10cSrcweir #ifdef UNX 526cdf0e10cSrcweir // Until java 1.5 we need to put a plugin.jar or javaplugin.jar (<1.4.2) 527cdf0e10cSrcweir // in the class path in order to have applet support. 528cdf0e10cSrcweir rtl::OString sClassPath = arOptions[i].optionString; 529cdf0e10cSrcweir if (sClassPath.match(sClassPathProp, 0) == sal_True) 530cdf0e10cSrcweir { 531cdf0e10cSrcweir char sep[] = {SAL_PATHSEPARATOR, 0}; 532cdf0e10cSrcweir OString sAddPath = getPluginJarPath(pInfo->sVendor, pInfo->sLocation,pInfo->sVersion); 533cdf0e10cSrcweir if (sAddPath.getLength()) 534cdf0e10cSrcweir sClassPathOption = sClassPath + rtl::OString(sep) + sAddPath; 535cdf0e10cSrcweir else 536cdf0e10cSrcweir sClassPathOption = sClassPath; 537cdf0e10cSrcweir options[i+1].optionString = (char *) sClassPathOption.getStr(); 538cdf0e10cSrcweir options[i+1].extraInfo = arOptions[i].extraInfo; 539cdf0e10cSrcweir } 540cdf0e10cSrcweir else 541cdf0e10cSrcweir { 542cdf0e10cSrcweir #endif 543cdf0e10cSrcweir options[i+1].optionString = arOptions[i].optionString; 544cdf0e10cSrcweir options[i+1].extraInfo = arOptions[i].extraInfo; 545cdf0e10cSrcweir #ifdef UNX 546cdf0e10cSrcweir } 547cdf0e10cSrcweir #endif 548cdf0e10cSrcweir #if OSL_DEBUG_LEVEL >= 2 549cdf0e10cSrcweir JFW_TRACE2(OString("VM option: ") + OString(options[i+1].optionString) + 550cdf0e10cSrcweir OString("\n")); 551cdf0e10cSrcweir #endif 552cdf0e10cSrcweir } 553cdf0e10cSrcweir 554cdf0e10cSrcweir #ifdef MACOSX 555cdf0e10cSrcweir vm_args.version= JNI_VERSION_1_4; // issue 88987 556cdf0e10cSrcweir #else 557cdf0e10cSrcweir vm_args.version= JNI_VERSION_1_2; 558cdf0e10cSrcweir #endif 559cdf0e10cSrcweir vm_args.options= options; 560cdf0e10cSrcweir vm_args.nOptions= cOptions + 1; 561cdf0e10cSrcweir vm_args.ignoreUnrecognized= JNI_TRUE; 562cdf0e10cSrcweir 563cdf0e10cSrcweir /* We set a global flag which is used by the abort handler in order to 564cdf0e10cSrcweir determine whether it is should use longjmp to get back into this function. 565cdf0e10cSrcweir That is, the abort handler determines if it is on the same stack as this function 566cdf0e10cSrcweir and then jumps back into this function. 567cdf0e10cSrcweir */ 568cdf0e10cSrcweir g_bInGetJavaVM = 1; 569cdf0e10cSrcweir jint err; 570cdf0e10cSrcweir JavaVM * pJavaVM = 0; 571cdf0e10cSrcweir memset( jmp_jvm_abort, 0, sizeof(jmp_jvm_abort)); 572cdf0e10cSrcweir int jmpval= setjmp( jmp_jvm_abort ); 573cdf0e10cSrcweir /* If jmpval is not "0" then this point was reached by a longjmp in the 574cdf0e10cSrcweir abort_handler, which was called indirectly by JNI_CreateVM. 575cdf0e10cSrcweir */ 576cdf0e10cSrcweir if( jmpval == 0) 577cdf0e10cSrcweir { 578cdf0e10cSrcweir //returns negative number on failure 579cdf0e10cSrcweir err= pCreateJavaVM(&pJavaVM, ppEnv, &vm_args); 580cdf0e10cSrcweir g_bInGetJavaVM = 0; 581cdf0e10cSrcweir } 582cdf0e10cSrcweir else 583cdf0e10cSrcweir // set err to a positive number, so as or recognize that an abort (longjmp) 584cdf0e10cSrcweir //occurred 585cdf0e10cSrcweir err= 1; 586cdf0e10cSrcweir 587cdf0e10cSrcweir if(err != 0) 588cdf0e10cSrcweir { 589cdf0e10cSrcweir rtl::OUString message; 590cdf0e10cSrcweir if( err < 0) 591cdf0e10cSrcweir { 592cdf0e10cSrcweir fprintf(stderr,"[Java framework] sunjavaplugin"SAL_DLLEXTENSION 593cdf0e10cSrcweir "Can not create Java Virtual Machine\n"); 594cdf0e10cSrcweir errcode = JFW_PLUGIN_E_VM_CREATION_FAILED; 595cdf0e10cSrcweir } 596cdf0e10cSrcweir else if( err > 0) 597cdf0e10cSrcweir { 598cdf0e10cSrcweir fprintf(stderr,"[Java framework] sunjavaplugin"SAL_DLLEXTENSION 599cdf0e10cSrcweir "Can not create JavaVirtualMachine, abort handler was called.\n"); 600cdf0e10cSrcweir errcode = JFW_PLUGIN_E_VM_CREATION_FAILED; 601cdf0e10cSrcweir } 602cdf0e10cSrcweir } 603cdf0e10cSrcweir else 604cdf0e10cSrcweir { 605cdf0e10cSrcweir *ppVm = pJavaVM; 606cdf0e10cSrcweir JFW_TRACE2("[Java framework] sunjavaplugin"SAL_DLLEXTENSION " has created a VM.\n"); 607cdf0e10cSrcweir } 608cdf0e10cSrcweir 609cdf0e10cSrcweir 610cdf0e10cSrcweir return errcode; 611cdf0e10cSrcweir } 612cdf0e10cSrcweir 613cdf0e10cSrcweir extern "C" 614cdf0e10cSrcweir javaPluginError jfw_plugin_existJRE(const JavaInfo *pInfo, sal_Bool *exist) 615cdf0e10cSrcweir { 616cdf0e10cSrcweir javaPluginError ret = JFW_PLUGIN_E_NONE; 617cdf0e10cSrcweir if (!pInfo || !exist) 618cdf0e10cSrcweir return JFW_PLUGIN_E_INVALID_ARG; 619cdf0e10cSrcweir ::rtl::OUString sLocation(pInfo->sLocation); 620cdf0e10cSrcweir 621cdf0e10cSrcweir if (sLocation.getLength() == 0) 622cdf0e10cSrcweir return JFW_PLUGIN_E_INVALID_ARG; 623cdf0e10cSrcweir ::osl::DirectoryItem item; 624cdf0e10cSrcweir ::osl::File::RC rc_item = ::osl::DirectoryItem::get(sLocation, item); 625cdf0e10cSrcweir if (::osl::File::E_None == rc_item) 626cdf0e10cSrcweir { 627cdf0e10cSrcweir *exist = sal_True; 628cdf0e10cSrcweir } 629cdf0e10cSrcweir else if (::osl::File::E_NOENT == rc_item) 630cdf0e10cSrcweir { 631cdf0e10cSrcweir *exist = sal_False; 632cdf0e10cSrcweir } 633cdf0e10cSrcweir else 634cdf0e10cSrcweir { 635cdf0e10cSrcweir ret = JFW_PLUGIN_E_ERROR; 636cdf0e10cSrcweir } 637cdf0e10cSrcweir #ifdef MACOSX 638cdf0e10cSrcweir //We can have the situation that the JavaVM runtime library is not 639cdf0e10cSrcweir //contained within JAVA_HOME. Then the check for JAVA_HOME would return 640cdf0e10cSrcweir //true although the runtime library may not be loadable. 641cdf0e10cSrcweir if (ret == JFW_PLUGIN_E_NONE && *exist == sal_True) 642cdf0e10cSrcweir { 643cdf0e10cSrcweir rtl::OUString sRuntimeLib = getRuntimeLib(pInfo->arVendorData); 644cdf0e10cSrcweir JFW_TRACE2(OUSTR("[Java framework] Checking existence of Java runtime library.\n")); 645cdf0e10cSrcweir 646cdf0e10cSrcweir ::osl::DirectoryItem itemRt; 647cdf0e10cSrcweir ::osl::File::RC rc_itemRt = ::osl::DirectoryItem::get(sRuntimeLib, itemRt); 648cdf0e10cSrcweir if (::osl::File::E_None == rc_itemRt) 649cdf0e10cSrcweir { 650cdf0e10cSrcweir *exist = sal_True; 651cdf0e10cSrcweir JFW_TRACE2(OUSTR("[Java framework] Java runtime library exist: ") 652cdf0e10cSrcweir + sRuntimeLib + OUSTR("\n")); 653cdf0e10cSrcweir 654cdf0e10cSrcweir } 655cdf0e10cSrcweir else if (::osl::File::E_NOENT == rc_itemRt) 656cdf0e10cSrcweir { 657cdf0e10cSrcweir *exist = sal_False; 658cdf0e10cSrcweir JFW_TRACE2(OUSTR("[Java framework] Java runtime library does not exist: ") 659cdf0e10cSrcweir + sRuntimeLib + OUSTR("\n")); 660cdf0e10cSrcweir } 661cdf0e10cSrcweir else 662cdf0e10cSrcweir { 663cdf0e10cSrcweir ret = JFW_PLUGIN_E_ERROR; 664cdf0e10cSrcweir JFW_TRACE2(OUSTR("[Java framework] Error while looking for Java runtime library: ") 665cdf0e10cSrcweir + sRuntimeLib + OUSTR(" \n")); 666cdf0e10cSrcweir } 667cdf0e10cSrcweir } 668cdf0e10cSrcweir #endif 669cdf0e10cSrcweir return ret; 670cdf0e10cSrcweir } 671cdf0e10cSrcweir 672cdf0e10cSrcweir 673