Search in sources :

Example 1 with ActivationEvent

use of org.qi4j.api.activation.ActivationEvent in project qi4j-sdk by Qi4j.

the class ActivationDelegate method activate.

@SuppressWarnings("unchecked")
public void activate(ActivatorsInstance targetActivators, Iterable<? extends Activation> children, Runnable callback) throws ActivationException {
    if (this.targetActivators != null) {
        throw new IllegalStateException("Activation.activate() called multiple times " + "or without calling passivate() first!");
    }
    try {
        // Before Activation Events
        if (fireEvents) {
            fireEvent(new ActivationEvent(target, ACTIVATING));
        }
        // Before Activation for Activators
        targetActivators.beforeActivation(target instanceof ServiceReference ? new PassiveServiceReference((ServiceReference) target) : target);
        // Activation
        for (Activation child : children) {
            if (!activeChildren.contains(child)) {
                child.activate();
            }
            activeChildren.addFirst(child);
        }
        // Internal Activation Callback
        if (callback != null) {
            callback.run();
        }
        // After Activation
        targetActivators.afterActivation(target);
        // After Activation Events
        if (fireEvents) {
            fireEvent(new ActivationEvent(target, ACTIVATED));
        }
        // Activated
        this.targetActivators = targetActivators;
    } catch (Exception e) {
        // Passivate actives
        try {
            passivate();
        } catch (PassivationException e1) {
            ActivationException activationEx = new ActivationException("Unable to Activate application.", e);
            activationEx.addSuppressed(e1);
            throw activationEx;
        }
        if (e instanceof ActivationException) {
            throw ((ActivationException) e);
        }
        throw new ActivationException("Unable to Activate application.", e);
    }
}
Also used : ActivationException(org.qi4j.api.activation.ActivationException) PassivationException(org.qi4j.api.activation.PassivationException) ActivationEvent(org.qi4j.api.activation.ActivationEvent) Activation(org.qi4j.api.activation.Activation) ActivationException(org.qi4j.api.activation.ActivationException) PassivationException(org.qi4j.api.activation.PassivationException) ServiceReference(org.qi4j.api.service.ServiceReference)

Example 2 with ActivationEvent

use of org.qi4j.api.activation.ActivationEvent in project qi4j-sdk by Qi4j.

the class FileConfigurationDataWiper method registerApplicationPassivationDataWiper.

public static void registerApplicationPassivationDataWiper(FileConfiguration fileConfig, Application application) {
    final List<File> dataDirectories = new ArrayList<File>();
    dataDirectories.add(fileConfig.configurationDirectory());
    dataDirectories.add(fileConfig.cacheDirectory());
    dataDirectories.add(fileConfig.dataDirectory());
    dataDirectories.add(fileConfig.logDirectory());
    dataDirectories.add(fileConfig.temporaryDirectory());
    application.registerActivationEventListener(new ActivationEventListener() {

        @Override
        public void onEvent(ActivationEvent event) {
            if (event.type() == ActivationEvent.EventType.PASSIVATED && Application.class.isAssignableFrom(event.source().getClass())) {
                for (File dataDir : dataDirectories) {
                    if (!delete(dataDir)) {
                        System.err.println("Unable to delete " + dataDir);
                    }
                }
            }
        }
    });
}
Also used : ArrayList(java.util.ArrayList) ActivationEventListener(org.qi4j.api.activation.ActivationEventListener) ActivationEvent(org.qi4j.api.activation.ActivationEvent) File(java.io.File)

Example 3 with ActivationEvent

use of org.qi4j.api.activation.ActivationEvent in project qi4j-sdk by Qi4j.

the class LiquibaseServiceTest method testLiquibase.

@Test
public void testLiquibase() throws SQLException, IOException, ActivationException, AssemblyException {
    final SingletonAssembler assembler = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            ModuleAssembly configModule = module;
            // Create in-memory store for configurations
            new EntityTestAssembler().assemble(configModule);
            new C3P0DataSourceServiceAssembler().identifiedBy("datasource-service").withConfig(configModule, Visibility.layer).assemble(module);
            new DataSourceAssembler().withDataSourceServiceIdentity("datasource-service").identifiedBy("testds-liquibase").withCircuitBreaker().assemble(module);
            module.values(SomeValue.class);
            // Set up Liquibase service that will create the tables
            // START SNIPPET: assembly
            new LiquibaseAssembler().withConfig(configModule, Visibility.layer).assemble(module);
            // END SNIPPET: assembly
            module.forMixin(LiquibaseConfiguration.class).declareDefaults().enabled().set(true);
            module.forMixin(LiquibaseConfiguration.class).declareDefaults().changeLog().set("changelog.xml");
        }

        @Override
        public void beforeActivation(Application application) {
            application.registerActivationEventListener(new ActivationEventListener() {

                @Override
                public void onEvent(ActivationEvent event) {
                    System.out.println(event);
                }
            });
        }
    };
    Module module = assembler.module();
    // START SNIPPET: io
    // Look up the DataSource
    DataSource ds = module.findService(DataSource.class).get();
    // Instanciate Databases helper
    Databases database = new Databases(ds);
    // Assert that insertion works
    assertTrue(database.update("insert into test values ('someid', 'bar')") == 1);
    // END SNIPPET: io
    database.query("select * from test", new Databases.ResultSetVisitor() {

        @Override
        public boolean visit(ResultSet visited) throws SQLException {
            assertThat(visited.getString("id"), equalTo("someid"));
            assertThat(visited.getString("foo"), equalTo("bar"));
            return true;
        }
    });
    Function<ResultSet, SomeValue> toValue = new Function<ResultSet, SomeValue>() {

        @Override
        public SomeValue map(ResultSet resultSet) {
            ValueBuilder<SomeValue> builder = assembler.module().newValueBuilder(SomeValue.class);
            try {
                builder.prototype().id().set(resultSet.getString("id"));
                builder.prototype().foo().set(resultSet.getString("foo"));
            } catch (SQLException e) {
                throw new IllegalArgumentException("Could not convert to SomeValue", e);
            }
            return builder.newInstance();
        }
    };
    // START SNIPPET: io
    // Select rows and load them in a List
    List<SomeValue> rows = new ArrayList<SomeValue>();
    database.query("select * from test").transferTo(map(toValue, collection(rows)));
    // Transfer all rows to System.out
    Inputs.iterable(rows).transferTo(Outputs.systemOut());
// END SNIPPET: io
}
Also used : C3P0DataSourceServiceAssembler(org.qi4j.library.sql.c3p0.C3P0DataSourceServiceAssembler) DataSourceAssembler(org.qi4j.library.sql.assembly.DataSourceAssembler) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DataSource(javax.sql.DataSource) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) Function(org.qi4j.functional.Function) Databases(org.qi4j.library.sql.common.Databases) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) ActivationEventListener(org.qi4j.api.activation.ActivationEventListener) ResultSet(java.sql.ResultSet) ActivationEvent(org.qi4j.api.activation.ActivationEvent) Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Example 4 with ActivationEvent

use of org.qi4j.api.activation.ActivationEvent in project qi4j-sdk by Qi4j.

the class ActivationDelegate method passivate.

@SuppressWarnings("unchecked")
public void passivate(Runnable callback) throws PassivationException {
    Set<Exception> exceptions = new LinkedHashSet<>();
    // Before Passivation Events
    if (fireEvents) {
        ActivationEvent event = new ActivationEvent(target, PASSIVATING);
        for (ActivationEventListener listener : listeners) {
            try {
                listener.onEvent(event);
            } catch (Exception ex) {
                if (ex instanceof PassivationException) {
                    exceptions.addAll(((PassivationException) ex).causes());
                } else {
                    exceptions.add(ex);
                }
            }
        }
    }
    // Before Passivation for Activators
    if (targetActivators != null) {
        try {
            targetActivators.beforePassivation(target);
        } catch (PassivationException ex) {
            exceptions.addAll(ex.causes());
        } catch (Exception ex) {
            exceptions.add(ex);
        }
    }
    // Passivation
    while (!activeChildren.isEmpty()) {
        passivateOneChild(exceptions);
    }
    // Internal Passivation Callback
    if (callback != null) {
        try {
            callback.run();
        } catch (Exception ex) {
            if (ex instanceof PassivationException) {
                exceptions.addAll(((PassivationException) ex).causes());
            } else {
                exceptions.add(ex);
            }
        }
    }
    // After Passivation for Activators
    if (targetActivators != null) {
        try {
            targetActivators.afterPassivation(target instanceof ServiceReference ? new PassiveServiceReference((ServiceReference) target) : target);
        } catch (PassivationException ex) {
            exceptions.addAll(ex.causes());
        } catch (Exception ex) {
            exceptions.add(ex);
        }
    }
    targetActivators = null;
    // After Passivation Events
    if (fireEvents) {
        ActivationEvent event = new ActivationEvent(target, PASSIVATED);
        for (ActivationEventListener listener : listeners) {
            try {
                listener.onEvent(event);
            } catch (Exception ex) {
                if (ex instanceof PassivationException) {
                    exceptions.addAll(((PassivationException) ex).causes());
                } else {
                    exceptions.add(ex);
                }
            }
        }
    }
    // Error handling
    if (exceptions.isEmpty()) {
        return;
    }
    throw new PassivationException(exceptions);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PassivationException(org.qi4j.api.activation.PassivationException) ActivationEventListener(org.qi4j.api.activation.ActivationEventListener) ActivationEvent(org.qi4j.api.activation.ActivationEvent) ActivationException(org.qi4j.api.activation.ActivationException) PassivationException(org.qi4j.api.activation.PassivationException) ServiceReference(org.qi4j.api.service.ServiceReference)

Aggregations

ActivationEvent (org.qi4j.api.activation.ActivationEvent)4 ActivationEventListener (org.qi4j.api.activation.ActivationEventListener)3 ArrayList (java.util.ArrayList)2 ActivationException (org.qi4j.api.activation.ActivationException)2 PassivationException (org.qi4j.api.activation.PassivationException)2 ServiceReference (org.qi4j.api.service.ServiceReference)2 File (java.io.File)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 LinkedHashSet (java.util.LinkedHashSet)1 DataSource (javax.sql.DataSource)1 Test (org.junit.Test)1 Activation (org.qi4j.api.activation.Activation)1 Application (org.qi4j.api.structure.Application)1 Module (org.qi4j.api.structure.Module)1 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)1 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)1 Function (org.qi4j.functional.Function)1 DataSourceAssembler (org.qi4j.library.sql.assembly.DataSourceAssembler)1 C3P0DataSourceServiceAssembler (org.qi4j.library.sql.c3p0.C3P0DataSourceServiceAssembler)1