All Downloads are FREE. Search and download functionalities are using the official Maven repository.

apoc.ApocExtensionFactory Maven / Gradle / Ivy

There is a newer version: 5.25.1
Show newest version
package apoc;

import org.neo4j.annotations.service.ServiceProvider;
import org.neo4j.dbms.api.DatabaseManagementService;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.api.procedure.GlobalProcedures;
import org.neo4j.kernel.availability.AvailabilityGuard;
import org.neo4j.kernel.availability.AvailabilityListener;
import org.neo4j.kernel.extension.ExtensionFactory;
import org.neo4j.kernel.extension.ExtensionType;
import org.neo4j.kernel.extension.context.ExtensionContext;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.kernel.lifecycle.Lifecycle;
import org.neo4j.kernel.lifecycle.LifecycleAdapter;
import org.neo4j.logging.Log;
import org.neo4j.logging.internal.LogService;
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.service.Services;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import static org.neo4j.configuration.GraphDatabaseSettings.SYSTEM_DATABASE_NAME;

/**
 * @author mh
 * @since 14.05.16
 */
@ServiceProvider
public class ApocExtensionFactory extends ExtensionFactory {

    public ApocExtensionFactory() {
        super(ExtensionType.DATABASE, "APOC");
    }

    public interface Dependencies {
        GraphDatabaseAPI graphdatabaseAPI();
        JobScheduler scheduler();
        LogService log();
        AvailabilityGuard availabilityGuard();
        DatabaseManagementService databaseManagementService();
        ApocConfig apocConfig();
        GlobalProcedures globalProceduresRegistry();
        RegisterComponentFactory.RegisterComponentLifecycle registerComponentLifecycle();
        Pools pools();
    }

    @Override
    public Lifecycle newInstance(ExtensionContext context, Dependencies dependencies) {
        GraphDatabaseAPI db = dependencies.graphdatabaseAPI();
        LogService log = dependencies.log();
        return new ApocLifecycle(log, db, dependencies);
    }

    public static class ApocLifecycle extends LifecycleAdapter {
        private final Log userLog;
        private final GraphDatabaseAPI db;
        private final Dependencies dependencies;
        private final Map services = new HashMap<>();
        private final Collection apocGlobalComponents;
        private final Collection registeredListeners = new ArrayList<>();


        public ApocLifecycle(LogService log, GraphDatabaseAPI db, Dependencies dependencies) {
            this.db = db;
            this.dependencies = dependencies;
            this.userLog = log.getUserLog(ApocExtensionFactory.class);
            this.apocGlobalComponents = Services.loadAll(ApocGlobalComponents.class);
        }

        public static void withNonSystemDatabase(GraphDatabaseService db, Consumer consumer) {
            if (!SYSTEM_DATABASE_NAME.equals(db.databaseName())) {
                consumer.accept(null);
            }
        }

        @Override
        public void init() {
            withNonSystemDatabase(db, aVoid -> {
                for (ApocGlobalComponents c: apocGlobalComponents) {
                    services.putAll(c.getServices(db, dependencies));
                }

                String databaseName = db.databaseName();
                services.values().forEach(lifecycle -> dependencies.registerComponentLifecycle().addResolver(
                        databaseName,
                        lifecycle.getClass(),
                        lifecycle));

            });
        }

        @Override
        public void start() {
            withNonSystemDatabase(db, aVoid -> {
                services.forEach((key, value) -> {
                    try {
                        value.start();
                    } catch (Exception e) {
                        userLog.error("failed to start service " + key, e);
                    }
                });

            });

            AvailabilityGuard availabilityGuard = dependencies.availabilityGuard();
            for (ApocGlobalComponents c: apocGlobalComponents) {
                for (AvailabilityListener listener: c.getListeners(db, dependencies)) {
                    registeredListeners.add( listener );
                    availabilityGuard.addListener(listener);
                }
            }
        }

        @Override
        public void stop() {
            withNonSystemDatabase(db, aVoid -> {
                services.forEach((key, value) -> {
                    try {
                        value.stop();
                    } catch (Exception e) {
                        userLog.error("failed to stop service " + key, e);
                    }
                });
            });
        }

        public Collection getRegisteredListeners()
        {
            return registeredListeners;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy