1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * Copyright 2000, 2010 Oracle and/or its affiliates. 5 * 6 * OpenOffice.org - a multi-platform office productivity suite 7 * 8 * This file is part of OpenOffice.org. 9 * 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. 13 * 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). 19 * 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. 24 * 25 */ 26 27 package util.db; 28 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; 44 45 /** wraps a com.sun.star.sdb.DataSource 46 * 47 * @author fs93730 48 */ 49 public class DataSource 50 { 51 protected DataSource( XMultiServiceFactory _orb, DataSourceDescriptor _descriptor ) 52 { 53 m_orb = _orb; 54 try 55 { 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 ); 60 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] ); 69 } 70 catch ( Exception e ) 71 { 72 throw new StatusException( "could not create/fill a css.sdb.DataSource object", e ); 73 } 74 } 75 76 public XDataSource getDataSource() 77 { 78 return m_dataSource; 79 } 80 81 /** 82 * retrieves the css.sdb.OfficeDatabaseDocument associated with the data source 83 * @return 84 */ 85 public DatabaseDocument getDatabaseDocument() 86 { 87 synchronized ( this ) 88 { 89 if ( m_document == null ) 90 m_document = new DatabaseDocument( m_orb, this ); 91 } 92 return m_document; 93 } 94 95 public void revokeRegistration() 96 { 97 String dataSourceName = ""; 98 try 99 { 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 ); 104 } 105 catch ( Exception e ) 106 { 107 throw new StatusException( "DataSource.revokeRegistration: could not revoke the object (" + dataSourceName + ")", e ); 108 } 109 } 110 111 public void registerAs( final String _registrationName, final boolean _revokeIfRegistered ) 112 { 113 String doing = null; 114 try 115 { 116 doing = "creating database context"; 117 XNamingService dbContext = UnoRuntime.queryInterface( XNamingService.class, 118 m_orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) ); 119 120 if ( _revokeIfRegistered ) 121 { 122 doing = "revoking previously registered data source"; 123 try 124 { 125 dbContext.revokeObject( _registrationName ); 126 } 127 catch( NoSuchElementException e ) 128 { /* allowed here */ } 129 } 130 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 ) 135 { 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 ); 147 } 148 149 // register the data soource 150 doing = "registering the data source at the database context"; 151 dbContext.registerObject( _registrationName, m_dataSource ); 152 } 153 catch( final java.lang.Exception e ) 154 { 155 throw new StatusException( "DataSource.registerAs: error during " + doing, e ); 156 } 157 } 158 159 private XMultiServiceFactory m_orb = null; 160 private XDataSource m_dataSource = null; 161 private XPropertySet m_properties = null; 162 private DatabaseDocument m_document = null; 163 } 164