Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / runner / util / db / DataSource.java
blob60c613edd854f3555baad36be0cb133ba0de01eb
1 /*
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.
27 package util.db;
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
47 * @author fs93730
49 public class DataSource
51 protected DataSource( XMultiServiceFactory _orb, DataSourceDescriptor _descriptor )
53 m_orb = _orb;
54 try
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,
59 m_dataSource );
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] );
70 catch ( Exception e )
72 throw new StatusException( "could not create/fill a css.sdb.DataSource object", e );
76 public XDataSource getDataSource()
78 return m_dataSource;
81 /**
82 * retrieves the css.sdb.OfficeDatabaseDocument associated with the data source
83 * @return
85 public DatabaseDocument getDatabaseDocument()
87 synchronized ( this )
89 if ( m_document == null )
90 m_document = new DatabaseDocument( m_orb, this );
92 return m_document;
95 public void revokeRegistration()
97 String dataSourceName = "";
98 try
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 )
113 String doing = null;
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)
141 tempFile.delete();
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;