LCOV - code coverage report
Current view: top level - jvmfwk/plugins/sunmajor/javaenvsetup - javaldx.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 32 58 55.2 %
Date: 2015-06-13 12:38:46 Functions: 4 5 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include <stdio.h>
      22             : #include <stdlib.h>
      23             : #include <string.h>
      24             : #include "sal/main.h"
      25             : #include "sal/types.h"
      26             : #include "osl/thread.h"
      27             : #include "rtl/ustring.hxx"
      28             : #include "rtl/byteseq.hxx"
      29             : #include "jvmfwk/framework.h"
      30             : 
      31             : 
      32             : static bool hasOption(char const * szOption, int argc, char** argv);
      33             : static OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData);
      34             : static bool findAndSelect(JavaInfo**);
      35             : 
      36             : #define HELP_TEXT    \
      37             : "\njavaldx is necessary to make Java work on some UNIX platforms." \
      38             : "It prints a string to std out that consists of directories which " \
      39             : "have to be included into the LD_LIBRARY_PATH variable.The setting of " \
      40             : "the variable usually occurs in a shell script that runs javaldx.\n" \
      41             : "The directories are from the chosen java installation. \n" \
      42             : "Options are: \n"\
      43             : "--help or -h\n"
      44             : 
      45         140 : SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
      46             : {
      47             :     try
      48             :     {
      49          70 :         if( hasOption("--help",argc, argv) || hasOption("-h", argc, argv))
      50             :         {
      51           0 :             fprintf(stdout, HELP_TEXT);// default
      52           0 :             return 0;
      53             :         }
      54          70 :         javaFrameworkError errcode = JFW_E_NONE;
      55          70 :         sal_Bool bEnabled = sal_False;
      56          70 :         errcode = jfw_getEnabled( & bEnabled);
      57          70 :         if (errcode == JFW_E_NONE && bEnabled == sal_False)
      58             :         {
      59             :                 //Do not do any preparation because that may only slow startup time.
      60           0 :             return 0;
      61             :         }
      62          70 :         else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE)
      63             :         {
      64           0 :             fprintf(stderr,"javaldx failed!\n");
      65           0 :             return -1;
      66             :         }
      67             : 
      68          70 :         JavaInfo * pInfo = NULL;
      69          70 :         errcode = jfw_getSelectedJRE( & pInfo);
      70             : 
      71          70 :         if (errcode != JFW_E_NONE && errcode != JFW_E_INVALID_SETTINGS)
      72             :         {
      73           0 :             fprintf(stderr,"javaldx failed!\n");
      74           0 :             return -1;
      75             :         }
      76             : 
      77          70 :         if (pInfo == NULL)
      78             :         {
      79           0 :             if (!findAndSelect(&pInfo))
      80           0 :                 return -1;
      81             :         }
      82             :         else
      83             :         {
      84             :             //check if the JRE was not uninstalled
      85          70 :             sal_Bool bExist = sal_False;
      86          70 :             errcode = jfw_existJRE(pInfo, &bExist);
      87          70 :             if (errcode == JFW_E_NONE)
      88             :             {
      89          70 :                 if (!bExist && !findAndSelect(&pInfo))
      90           0 :                     return -1;
      91             :             }
      92             :             else
      93             :             {
      94           0 :                 fprintf(stderr, "javaldx: Could not determine if JRE still exist\n");
      95           0 :                 return -1;
      96             :             }
      97             :         }
      98             : 
      99          70 :         OString sPaths = getLD_LIBRARY_PATH(pInfo->arVendorData);
     100          70 :         fprintf(stdout, "%s\n", sPaths.getStr());
     101          70 :         jfw_freeJavaInfo(pInfo);
     102             :     }
     103           0 :     catch (const std::exception&)
     104             :     {
     105           0 :         fprintf(stderr,"javaldx failed!\n");
     106           0 :         return -1;
     107             :     }
     108             : 
     109          70 :     return 0;
     110             : }
     111             : 
     112          70 : OString getLD_LIBRARY_PATH(const rtl::ByteSequence & vendorData)
     113             : {
     114          70 :     const sal_Unicode* chars = reinterpret_cast<sal_Unicode const *>(vendorData.getConstArray());
     115          70 :     sal_Int32 len = vendorData.getLength();
     116          70 :     OUString sData(chars, len / 2);
     117             :     //the runtime lib is on the first line
     118          70 :     sal_Int32 index = 0;
     119         140 :     OUString aToken = sData.getToken( 1, '\n', index);
     120             : 
     121             :     OString paths =
     122          70 :         OUStringToOString(aToken, osl_getThreadTextEncoding());
     123         140 :     return paths;
     124             : }
     125             : 
     126         140 : static bool hasOption(char const * szOption, int argc, char** argv)
     127             : {
     128         140 :     bool retVal= false;
     129         564 :     for(int i= 1; i < argc; i++)
     130             :     {
     131         424 :         if( ! strcmp(argv[i], szOption))
     132             :         {
     133           0 :             retVal= true;
     134           0 :             break;
     135             :         }
     136             :     }
     137         140 :     return retVal;
     138             : }
     139             : 
     140           0 : static bool findAndSelect(JavaInfo ** ppInfo)
     141             : {
     142           0 :     javaFrameworkError errcode = jfw_findAndSelectJRE(ppInfo);
     143           0 :     if (errcode == JFW_E_NO_JAVA_FOUND)
     144             :     {
     145           0 :         fprintf(stderr,"javaldx: Could not find a Java Runtime Environment!\n");
     146           0 :         return false;
     147             :     }
     148           0 :     else if (errcode != JFW_E_NONE && errcode != JFW_E_DIRECT_MODE)
     149             :     {
     150           0 :         fprintf(stderr,"javaldx failed!\n");
     151           0 :         return false;
     152             :     }
     153           0 :     return true;
     154             : }
     155             : 
     156             : 
     157             : 
     158             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11