Search in sources :

Example 1 with ActivationEventListener

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

the class ApplicationBuilder method newApplication.

/**
     * Create and activate a new Application.
     * @return Activated Application
     * @throws AssemblyException if the assembly failed
     * @throws ActivationException if the activation failed
     */
public Application newApplication() throws AssemblyException, ActivationException {
    Energy4Java qi4j = new Energy4Java();
    ApplicationDescriptor model = qi4j.newApplicationModel(new ApplicationAssembler() {

        @Override
        public ApplicationAssembly assemble(ApplicationAssemblyFactory factory) throws AssemblyException {
            ApplicationAssembly assembly = factory.newApplicationAssembly();
            assembly.setName(applicationName);
            HashMap<String, LayerAssembly> createdLayers = new HashMap<>();
            for (Map.Entry<String, LayerDeclaration> entry : layers.entrySet()) {
                LayerAssembly layer = entry.getValue().createLayer(assembly);
                createdLayers.put(entry.getKey(), layer);
            }
            for (LayerDeclaration layer : layers.values()) {
                layer.initialize(createdLayers);
            }
            return assembly;
        }
    });
    Application application = model.newInstance(qi4j.api());
    for (ActivationEventListener activationListener : activationListeners) {
        application.registerActivationEventListener(activationListener);
    }
    beforeActivation();
    application.activate();
    afterActivation();
    return application;
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) HashMap(java.util.HashMap) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) ApplicationDescriptor(org.qi4j.api.structure.ApplicationDescriptor) LayerAssembly(org.qi4j.bootstrap.LayerAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) ActivationEventListener(org.qi4j.api.activation.ActivationEventListener) Energy4Java(org.qi4j.bootstrap.Energy4Java) Application(org.qi4j.api.structure.Application)

Example 2 with ActivationEventListener

use of org.qi4j.api.activation.ActivationEventListener 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 ActivationEventListener

use of org.qi4j.api.activation.ActivationEventListener 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 ActivationEventListener

use of org.qi4j.api.activation.ActivationEventListener 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

ActivationEventListener (org.qi4j.api.activation.ActivationEventListener)4 ActivationEvent (org.qi4j.api.activation.ActivationEvent)3 ArrayList (java.util.ArrayList)2 Application (org.qi4j.api.structure.Application)2 File (java.io.File)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 DataSource (javax.sql.DataSource)1 Test (org.junit.Test)1 ActivationException (org.qi4j.api.activation.ActivationException)1 PassivationException (org.qi4j.api.activation.PassivationException)1 ServiceReference (org.qi4j.api.service.ServiceReference)1 ApplicationDescriptor (org.qi4j.api.structure.ApplicationDescriptor)1 Module (org.qi4j.api.structure.Module)1 ApplicationAssembler (org.qi4j.bootstrap.ApplicationAssembler)1 ApplicationAssembly (org.qi4j.bootstrap.ApplicationAssembly)1 ApplicationAssemblyFactory (org.qi4j.bootstrap.ApplicationAssemblyFactory)1 AssemblyException (org.qi4j.bootstrap.AssemblyException)1