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_connectivity.hxx"
30 #if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include "uno/mapping.hxx"
35 #include "uno/environment.hxx"
36 #include "cppuhelper/bootstrap.hxx"
37 #include "cppuhelper/compbase1.hxx"
38 #include "cppuhelper/component_context.hxx"
39 #include "accesslog.hxx"
40 #include <com/sun/star/embed/XTransactedObject.hpp>
41 #include <comphelper/processfactory.hxx>
42 #include <com/sun/star/io/XStream.hpp>
43 #include <com/sun/star/container/XNameAccess.hpp>
44 #include <com/sun/star/document/XDocumentSubStorageSupplier.hpp>
45 #include <com/sun/star/embed/XStorage.hpp>
46 #include <com/sun/star/embed/ElementModes.hpp>
47 #include <comphelper/stl_types.hxx>
48 #include <comphelper/types.hxx>
49 #include "hsqldb/HStorageAccess.hxx"
50 #include "hsqldb/HStorageMap.hxx"
51 
52 #include "jvmaccess/virtualmachine.hxx"
53 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
54 #include "diagnose_ex.h"
55 
56 using namespace ::com::sun::star::container;
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::document;
59 using namespace ::com::sun::star::embed;
60 using namespace ::com::sun::star::io;
61 using namespace ::com::sun::star::lang;
62 using namespace ::connectivity::hsqldb;
63 
64 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
65 /*****************************************************************************/
66 /* exception macros */
67 
68 #define ThrowException(env, type, msg) { \
69 	env->ThrowNew(env->FindClass(type), msg); }
70 
71 // -----------------------------------------------------------------------------
72 /*
73  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
74  * Method:    openStream
75  * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
76  */
77 extern "C" SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_openStream
78   (JNIEnv * env, jobject /*obj_this*/, jstring name, jstring key, jint mode)
79 {
80 #ifdef HSQLDB_DBG
81     {
82         OperationLogFile( env, name, "output" ).logOperation( "openStream" );
83         LogFile( env, name, "output" ).create();
84     }
85 #endif
86 	StorageContainer::registerStream(env,name,key,mode);
87 }
88 /*
89  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
90  * Method:    write
91  * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)V
92  */
93 extern "C" SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_write__Ljava_lang_String_2Ljava_lang_String_2_3BII
94   (JNIEnv * env, jobject obj_this, jstring key, jstring name, jbyteArray buffer, jint off, jint len)
95 {
96 #ifdef HSQLDB_DBG
97     OperationLogFile( env, name, "output" ).logOperation( "write( byte[], int, int )" );
98 
99     DataLogFile aDataLog( env, name, "output" );
100     write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
101 #else
102     write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, off, len );
103 #endif
104 }
105 // -----------------------------------------------------------------------------
106 /*
107  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
108  * Method:    write
109  * Signature: (Ljava/lang/String;Ljava/lang/String;[B)V
110  */
111 extern "C" SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_write__Ljava_lang_String_2Ljava_lang_String_2_3B
112   (JNIEnv * env, jobject obj_this, jstring key, jstring name, jbyteArray buffer)
113 {
114 #ifdef HSQLDB_DBG
115     OperationLogFile( env, name, "output" ).logOperation( "write( byte[] )" );
116 
117     DataLogFile aDataLog( env, name, "output" );
118     write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, 0, env->GetArrayLength( buffer ), &aDataLog );
119 #else
120     write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, 0, env->GetArrayLength( buffer ) );
121 #endif
122 }
123 // -----------------------------------------------------------------------------
124 /*
125  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
126  * Method:    close
127  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
128  */
129 extern "C" SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_close
130   (JNIEnv * env, jobject /*obj_this*/, jstring key, jstring name)
131 {
132 #ifdef HSQLDB_DBG
133     OperationLogFile aOpLog( env, name, "output" );
134     aOpLog.logOperation( "close" );
135 
136     LogFile aDataLog( env, name, "output" );
137 #endif
138 
139     ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
140     Reference< XOutputStream> xFlush = pHelper.get() ? pHelper->getOutputStream() : Reference< XOutputStream>();
141     if ( xFlush.is() )
142         try
143         {
144             xFlush->flush();
145         }
146         catch(Exception&)
147         {}
148 
149 #ifdef HSQLDB_DBG
150     aDataLog.close();
151     aOpLog.close();
152 #endif
153 	StorageContainer::revokeStream(env,name,key);
154 }
155 // -----------------------------------------------------------------------------
156 /*
157  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
158  * Method:    write
159  * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
160  */
161 extern "C" SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_write__Ljava_lang_String_2Ljava_lang_String_2I
162   (JNIEnv * env, jobject obj_this, jstring key, jstring name,jint b)
163 {
164 #ifdef HSQLDB_DBG
165     OperationLogFile( env, name, "output" ).logOperation( "write( int )" );
166 
167     DataLogFile aDataLog( env, name, "output" );
168     write_to_storage_stream( env, obj_this, name, key, b, &aDataLog );
169 #else
170     write_to_storage_stream( env, obj_this, name, key, b );
171 #endif
172 }
173 // -----------------------------------------------------------------------------
174 /*
175  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
176  * Method:    flush
177  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
178  */
179 extern "C" SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_flush
180   (JNIEnv * env, jobject /*obj_this*/, jstring key, jstring name)
181 {
182     OSL_UNUSED( env );
183     OSL_UNUSED( key );
184     OSL_UNUSED( name );
185 #ifdef HSQLDB_DBG
186     OperationLogFile( env, name, "output" ).logOperation( "flush" );
187 
188     ::rtl::OUString sKey = StorageContainer::jstring2ustring(env,key);
189     ::rtl::OUString sName = StorageContainer::jstring2ustring(env,name);
190 #endif
191 }
192 // -----------------------------------------------------------------------------
193 /*
194  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
195  * Method:    sync
196  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
197  */
198 extern "C" SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_sync
199   (JNIEnv * env, jobject /*obj_this*/, jstring key, jstring name)
200 {
201 #ifdef HSQLDB_DBG
202     OperationLogFile( env, name, "output" ).logOperation( "sync" );
203 #endif
204     ::boost::shared_ptr< StreamHelper > pStream = StorageContainer::getRegisteredStream( env, name, key );
205     Reference< XOutputStream > xFlush = pStream.get() ? pStream->getOutputStream() : Reference< XOutputStream>();
206     OSL_ENSURE( xFlush.is(), "StorageNativeOutputStream::sync: could not retrieve an output stream!" );
207     if ( xFlush.is() )
208     {
209         try
210         {
211             xFlush->flush();
212         }
213         catch(Exception&)
214         {
215             OSL_ENSURE( false, "StorageNativeOutputStream::sync: could not flush output stream!" );
216         }
217     }
218 }
219 // -----------------------------------------------------------------------------
220