Search in sources :

Example 1 with StorageFile

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

the class DataStore method move.

/**
 * Moves / renames a file.
 *
 * @param currentFile the current file
 * @param newFile the new file
 * @return {@code true} if the file was moved, {@code false} if the new
 *      file already existed or the existing file doesn't exist.
 */
public boolean move(StorageFile currentFile, StorageFile newFile) {
    final String currentPath = new File(currentFile.getPath()).getPath();
    final String newPath = new File(newFile.getPath()).getPath();
    synchronized (LOCK) {
        if (files.containsKey(newPath)) {
            return false;
        }
        DataStoreEntry current = files.remove(currentPath);
        if (current == null) {
            return false;
        }
        files.put(newPath, current);
        return true;
    }
}
Also used : StorageFile(org.apache.derby.io.StorageFile) File(java.io.File)

Example 2 with StorageFile

use of org.apache.derby.io.StorageFile 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 3 with StorageFile

use of org.apache.derby.io.StorageFile 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 4 with StorageFile

use of org.apache.derby.io.StorageFile 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 StorageFile

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

the class JarUtil method upgradeJar.

/**
 * Upgrade code: upgrade one jar file to new style (&gt;= 10.9)
 *
 * @param tc transaction controller
 * @param fid the jar file to be upgraded
 * @throws StandardException
 */
public static void upgradeJar(TransactionController tc, FileInfoDescriptor fid) throws StandardException {
    FileResource fh = tc.getFileHandler();
    StorageFile oldFile = fh.getAsFile(mkExternalNameInternal(fid.getUUID(), fid.getSchemaDescriptor().getSchemaName(), fid.getName(), File.separatorChar, true, false), fid.getGenerationId());
    StorageFile newFile = fh.getAsFile(mkExternalNameInternal(fid.getUUID(), fid.getSchemaDescriptor().getSchemaName(), fid.getName(), File.separatorChar, true, true), fid.getGenerationId());
    FileUtil.copyFile(new File(oldFile.getPath()), new File(newFile.getPath()), null);
}
Also used : FileResource(org.apache.derby.iapi.store.access.FileResource) StorageFile(org.apache.derby.io.StorageFile) StorageFile(org.apache.derby.io.StorageFile) File(java.io.File)

Aggregations

StorageFile (org.apache.derby.io.StorageFile)53 IOException (java.io.IOException)21 StorageRandomAccessFile (org.apache.derby.io.StorageRandomAccessFile)13 File (java.io.File)12 PrivilegedActionException (java.security.PrivilegedActionException)9 StandardException (org.apache.derby.shared.common.error.StandardException)9 StorageFactory (org.apache.derby.io.StorageFactory)6 FileNotFoundException (java.io.FileNotFoundException)4 Properties (java.util.Properties)4 RawTransaction (org.apache.derby.iapi.store.raw.xact.RawTransaction)4 WritableStorageFactory (org.apache.derby.io.WritableStorageFactory)4 OutputStreamWriter (java.io.OutputStreamWriter)3 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)3 FileInputStream (java.io.FileInputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ContextManager (org.apache.derby.iapi.services.context.ContextManager)2 DataStore (org.apache.derby.impl.io.vfmem.DataStore)2 VirtualFile (org.apache.derby.impl.io.vfmem.VirtualFile)2