2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com
.sun
.star
.beans
.PropertyValue
;
20 import com
.sun
.star
.beans
.XPropertySet
;
21 import com
.sun
.star
.container
.XNameAccess
;
22 import com
.sun
.star
.frame
.XStorable
;
23 import com
.sun
.star
.sdb
.XOfficeDatabaseDocument
;
24 import com
.sun
.star
.sdbc
.SQLException
;
25 import com
.sun
.star
.sdbc
.XCloseable
;
26 import com
.sun
.star
.sdbc
.XConnection
;
27 import com
.sun
.star
.uno
.UnoRuntime
;
28 import com
.sun
.star
.sdb
.XDocumentDataSource
;
29 import com
.sun
.star
.sdbc
.XDataSource
;
30 import com
.sun
.star
.uno
.XComponentContext
;
33 import com
.sun
.star
.util
.CloseVetoException
;
35 public class HsqlDatabase
37 private XComponentContext m_context
;
38 // the URL of the temporary file used for the database document
39 private String m_databaseDocumentFile
;
40 // the database document
41 private XOfficeDatabaseDocument m_databaseDocument
;
42 // the data source belonging to the database document
43 // the default connection
44 private XConnection m_connection
;
47 public HsqlDatabase( XComponentContext _context
) throws Exception
54 public HsqlDatabase( XComponentContext _context
, String _existingDocumentURL
) throws Exception
57 createDBDocument( _existingDocumentURL
);
61 private void createDBDocument( String _docURL
) throws Exception
63 m_databaseDocumentFile
= _docURL
;
65 XNameAccess dbContext
= UnoRuntime
.queryInterface( XNameAccess
.class,
66 m_context
.getServiceManager().createInstanceWithContext( "com.sun.star.sdb.DatabaseContext", m_context
) );
67 XDocumentDataSource dataSource
= UnoRuntime
.queryInterface( XDocumentDataSource
.class,
68 dbContext
.getByName( _docURL
) );
70 m_databaseDocument
= dataSource
.getDatabaseDocument();
73 /** creates an empty database document in a temporary location
75 private void createDBDocument() throws Exception
77 File documentFile
= File
.createTempFile("testdb",".odb");
78 documentFile
.deleteOnExit();
79 m_databaseDocumentFile
= URLHelper
.getFileURLFromSystemPath( documentFile
);
81 m_databaseDocument
= UnoRuntime
.queryInterface(
82 XOfficeDatabaseDocument
.class, m_context
.getServiceManager().createInstanceWithContext(
83 "com.sun.star.sdb.OfficeDatabaseDocument", m_context
) );
85 XPropertySet dsProperties
= UnoRuntime
.queryInterface( XPropertySet
.class, m_databaseDocument
.getDataSource() );
86 dsProperties
.setPropertyValue("URL", "sdbc:embedded:hsqldb");
88 XStorable storable
= UnoRuntime
.queryInterface( XStorable
.class, m_databaseDocument
);
89 storable
.storeAsURL( m_databaseDocumentFile
, new PropertyValue
[]{} );
92 /** closes the database document
94 * Any CloseVetoExceptions fired by third parties are ignored, and any reference to the
95 * database document is released.
100 XCloseable closeConn
= UnoRuntime
.queryInterface( XCloseable
.class,
102 if ( closeConn
!= null )
108 catch( SQLException e
)
115 com
.sun
.star
.util
.XCloseable closeDoc
= UnoRuntime
.queryInterface(
116 com
.sun
.star
.util
.XCloseable
.class, m_databaseDocument
);
117 if ( closeDoc
!= null )
121 closeDoc
.close( true );
123 catch( CloseVetoException e
)
127 m_databaseDocument
= null;
130 /** closes the document, and deletes the underlying file
132 private void closeAndDelete()
136 if ( m_databaseDocumentFile
!= null )
140 File file
= new File(m_databaseDocumentFile
);
146 m_databaseDocumentFile
= null;
150 /** returns the underlying database document
152 public XOfficeDatabaseDocument
getDatabaseDocument()
154 return m_databaseDocument
;
157 /** returns the associated data source
159 public XDataSource
getDataSource()
161 return m_databaseDocument
.getDataSource();
166 /** returns the URL of the ODB document represented by this instance
168 public String
getDocumentURL()
170 return m_databaseDocumentFile
;
176 protected void finalize() throws Throwable