Search in sources :

Example 6 with PersistentService

use of org.apache.derby.iapi.services.monitor.PersistentService in project derby by apache.

the class BaseDataFileFactory method boot.

public void boot(boolean create, Properties startParams) throws StandardException {
    jbmsVersion = getMonitor().getEngineVersion();
    jvmVersion = buildJvmVersion();
    osInfo = buildOSinfo();
    jarCPath = jarClassPath(getClass());
    dataDirectory = startParams.getProperty(PersistentService.ROOT);
    UUIDFactory uf = getMonitor().getUUIDFactory();
    identifier = uf.createUUID();
    PersistentService ps = getMonitor().getServiceType(this);
    try {
        storageFactory = ps.getStorageFactoryInstance(true, dataDirectory, startParams.getProperty(Property.STORAGE_TEMP_DIRECTORY, PropertyUtil.getSystemProperty(Property.STORAGE_TEMP_DIRECTORY)), identifier.toANSIidentifier());
    } catch (IOException ioe) {
        if (create) {
            throw StandardException.newException(SQLState.SERVICE_DIRECTORY_CREATE_ERROR, ioe, dataDirectory);
        } else {
            throw StandardException.newException(SQLState.DATABASE_NOT_FOUND, ioe, dataDirectory);
        }
    }
    // you can't encrypt a database if the Lucene plugin is loaded
    if (luceneLoaded()) {
        String encryptionProp = startParams.getProperty(Attribute.DATA_ENCRYPTION);
        if ((encryptionProp != null) && "TRUE".equals(encryptionProp.toUpperCase())) {
            throw StandardException.newException(SQLState.LUCENE_ENCRYPTED_DB);
        }
    }
    if (storageFactory instanceof WritableStorageFactory)
        writableStorageFactory = (WritableStorageFactory) storageFactory;
    actionCode = BOOT_ACTION;
    try {
        AccessController.doPrivileged(this);
    } catch (PrivilegedActionException pae) {
    // BOOT_ACTION does not throw any exceptions.
    }
    String value = startParams.getProperty(Property.FORCE_DATABASE_LOCK, PropertyUtil.getSystemProperty(Property.FORCE_DATABASE_LOCK));
    throwDBlckException = Boolean.valueOf((value != null ? value.trim() : value)).booleanValue();
    if (// read only db, not interested in filelock
    !isReadOnly())
        getJBMSLockOnDB(identifier, uf, dataDirectory);
    // If the database is being restored/created from backup
    // the restore the data directory(seg*) from backup
    String restoreFrom = null;
    restoreFrom = startParams.getProperty(Attribute.CREATE_FROM);
    if (restoreFrom == null)
        restoreFrom = startParams.getProperty(Attribute.RESTORE_FROM);
    if (restoreFrom == null)
        restoreFrom = startParams.getProperty(Attribute.ROLL_FORWARD_RECOVERY_FROM);
    if (restoreFrom != null) {
        try {
            // restoreFrom and createFrom operations also need to know if database
            // is encrypted
            String dataEncryption = startParams.getProperty(Attribute.DATA_ENCRYPTION);
            databaseEncrypted = Boolean.valueOf(dataEncryption).booleanValue();
            restoreDataDirectory(restoreFrom);
        } catch (StandardException se) {
            releaseJBMSLockOnDB();
            throw se;
        }
    }
    logMsg(LINE);
    String messageID = (isReadOnly()) ? MessageId.STORE_BOOT_MSG_READ_ONLY : MessageId.STORE_BOOT_MSG;
    boolean logBootTrace = Boolean.valueOf(startParams.getProperty(Property.LOG_BOOT_TRACE, PropertyUtil.getSystemProperty(Property.LOG_BOOT_TRACE))).booleanValue();
    logMsg(new Date() + MessageService.getTextMessage(messageID, jbmsVersion, identifier, dataDirectory, // cast to Object so we get object hash code
    (Object) this.getClass().getClassLoader(), jarCPath));
    // Log the JVM version info
    logMsg(jvmVersion);
    // Log the OS info
    logMsg(osInfo);
    // Log derby.system.home It will have null value if user didn't set it
    logMsg(Property.SYSTEM_HOME_PROPERTY + "=" + PropertyUtil.getSystemProperty(Property.SYSTEM_HOME_PROPERTY));
    // Log properties related to redirection of derby.log
    String target = PropertyUtil.getSystemProperty(Property.ERRORLOG_STYLE_PROPERTY);
    if (target != null)
        logMsg(Property.ERRORLOG_STYLE_PROPERTY + "=" + target);
    target = PropertyUtil.getSystemProperty(Property.ERRORLOG_FILE_PROPERTY);
    if (target != null)
        logMsg(Property.ERRORLOG_FILE_PROPERTY + "=" + target);
    target = PropertyUtil.getSystemProperty(Property.ERRORLOG_METHOD_PROPERTY);
    if (target != null)
        logMsg(Property.ERRORLOG_METHOD_PROPERTY + "=" + target);
    target = PropertyUtil.getSystemProperty(Property.ERRORLOG_FIELD_PROPERTY);
    if (target != null)
        logMsg(Property.ERRORLOG_FIELD_PROPERTY + "=" + target);
    if (logBootTrace)
        Monitor.logThrowable(new Throwable("boot trace"));
    uf = null;
    CacheFactory cf = (CacheFactory) startSystemModule(org.apache.derby.shared.common.reference.Module.CacheFactory);
    // Initialize the page cache
    int pageCacheSize = getIntParameter(RawStoreFactory.PAGE_CACHE_SIZE_PARAMETER, null, RawStoreFactory.PAGE_CACHE_SIZE_DEFAULT, RawStoreFactory.PAGE_CACHE_SIZE_MINIMUM, RawStoreFactory.PAGE_CACHE_SIZE_MAXIMUM);
    pageCache = cf.newCacheManager(this, "PageCache", pageCacheSize / 2, pageCacheSize);
    // Initialize the container cache
    int fileCacheSize = getIntParameter(RawStoreFactory.CONTAINER_CACHE_SIZE_PARAMETER, null, RawStoreFactory.CONTAINER_CACHE_SIZE_DEFAULT, RawStoreFactory.CONTAINER_CACHE_SIZE_MINIMUM, RawStoreFactory.CONTAINER_CACHE_SIZE_MAXIMUM);
    containerCache = cf.newCacheManager(this, "ContainerCache", fileCacheSize / 2, fileCacheSize);
    // Register MBeans that allow users to monitor the page cache
    // and the container cache.
    pageCache.registerMBean(dataDirectory);
    containerCache.registerMBean(dataDirectory);
    if (create) {
        String noLog = startParams.getProperty(Property.CREATE_WITH_NO_LOG);
        inCreateNoLog = (noLog != null && Boolean.valueOf(noLog).booleanValue());
    }
    droppedTableStubInfo = new Hashtable<LogInstant, Object[]>();
    // writes during checkpoint
    if (Property.DURABILITY_TESTMODE_NO_SYNC.equalsIgnoreCase(PropertyUtil.getSystemProperty(Property.DURABILITY_PROPERTY))) {
        // - disable syncing of data during checkpoint.
        dataNotSyncedAtCheckpoint = true;
        // log message stating that derby.system.durability
        // is set to a mode, where syncs wont be forced and the
        // possible consequences of setting this mode
        Monitor.logMessage(MessageService.getTextMessage(MessageId.STORE_DURABILITY_TESTMODE_NO_SYNC, Property.DURABILITY_PROPERTY, Property.DURABILITY_TESTMODE_NO_SYNC));
    } else if (Performance.MEASURE) {
        // development build only feature, must by hand set the
        // Performance.MEASURE variable and rebuild.  Useful during
        // development to compare/contrast effect of syncing, release
        // users can use the above relaxed durability option to disable
        // all syncing.
        // debug only flag - disable syncing of data during checkpoint.
        dataNotSyncedAtCheckpoint = PropertyUtil.getSystemBoolean(Property.STORAGE_DATA_NOT_SYNCED_AT_CHECKPOINT);
        if (dataNotSyncedAtCheckpoint)
            Monitor.logMessage("Warning: " + Property.STORAGE_DATA_NOT_SYNCED_AT_CHECKPOINT + "set to true.");
    }
    fileHandler = new RFResource(this);
}
Also used : LogInstant(org.apache.derby.iapi.store.raw.log.LogInstant) PrivilegedActionException(java.security.PrivilegedActionException) UUIDFactory(org.apache.derby.iapi.services.uuid.UUIDFactory) WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) IOException(java.io.IOException) Date(java.util.Date) StandardException(org.apache.derby.shared.common.error.StandardException) PersistentService(org.apache.derby.iapi.services.monitor.PersistentService) CacheFactory(org.apache.derby.iapi.services.cache.CacheFactory)

Example 7 with PersistentService

use of org.apache.derby.iapi.services.monitor.PersistentService in project derby by apache.

the class RawStore method setupEncryptionEngines.

/*
	** data encryption/decryption support
	*/
/**
 * Setup encryption engines according to the user properties and the
 * current database state.
 *
 * @param create whether a new database is being created, or if this is
 *      an existing database
 * @param properties database properties, including connection attributes
 * @return {@code true} if the existing data in the database should be
 *      transformed by applying a cryptographic operation.
 * @throws StandardException if the properties are conflicting, if the
 *      requested configuration is denied, or if something else goes wrong
 */
private boolean setupEncryptionEngines(boolean create, Properties properties) throws StandardException {
    // Check if user has requested to decrypt the database.
    boolean decryptDatabase = isTrue(properties, Attribute.DECRYPT_DATABASE);
    // Check if user has requested to encrypt the database.
    boolean encryptDatabase = isTrue(properties, Attribute.DATA_ENCRYPTION);
    boolean reEncrypt = false;
    if (!create) {
        // Check if database is already encrypted, by directly peeking at
        // the database service propertes instead of the properties passed
        // to this method. By looking at properties passed to the boot
        // method, one can not differentiate between a request to encrypt
        // a plaintext database and booting an already encrypted database.
        // Attribute.DATA_ENCRYPTION is stored in the service properties
        // for encrypted database, and this takes precedence over the
        // value specified by the user.
        String name = getServiceName(this);
        PersistentService ps = getMonitor().getServiceType(this);
        String canonicalName = ps.getCanonicalServiceName(name);
        Properties serviceprops = ps.getServiceProperties(canonicalName, (Properties) null);
        isEncryptedDatabase = isTrue(serviceprops, Attribute.DATA_ENCRYPTION);
        if (isEncryptedDatabase) {
            // Check if the user has requested to re-encrypt an
            // encrypted datbase with a new encryption password/key.
            // See also attribute check in EmbedConnection.
            reEncrypt = isSet(properties, Attribute.NEW_BOOT_PASSWORD) || isSet(properties, Attribute.NEW_CRYPTO_EXTERNAL_KEY);
            encryptDatabase = reEncrypt;
        } else if (encryptDatabase && decryptDatabase) {
            // We cannot both encrypt and decrypt at the same time.
            throw StandardException.newException(SQLState.CONFLICTING_BOOT_ATTRIBUTES, Attribute.DECRYPT_DATABASE + ", " + Attribute.DATA_ENCRYPTION);
        }
        // Prevent attempts to (re)encrypt or decrypt a read-only database.
        if ((encryptDatabase || decryptDatabase) && isReadOnly()) {
            throw StandardException.newException(SQLState.CANNOT_ENCRYPT_READONLY_DATABASE);
        }
    }
    // setup encryption engines.
    if (isEncryptedDatabase || encryptDatabase) {
        // Check if database is or will be encrypted. We save encryption
        // properties as service properties, such that
        // user does not have to specify them on the URL everytime.
        // Incase of re-encryption of an already of encrypted database
        // only some information needs to updated; it is not treated
        // like the configuring the database for encryption first time.
        boolean setupEncryption = create || (encryptDatabase && !reEncrypt);
        // start the cipher factory module, that is is used to create
        // instances of the cipher factory with specific enctyption
        // properties.
        CipherFactoryBuilder cb = (CipherFactoryBuilder) startSystemModule(org.apache.derby.shared.common.reference.Module.CipherFactoryBuilder);
        // create instance of the cipher factory with the
        // specified encryption properties.
        currentCipherFactory = cb.createCipherFactory(setupEncryption, properties, false);
        // The database can be encrypted using an encryption key that is
        // specified in the connection URL. For security reasons this key
        // is not persisted in the database, but it is necessary to verify
        // the encryption key whenever booting the database. This needs to
        // happen before we access the data/logs to avoid the risk of
        // corrupting the database because of a wrong encryption key.
        // Please note this verification process does not provide any added
        // security. Its purpose is to allow us to fail gracefully if an
        // incorrect encryption key is used during boot time.
        currentCipherFactory.verifyKey(setupEncryption, storageFactory, properties);
        // Initializes the encryption and decryption engines.
        encryptionEngine = currentCipherFactory.createNewCipher(CipherFactory.ENCRYPT);
        if (setupEncryption) {
            encryptionBlockSize = encryptionEngine.getEncryptionBlockSize();
            // this will be saved after encrypting the exisiting data.
            if (create) {
                properties.put(RawStoreFactory.ENCRYPTION_BLOCKSIZE, String.valueOf(encryptionBlockSize));
            }
        } else {
            if (isSet(properties, RawStoreFactory.ENCRYPTION_BLOCKSIZE)) {
                encryptionBlockSize = Integer.parseInt(properties.getProperty(RawStoreFactory.ENCRYPTION_BLOCKSIZE));
            } else {
                encryptionBlockSize = encryptionEngine.getEncryptionBlockSize();
            }
        }
        decryptionEngine = currentCipherFactory.createNewCipher(CipherFactory.DECRYPT);
        random = currentCipherFactory.getSecureRandom();
        if (encryptDatabase) {
            if (reEncrypt) {
                // Create new cipher factory with the new encryption
                // properties specified by the user. This cipher factory
                // is used to create the new encryption/decryption
                // engines to re-encrypt the database with the new
                // encryption keys.
                newCipherFactory = cb.createCipherFactory(setupEncryption, properties, true);
                newDecryptionEngine = newCipherFactory.createNewCipher(CipherFactory.DECRYPT);
                newEncryptionEngine = newCipherFactory.createNewCipher(CipherFactory.ENCRYPT);
            } else {
                // There is only one engine when configuring an
                // un-encrypted database for encryption.
                newDecryptionEngine = decryptionEngine;
                newEncryptionEngine = encryptionEngine;
            }
        }
        // at database creation time.
        if (create) {
            currentCipherFactory.saveProperties(properties);
            isEncryptedDatabase = true;
        }
    }
    // existing database, or decrypting an already encrypted database.
    return (!create && (encryptDatabase || (isEncryptedDatabase && decryptDatabase)));
}
Also used : CipherFactoryBuilder(org.apache.derby.iapi.services.crypto.CipherFactoryBuilder) PersistentService(org.apache.derby.iapi.services.monitor.PersistentService) UpdateServiceProperties(org.apache.derby.impl.services.monitor.UpdateServiceProperties) Properties(java.util.Properties)

Example 8 with PersistentService

use of org.apache.derby.iapi.services.monitor.PersistentService in project derby by apache.

the class BaseMonitor method removePersistentService.

/**
 *  Removes a PersistentService.
 *  Could be used for drop database.
 *       @param name : Service name to be removed.
 */
public void removePersistentService(String name) throws StandardException {
    PersistentService provider = null;
    provider = findProviderForCreate(name);
    String serviceName = provider.getCanonicalServiceName(name);
    boolean removed = provider.removeServiceRoot(serviceName);
    if (removed == false)
        throw StandardException.newException(SQLState.SERVICE_DIRECTORY_REMOVE_ERROR, serviceName);
}
Also used : PersistentService(org.apache.derby.iapi.services.monitor.PersistentService)

Example 9 with PersistentService

use of org.apache.derby.iapi.services.monitor.PersistentService in project derby by apache.

the class BaseMonitor method bootPersistentServices.

/**
 *		Boot all persistent services that can be located at run time.
 *
 *		<BR>
 *		This method enumerates through all the service providers that
 *		are active and calls bootPersistentServices(PersistentService)
 *		to boot all the services that that provider knows about.
 */
private void bootPersistentServices() {
    Enumeration e = new ProviderEnumeration(applicationProperties);
    while (e.hasMoreElements()) {
        PersistentService provider = (PersistentService) e.nextElement();
        bootProviderServices(provider);
    }
}
Also used : Enumeration(java.util.Enumeration) PersistentService(org.apache.derby.iapi.services.monitor.PersistentService)

Aggregations

PersistentService (org.apache.derby.iapi.services.monitor.PersistentService)9 Properties (java.util.Properties)4 IOException (java.io.IOException)3 StandardException (org.apache.derby.shared.common.error.StandardException)3 Enumeration (java.util.Enumeration)2 DataFactory (org.apache.derby.iapi.store.raw.data.DataFactory)2 LogInstant (org.apache.derby.iapi.store.raw.log.LogInstant)2 UpdateServiceProperties (org.apache.derby.impl.services.monitor.UpdateServiceProperties)2 File (java.io.File)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrivilegedActionException (java.security.PrivilegedActionException)1 Date (java.util.Date)1 CacheFactory (org.apache.derby.iapi.services.cache.CacheFactory)1 CipherFactoryBuilder (org.apache.derby.iapi.services.crypto.CipherFactoryBuilder)1 UUIDFactory (org.apache.derby.iapi.services.uuid.UUIDFactory)1 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)1 LogFactory (org.apache.derby.iapi.store.raw.log.LogFactory)1 StorageFile (org.apache.derby.io.StorageFile)1 WritableStorageFactory (org.apache.derby.io.WritableStorageFactory)1