LCOV - code coverage report
Current view: top level - libreoffice/framework/source/jobs - shelljob.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 64 0.0 %
Date: 2012-12-27 Functions: 0 17 0.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             : //_______________________________________________
      22             : // include own header
      23             : 
      24             : #include <jobs/shelljob.hxx>
      25             : #include <jobs/jobconst.hxx>
      26             : #include <threadhelp/readguard.hxx>
      27             : #include <services.h>
      28             : 
      29             : //_______________________________________________
      30             : // include others
      31             : 
      32             : #include <osl/file.hxx>
      33             : #include <osl/process.h>
      34             : #include <vcl/svapp.hxx>
      35             : #include <rtl/ustrbuf.hxx>
      36             : #include <comphelper/processfactory.hxx>
      37             : #include <comphelper/sequenceashashmap.hxx>
      38             : 
      39             : //_______________________________________________
      40             : // include interfaces
      41             : 
      42             : #include <com/sun/star/util/PathSubstitution.hpp>
      43             : #include <com/sun/star/util/XStringSubstitution.hpp>
      44             : 
      45             : 
      46             : namespace framework{
      47             : 
      48             : 
      49             : /** address job configuration inside argument set provided on method execute(). */
      50           0 : static const ::rtl::OUString PROP_JOBCONFIG("JobConfig");
      51             : 
      52             : /** address job configuration property "Command". */
      53           0 : static const ::rtl::OUString PROP_COMMAND("Command");
      54             : 
      55             : /** address job configuration property "Arguments". */
      56           0 : static const ::rtl::OUString PROP_ARGUMENTS("Arguments");
      57             : 
      58             : /** address job configuration property "DeactivateJobIfDone". */
      59           0 : static const ::rtl::OUString PROP_DEACTIVATEJOBIFDONE("DeactivateJobIfDone");
      60             : 
      61             : /** address job configuration property "CheckExitCode". */
      62           0 : static const ::rtl::OUString PROP_CHECKEXITCODE("CheckExitCode");
      63             : 
      64             : //-----------------------------------------------
      65             : 
      66           0 : DEFINE_XSERVICEINFO_MULTISERVICE(ShellJob                   ,
      67             :                                  ::cppu::OWeakObject        ,
      68             :                                  SERVICENAME_JOB            ,
      69             :                                  IMPLEMENTATIONNAME_SHELLJOB)
      70             : 
      71           0 : DEFINE_INIT_SERVICE(ShellJob,
      72             :                     {
      73             :                         /*  Attention
      74             :                             I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
      75             :                             to create a new instance of this class by our own supported service factory.
      76             :                             see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations!
      77             :                         */
      78             :                     }
      79             :                    )
      80             : 
      81             : //-----------------------------------------------
      82           0 : ShellJob::ShellJob(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
      83             :     : ThreadHelpBase(     )
      84           0 :     , m_xSMGR       (xSMGR)
      85             : {
      86           0 : }
      87             : 
      88             : //-----------------------------------------------
      89           0 : ShellJob::~ShellJob()
      90             : {
      91           0 : }
      92             : 
      93             : //-----------------------------------------------
      94           0 : css::uno::Any SAL_CALL ShellJob::execute(const css::uno::Sequence< css::beans::NamedValue >& lJobArguments)
      95             :     throw(css::lang::IllegalArgumentException,
      96             :           css::uno::Exception                ,
      97             :           css::uno::RuntimeException         )
      98             : {
      99           0 :     ::comphelper::SequenceAsHashMap lArgs  (lJobArguments);
     100           0 :     ::comphelper::SequenceAsHashMap lOwnCfg(lArgs.getUnpackedValueOrDefault(PROP_JOBCONFIG, css::uno::Sequence< css::beans::NamedValue >()));
     101             : 
     102           0 :     const ::rtl::OUString                       sCommand                   = lOwnCfg.getUnpackedValueOrDefault(PROP_COMMAND                  , ::rtl::OUString());
     103           0 :     const css::uno::Sequence< ::rtl::OUString > lCommandArguments          = lOwnCfg.getUnpackedValueOrDefault(PROP_ARGUMENTS                , css::uno::Sequence< ::rtl::OUString >());
     104           0 :     const ::sal_Bool                            bDeactivateJobIfDone       = lOwnCfg.getUnpackedValueOrDefault(PROP_DEACTIVATEJOBIFDONE      , sal_True         );
     105           0 :     const ::sal_Bool                            bCheckExitCode             = lOwnCfg.getUnpackedValueOrDefault(PROP_CHECKEXITCODE            , sal_True         );
     106             : 
     107             :     // replace all might existing place holder.
     108           0 :     ::rtl::OUString sRealCommand = impl_substituteCommandVariables(sCommand);
     109             : 
     110             :     // Command is required as minimum.
     111             :     // If it does not exists ... we cant do our job.
     112             :     // Deactivate such miss configured job silently .-)
     113           0 :     if (sRealCommand.isEmpty())
     114           0 :         return ShellJob::impl_generateAnswer4Deactivation();
     115             : 
     116             :     // do it
     117           0 :     ::sal_Bool bDone = impl_execute(sRealCommand, lCommandArguments, bCheckExitCode);
     118           0 :     if (! bDone)
     119           0 :         return css::uno::Any();
     120             : 
     121             :     // Job was done ... user configured deactivation of this job
     122             :     // in such case.
     123           0 :     if (bDeactivateJobIfDone)
     124           0 :         return ShellJob::impl_generateAnswer4Deactivation();
     125             : 
     126             :     // There was no decision about deactivation of this job.
     127             :     // So we have to return nothing here !
     128           0 :     return css::uno::Any();
     129             : }
     130             : 
     131             : //-----------------------------------------------
     132           0 : css::uno::Any ShellJob::impl_generateAnswer4Deactivation()
     133             : {
     134           0 :     css::uno::Sequence< css::beans::NamedValue > aAnswer(1);
     135           0 :     aAnswer[0].Name  = JobConst::ANSWER_DEACTIVATE_JOB();
     136           0 :     aAnswer[0].Value = css::uno::makeAny(sal_True);
     137             : 
     138           0 :     return css::uno::makeAny(aAnswer);
     139             : }
     140             : 
     141             : //-----------------------------------------------
     142           0 : ::rtl::OUString ShellJob::impl_substituteCommandVariables(const ::rtl::OUString& sCommand)
     143             : {
     144             :     // SYNCHRONIZED ->
     145           0 :     ReadGuard aReadLock(m_aLock);
     146           0 :     css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
     147           0 :     aReadLock.unlock();
     148             :     // <- SYNCHRONIZED
     149             : 
     150             :     try
     151             :     {
     152           0 :         css::uno::Reference< css::uno::XComponentContext >    xContext( comphelper::getComponentContext(xSMGR) );
     153           0 :         css::uno::Reference< css::util::XStringSubstitution > xSubst(  css::util::PathSubstitution::create(xContext) );
     154           0 :         const ::sal_Bool                                      bSubstRequired   = sal_True;
     155           0 :         const ::rtl::OUString                                 sCompleteCommand = xSubst->substituteVariables(sCommand, bSubstRequired);
     156             : 
     157           0 :         return sCompleteCommand;
     158             :     }
     159           0 :     catch(const css::uno::Exception&)
     160             :     {}
     161             : 
     162           0 :     return ::rtl::OUString();
     163             : }
     164             : 
     165             : //-----------------------------------------------
     166           0 : ::sal_Bool ShellJob::impl_execute(const ::rtl::OUString&                       sCommand      ,
     167             :                                   const css::uno::Sequence< ::rtl::OUString >& lArguments    ,
     168             :                                         ::sal_Bool                             bCheckExitCode)
     169             : {
     170           0 :           ::rtl_uString**  pArgs    = NULL;
     171           0 :     const ::sal_Int32      nArgs    = lArguments.getLength ();
     172           0 :           oslProcessOption nOptions = osl_Process_WAIT;
     173           0 :           oslProcess       hProcess(0);
     174             : 
     175           0 :     if (nArgs > 0)
     176           0 :         pArgs = reinterpret_cast< ::rtl_uString** >(const_cast< ::rtl::OUString* >(lArguments.getConstArray()));
     177             : 
     178           0 :     oslProcessError eError = osl_executeProcess(sCommand.pData, pArgs, nArgs, nOptions, NULL, NULL, NULL, 0, &hProcess);
     179             : 
     180             :     // executable not found or couldnt be started
     181           0 :     if (eError != osl_Process_E_None)
     182           0 :         return sal_False;
     183             : 
     184           0 :     ::sal_Bool bRet = sal_True;
     185           0 :     if (bCheckExitCode)
     186             :     {
     187             :         // check its return codes ...
     188             :         oslProcessInfo aInfo;
     189           0 :         aInfo.Size = sizeof (oslProcessInfo);
     190           0 :         eError = osl_getProcessInfo(hProcess, osl_Process_EXITCODE, &aInfo);
     191             : 
     192           0 :         if (eError != osl_Process_E_None)
     193           0 :             bRet = sal_False;
     194             :         else
     195           0 :             bRet = (aInfo.Code == 0);
     196             :     }
     197           0 :     osl_freeProcessHandle(hProcess);
     198           0 :     return bRet;
     199             : }
     200             : 
     201           0 : } // namespace framework
     202             : 
     203             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10