LibreOffice Module framework (master) 1
shelljob.cxx
Go to the documentation of this file.
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// include own header
21
22#include <jobs/shelljob.hxx>
23#include <jobs/jobconst.hxx>
24#include <services.h>
25
26// include others
27
28#include <osl/process.h>
30
31// include interfaces
32
33#include <com/sun/star/util/PathSubstitution.hpp>
34#include <com/sun/star/util/XStringSubstitution.hpp>
36#include <utility>
37
38namespace framework{
39
40
41// XInterface, XTypeProvider, XServiceInfo
42
44{
45 return "com.sun.star.comp.framework.ShellJob";
46}
47
48sal_Bool SAL_CALL ShellJob::supportsService( const OUString& sServiceName )
49{
51}
52
53css::uno::Sequence< OUString > SAL_CALL ShellJob::getSupportedServiceNames()
54{
55 return { SERVICENAME_JOB };
56}
57
58
59ShellJob::ShellJob(css::uno::Reference< css::uno::XComponentContext > xContext)
60 : m_xContext (std::move(xContext))
61{
62}
63
65{
66}
67
68css::uno::Any SAL_CALL ShellJob::execute(const css::uno::Sequence< css::beans::NamedValue >& lJobArguments)
69{
70 ::comphelper::SequenceAsHashMap lArgs (lJobArguments);
72 ::comphelper::SequenceAsHashMap lOwnCfg(lArgs.getUnpackedValueOrDefault("JobConfig", css::uno::Sequence< css::beans::NamedValue >()));
73
74 const OUString sCommand = lOwnCfg.getUnpackedValueOrDefault("Command" , OUString());
75 const css::uno::Sequence< OUString > lCommandArguments = lOwnCfg.getUnpackedValueOrDefault("Arguments" , css::uno::Sequence< OUString >());
76 const bool bDeactivateJobIfDone = lOwnCfg.getUnpackedValueOrDefault("DeactivateJobIfDone" , true );
77 const bool bCheckExitCode = lOwnCfg.getUnpackedValueOrDefault("CheckExitCode" , true );
78
79 // replace all might existing place holder.
80 OUString sRealCommand = impl_substituteCommandVariables(sCommand);
81
82 // Command is required as minimum.
83 // If it does not exists ... we can't do our job.
84 // Deactivate such miss configured job silently .-)
85 if (sRealCommand.isEmpty())
87
88 // do it
89 bool bDone = impl_execute(sRealCommand, lCommandArguments, bCheckExitCode);
90 if (! bDone)
91 return css::uno::Any();
92
93 // Job was done ... user configured deactivation of this job
94 // in such case.
95 if (bDeactivateJobIfDone)
97
98 // There was no decision about deactivation of this job.
99 // So we have to return nothing here !
100 return css::uno::Any();
101}
102
104{
105 css::uno::Sequence< css::beans::NamedValue > aAnswer { { JobConst::ANSWER_DEACTIVATE_JOB, css::uno::Any(true) } };
106 return css::uno::Any(aAnswer);
107}
108
109OUString ShellJob::impl_substituteCommandVariables(const OUString& sCommand)
110{
111 try
112 {
113 css::uno::Reference< css::util::XStringSubstitution > xSubst( css::util::PathSubstitution::create(m_xContext) );
114 const bool bSubstRequired = true;
115 const OUString sCompleteCommand = xSubst->substituteVariables(sCommand, bSubstRequired);
116
117 return sCompleteCommand;
118 }
119 catch(const css::uno::Exception&)
120 {}
121
122 return OUString();
123}
124
125bool ShellJob::impl_execute(const OUString& sCommand ,
126 const css::uno::Sequence< OUString >& lArguments ,
127 bool bCheckExitCode)
128{
129 ::rtl_uString** pArgs = nullptr;
130 const ::sal_Int32 nArgs = lArguments.getLength ();
131 oslProcess hProcess(nullptr);
132
133 if (nArgs > 0)
134 pArgs = reinterpret_cast< ::rtl_uString** >(const_cast< OUString* >(lArguments.getConstArray()));
135
136 oslProcessError eError = osl_executeProcess(sCommand.pData, pArgs, nArgs, osl_Process_WAIT, nullptr, nullptr, nullptr, 0, &hProcess);
137
138 // executable not found or couldn't be started
139 if (eError != osl_Process_E_None)
140 return false;
141
142 bool bRet = true;
143 if (bCheckExitCode)
144 {
145 // check its return codes ...
146 oslProcessInfo aInfo;
147 aInfo.Size = sizeof (oslProcessInfo);
148 eError = osl_getProcessInfo(hProcess, osl_Process_EXITCODE, &aInfo);
149
150 if (eError != osl_Process_E_None)
151 bRet = false;
152 else
153 bRet = (aInfo.Code == 0);
154 }
155 osl_freeProcessHandle(hProcess);
156 return bRet;
157}
158
159} // namespace framework
160
161extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
163 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
164{
165 return cppu::acquire(new framework::ShellJob(context));
166}
167
168/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral sServiceName
TValueType getUnpackedValueOrDefault(const OUString &sKey, const TValueType &aDefault) const
static constexpr OUStringLiteral ANSWER_DEACTIVATE_JOB
Definition: jobconst.hxx:37
implements a job component which can be used to execute system shell commands.
Definition: shelljob.hxx:40
css::uno::Reference< css::uno::XComponentContext > m_xContext
reference to a uno service manager.
Definition: shelljob.hxx:46
ShellJob(css::uno::Reference< css::uno::XComponentContext > xContext)
create new instance of this class.
Definition: shelljob.cxx:59
OUString impl_substituteCommandVariables(const OUString &sCommand)
substitute all might existing placeholder variables within the configured command.
Definition: shelljob.cxx:109
virtual ~ShellJob() override
does nothing real ...
Definition: shelljob.cxx:64
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
Definition: shelljob.cxx:48
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: shelljob.cxx:53
bool impl_execute(const OUString &sCommand, const css::uno::Sequence< OUString > &lArguments, bool bCheckExitCode)
executes the command.
Definition: shelljob.cxx:125
static css::uno::Any impl_generateAnswer4Deactivation()
generate a return value for method execute() which will force deactivation of this job for further re...
Definition: shelljob.cxx:103
virtual css::uno::Any SAL_CALL execute(const css::uno::Sequence< css::beans::NamedValue > &lArguments) override
Definition: shelljob.cxx:68
virtual OUString SAL_CALL getImplementationName() override
Definition: shelljob.cxx:43
css::uno::Reference< css::uno::XComponentContext > m_xContext
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral SERVICENAME_JOB
Definition: services.h:31
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * framework_ShellJob_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: shelljob.cxx:162
unsigned char sal_Bool