1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_comphelper.hxx"
26 
27 #include <com/sun/star/frame/DoubleInitializationException.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 
30 #include <comphelper_module.hxx>
31 
32 #include "documentiologring.hxx"
33 
34 using namespace ::com::sun::star;
35 
36 namespace comphelper
37 {
38 
39 // ----------------------------------------------------------
OSimpleLogRing(const uno::Reference<uno::XComponentContext> &)40 OSimpleLogRing::OSimpleLogRing( const uno::Reference< uno::XComponentContext >& /*xContext*/ )
41 : m_aMessages( SIMPLELOGRING_SIZE )
42 , m_bInitialized( sal_False )
43 , m_bFull( sal_False )
44 , m_nPos( 0 )
45 {
46 }
47 
48 // ----------------------------------------------------------
~OSimpleLogRing()49 OSimpleLogRing::~OSimpleLogRing()
50 {
51 }
52 
53 // ----------------------------------------------------------
getSupportedServiceNames_static()54 uno::Sequence< ::rtl::OUString > SAL_CALL OSimpleLogRing::getSupportedServiceNames_static()
55 {
56     uno::Sequence< rtl::OUString > aResult( 1 );
57     aResult[0] = getServiceName_static();
58     return aResult;
59 }
60 
61 // ----------------------------------------------------------
getImplementationName_static()62 ::rtl::OUString SAL_CALL OSimpleLogRing::getImplementationName_static()
63 {
64     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.logging.SimpleLogRing" ) );
65 }
66 
67 // ----------------------------------------------------------
getSingletonName_static()68 ::rtl::OUString SAL_CALL OSimpleLogRing::getSingletonName_static()
69 {
70     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.DocumentIOLogRing" ) );
71 }
72 
73 // ----------------------------------------------------------
getServiceName_static()74 ::rtl::OUString SAL_CALL OSimpleLogRing::getServiceName_static()
75 {
76     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.SimpleLogRing" ) );
77 }
78 
79 // ----------------------------------------------------------
Create(const uno::Reference<uno::XComponentContext> & rxContext)80 uno::Reference< uno::XInterface > SAL_CALL OSimpleLogRing::Create( const uno::Reference< uno::XComponentContext >& rxContext )
81 {
82     return static_cast< cppu::OWeakObject* >( new OSimpleLogRing( rxContext ) );
83 }
84 
85 // XSimpleLogRing
86 // ----------------------------------------------------------
logString(const::rtl::OUString & aMessage)87 void SAL_CALL OSimpleLogRing::logString( const ::rtl::OUString& aMessage ) throw (uno::RuntimeException)
88 {
89     ::osl::MutexGuard aGuard( m_aMutex );
90 
91     m_aMessages[m_nPos] = aMessage;
92     if ( ++m_nPos >= m_aMessages.getLength() )
93     {
94         m_nPos = 0;
95         m_bFull = sal_True;
96     }
97 
98     // if used once then default initialized
99     m_bInitialized = sal_True;
100 }
101 
102 // ----------------------------------------------------------
getCollectedLog()103 uno::Sequence< ::rtl::OUString > SAL_CALL OSimpleLogRing::getCollectedLog() throw (uno::RuntimeException)
104 {
105     ::osl::MutexGuard aGuard( m_aMutex );
106 
107     sal_Int32 nResLen = m_bFull ? m_aMessages.getLength() : m_nPos;
108     sal_Int32 nStart = m_bFull ? m_nPos : 0;
109     uno::Sequence< ::rtl::OUString > aResult( nResLen );
110 
111     for ( sal_Int32 nInd = 0; nInd < nResLen; nInd++ )
112         aResult[nInd] = m_aMessages[ ( nStart + nInd ) % m_aMessages.getLength() ];
113 
114     // if used once then default initialized
115     m_bInitialized = sal_True;
116 
117     return aResult;
118 }
119 
120 // XInitialization
121 // ----------------------------------------------------------
initialize(const uno::Sequence<uno::Any> & aArguments)122 void SAL_CALL OSimpleLogRing::initialize( const uno::Sequence< uno::Any >& aArguments ) throw (uno::Exception, uno::RuntimeException)
123 {
124     ::osl::MutexGuard aGuard( m_aMutex );
125 	if ( m_bInitialized )
126 		throw frame::DoubleInitializationException();
127 
128 	if ( !m_refCount )
129 		throw uno::RuntimeException(); // the object must be refcounted already!
130 
131     sal_Int32 nLen = 0;
132     if ( aArguments.getLength() == 1 && ( aArguments[0] >>= nLen ) && nLen )
133         m_aMessages.realloc( nLen );
134     else
135         throw lang::IllegalArgumentException(
136                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Nonnull size is expected as the first argument!" ) ),
137                 uno::Reference< uno::XInterface >(),
138                 0 );
139 
140     m_bInitialized = sal_True;
141 }
142 
143 // XServiceInfo
144 // ----------------------------------------------------------
getImplementationName()145 ::rtl::OUString SAL_CALL OSimpleLogRing::getImplementationName() throw (uno::RuntimeException)
146 {
147     return getImplementationName_static();
148 }
149 
150 // ----------------------------------------------------------
supportsService(const::rtl::OUString & aServiceName)151 ::sal_Bool SAL_CALL OSimpleLogRing::supportsService( const ::rtl::OUString& aServiceName ) throw (uno::RuntimeException)
152 {
153     const uno::Sequence< rtl::OUString > & aSupportedNames = getSupportedServiceNames_static();
154     for ( sal_Int32 nInd = 0; nInd < aSupportedNames.getLength(); nInd++ )
155     {
156         if ( aSupportedNames[ nInd ].equals( aServiceName ) )
157             return sal_True;
158     }
159 
160     return sal_False;
161 }
162 
163 // ----------------------------------------------------------
getSupportedServiceNames()164 uno::Sequence< ::rtl::OUString > SAL_CALL OSimpleLogRing::getSupportedServiceNames() throw (uno::RuntimeException)
165 {
166     return getSupportedServiceNames_static();
167 }
168 
169 } // namespace comphelper
170 
createRegistryInfo_OSimpleLogRing()171 void createRegistryInfo_OSimpleLogRing()
172 {
173     static ::comphelper::module::OAutoRegistration< ::comphelper::OSimpleLogRing > aAutoRegistration;
174     static ::comphelper::module::OSingletonRegistration< ::comphelper::OSimpleLogRing > aSingletonRegistration;
175 }
176