1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package util.db; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 27cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 28cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException; 29cdf0e10cSrcweir import com.sun.star.frame.XModel; 30cdf0e10cSrcweir import com.sun.star.frame.XStorable; 31cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 32cdf0e10cSrcweir import com.sun.star.sdb.XDocumentDataSource; 33cdf0e10cSrcweir import com.sun.star.sdb.XOfficeDatabaseDocument; 34cdf0e10cSrcweir import com.sun.star.sdbc.XDataSource; 35cdf0e10cSrcweir import com.sun.star.uno.Exception; 36cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 37cdf0e10cSrcweir import com.sun.star.uno.XNamingService; 38cdf0e10cSrcweir import java.util.logging.Level; 39cdf0e10cSrcweir import java.util.logging.Logger; 40cdf0e10cSrcweir import lib.StatusException; 41cdf0e10cSrcweir 42cdf0e10cSrcweir /** wraps a com.sun.star.sdb.DataSource 43cdf0e10cSrcweir * 44cdf0e10cSrcweir * @author fs93730 45cdf0e10cSrcweir */ 46cdf0e10cSrcweir public class DataSource 47cdf0e10cSrcweir { DataSource( XMultiServiceFactory _orb, DataSourceDescriptor _descriptor )48cdf0e10cSrcweir protected DataSource( XMultiServiceFactory _orb, DataSourceDescriptor _descriptor ) 49cdf0e10cSrcweir { 50cdf0e10cSrcweir m_orb = _orb; 51cdf0e10cSrcweir try 52cdf0e10cSrcweir { 53cdf0e10cSrcweir m_dataSource = (XDataSource)UnoRuntime.queryInterface( XDataSource.class, 54cdf0e10cSrcweir m_orb.createInstance( "com.sun.star.sdb.DataSource" ) ); 55cdf0e10cSrcweir m_properties = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, 56cdf0e10cSrcweir m_dataSource ); 57cdf0e10cSrcweir 58cdf0e10cSrcweir Object[] descriptorProperties = new Object[] { 59cdf0e10cSrcweir _descriptor.Name, _descriptor.URL, _descriptor.Info, _descriptor.User, _descriptor.Password, 60cdf0e10cSrcweir _descriptor.IsPasswordRequired }; 61cdf0e10cSrcweir String[] propertyNames = new String[] { 62cdf0e10cSrcweir "Name", "URL", "Info", "User", "Password", "IsPasswordRequired" }; 63cdf0e10cSrcweir for ( int i=0; i < descriptorProperties.length; ++i ) 64cdf0e10cSrcweir if ( descriptorProperties[i] != null ) 65cdf0e10cSrcweir m_properties.setPropertyValue( propertyNames[i], descriptorProperties[i] ); 66cdf0e10cSrcweir } 67cdf0e10cSrcweir catch ( Exception e ) 68cdf0e10cSrcweir { 69cdf0e10cSrcweir throw new StatusException( "could not create/fill a css.sdb.DataSource object", e ); 70cdf0e10cSrcweir } 71cdf0e10cSrcweir } 72cdf0e10cSrcweir getDataSource()73cdf0e10cSrcweir public XDataSource getDataSource() 74cdf0e10cSrcweir { 75cdf0e10cSrcweir return m_dataSource; 76cdf0e10cSrcweir } 77cdf0e10cSrcweir 78cdf0e10cSrcweir /** 79cdf0e10cSrcweir * retrieves the css.sdb.OfficeDatabaseDocument associated with the data source 80cdf0e10cSrcweir * @return 81cdf0e10cSrcweir */ getDatabaseDocument()82cdf0e10cSrcweir public DatabaseDocument getDatabaseDocument() 83cdf0e10cSrcweir { 84cdf0e10cSrcweir synchronized ( this ) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir if ( m_document == null ) 87cdf0e10cSrcweir m_document = new DatabaseDocument( m_orb, this ); 88cdf0e10cSrcweir } 89cdf0e10cSrcweir return m_document; 90cdf0e10cSrcweir } 91cdf0e10cSrcweir revokeRegistration()92cdf0e10cSrcweir public void revokeRegistration() 93cdf0e10cSrcweir { 94cdf0e10cSrcweir String dataSourceName = ""; 95cdf0e10cSrcweir try 96cdf0e10cSrcweir { 97cdf0e10cSrcweir dataSourceName = (String)m_properties.getPropertyValue( "Name" ); 98cdf0e10cSrcweir XNamingService dbContext = (XNamingService)UnoRuntime.queryInterface( XNamingService.class, 99cdf0e10cSrcweir m_orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) ); 100cdf0e10cSrcweir dbContext.revokeObject( dataSourceName ); 101cdf0e10cSrcweir } 102cdf0e10cSrcweir catch ( Exception e ) 103cdf0e10cSrcweir { 104cdf0e10cSrcweir throw new StatusException( "DataSource.revokeRegistration: could not revoke the object (" + dataSourceName + ")", e ); 105cdf0e10cSrcweir } 106cdf0e10cSrcweir } 107cdf0e10cSrcweir registerAs( final String _registrationName, final boolean _revokeIfRegistered )108cdf0e10cSrcweir public void registerAs( final String _registrationName, final boolean _revokeIfRegistered ) 109cdf0e10cSrcweir { 110cdf0e10cSrcweir String doing = null; 111cdf0e10cSrcweir try 112cdf0e10cSrcweir { 113cdf0e10cSrcweir doing = "creating database context"; 114cdf0e10cSrcweir XNamingService dbContext = UnoRuntime.queryInterface( XNamingService.class, 115cdf0e10cSrcweir m_orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) ); 116cdf0e10cSrcweir 117cdf0e10cSrcweir if ( _revokeIfRegistered ) 118cdf0e10cSrcweir { 119cdf0e10cSrcweir doing = "revoking previously registered data source"; 120cdf0e10cSrcweir try 121cdf0e10cSrcweir { 122cdf0e10cSrcweir dbContext.revokeObject( _registrationName ); 123cdf0e10cSrcweir } 124cdf0e10cSrcweir catch( NoSuchElementException e ) 125cdf0e10cSrcweir { /* allowed here */ } 126cdf0e10cSrcweir } 127cdf0e10cSrcweir 128cdf0e10cSrcweir // if the document associated with the database document has not yet been saved, then we need to do so 129cdf0e10cSrcweir DatabaseDocument doc = getDatabaseDocument(); 130cdf0e10cSrcweir String docURL = doc.getURL(); 131cdf0e10cSrcweir if ( docURL.length() == 0 ) 132cdf0e10cSrcweir { 133cdf0e10cSrcweir final java.io.File tempFile = java.io.File.createTempFile( _registrationName + "_", ".odb" ); 134cdf0e10cSrcweir if ( tempFile.exists() ) 135cdf0e10cSrcweir // we did not really want to create that file, we just wanted its local name, but 136cdf0e10cSrcweir // createTempFile actually creates it => throw it away 137cdf0e10cSrcweir // (This is necessary since some JVM/platform combinations seem to actually lock the file) 138cdf0e10cSrcweir tempFile.delete(); 139cdf0e10cSrcweir String localPart = tempFile.toURI().toURL().toString(); 140cdf0e10cSrcweir localPart = localPart.substring( localPart.lastIndexOf( '/' ) + 1 ); 141cdf0e10cSrcweir docURL = util.utils.getOfficeTemp( m_orb ) + localPart; 142cdf0e10cSrcweir doing = "storing database document to temporary location (" + docURL + ")"; 143cdf0e10cSrcweir doc.storeAsURL( docURL ); 144cdf0e10cSrcweir } 145cdf0e10cSrcweir 146cdf0e10cSrcweir // register the data soource 147cdf0e10cSrcweir doing = "registering the data source at the database context"; 148cdf0e10cSrcweir dbContext.registerObject( _registrationName, m_dataSource ); 149cdf0e10cSrcweir } 150cdf0e10cSrcweir catch( final java.lang.Exception e ) 151cdf0e10cSrcweir { 152cdf0e10cSrcweir throw new StatusException( "DataSource.registerAs: error during " + doing, e ); 153cdf0e10cSrcweir } 154cdf0e10cSrcweir } 155cdf0e10cSrcweir 156cdf0e10cSrcweir private XMultiServiceFactory m_orb = null; 157cdf0e10cSrcweir private XDataSource m_dataSource = null; 158cdf0e10cSrcweir private XPropertySet m_properties = null; 159cdf0e10cSrcweir private DatabaseDocument m_document = null; 160cdf0e10cSrcweir } 161