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_connectivity.hxx"
26 
27 #if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <com/sun/star/container/XNameAccess.hpp>
31 #include <com/sun/star/embed/XStorage.hpp>
32 #include "hsqldb/StorageFileAccess.h"
33 #include "hsqldb/HStorageMap.hxx"
34 
35 #include <rtl/logfile.hxx>
36 
37 using namespace ::com::sun::star::container;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::embed;
40 using namespace ::com::sun::star::io;
41 using namespace ::com::sun::star::lang;
42 using namespace ::connectivity::hsqldb;
43 
44 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
45 /*****************************************************************************/
46 /* exception macros */
47 
48 #define ThrowException(env, type, msg) { \
49 	env->ThrowNew(env->FindClass(type), msg); }
50 
51 /*
52  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess
53  * Method:    isStreamElement
54  * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
55  */
Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_isStreamElement(JNIEnv * env,jobject,jstring key,jstring name)56 SAL_DLLPUBLIC_EXPORT jboolean JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_isStreamElement
57   (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
58 {
59 	TStorages::mapped_type aStoragePair = StorageContainer::getRegisteredStorage(StorageContainer::jstring2ustring(env,key));
60 	if ( aStoragePair.first.first.is() )
61 	{
62 		try
63 		{
64             ::rtl::OUString sName = StorageContainer::jstring2ustring(env,name);
65             try
66 		    {
67                 ::rtl::OUString sOldName = StorageContainer::removeOldURLPrefix(sName);
68                 if ( aStoragePair.first.first->isStreamElement(sOldName) )
69                 {
70                     try
71                     {
72                         aStoragePair.first.first->renameElement(sOldName,StorageContainer::removeURLPrefix(sName,aStoragePair.first.second));
73                     }
74                     catch(Exception&)
75                     {
76                     }
77                 }
78             }
79 		    catch(NoSuchElementException&)
80 		    {
81             }
82 			return aStoragePair.first.first->isStreamElement(StorageContainer::removeURLPrefix(sName,aStoragePair.first.second));
83 		}
84 		catch(NoSuchElementException&)
85 		{
86 		}
87 		catch(Exception& e)
88 		{
89             OSL_ENSURE(0,"Exception catched! : Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_isStreamElement");
90 			if (JNI_FALSE != env->ExceptionCheck())
91 				env->ExceptionClear();
92 			::rtl::OString cstr( ::rtl::OUStringToOString(e.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
93 			OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
94 		}
95 	}
96 	return JNI_FALSE;
97 }
98 // -----------------------------------------------------------------------------
99 
100 /*
101  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess
102  * Method:    removeElement
103  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
104  */
Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_removeElement(JNIEnv * env,jobject,jstring key,jstring name)105 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_removeElement
106   (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
107 {
108 #ifdef HSQLDB_DBG
109     {
110         ::rtl::OUString sKey = StorageContainer::jstring2ustring(env,key);
111         ::rtl::OUString sName = StorageContainer::jstring2ustring(env,name);
112     }
113 #endif
114 	TStorages::mapped_type aStoragePair = StorageContainer::getRegisteredStorage(StorageContainer::jstring2ustring(env,key));
115 	if ( aStoragePair.first.first.is() )
116 	{
117 		try
118 		{
119 			aStoragePair.first.first->removeElement(StorageContainer::removeURLPrefix(StorageContainer::jstring2ustring(env,name),aStoragePair.first.second));
120 		}
121 		catch(NoSuchElementException&)
122 		{
123 			if (JNI_FALSE != env->ExceptionCheck())
124 				env->ExceptionClear();
125 		}
126 		catch(Exception& e)
127 		{
128             OSL_ENSURE(0,"Exception catched! : Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_removeElement");
129             StorageContainer::throwJavaException(e,env);
130 		}
131 	}
132 }
133 // -----------------------------------------------------------------------------
134 
135 /*
136  * Class:     com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess
137  * Method:    renameElement
138  * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
139  */
Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_renameElement(JNIEnv * env,jobject,jstring key,jstring oldname,jstring newname)140 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_renameElement
141   (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring oldname, jstring newname)
142 {
143 #ifdef HSQLDB_DBG
144     {
145         ::rtl::OUString sKey = StorageContainer::jstring2ustring(env,key);
146         ::rtl::OUString sNewName = StorageContainer::jstring2ustring(env,newname);
147         ::rtl::OUString sOldName = StorageContainer::jstring2ustring(env,oldname);
148     }
149 #endif
150 	TStorages::mapped_type aStoragePair = StorageContainer::getRegisteredStorage(StorageContainer::jstring2ustring(env,key));
151 	if ( aStoragePair.first.first.is() )
152 	{
153 		try
154 		{
155             aStoragePair.first.first->renameElement(
156                 StorageContainer::removeURLPrefix(StorageContainer::jstring2ustring(env,oldname),aStoragePair.first.second),
157                 StorageContainer::removeURLPrefix(StorageContainer::jstring2ustring(env,newname),aStoragePair.first.second)
158             );
159 #ifdef HSQLDB_DBG
160             {
161                 ::rtl::OUString sNewName = StorageContainer::removeURLPrefix(StorageContainer::jstring2ustring(env,newname),aStoragePair.first.second);
162                 OSL_ENSURE(aStoragePair.first.first->isStreamElement(sNewName),"Stream could not be renamed");
163             }
164 #endif
165 		}
166 		catch(NoSuchElementException&)
167 		{
168 		}
169 		catch(Exception& e)
170 		{
171 			OSL_ENSURE(0,"Exception catched! : Java_com_sun_star_sdbcx_comp_hsqldb_StorageFileAccess_renameElement");
172             StorageContainer::throwJavaException(e,env);
173 		}
174 	}
175 }
176 // -----------------------------------------------------------------------------
177