LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/framework/source/jobs - shelljob.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 63 0.0 %
Date: 2013-07-09 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 OUString PROP_JOBCONFIG("JobConfig");
      51             : 
      52             : /** address job configuration property "Command". */
      53           0 : static const OUString PROP_COMMAND("Command");
      54             : 
      55             : /** address job configuration property "Arguments". */
      56           0 : static const OUString PROP_ARGUMENTS("Arguments");
      57             : 
      58             : /** address job configuration property "DeactivateJobIfDone". */
      59           0 : static const OUString PROP_DEACTIVATEJOBIFDONE("DeactivateJobIfDone");
      60             : 
      61             : /** address job configuration property "CheckExitCode". */
      62           0 : static const OUString PROP_CHECKEXITCODE("CheckExitCode");
      63             : 
      64             : //-----------------------------------------------
      65             : 
      66           0 : DEFINE_XSERVICEINFO_MULTISERVICE_2(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 information!
      77             :                         */
      78             :                     }
      79             :                    )
      80             : 
      81             : //-----------------------------------------------
      82           0 : ShellJob::ShellJob(const css::uno::Reference< css::uno::XComponentContext >& xContext)
      83             :     : ThreadHelpBase(     )
      84           0 :     , m_xContext    (xContext)
      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 OUString                       sCommand                   = lOwnCfg.getUnpackedValueOrDefault(PROP_COMMAND                  , OUString());
     103           0 :     const css::uno::Sequence< OUString > lCommandArguments          = lOwnCfg.getUnpackedValueOrDefault(PROP_ARGUMENTS                , css::uno::Sequence< 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 :     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 : OUString ShellJob::impl_substituteCommandVariables(const OUString& sCommand)
     143             : {
     144             :     // SYNCHRONIZED ->
     145           0 :     ReadGuard aReadLock(m_aLock);
     146           0 :     css::uno::Reference< css::uno::XComponentContext > xContext = m_xContext;
     147           0 :     aReadLock.unlock();
     148             :     // <- SYNCHRONIZED
     149             : 
     150             :     try
     151             :     {
     152           0 :         css::uno::Reference< css::util::XStringSubstitution > xSubst(  css::util::PathSubstitution::create(xContext) );
     153           0 :         const ::sal_Bool                                      bSubstRequired   = sal_True;
     154           0 :         const OUString                                 sCompleteCommand = xSubst->substituteVariables(sCommand, bSubstRequired);
     155             : 
     156           0 :         return sCompleteCommand;
     157             :     }
     158           0 :     catch(const css::uno::Exception&)
     159             :     {}
     160             : 
     161           0 :     return OUString();
     162             : }
     163             : 
     164             : //-----------------------------------------------
     165           0 : ::sal_Bool ShellJob::impl_execute(const OUString&                       sCommand      ,
     166             :                                   const css::uno::Sequence< OUString >& lArguments    ,
     167             :                                         ::sal_Bool                             bCheckExitCode)
     168             : {
     169           0 :           ::rtl_uString**  pArgs    = NULL;
     170           0 :     const ::sal_Int32      nArgs    = lArguments.getLength ();
     171           0 :           oslProcessOption nOptions = osl_Process_WAIT;
     172           0 :           oslProcess       hProcess(0);
     173             : 
     174           0 :     if (nArgs > 0)
     175           0 :         pArgs = reinterpret_cast< ::rtl_uString** >(const_cast< OUString* >(lArguments.getConstArray()));
     176             : 
     177           0 :     oslProcessError eError = osl_executeProcess(sCommand.pData, pArgs, nArgs, nOptions, NULL, NULL, NULL, 0, &hProcess);
     178             : 
     179             :     // executable not found or couldnt be started
     180           0 :     if (eError != osl_Process_E_None)
     181           0 :         return sal_False;
     182             : 
     183           0 :     ::sal_Bool bRet = sal_True;
     184           0 :     if (bCheckExitCode)
     185             :     {
     186             :         // check its return codes ...
     187             :         oslProcessInfo aInfo;
     188           0 :         aInfo.Size = sizeof (oslProcessInfo);
     189           0 :         eError = osl_getProcessInfo(hProcess, osl_Process_EXITCODE, &aInfo);
     190             : 
     191           0 :         if (eError != osl_Process_E_None)
     192           0 :             bRet = sal_False;
     193             :         else
     194           0 :             bRet = (aInfo.Code == 0);
     195             :     }
     196           0 :     osl_freeProcessHandle(hProcess);
     197           0 :     return bRet;
     198             : }
     199             : 
     200           0 : } // namespace framework
     201             : 
     202             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10