2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * Copyright 2000, 2010 Oracle and/or its affiliates.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * This file is part of OpenOffice.org.
10 * OpenOffice.org is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License version 3
12 * only, as published by the Free Software Foundation.
14 * OpenOffice.org is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License version 3 for more details
18 * (a copy is included in the LICENSE file that accompanied this code).
20 * You should have received a copy of the GNU Lesser General Public License
21 * version 3 along with OpenOffice.org. If not, see
22 * <http://www.openoffice.org/license.html>
23 * for a copy of the LGPLv3 License.
29 import com
.sun
.star
.beans
.PropertyValue
;
30 import com
.sun
.star
.beans
.XPropertySet
;
31 import com
.sun
.star
.container
.NoSuchElementException
;
32 import com
.sun
.star
.frame
.XModel
;
33 import com
.sun
.star
.frame
.XStorable
;
34 import com
.sun
.star
.lang
.XMultiServiceFactory
;
35 import com
.sun
.star
.sdb
.XDocumentDataSource
;
36 import com
.sun
.star
.sdb
.XOfficeDatabaseDocument
;
37 import com
.sun
.star
.sdbc
.XDataSource
;
38 import com
.sun
.star
.uno
.Exception
;
39 import com
.sun
.star
.uno
.UnoRuntime
;
40 import com
.sun
.star
.uno
.XNamingService
;
41 import java
.util
.logging
.Level
;
42 import java
.util
.logging
.Logger
;
43 import lib
.StatusException
;
45 /** wraps a com.sun.star.sdb.DataSource
49 public class DataSource
51 protected DataSource( XMultiServiceFactory _orb
, DataSourceDescriptor _descriptor
)
56 m_dataSource
= (XDataSource
)UnoRuntime
.queryInterface( XDataSource
.class,
57 m_orb
.createInstance( "com.sun.star.sdb.DataSource" ) );
58 m_properties
= (XPropertySet
)UnoRuntime
.queryInterface( XPropertySet
.class,
61 Object
[] descriptorProperties
= new Object
[] {
62 _descriptor
.Name
, _descriptor
.URL
, _descriptor
.Info
, _descriptor
.User
, _descriptor
.Password
,
63 _descriptor
.IsPasswordRequired
};
64 String
[] propertyNames
= new String
[] {
65 "Name", "URL", "Info", "User", "Password", "IsPasswordRequired" };
66 for ( int i
=0; i
< descriptorProperties
.length
; ++i
)
67 if ( descriptorProperties
[i
] != null )
68 m_properties
.setPropertyValue( propertyNames
[i
], descriptorProperties
[i
] );
72 throw new StatusException( "could not create/fill a css.sdb.DataSource object", e
);
76 public XDataSource
getDataSource()
82 * retrieves the css.sdb.OfficeDatabaseDocument associated with the data source
85 public DatabaseDocument
getDatabaseDocument()
89 if ( m_document
== null )
90 m_document
= new DatabaseDocument( m_orb
, this );
95 public void revokeRegistration()
97 String dataSourceName
= "";
100 dataSourceName
= (String
)m_properties
.getPropertyValue( "Name" );
101 XNamingService dbContext
= (XNamingService
)UnoRuntime
.queryInterface( XNamingService
.class,
102 m_orb
.createInstance( "com.sun.star.sdb.DatabaseContext" ) );
103 dbContext
.revokeObject( dataSourceName
);
105 catch ( Exception e
)
107 throw new StatusException( "DataSource.revokeRegistration: could not revoke the object (" + dataSourceName
+ ")", e
);
111 public void registerAs( final String _registrationName
, final boolean _revokeIfRegistered
)
116 doing
= "creating database context";
117 XNamingService dbContext
= UnoRuntime
.queryInterface( XNamingService
.class,
118 m_orb
.createInstance( "com.sun.star.sdb.DatabaseContext" ) );
120 if ( _revokeIfRegistered
)
122 doing
= "revoking previously registered data source";
125 dbContext
.revokeObject( _registrationName
);
127 catch( NoSuchElementException e
)
128 { /* allowed here */ }
131 // if the document associated with the database document has not yet been saved, then we need to do so
132 DatabaseDocument doc
= getDatabaseDocument();
133 String docURL
= doc
.getURL();
134 if ( docURL
.length() == 0 )
136 final java
.io
.File tempFile
= java
.io
.File
.createTempFile( _registrationName
+ "_", ".odb" );
137 if ( tempFile
.exists() )
138 // we did not really want to create that file, we just wanted its local name, but
139 // createTempFile actually creates it => throw it away
140 // (This is necessary since some JVM/platform combinations seem to actually lock the file)
142 String localPart
= tempFile
.toURI().toURL().toString();
143 localPart
= localPart
.substring( localPart
.lastIndexOf( '/' ) + 1 );
144 docURL
= util
.utils
.getOfficeTemp( m_orb
) + localPart
;
145 doing
= "storing database document to temporary location (" + docURL
+ ")";
146 doc
.storeAsURL( docURL
);
149 // register the data soource
150 doing
= "registering the data source at the database context";
151 dbContext
.registerObject( _registrationName
, m_dataSource
);
153 catch( final java
.lang
.Exception e
)
155 throw new StatusException( "DataSource.registerAs: error during " + doing
, e
);
159 private XMultiServiceFactory m_orb
= null;
160 private XDataSource m_dataSource
= null;
161 private XPropertySet m_properties
= null;
162 private DatabaseDocument m_document
= null;