1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <uno/mapping.hxx>
25 #include <uno/environment.hxx>
26 #include <cppuhelper/bootstrap.hxx>
27 #include <cppuhelper/component_context.hxx>
28 #include "accesslog.hxx"
29 #include <com/sun/star/embed/XTransactedObject.hpp>
30 #include <com/sun/star/io/XStream.hpp>
31 #include <com/sun/star/container/XNameAccess.hpp>
32 #include <com/sun/star/document/XDocumentSubStorageSupplier.hpp>
33 #include <com/sun/star/embed/XStorage.hpp>
34 #include <com/sun/star/embed/ElementModes.hpp>
35 #include <hsqldb/HStorageAccess.hxx>
36 #include <hsqldb/HStorageMap.hxx>
37 
38 #include <jvmaccess/virtualmachine.hxx>
39 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
40 
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::document;
44 using namespace ::com::sun::star::embed;
45 using namespace ::com::sun::star::io;
46 using namespace ::com::sun::star::lang;
47 using namespace ::connectivity::hsqldb;
48 
49 
50 /*
51  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
52  * Method:    openStream
53  * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
54  */
Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_openStream(JNIEnv * env,jobject,jstring name,jstring key,jint mode)55 extern "C" SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_openStream
56   (JNIEnv * env, jobject /*obj_this*/, jstring name, jstring key, jint mode)
57 {
58 #ifdef HSQLDB_DBG
59     {
60         OperationLogFile( env, name, "output" ).logOperation( "openStream" );
61         LogFile( env, name, "output" ).create();
62     }
63 #endif
64     StorageContainer::registerStream(env,name,key,mode);
65 }
66 /*
67  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
68  * Method:    write
69  * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)V
70  */
Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_write__Ljava_lang_String_2Ljava_lang_String_2_3BII(JNIEnv * env,jobject obj_this,jstring key,jstring name,jbyteArray buffer,jint off,jint len)71 extern "C" SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_write__Ljava_lang_String_2Ljava_lang_String_2_3BII
72   (JNIEnv * env, jobject obj_this, jstring key, jstring name, jbyteArray buffer, jint off, jint len)
73 {
74 #ifdef HSQLDB_DBG
75     OperationLogFile( env, name, "output" ).logOperation( "write( byte[], int, int )" );
76 
77     DataLogFile aDataLog( env, name, "output" );
78     write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
79 #else
80     (void)obj_this;
81     write_to_storage_stream_from_buffer( env, name, key, buffer, off, len );
82 #endif
83 }
84 
85 /*
86  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
87  * Method:    write
88  * Signature: (Ljava/lang/String;Ljava/lang/String;[B)V
89  */
Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_write__Ljava_lang_String_2Ljava_lang_String_2_3B(JNIEnv * env,jobject obj_this,jstring key,jstring name,jbyteArray buffer)90 extern "C" SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_write__Ljava_lang_String_2Ljava_lang_String_2_3B
91   (JNIEnv * env, jobject obj_this, jstring key, jstring name, jbyteArray buffer)
92 {
93 #ifdef HSQLDB_DBG
94     OperationLogFile( env, name, "output" ).logOperation( "write( byte[] )" );
95 
96     DataLogFile aDataLog( env, name, "output" );
97     write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, 0, env->GetArrayLength( buffer ), &aDataLog );
98 #else
99     (void)obj_this;
100     write_to_storage_stream_from_buffer( env, name, key, buffer, 0, env->GetArrayLength( buffer ) );
101 #endif
102 }
103 
104 /*
105  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
106  * Method:    close
107  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
108  */
Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_close(JNIEnv * env,jobject,jstring key,jstring name)109 extern "C" SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_close
110   (JNIEnv * env, jobject /*obj_this*/, jstring key, jstring name)
111 {
112 #ifdef HSQLDB_DBG
113     OperationLogFile aOpLog( env, name, "output" );
114     aOpLog.logOperation( "close" );
115 
116     LogFile aDataLog( env, name, "output" );
117 #endif
118 
119     std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
120     Reference< XOutputStream> xFlush = pHelper.get() ? pHelper->getOutputStream() : Reference< XOutputStream>();
121     if ( xFlush.is() )
122         try
123         {
124             xFlush->flush();
125         }
126         catch(Exception&)
127         {}
128 
129 #ifdef HSQLDB_DBG
130     aDataLog.close();
131     aOpLog.close();
132 #endif
133     StorageContainer::revokeStream(env,name,key);
134 }
135 
136 /*
137  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
138  * Method:    write
139  * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
140  */
Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_write__Ljava_lang_String_2Ljava_lang_String_2I(JNIEnv * env,jobject obj_this,jstring key,jstring name,jint b)141 extern "C" SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_write__Ljava_lang_String_2Ljava_lang_String_2I
142   (JNIEnv * env, jobject obj_this, jstring key, jstring name,jint b)
143 {
144 #ifdef HSQLDB_DBG
145     OperationLogFile( env, name, "output" ).logOperation( "write( int )" );
146 
147     DataLogFile aDataLog( env, name, "output" );
148     write_to_storage_stream( env, name, key, b, &aDataLog );
149 #else
150     (void)obj_this;
151     write_to_storage_stream( env, name, key, b );
152 #endif
153 }
154 
155 /*
156  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
157  * Method:    flush
158  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
159  */
Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_flush(JNIEnv * env,jobject,jstring key,jstring name)160 extern "C" SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_flush
161   (JNIEnv * env, jobject /*obj_this*/, jstring key, jstring name)
162 {
163 #ifdef HSQLDB_DBG
164     OperationLogFile( env, name, "output" ).logOperation( "flush" );
165 
166     OUString sKey = StorageContainer::jstring2ustring(env,key);
167     OUString sName = StorageContainer::jstring2ustring(env,name);
168 #else
169     (void) env;
170     (void) key;
171     (void) name;
172 #endif
173 }
174 
175 /*
176  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream
177  * Method:    sync
178  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
179  */
Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_sync(JNIEnv * env,jobject,jstring key,jstring name)180 extern "C" SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeOutputStream_sync
181   (JNIEnv * env, jobject /*obj_this*/, jstring key, jstring name)
182 {
183 #ifdef HSQLDB_DBG
184     OperationLogFile( env, name, "output" ).logOperation( "sync" );
185 #endif
186     std::shared_ptr< StreamHelper > pStream = StorageContainer::getRegisteredStream( env, name, key );
187     Reference< XOutputStream > xFlush = pStream.get() ? pStream->getOutputStream() : Reference< XOutputStream>();
188     OSL_ENSURE( xFlush.is(), "StorageNativeOutputStream::sync: could not retrieve an output stream!" );
189     if ( xFlush.is() )
190     {
191         try
192         {
193             xFlush->flush();
194         }
195         catch(Exception&)
196         {
197             OSL_FAIL( "StorageNativeOutputStream::sync: could not flush output stream!" );
198         }
199     }
200 }
201 
202 
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
204