Search in sources :

Example 1 with StorageFactory

use of org.apache.derby.io.StorageFactory in project derby by apache.

the class StorageFactoryService method getServiceProperties.

/**
 *		Open the service properties in the directory identified by the service name.
 *
 *		@return A Properties object or null if serviceName does not represent a valid service.
 *
 *		@exception StandardException Service appears valid but the properties cannot be created.
 */
public Properties getServiceProperties(final String serviceName, Properties defaultProperties) throws StandardException {
    if (SanityManager.DEBUG) {
        if (!serviceName.equals(getCanonicalServiceName(serviceName))) {
            SanityManager.THROWASSERT("serviceName (" + serviceName + ") expected to equal getCanonicalServiceName(serviceName) (" + getCanonicalServiceName(serviceName) + ")");
        }
    }
    // recreate the service root  if requested by the user.
    final String recreateFrom = recreateServiceRoot(serviceName, defaultProperties);
    final Properties serviceProperties = new Properties(defaultProperties);
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            public Object run() throws IOException, StandardException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
                if (// restore from a file
                recreateFrom != null) {
                    File propFile = new File(recreateFrom, PersistentService.PROPERTIES_NAME);
                    InputStream is = new FileInputStream(propFile);
                    try {
                        serviceProperties.load(new BufferedInputStream(is));
                    } finally {
                        is.close();
                    }
                } else {
                    StorageFactory storageFactory = privGetStorageFactoryInstance(true, serviceName, null, null);
                    StorageFile file = storageFactory.newStorageFile(PersistentService.PROPERTIES_NAME);
                    resolveServicePropertiesFiles(storageFactory, file);
                    try {
                        InputStream is = file.getInputStream();
                        try {
                            // Need to load the properties before closing the
                            // StorageFactory.
                            serviceProperties.load(new BufferedInputStream(is));
                        } finally {
                            is.close();
                        }
                    } finally {
                        storageFactory.shutdown();
                    }
                }
                return null;
            }
        });
        return serviceProperties;
    } catch (PrivilegedActionException pae) {
        if (pae.getException() instanceof FileNotFoundException)
            return null;
        throw Monitor.exceptionStartingModule(pae.getException());
    } catch (SecurityException se) {
        throw Monitor.exceptionStartingModule(/*serviceName, */
        se);
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) InvocationTargetException(java.lang.reflect.InvocationTargetException) FileInputStream(java.io.FileInputStream) StandardException(org.apache.derby.shared.common.error.StandardException) WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) StorageFactory(org.apache.derby.io.StorageFactory) BufferedInputStream(java.io.BufferedInputStream) StorageFile(org.apache.derby.io.StorageFile) StorageFile(org.apache.derby.io.StorageFile) StorageRandomAccessFile(org.apache.derby.io.StorageRandomAccessFile) File(java.io.File)

Example 2 with StorageFactory

use of org.apache.derby.io.StorageFactory in project derby by apache.

the class StorageFactoryService method removeServiceRoot.

// end of getDirectoryPath
public boolean removeServiceRoot(final String serviceName) {
    if (!(rootStorageFactory instanceof WritableStorageFactory))
        return false;
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            public Object run() throws StandardException, IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
                StorageFactory storageFactory = privGetStorageFactoryInstance(true, serviceName, null, null);
                try {
                    if (SanityManager.DEBUG) {
                        // Run this through getCanonicalServiceName as
                        // an extra sanity check. Prepending the
                        // protocol lead in to the canonical name from
                        // the storage factory should be enough.
                        String tmpCanonical = getCanonicalServiceName(getProtocolLeadIn() + storageFactory.getCanonicalName());
                        // These should give the same result.
                        SanityManager.ASSERT(tmpCanonical.equals(getProtocolLeadIn() + storageFactory.getCanonicalName()));
                        SanityManager.ASSERT(serviceName.equals(tmpCanonical), "serviceName = " + serviceName + " ; protocolLeadIn + " + "storageFactory.getCanoicalName = " + tmpCanonical);
                    }
                    StorageFile serviceDirectory = storageFactory.newStorageFile(null);
                    return serviceDirectory.deleteAll() ? this : null;
                } finally {
                    storageFactory.shutdown();
                }
            }
        }) != null;
    } catch (PrivilegedActionException pae) {
        return false;
    }
}
Also used : WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) StorageFactory(org.apache.derby.io.StorageFactory) PrivilegedActionException(java.security.PrivilegedActionException) WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) StorageFile(org.apache.derby.io.StorageFile) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction)

Example 3 with StorageFactory

use of org.apache.derby.io.StorageFactory in project derby by apache.

the class StorageFactoryService method privGetStorageFactoryInstance.

// end of getStorageFactoryInstance
private StorageFactory privGetStorageFactoryInstance(boolean useHome, String databaseName, String tempDirName, String uniqueName) throws InstantiationException, IllegalAccessException, IOException, NoSuchMethodException, InvocationTargetException {
    StorageFactory storageFactory = (StorageFactory) storageFactoryClass.getConstructor().newInstance();
    String dbn;
    if (databaseName != null && subSubProtocol != null && databaseName.startsWith(subSubProtocol + ":"))
        dbn = databaseName.substring(subSubProtocol.length() + 1);
    else
        dbn = databaseName;
    storageFactory.init(useHome ? home : null, dbn, tempDirName, uniqueName);
    return storageFactory;
}
Also used : WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) StorageFactory(org.apache.derby.io.StorageFactory)

Example 4 with StorageFactory

use of org.apache.derby.io.StorageFactory in project derby by apache.

the class StorageFactoryService method recreateServiceRoot.

/*
	**Recreates service root if required depending on which of the following
	**attribute is specified on the conection URL:
	** Attribute.CREATE_FROM (Create database from backup if it does not exist):
	** When a database not exist, the service(database) root is created
	** and the PersistentService.PROPERTIES_NAME (service.properties) file
	** is restored from the backup.
	** Attribute.RESTORE_FROM (Delete the whole database if it exists and then restore
	** it from backup)
	** Existing database root  is deleted and the new the service(database) root is created.
	** PersistentService.PROPERTIES_NAME (service.properties) file is restored from the backup.
	** Attribute.ROLL_FORWARD_RECOVERY_FROM:(Perform Rollforward Recovery;
	** except for the log directory everthing else is replced  by the copy  from
	** backup. log files in the backup are copied to the existing online log
	** directory.):
	** When a database not exist, the service(database) root is created.
	** PersistentService.PROPERTIES_NAME (service.properties) file is deleted
	** from the service dir and  recreated with the properties from backup.
	*/
protected String recreateServiceRoot(final String serviceName, Properties properties) throws StandardException {
    // if there are no propertues then nothing to do in this routine
    if (properties == null) {
        return null;
    }
    // location where backup copy of service properties available
    String restoreFrom;
    boolean createRoot = false;
    boolean deleteExistingRoot = false;
    // check if user wants to create a database from a backup copy
    restoreFrom = properties.getProperty(Attribute.CREATE_FROM);
    if (restoreFrom != null) {
        // create root dicretory if it  does not exist.
        createRoot = true;
        deleteExistingRoot = false;
    } else {
        // check if user requested a complete restore(version recovery) from backup
        restoreFrom = properties.getProperty(Attribute.RESTORE_FROM);
        // create root dir if it does not exists and  if there exists one already delete and recreate
        if (restoreFrom != null) {
            createRoot = true;
            deleteExistingRoot = true;
        } else {
            // check if user has requested roll forward recovery using a backup
            restoreFrom = properties.getProperty(Attribute.ROLL_FORWARD_RECOVERY_FROM);
            if (restoreFrom != null) {
                // failed and user is trying to restore it some other device.
                try {
                    if (AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

                        public Object run() throws IOException, StandardException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
                            StorageFactory storageFactory = privGetStorageFactoryInstance(true, serviceName, null, null);
                            try {
                                StorageFile serviceDirectory = storageFactory.newStorageFile(null);
                                return serviceDirectory.exists() ? this : null;
                            } finally {
                                storageFactory.shutdown();
                            }
                        }
                    }) == null) {
                        createRoot = true;
                        deleteExistingRoot = false;
                    }
                } catch (PrivilegedActionException pae) {
                    throw Monitor.exceptionStartingModule((IOException) pae.getException());
                }
            }
        }
    }
    // restore the service properties from backup
    if (restoreFrom != null) {
        // First make sure backup service directory exists in the specified path
        File backupRoot = new File(restoreFrom);
        if (fileExists(backupRoot)) {
            // First make sure backup have service.properties
            File bserviceProp = new File(restoreFrom, PersistentService.PROPERTIES_NAME);
            if (fileExists(bserviceProp)) {
                // create service root if required
                if (createRoot)
                    createServiceRoot(serviceName, deleteExistingRoot);
                try {
                    AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

                        public Object run() throws IOException, StandardException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
                            WritableStorageFactory storageFactory = (WritableStorageFactory) privGetStorageFactoryInstance(true, serviceName, null, null);
                            try {
                                StorageFile cserviceProp = storageFactory.newStorageFile(PersistentService.PROPERTIES_NAME);
                                if (cserviceProp.exists())
                                    if (!cserviceProp.delete())
                                        throw StandardException.newException(SQLState.UNABLE_TO_DELETE_FILE, cserviceProp);
                                return null;
                            } finally {
                                storageFactory.shutdown();
                            }
                        }
                    });
                } catch (PrivilegedActionException pae) {
                    throw Monitor.exceptionStartingModule((IOException) pae.getException());
                }
            } else
                throw StandardException.newException(SQLState.PROPERTY_FILE_NOT_FOUND_IN_BACKUP, bserviceProp);
        } else
            throw StandardException.newException(SQLState.SERVICE_DIRECTORY_NOT_IN_BACKUP, backupRoot);
        properties.put(Property.IN_RESTORE_FROM_BACKUP, "True");
        if (createRoot)
            properties.put(Property.DELETE_ROOT_ON_ERROR, "True");
    }
    return restoreFrom;
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) StandardException(org.apache.derby.shared.common.error.StandardException) WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) StorageFactory(org.apache.derby.io.StorageFactory) StorageFile(org.apache.derby.io.StorageFile) StorageFile(org.apache.derby.io.StorageFile) StorageRandomAccessFile(org.apache.derby.io.StorageRandomAccessFile) File(java.io.File)

Example 5 with StorageFactory

use of org.apache.derby.io.StorageFactory in project derby by apache.

the class LuceneSupport method unloadTool.

/**
 * Removes the functions and procedures loaded by loadTool and created by createIndex.
 * Drop the LuceneSupport schema. Drop the lucene subdirectory.
 */
public void unloadTool(String... configurationParameters) throws SQLException {
    forbidReadOnlyConnections();
    Connection conn = getDefaultConnection();
    ToolUtilities.mustBeDBO(conn);
    if (!luceneSchemaExists(conn)) {
        throw ToolUtilities.newSQLException(SQLState.LUCENE_ALREADY_UNLOADED);
    }
    // 
    // Drop all of the functions and procedures bound to methods in this package.
    // 
    String className = getClass().getName();
    int endPackageIdx = className.lastIndexOf(".");
    String packageName = className.substring(0, endPackageIdx);
    PreparedStatement ps = conn.prepareStatement("select s.schemaName, a.alias, a.aliastype\n" + "from sys.sysschemas s, sys.sysaliases a\n" + "where s.schemaID = a.schemaID\n" + "and substr( cast( a.javaclassname as varchar( 32672 ) ), 1, ? ) = ?\n");
    ps.setInt(1, packageName.length());
    ps.setString(2, packageName);
    ResultSet routines = ps.executeQuery();
    try {
        while (routines.next()) {
            String schema = routines.getString(1);
            String routineName = routines.getString(2);
            String routineType = ("P".equals(routines.getString(3))) ? "procedure" : "function";
            conn.prepareStatement("drop " + routineType + " " + makeTableName(schema, routineName)).execute();
        }
    } finally {
        routines.close();
    }
    // 
    // Drop the LuceneSupport schema.
    // 
    conn.prepareStatement("drop schema " + LUCENE_SCHEMA + " restrict").execute();
    // 
    // Now delete the Lucene subdirectory;
    // 
    StorageFactory storageFactory = getStorageFactory(conn);
    StorageFile luceneDir = storageFactory.newStorageFile(Database.LUCENE_DIR);
    if (exists(luceneDir)) {
        deleteFile(luceneDir);
    }
}
Also used : StorageFactory(org.apache.derby.io.StorageFactory) Connection(java.sql.Connection) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection) ResultSet(java.sql.ResultSet) StorageFile(org.apache.derby.io.StorageFile) PreparedStatement(java.sql.PreparedStatement)

Aggregations

StorageFactory (org.apache.derby.io.StorageFactory)9 PrivilegedActionException (java.security.PrivilegedActionException)6 StorageFile (org.apache.derby.io.StorageFile)6 WritableStorageFactory (org.apache.derby.io.WritableStorageFactory)6 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)4 File (java.io.File)3 IOException (java.io.IOException)3 StandardException (org.apache.derby.shared.common.error.StandardException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 StorageRandomAccessFile (org.apache.derby.io.StorageRandomAccessFile)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Properties (java.util.Properties)1 EmbedConnection (org.apache.derby.impl.jdbc.EmbedConnection)1