12a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52a97ec55SAndrew Rist  * distributed with this work for additional information
62a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
82a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
92a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
102a97ec55SAndrew Rist  *
112a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122a97ec55SAndrew Rist  *
132a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
142a97ec55SAndrew Rist  * software distributed under the License is distributed on an
152a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
172a97ec55SAndrew Rist  * specific language governing permissions and limitations
182a97ec55SAndrew Rist  * under the License.
192a97ec55SAndrew Rist  *
202a97ec55SAndrew Rist  *************************************************************/
212a97ec55SAndrew Rist 
222a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "log_module.hxx"
28cdf0e10cSrcweir #include "methodguard.hxx"
29cdf0e10cSrcweir #include "loghandler.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /** === begin UNO includes === **/
32cdf0e10cSrcweir #include <com/sun/star/logging/XLogHandler.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
34cdf0e10cSrcweir #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
35cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
36cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
37cdf0e10cSrcweir #include <com/sun/star/util/XStringSubstitution.hpp>
38cdf0e10cSrcweir /** === end UNO includes === **/
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <tools/diagnose_ex.h>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include <cppuhelper/compbase3.hxx>
45cdf0e10cSrcweir #include <cppuhelper/basemutex.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <osl/thread.h>
48cdf0e10cSrcweir #include <osl/file.hxx>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #include <rtl/strbuf.hxx>
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #include <memory>
53cdf0e10cSrcweir 
54cdf0e10cSrcweir //........................................................................
55cdf0e10cSrcweir namespace logging
56cdf0e10cSrcweir {
57cdf0e10cSrcweir //........................................................................
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	/** === begin UNO using === **/
60cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
61cdf0e10cSrcweir     using ::com::sun::star::logging::LogRecord;
62cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
63cdf0e10cSrcweir     using ::com::sun::star::logging::XLogFormatter;
64cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
65cdf0e10cSrcweir     using ::com::sun::star::uno::XInterface;
66cdf0e10cSrcweir     using ::com::sun::star::uno::XComponentContext;
67cdf0e10cSrcweir     using ::com::sun::star::logging::XLogHandler;
68cdf0e10cSrcweir     using ::com::sun::star::lang::XServiceInfo;
69cdf0e10cSrcweir     using ::com::sun::star::ucb::AlreadyInitializedException;
70cdf0e10cSrcweir     using ::com::sun::star::lang::XInitialization;
71cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
72cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
73cdf0e10cSrcweir     using ::com::sun::star::lang::IllegalArgumentException;
74cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
75cdf0e10cSrcweir     using ::com::sun::star::util::XStringSubstitution;
76cdf0e10cSrcweir     using ::com::sun::star::beans::NamedValue;
77cdf0e10cSrcweir 	/** === end UNO using === **/
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	//====================================================================
80cdf0e10cSrcweir 	//= FileHandler - declaration
81cdf0e10cSrcweir 	//====================================================================
82cdf0e10cSrcweir     typedef ::cppu::WeakComponentImplHelper3    <   XLogHandler
83cdf0e10cSrcweir                                                 ,   XServiceInfo
84cdf0e10cSrcweir                                                 ,   XInitialization
85cdf0e10cSrcweir                                                 >   FileHandler_Base;
86cdf0e10cSrcweir     class FileHandler   :public ::cppu::BaseMutex
87cdf0e10cSrcweir                         ,public FileHandler_Base
88cdf0e10cSrcweir 	{
89cdf0e10cSrcweir     private:
90cdf0e10cSrcweir         enum FileValidity
91cdf0e10cSrcweir         {
92cdf0e10cSrcweir             /// never attempted to open the file
93cdf0e10cSrcweir             eUnknown,
94cdf0e10cSrcweir             /// file is valid
95cdf0e10cSrcweir             eValid,
96cdf0e10cSrcweir             /// file is invalid
97cdf0e10cSrcweir             eInvalid
98cdf0e10cSrcweir         };
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     private:
101cdf0e10cSrcweir         ::comphelper::ComponentContext  m_aContext;
102cdf0e10cSrcweir         LogHandlerHelper                m_aHandlerHelper;
103cdf0e10cSrcweir         ::rtl::OUString                 m_sFileURL;
104cdf0e10cSrcweir         ::std::auto_ptr< ::osl::File >  m_pFile;
105cdf0e10cSrcweir         FileValidity                    m_eFileValidity;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     protected:
108cdf0e10cSrcweir         FileHandler( const Reference< XComponentContext >& _rxContext );
109cdf0e10cSrcweir         virtual ~FileHandler();
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         // XLogHandler
112cdf0e10cSrcweir         virtual ::rtl::OUString SAL_CALL getEncoding() throw (RuntimeException);
113cdf0e10cSrcweir         virtual void SAL_CALL setEncoding( const ::rtl::OUString& _encoding ) throw (RuntimeException);
114cdf0e10cSrcweir         virtual Reference< XLogFormatter > SAL_CALL getFormatter() throw (RuntimeException);
115cdf0e10cSrcweir         virtual void SAL_CALL setFormatter( const Reference< XLogFormatter >& _formatter ) throw (RuntimeException);
116cdf0e10cSrcweir         virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException);
117cdf0e10cSrcweir         virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException);
118cdf0e10cSrcweir         virtual void SAL_CALL flush(  ) throw (RuntimeException);
119cdf0e10cSrcweir         virtual ::sal_Bool SAL_CALL publish( const LogRecord& Record ) throw (RuntimeException);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         // XInitialization
122cdf0e10cSrcweir         virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         // XServiceInfo
125cdf0e10cSrcweir 		virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
126cdf0e10cSrcweir         virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
127cdf0e10cSrcweir         virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         // OComponentHelper
130cdf0e10cSrcweir         virtual void SAL_CALL disposing();
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     public:
133cdf0e10cSrcweir         // XServiceInfo - static version
134cdf0e10cSrcweir 		static ::rtl::OUString SAL_CALL getImplementationName_static();
135cdf0e10cSrcweir         static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
136cdf0e10cSrcweir         static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     public:
139cdf0e10cSrcweir         typedef ComponentMethodGuard< FileHandler > MethodGuard;
140cdf0e10cSrcweir         void    enterMethod( MethodGuard::Access );
141cdf0e10cSrcweir         void    leaveMethod( MethodGuard::Access );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     private:
144cdf0e10cSrcweir         /** prepares our output file for writing
145cdf0e10cSrcweir         */
146cdf0e10cSrcweir         bool    impl_prepareFile_nothrow();
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         /// writes the given string to our file
149cdf0e10cSrcweir         void    impl_writeString_nothrow( const ::rtl::OString& _rEntry );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         /** does string substitution on a (usually externally provided) file url
152cdf0e10cSrcweir         */
153cdf0e10cSrcweir         void    impl_doStringsubstitution_nothrow( ::rtl::OUString& _inout_rURL );
154cdf0e10cSrcweir 	};
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     //====================================================================
157cdf0e10cSrcweir 	//= FileHandler - implementation
158cdf0e10cSrcweir 	//====================================================================
159cdf0e10cSrcweir 	//--------------------------------------------------------------------
160cdf0e10cSrcweir     FileHandler::FileHandler( const Reference< XComponentContext >& _rxContext )
161cdf0e10cSrcweir         :FileHandler_Base( m_aMutex )
162cdf0e10cSrcweir         ,m_aContext( _rxContext )
163cdf0e10cSrcweir         ,m_aHandlerHelper( _rxContext, m_aMutex, rBHelper )
164cdf0e10cSrcweir         ,m_sFileURL( )
165cdf0e10cSrcweir         ,m_pFile( )
166cdf0e10cSrcweir         ,m_eFileValidity( eUnknown )
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     //--------------------------------------------------------------------
171cdf0e10cSrcweir     FileHandler::~FileHandler()
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir         if ( !rBHelper.bDisposed )
174cdf0e10cSrcweir         {
175cdf0e10cSrcweir             acquire();
176cdf0e10cSrcweir             dispose();
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     //--------------------------------------------------------------------
181cdf0e10cSrcweir     bool FileHandler::impl_prepareFile_nothrow()
182cdf0e10cSrcweir     {
183cdf0e10cSrcweir         if ( m_eFileValidity == eUnknown )
184cdf0e10cSrcweir         {
185cdf0e10cSrcweir             m_pFile.reset( new ::osl::File( m_sFileURL ) );
186cdf0e10cSrcweir             // check whether the log file already exists
187cdf0e10cSrcweir             ::osl::DirectoryItem aFileItem;
188cdf0e10cSrcweir             ::osl::DirectoryItem::get( m_sFileURL, aFileItem );
189cdf0e10cSrcweir             ::osl::FileStatus aStatus( FileStatusMask_Validate );
190cdf0e10cSrcweir             if ( ::osl::FileBase::E_None == aFileItem.getFileStatus( aStatus ) )
191cdf0e10cSrcweir                 ::osl::File::remove( m_sFileURL );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir             ::osl::FileBase::RC res = m_pFile->open( osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
194cdf0e10cSrcweir             m_eFileValidity =   res == ::osl::FileBase::E_None
195cdf0e10cSrcweir                             ?   eValid
196cdf0e10cSrcweir                             :   eInvalid;
197cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 0
198cdf0e10cSrcweir             if ( m_eFileValidity == eInvalid )
199cdf0e10cSrcweir             {
200cdf0e10cSrcweir                 ::rtl::OStringBuffer sMessage;
201cdf0e10cSrcweir                 sMessage.append( "FileHandler::impl_prepareFile_nothrow: could not open the designated log file:" );
202cdf0e10cSrcweir                 sMessage.append( "\nURL: " );
203cdf0e10cSrcweir                 sMessage.append( ::rtl::OString( m_sFileURL.getStr(), m_sFileURL.getLength(), osl_getThreadTextEncoding() ) );
204cdf0e10cSrcweir                 sMessage.append( "\nerror code: " );
205cdf0e10cSrcweir                 sMessage.append( (sal_Int32)res );
206*24c56ab9SHerbert Dürr                 OSL_ENSURE( false, sMessage.getStr() );
207cdf0e10cSrcweir             }
208cdf0e10cSrcweir         #endif
209cdf0e10cSrcweir             if ( m_eFileValidity == eValid )
210cdf0e10cSrcweir             {
211cdf0e10cSrcweir                 ::rtl::OString sHead;
212cdf0e10cSrcweir                 if ( m_aHandlerHelper.getEncodedHead( sHead ) )
213cdf0e10cSrcweir                     impl_writeString_nothrow( sHead );
214cdf0e10cSrcweir             }
215cdf0e10cSrcweir         }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir         return m_eFileValidity == eValid;
218cdf0e10cSrcweir     }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 	//--------------------------------------------------------------------
221cdf0e10cSrcweir     void FileHandler::impl_writeString_nothrow( const ::rtl::OString& _rEntry )
222cdf0e10cSrcweir     {
223cdf0e10cSrcweir         OSL_PRECOND( m_pFile.get(), "FileHandler::impl_writeString_nothrow: no file!" );
224cdf0e10cSrcweir 
225cdf0e10cSrcweir         sal_uInt64 nBytesToWrite( _rEntry.getLength() );
226cdf0e10cSrcweir         sal_uInt64 nBytesWritten( 0 );
227cdf0e10cSrcweir     #if OSL_DEBUG_LEVEL > 0
228cdf0e10cSrcweir         ::osl::FileBase::RC res =
229cdf0e10cSrcweir     #endif
230cdf0e10cSrcweir         m_pFile->write( _rEntry.getStr(), nBytesToWrite, nBytesWritten );
231cdf0e10cSrcweir         OSL_ENSURE( ( res == ::osl::FileBase::E_None ) && ( nBytesWritten == nBytesToWrite ),
232cdf0e10cSrcweir             "FileHandler::impl_writeString_nothrow: could not write the log entry!" );
233cdf0e10cSrcweir     }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir     //--------------------------------------------------------------------
236cdf0e10cSrcweir     void FileHandler::impl_doStringsubstitution_nothrow( ::rtl::OUString& _inout_rURL )
237cdf0e10cSrcweir     {
238cdf0e10cSrcweir         try
239cdf0e10cSrcweir         {
240cdf0e10cSrcweir             Reference< XStringSubstitution > xStringSubst;
241cdf0e10cSrcweir             if ( m_aContext.createComponent( "com.sun.star.util.PathSubstitution", xStringSubst ) )
242cdf0e10cSrcweir                 _inout_rURL = xStringSubst->substituteVariables( _inout_rURL, true );
243cdf0e10cSrcweir         }
244cdf0e10cSrcweir         catch( const Exception& )
245cdf0e10cSrcweir         {
246cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
247cdf0e10cSrcweir         }
248cdf0e10cSrcweir     }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     //--------------------------------------------------------------------
251cdf0e10cSrcweir     void SAL_CALL FileHandler::disposing()
252cdf0e10cSrcweir     {
253cdf0e10cSrcweir         if ( m_eFileValidity == eValid )
254cdf0e10cSrcweir         {
255cdf0e10cSrcweir             ::rtl::OString sTail;
256cdf0e10cSrcweir             if ( m_aHandlerHelper.getEncodedTail( sTail ) )
257cdf0e10cSrcweir                 impl_writeString_nothrow( sTail );
258cdf0e10cSrcweir         }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir         m_pFile.reset( NULL );
261cdf0e10cSrcweir         m_aHandlerHelper.setFormatter( NULL );
262cdf0e10cSrcweir     }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir     //--------------------------------------------------------------------
265cdf0e10cSrcweir     void FileHandler::enterMethod( MethodGuard::Access )
266cdf0e10cSrcweir     {
267cdf0e10cSrcweir         m_aHandlerHelper.enterMethod();
268cdf0e10cSrcweir     }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir     //--------------------------------------------------------------------
271cdf0e10cSrcweir     void FileHandler::leaveMethod( MethodGuard::Access )
272cdf0e10cSrcweir     {
273cdf0e10cSrcweir         m_aMutex.release();
274cdf0e10cSrcweir     }
275cdf0e10cSrcweir 
276cdf0e10cSrcweir     //--------------------------------------------------------------------
277cdf0e10cSrcweir     ::rtl::OUString SAL_CALL FileHandler::getEncoding() throw (RuntimeException)
278cdf0e10cSrcweir     {
279cdf0e10cSrcweir         MethodGuard aGuard( *this );
280cdf0e10cSrcweir         ::rtl::OUString sEncoding;
281cdf0e10cSrcweir         OSL_VERIFY( m_aHandlerHelper.getEncoding( sEncoding ) );
282cdf0e10cSrcweir         return sEncoding;
283cdf0e10cSrcweir     }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir     //--------------------------------------------------------------------
286cdf0e10cSrcweir     void SAL_CALL FileHandler::setEncoding( const ::rtl::OUString& _rEncoding ) throw (RuntimeException)
287cdf0e10cSrcweir     {
288cdf0e10cSrcweir         MethodGuard aGuard( *this );
289cdf0e10cSrcweir         OSL_VERIFY( m_aHandlerHelper.setEncoding( _rEncoding ) );
290cdf0e10cSrcweir     }
291cdf0e10cSrcweir 
292cdf0e10cSrcweir     //--------------------------------------------------------------------
293cdf0e10cSrcweir     Reference< XLogFormatter > SAL_CALL FileHandler::getFormatter() throw (RuntimeException)
294cdf0e10cSrcweir     {
295cdf0e10cSrcweir         MethodGuard aGuard( *this );
296cdf0e10cSrcweir         return m_aHandlerHelper.getFormatter();
297cdf0e10cSrcweir     }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir     //--------------------------------------------------------------------
300cdf0e10cSrcweir     void SAL_CALL FileHandler::setFormatter( const Reference< XLogFormatter >& _rxFormatter ) throw (RuntimeException)
301cdf0e10cSrcweir     {
302cdf0e10cSrcweir         MethodGuard aGuard( *this );
303cdf0e10cSrcweir         m_aHandlerHelper.setFormatter( _rxFormatter );
304cdf0e10cSrcweir     }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir     //--------------------------------------------------------------------
307cdf0e10cSrcweir     ::sal_Int32 SAL_CALL FileHandler::getLevel() throw (RuntimeException)
308cdf0e10cSrcweir     {
309cdf0e10cSrcweir         MethodGuard aGuard( *this );
310cdf0e10cSrcweir         return m_aHandlerHelper.getLevel();
311cdf0e10cSrcweir     }
312cdf0e10cSrcweir 
313cdf0e10cSrcweir     //--------------------------------------------------------------------
314cdf0e10cSrcweir     void SAL_CALL FileHandler::setLevel( ::sal_Int32 _nLevel ) throw (RuntimeException)
315cdf0e10cSrcweir     {
316cdf0e10cSrcweir         MethodGuard aGuard( *this );
317cdf0e10cSrcweir         m_aHandlerHelper.setLevel( _nLevel );
318cdf0e10cSrcweir     }
319cdf0e10cSrcweir 
320cdf0e10cSrcweir     //--------------------------------------------------------------------
321cdf0e10cSrcweir     void SAL_CALL FileHandler::flush(  ) throw (RuntimeException)
322cdf0e10cSrcweir     {
323cdf0e10cSrcweir         MethodGuard aGuard( *this );
324cdf0e10cSrcweir         if(!m_pFile.get())
325cdf0e10cSrcweir         {
326cdf0e10cSrcweir             OSL_PRECOND(false, "FileHandler::flush: no file!");
327cdf0e10cSrcweir             return;
328cdf0e10cSrcweir         }
329cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 0
330cdf0e10cSrcweir             ::osl::FileBase::RC res =
331cdf0e10cSrcweir         #endif
332cdf0e10cSrcweir                 m_pFile->sync();
333cdf0e10cSrcweir         OSL_ENSURE(res == ::osl::FileBase::E_None, "FileHandler::flush: Could not sync logfile to filesystem.");
334cdf0e10cSrcweir     }
335cdf0e10cSrcweir 
336cdf0e10cSrcweir     //--------------------------------------------------------------------
337cdf0e10cSrcweir     ::sal_Bool SAL_CALL FileHandler::publish( const LogRecord& _rRecord ) throw (RuntimeException)
338cdf0e10cSrcweir     {
339cdf0e10cSrcweir         MethodGuard aGuard( *this );
340cdf0e10cSrcweir 
341cdf0e10cSrcweir         if ( !impl_prepareFile_nothrow() )
342cdf0e10cSrcweir             return sal_False;
343cdf0e10cSrcweir 
344cdf0e10cSrcweir         ::rtl::OString sEntry;
345cdf0e10cSrcweir         if ( !m_aHandlerHelper.formatForPublishing( _rRecord, sEntry ) )
346cdf0e10cSrcweir             return sal_False;
347cdf0e10cSrcweir 
348cdf0e10cSrcweir         impl_writeString_nothrow( sEntry );
349cdf0e10cSrcweir         return sal_True;
350cdf0e10cSrcweir     }
351cdf0e10cSrcweir 
352cdf0e10cSrcweir     //--------------------------------------------------------------------
353cdf0e10cSrcweir     void SAL_CALL FileHandler::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
354cdf0e10cSrcweir     {
355cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
356cdf0e10cSrcweir 
357cdf0e10cSrcweir         if ( m_aHandlerHelper.getIsInitialized() )
358cdf0e10cSrcweir             throw AlreadyInitializedException();
359cdf0e10cSrcweir 
360cdf0e10cSrcweir         if ( _rArguments.getLength() != 1 )
361cdf0e10cSrcweir             throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
362cdf0e10cSrcweir 
363cdf0e10cSrcweir         Sequence< NamedValue > aSettings;
364cdf0e10cSrcweir         if ( _rArguments[0] >>= m_sFileURL )
365cdf0e10cSrcweir         {
366cdf0e10cSrcweir             // create( [in] string URL );
367cdf0e10cSrcweir             impl_doStringsubstitution_nothrow( m_sFileURL );
368cdf0e10cSrcweir         }
369cdf0e10cSrcweir         else if ( _rArguments[0] >>= aSettings )
370cdf0e10cSrcweir         {
371cdf0e10cSrcweir             // createWithSettings( [in] sequence< ::com::sun::star::beans::NamedValue > Settings )
372cdf0e10cSrcweir             ::comphelper::NamedValueCollection aTypedSettings( aSettings );
373cdf0e10cSrcweir             m_aHandlerHelper.initFromSettings( aTypedSettings );
374cdf0e10cSrcweir 
375cdf0e10cSrcweir             if ( aTypedSettings.get_ensureType( "FileURL", m_sFileURL ) )
376cdf0e10cSrcweir                 impl_doStringsubstitution_nothrow( m_sFileURL );
377cdf0e10cSrcweir         }
378cdf0e10cSrcweir         else
379cdf0e10cSrcweir             throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
380cdf0e10cSrcweir 
381cdf0e10cSrcweir         m_aHandlerHelper.setIsInitialized();
382cdf0e10cSrcweir     }
383cdf0e10cSrcweir 
384cdf0e10cSrcweir     //--------------------------------------------------------------------
385cdf0e10cSrcweir     ::rtl::OUString SAL_CALL FileHandler::getImplementationName() throw(RuntimeException)
386cdf0e10cSrcweir     {
387cdf0e10cSrcweir         return getImplementationName_static();
388cdf0e10cSrcweir     }
389cdf0e10cSrcweir 
390cdf0e10cSrcweir     //--------------------------------------------------------------------
391cdf0e10cSrcweir     ::sal_Bool SAL_CALL FileHandler::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
392cdf0e10cSrcweir     {
393cdf0e10cSrcweir         const Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() );
394cdf0e10cSrcweir         for (   const ::rtl::OUString* pServiceNames = aServiceNames.getConstArray();
395cdf0e10cSrcweir                 pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
396cdf0e10cSrcweir                 ++pServiceNames
397cdf0e10cSrcweir             )
398cdf0e10cSrcweir             if ( _rServiceName == *pServiceNames )
399cdf0e10cSrcweir                 return sal_True;
400cdf0e10cSrcweir         return sal_False;
401cdf0e10cSrcweir     }
402cdf0e10cSrcweir 
403cdf0e10cSrcweir     //--------------------------------------------------------------------
404cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL FileHandler::getSupportedServiceNames() throw(RuntimeException)
405cdf0e10cSrcweir     {
406cdf0e10cSrcweir         return getSupportedServiceNames_static();
407cdf0e10cSrcweir     }
408cdf0e10cSrcweir 
409cdf0e10cSrcweir     //--------------------------------------------------------------------
410cdf0e10cSrcweir     ::rtl::OUString SAL_CALL FileHandler::getImplementationName_static()
411cdf0e10cSrcweir     {
412cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.FileHandler" ) );
413cdf0e10cSrcweir     }
414cdf0e10cSrcweir 
415cdf0e10cSrcweir     //--------------------------------------------------------------------
416cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL FileHandler::getSupportedServiceNames_static()
417cdf0e10cSrcweir     {
418cdf0e10cSrcweir         Sequence< ::rtl::OUString > aServiceNames(1);
419cdf0e10cSrcweir         aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.FileHandler" ) );
420cdf0e10cSrcweir         return aServiceNames;
421cdf0e10cSrcweir     }
422cdf0e10cSrcweir 
423cdf0e10cSrcweir     //--------------------------------------------------------------------
424cdf0e10cSrcweir     Reference< XInterface > FileHandler::Create( const Reference< XComponentContext >& _rxContext )
425cdf0e10cSrcweir     {
426cdf0e10cSrcweir         return *( new FileHandler( _rxContext ) );
427cdf0e10cSrcweir     }
428cdf0e10cSrcweir 
429cdf0e10cSrcweir     //--------------------------------------------------------------------
430cdf0e10cSrcweir     void createRegistryInfo_FileHandler()
431cdf0e10cSrcweir     {
432cdf0e10cSrcweir         static OAutoRegistration< FileHandler > aAutoRegistration;
433cdf0e10cSrcweir     }
434cdf0e10cSrcweir 
435cdf0e10cSrcweir //........................................................................
436cdf0e10cSrcweir } // namespace logging
437cdf0e10cSrcweir //........................................................................
438