1*2a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2a97ec55SAndrew Rist  * distributed with this work for additional information
6*2a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
9*2a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2a97ec55SAndrew Rist  *
11*2a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2a97ec55SAndrew Rist  *
13*2a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2a97ec55SAndrew Rist  * software distributed under the License is distributed on an
15*2a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2a97ec55SAndrew Rist  * specific language governing permissions and limitations
18*2a97ec55SAndrew Rist  * under the License.
19*2a97ec55SAndrew Rist  *
20*2a97ec55SAndrew Rist  *************************************************************/
21*2a97ec55SAndrew Rist 
22*2a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "onlogrotate_job.hxx"
28cdf0e10cSrcweir #include "config.hxx"
29cdf0e10cSrcweir #include "logpacker.hxx"
30cdf0e10cSrcweir #include "logstorage.hxx"
31cdf0e10cSrcweir #include "soaprequest.hxx"
32cdf0e10cSrcweir #include "soapsender.hxx"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
35cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
36cdf0e10cSrcweir #include <com/sun/star/frame/XTerminateListener.hpp>
37cdf0e10cSrcweir #include <osl/conditn.hxx>
38cdf0e10cSrcweir #include <osl/thread.hxx>
39cdf0e10cSrcweir #include <osl/time.h>
40cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
41cdf0e10cSrcweir #include <memory>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace ::com::sun::star::beans;
45cdf0e10cSrcweir using namespace ::com::sun::star::lang;
46cdf0e10cSrcweir using namespace ::com::sun::star::task;
47cdf0e10cSrcweir using namespace ::com::sun::star::uno;
48cdf0e10cSrcweir using ::com::sun::star::frame::XTerminateListener;
49cdf0e10cSrcweir using ::com::sun::star::frame::XDesktop;
50cdf0e10cSrcweir using ::com::sun::star::ucb::XSimpleFileAccess;
51cdf0e10cSrcweir using ::rtl::OUString;
52cdf0e10cSrcweir using ::std::vector;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir namespace
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     using namespace oooimprovement;
57cdf0e10cSrcweir 
packLogs(const Reference<XMultiServiceFactory> & sf)58cdf0e10cSrcweir     static void packLogs(const Reference<XMultiServiceFactory>& sf)
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         try
61cdf0e10cSrcweir         {
62cdf0e10cSrcweir             Config config(sf);
63cdf0e10cSrcweir             LogPacker log_packer(sf);
64cdf0e10cSrcweir             vector<OUString> csvfiles = LogStorage(sf).getUnzippedLogFiles();
65cdf0e10cSrcweir             for(
66cdf0e10cSrcweir                 vector<OUString>::iterator item = csvfiles.begin();
67cdf0e10cSrcweir                 item!=csvfiles.end();
68cdf0e10cSrcweir                 item++)
69cdf0e10cSrcweir                 config.incrementEventCount(log_packer.pack(*item));
70cdf0e10cSrcweir         } catch(...) {};
71cdf0e10cSrcweir     };
72cdf0e10cSrcweir 
uploadLogs(const Reference<XMultiServiceFactory> & sf)73cdf0e10cSrcweir     static void uploadLogs(const Reference<XMultiServiceFactory>& sf)
74cdf0e10cSrcweir     {
75cdf0e10cSrcweir         try
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir             Config config(sf);
78cdf0e10cSrcweir             Reference<XSimpleFileAccess> file_access(
79cdf0e10cSrcweir                 sf->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
80cdf0e10cSrcweir                 UNO_QUERY_THROW);
81cdf0e10cSrcweir             SoapSender sender(sf, config.getSoapUrl());
82cdf0e10cSrcweir             OUString soap_id = config.getSoapId();
83cdf0e10cSrcweir             vector<OUString> zipfiles = LogStorage(sf).getZippedLogFiles();
84cdf0e10cSrcweir             for(
85cdf0e10cSrcweir                 vector<OUString>::iterator item = zipfiles.begin();
86cdf0e10cSrcweir                 item!=zipfiles.end();
87cdf0e10cSrcweir                 item++)
88cdf0e10cSrcweir             {
89cdf0e10cSrcweir                 if(config.incrementFailedAttempts(1) > 25)
90cdf0e10cSrcweir                 {
91cdf0e10cSrcweir                     config.giveupUploading();
92cdf0e10cSrcweir                     LogStorage(sf).clear();
93cdf0e10cSrcweir                     return;
94cdf0e10cSrcweir                 }
95cdf0e10cSrcweir                 sender.send(SoapRequest(sf, soap_id, *item));
96cdf0e10cSrcweir                 config.incrementReportCount(1);
97cdf0e10cSrcweir                 file_access->kill(*item);
98cdf0e10cSrcweir                 config.resetFailedAttempts();
99cdf0e10cSrcweir             }
100cdf0e10cSrcweir         } catch(...) {};
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     class OnLogRotateThread : public ::osl::Thread
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         public:
106cdf0e10cSrcweir             OnLogRotateThread(Reference<XMultiServiceFactory> sf);
107cdf0e10cSrcweir             virtual void SAL_CALL run();
108cdf0e10cSrcweir             void stop();
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         private:
111cdf0e10cSrcweir             Reference<XMultiServiceFactory> m_ServiceFactory;
112cdf0e10cSrcweir             ::osl::Condition m_Stop;
113cdf0e10cSrcweir     };
114cdf0e10cSrcweir 
OnLogRotateThread(Reference<XMultiServiceFactory> sf)115cdf0e10cSrcweir     OnLogRotateThread::OnLogRotateThread(Reference<XMultiServiceFactory> sf)
116cdf0e10cSrcweir         : m_ServiceFactory(sf)
117cdf0e10cSrcweir     {
118cdf0e10cSrcweir         OSL_ASSERT(sf.is());
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
run()121cdf0e10cSrcweir     void SAL_CALL OnLogRotateThread::run()
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         TimeValue wait_intervall = {30,0};
124cdf0e10cSrcweir         if (m_Stop.wait(&wait_intervall) == ::osl::Condition::result_timeout)
125cdf0e10cSrcweir         {
126cdf0e10cSrcweir             try
127cdf0e10cSrcweir             {
128cdf0e10cSrcweir                 if(Config(m_ServiceFactory).getInvitationAccepted())
129cdf0e10cSrcweir                 {
130cdf0e10cSrcweir                     packLogs(m_ServiceFactory);
131cdf0e10cSrcweir                     uploadLogs(m_ServiceFactory);
132cdf0e10cSrcweir                 }
133cdf0e10cSrcweir                 else
134cdf0e10cSrcweir                     LogStorage(m_ServiceFactory).clear();
135cdf0e10cSrcweir             }
136cdf0e10cSrcweir             catch(...) {}
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
stop()140cdf0e10cSrcweir     void OnLogRotateThread::stop()
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         m_Stop.set();
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     class OnLogRotateThreadWatcher : public ::cppu::WeakImplHelper1<XTerminateListener>
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         public:
OnLogRotateThreadWatcher(Reference<XMultiServiceFactory> sf)148cdf0e10cSrcweir             OnLogRotateThreadWatcher(Reference<XMultiServiceFactory> sf)
149cdf0e10cSrcweir                 : m_Thread(new OnLogRotateThread(sf))
150cdf0e10cSrcweir             {
151cdf0e10cSrcweir                 m_Thread->create();
152cdf0e10cSrcweir             }
~OnLogRotateThreadWatcher()153cdf0e10cSrcweir             virtual ~OnLogRotateThreadWatcher()
154cdf0e10cSrcweir             {
155cdf0e10cSrcweir                 m_Thread->stop();
156cdf0e10cSrcweir                 m_Thread->join();
157cdf0e10cSrcweir             };
158cdf0e10cSrcweir 
159cdf0e10cSrcweir             // XTerminateListener
queryTermination(const EventObject &)160cdf0e10cSrcweir             virtual void SAL_CALL queryTermination(const EventObject&) throw(RuntimeException)
161cdf0e10cSrcweir                 { };
notifyTermination(const EventObject &)162cdf0e10cSrcweir             virtual void SAL_CALL notifyTermination(const EventObject&) throw(RuntimeException)
163cdf0e10cSrcweir             {
164cdf0e10cSrcweir                 m_Thread->stop();
165cdf0e10cSrcweir                 m_Thread->join();
166cdf0e10cSrcweir             };
167cdf0e10cSrcweir             // XEventListener
disposing(const EventObject &)168cdf0e10cSrcweir             virtual void SAL_CALL disposing(const EventObject&) throw(RuntimeException)
169cdf0e10cSrcweir             {
170cdf0e10cSrcweir                 m_Thread->stop();
171cdf0e10cSrcweir                 m_Thread->join();
172cdf0e10cSrcweir             };
173cdf0e10cSrcweir         private:
174cdf0e10cSrcweir             ::std::auto_ptr<OnLogRotateThread> m_Thread;
175cdf0e10cSrcweir     };
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir namespace oooimprovement
179cdf0e10cSrcweir {
OnLogRotateJob(const Reference<XComponentContext> & context)180cdf0e10cSrcweir     OnLogRotateJob::OnLogRotateJob(const Reference<XComponentContext>& context)
181cdf0e10cSrcweir         : m_ServiceFactory(Reference<XMultiServiceFactory>(
182cdf0e10cSrcweir             context->getServiceManager()->createInstanceWithContext(
183cdf0e10cSrcweir                 OUString::createFromAscii("com.sun.star.lang.XMultiServiceFactory"), context),
184cdf0e10cSrcweir             UNO_QUERY))
185cdf0e10cSrcweir     { }
186cdf0e10cSrcweir 
OnLogRotateJob(const Reference<XMultiServiceFactory> & sf)187cdf0e10cSrcweir     OnLogRotateJob::OnLogRotateJob(const Reference<XMultiServiceFactory>& sf)
188cdf0e10cSrcweir         : m_ServiceFactory(sf)
189cdf0e10cSrcweir     { }
190cdf0e10cSrcweir 
~OnLogRotateJob()191cdf0e10cSrcweir     OnLogRotateJob::~OnLogRotateJob()
192cdf0e10cSrcweir     { }
193cdf0e10cSrcweir 
executeAsync(const Sequence<NamedValue> &,const Reference<XJobListener> & listener)194cdf0e10cSrcweir     void SAL_CALL OnLogRotateJob::executeAsync(
195cdf0e10cSrcweir         const Sequence<NamedValue>&,
196cdf0e10cSrcweir         const Reference<XJobListener>& listener)
197cdf0e10cSrcweir         throw(RuntimeException)
198cdf0e10cSrcweir     {
199cdf0e10cSrcweir         Reference<XDesktop> xDesktop(
200cdf0e10cSrcweir             m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.frame.Desktop")),
201cdf0e10cSrcweir             UNO_QUERY);
202cdf0e10cSrcweir         if(xDesktop.is())
203cdf0e10cSrcweir             xDesktop->addTerminateListener(Reference<XTerminateListener>(new OnLogRotateThreadWatcher(m_ServiceFactory)));
204cdf0e10cSrcweir         Any result;
205cdf0e10cSrcweir         listener->jobFinished(Reference<XAsyncJob>(this), result);
206cdf0e10cSrcweir     }
207cdf0e10cSrcweir 
supportsService(const OUString & service_name)208cdf0e10cSrcweir     sal_Bool SAL_CALL OnLogRotateJob::supportsService(const OUString& service_name) throw(RuntimeException)
209cdf0e10cSrcweir     {
210cdf0e10cSrcweir         const Sequence<OUString> service_names(getSupportedServiceNames());
211cdf0e10cSrcweir         for (sal_Int32 idx = service_names.getLength()-1; idx>=0; --idx)
212cdf0e10cSrcweir             if(service_name == service_names[idx]) return sal_True;
213cdf0e10cSrcweir         return sal_False;
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir 
getImplementationName()216cdf0e10cSrcweir     OUString SAL_CALL OnLogRotateJob::getImplementationName() throw(RuntimeException)
217cdf0e10cSrcweir     { return getImplementationName_static(); }
218cdf0e10cSrcweir 
getSupportedServiceNames()219cdf0e10cSrcweir     Sequence<OUString> SAL_CALL OnLogRotateJob::getSupportedServiceNames() throw(RuntimeException)
220cdf0e10cSrcweir     { return getSupportedServiceNames_static(); }
221cdf0e10cSrcweir 
getImplementationName_static()222cdf0e10cSrcweir     OUString SAL_CALL OnLogRotateJob::getImplementationName_static()
223cdf0e10cSrcweir     { return OUString::createFromAscii("com.sun.star.comp.extensions.oooimprovement.OnLogRotateJob"); }
224cdf0e10cSrcweir 
getSupportedServiceNames_static()225cdf0e10cSrcweir     Sequence<OUString> SAL_CALL OnLogRotateJob::getSupportedServiceNames_static()
226cdf0e10cSrcweir     {
227cdf0e10cSrcweir         Sequence<OUString> aServiceNames(1);
228cdf0e10cSrcweir         aServiceNames[0] = OUString::createFromAscii("com.sun.star.task.AsyncJob");
229cdf0e10cSrcweir         return aServiceNames;
230cdf0e10cSrcweir     }
231cdf0e10cSrcweir 
Create(const Reference<XComponentContext> & context)232cdf0e10cSrcweir     Reference<XInterface> OnLogRotateJob::Create(const Reference<XComponentContext>& context)
233cdf0e10cSrcweir     { return *(new OnLogRotateJob(context)); }
234cdf0e10cSrcweir 
Create(const Reference<XMultiServiceFactory> & sf)235cdf0e10cSrcweir     Reference<XInterface> OnLogRotateJob::Create(const Reference<XMultiServiceFactory>& sf)
236cdf0e10cSrcweir     { return *(new OnLogRotateJob(sf)); }
237cdf0e10cSrcweir }
238