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 .
21 import com
.sun
.star
.beans
.XPropertySet
;
22 import com
.sun
.star
.container
.NoSuchElementException
;
23 import com
.sun
.star
.lang
.XMultiServiceFactory
;
24 import com
.sun
.star
.sdbc
.XDataSource
;
25 import com
.sun
.star
.uno
.Exception
;
26 import com
.sun
.star
.uno
.UnoRuntime
;
27 import com
.sun
.star
.uno
.XNamingService
;
28 import lib
.StatusException
;
30 /** wraps a com.sun.star.sdb.DataSource
32 public class DataSource
34 protected DataSource( XMultiServiceFactory _orb
, DataSourceDescriptor _descriptor
)
39 m_dataSource
= UnoRuntime
.queryInterface( XDataSource
.class,
40 m_orb
.createInstance( "com.sun.star.sdb.DataSource" ) );
41 m_properties
= UnoRuntime
.queryInterface( XPropertySet
.class,
44 Object
[] descriptorProperties
= new Object
[] {
45 null, _descriptor
.URL
, _descriptor
.Info
, _descriptor
.User
, _descriptor
.Password
,
46 _descriptor
.IsPasswordRequired
};
47 String
[] propertyNames
= new String
[] {
48 "Name", "URL", "Info", "User", "Password", "IsPasswordRequired" };
49 for ( int i
=0; i
< descriptorProperties
.length
; ++i
)
50 if ( descriptorProperties
[i
] != null )
51 m_properties
.setPropertyValue( propertyNames
[i
], descriptorProperties
[i
] );
55 throw new StatusException( "could not create/fill a css.sdb.DataSource object", e
);
59 public XDataSource
getDataSource()
65 * retrieves the css.sdb.OfficeDatabaseDocument associated with the data source
67 public synchronized DatabaseDocument
getDatabaseDocument()
69 if ( m_document
== null )
70 m_document
= new DatabaseDocument( this );
74 public void revokeRegistration()
76 String dataSourceName
= "";
79 dataSourceName
= (String
)m_properties
.getPropertyValue( "Name" );
80 XNamingService dbContext
= UnoRuntime
.queryInterface( XNamingService
.class,
81 m_orb
.createInstance( "com.sun.star.sdb.DatabaseContext" ) );
82 dbContext
.revokeObject( dataSourceName
);
86 throw new StatusException( "DataSource.revokeRegistration: could not revoke the object (" + dataSourceName
+ ")", e
);
90 public void registerAs( final String _registrationName
, final boolean _revokeIfRegistered
)
95 doing
= "creating database context";
96 XNamingService dbContext
= UnoRuntime
.queryInterface( XNamingService
.class,
97 m_orb
.createInstance( "com.sun.star.sdb.DatabaseContext" ) );
99 if ( _revokeIfRegistered
)
101 doing
= "revoking previously registered data source";
104 dbContext
.revokeObject( _registrationName
);
106 catch( NoSuchElementException e
)
107 { /* allowed here */ }
110 // if the document associated with the database document has not yet been saved, then we need to do so
111 DatabaseDocument doc
= getDatabaseDocument();
112 String docURL
= doc
.getURL();
113 if ( docURL
.length() == 0 )
115 final java
.io
.File tempFile
= java
.io
.File
.createTempFile( _registrationName
+ "_", ".odb" );
116 if ( tempFile
.exists() ) {
117 // we did not really want to create that file, we just wanted its local name, but
118 // createTempFile actually creates it => throw it away
119 // (This is necessary since some JVM/platform combinations seem to actually lock the file)
120 boolean bDeleteOk
= tempFile
.delete();
122 System
.out
.println("delete failed");
125 String localPart
= tempFile
.toURI().toURL().toString();
126 localPart
= localPart
.substring( localPart
.lastIndexOf( '/' ) + 1 );
127 docURL
= util
.utils
.getOfficeTemp( m_orb
) + localPart
;
128 doing
= "storing database document to temporary location (" + docURL
+ ")";
129 doc
.storeAsURL( docURL
);
132 // register the data source
133 doing
= "registering the data source at the database context";
134 dbContext
.registerObject( _registrationName
, m_dataSource
);
136 catch( final java
.lang
.Exception e
)
138 throw new StatusException( "DataSource.registerAs: error during " + doing
, e
);
142 private final XMultiServiceFactory m_orb
;
143 private final XDataSource m_dataSource
;
144 private final XPropertySet m_properties
;
145 private DatabaseDocument m_document
= null;