1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 
31 #include <com/sun/star/frame/DoubleInitializationException.hpp>
32 #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 
34 #include <comphelper_module.hxx>
35 
36 #include "documentiologring.hxx"
37 
38 using namespace ::com::sun::star;
39 
40 namespace comphelper
41 {
42 
43 // ----------------------------------------------------------
44 OSimpleLogRing::OSimpleLogRing( const uno::Reference< uno::XComponentContext >& /*xContext*/ )
45 : m_aMessages( SIMPLELOGRING_SIZE )
46 , m_bInitialized( sal_False )
47 , m_bFull( sal_False )
48 , m_nPos( 0 )
49 {
50 }
51 
52 // ----------------------------------------------------------
53 OSimpleLogRing::~OSimpleLogRing()
54 {
55 }
56 
57 // ----------------------------------------------------------
58 uno::Sequence< ::rtl::OUString > SAL_CALL OSimpleLogRing::getSupportedServiceNames_static()
59 {
60     uno::Sequence< rtl::OUString > aResult( 1 );
61     aResult[0] = getServiceName_static();
62     return aResult;
63 }
64 
65 // ----------------------------------------------------------
66 ::rtl::OUString SAL_CALL OSimpleLogRing::getImplementationName_static()
67 {
68     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.logging.SimpleLogRing" ) );
69 }
70 
71 // ----------------------------------------------------------
72 ::rtl::OUString SAL_CALL OSimpleLogRing::getSingletonName_static()
73 {
74     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.DocumentIOLogRing" ) );
75 }
76 
77 // ----------------------------------------------------------
78 ::rtl::OUString SAL_CALL OSimpleLogRing::getServiceName_static()
79 {
80     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.SimpleLogRing" ) );
81 }
82 
83 // ----------------------------------------------------------
84 uno::Reference< uno::XInterface > SAL_CALL OSimpleLogRing::Create( const uno::Reference< uno::XComponentContext >& rxContext )
85 {
86     return static_cast< cppu::OWeakObject* >( new OSimpleLogRing( rxContext ) );
87 }
88 
89 // XSimpleLogRing
90 // ----------------------------------------------------------
91 void SAL_CALL OSimpleLogRing::logString( const ::rtl::OUString& aMessage ) throw (uno::RuntimeException)
92 {
93     ::osl::MutexGuard aGuard( m_aMutex );
94 
95     m_aMessages[m_nPos] = aMessage;
96     if ( ++m_nPos >= m_aMessages.getLength() )
97     {
98         m_nPos = 0;
99         m_bFull = sal_True;
100     }
101 
102     // if used once then default initialized
103     m_bInitialized = sal_True;
104 }
105 
106 // ----------------------------------------------------------
107 uno::Sequence< ::rtl::OUString > SAL_CALL OSimpleLogRing::getCollectedLog() throw (uno::RuntimeException)
108 {
109     ::osl::MutexGuard aGuard( m_aMutex );
110 
111     sal_Int32 nResLen = m_bFull ? m_aMessages.getLength() : m_nPos;
112     sal_Int32 nStart = m_bFull ? m_nPos : 0;
113     uno::Sequence< ::rtl::OUString > aResult( nResLen );
114 
115     for ( sal_Int32 nInd = 0; nInd < nResLen; nInd++ )
116         aResult[nInd] = m_aMessages[ ( nStart + nInd ) % m_aMessages.getLength() ];
117 
118     // if used once then default initialized
119     m_bInitialized = sal_True;
120 
121     return aResult;
122 }
123 
124 // XInitialization
125 // ----------------------------------------------------------
126 void SAL_CALL OSimpleLogRing::initialize( const uno::Sequence< uno::Any >& aArguments ) throw (uno::Exception, uno::RuntimeException)
127 {
128     ::osl::MutexGuard aGuard( m_aMutex );
129 	if ( m_bInitialized )
130 		throw frame::DoubleInitializationException();
131 
132 	if ( !m_refCount )
133 		throw uno::RuntimeException(); // the object must be refcounted already!
134 
135     sal_Int32 nLen = 0;
136     if ( aArguments.getLength() == 1 && ( aArguments[0] >>= nLen ) && nLen )
137         m_aMessages.realloc( nLen );
138     else
139         throw lang::IllegalArgumentException(
140                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Nonnull size is expected as the first argument!" ) ),
141                 uno::Reference< uno::XInterface >(),
142                 0 );
143 
144     m_bInitialized = sal_True;
145 }
146 
147 // XServiceInfo
148 // ----------------------------------------------------------
149 ::rtl::OUString SAL_CALL OSimpleLogRing::getImplementationName() throw (uno::RuntimeException)
150 {
151     return getImplementationName_static();
152 }
153 
154 // ----------------------------------------------------------
155 ::sal_Bool SAL_CALL OSimpleLogRing::supportsService( const ::rtl::OUString& aServiceName ) throw (uno::RuntimeException)
156 {
157     const uno::Sequence< rtl::OUString > & aSupportedNames = getSupportedServiceNames_static();
158     for ( sal_Int32 nInd = 0; nInd < aSupportedNames.getLength(); nInd++ )
159     {
160         if ( aSupportedNames[ nInd ].equals( aServiceName ) )
161             return sal_True;
162     }
163 
164     return sal_False;
165 }
166 
167 // ----------------------------------------------------------
168 uno::Sequence< ::rtl::OUString > SAL_CALL OSimpleLogRing::getSupportedServiceNames() throw (uno::RuntimeException)
169 {
170     return getSupportedServiceNames_static();
171 }
172 
173 } // namespace comphelper
174 
175 void createRegistryInfo_OSimpleLogRing()
176 {
177     static ::comphelper::module::OAutoRegistration< ::comphelper::OSimpleLogRing > aAutoRegistration;
178     static ::comphelper::module::OSingletonRegistration< ::comphelper::OSimpleLogRing > aSingletonRegistration;
179 }
180