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