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

org.neo4j.server.plugins.PluginManager Maven / Gradle / Ivy

There is a newer version: 5.26.1
Show newest version
/*
 * Copyright (c) 2002-2016 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.server.plugins;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.neo4j.helpers.collection.Pair;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider;
import org.neo4j.server.rest.repr.BadInputException;
import org.neo4j.server.rest.repr.ExtensionInjector;
import org.neo4j.server.rest.repr.ExtensionPointRepresentation;
import org.neo4j.server.rest.repr.Representation;

public final class PluginManager implements ExtensionInjector, PluginInvocator
{
    private final Map extensions = new HashMap();

    public PluginManager( Config serverConfig, LogProvider logProvider )
    {
        this( serverConfig, ServerPlugin.load(), logProvider );
    }

    PluginManager( Config serverConfig, Iterable plugins, LogProvider logProvider )
    {
        Map> extensions = new HashMap>();
        Log log = logProvider.getLog( getClass() );
        for ( ServerPlugin plugin : plugins )
        {
            PluginPointFactory factory = new PluginPointFactoryImpl();
            final ServerExtender extender = new ServerExtender( factory );
            try
            {
                plugin.loadServerExtender( extender );
            }
            catch ( Exception ex )
            {
                log.warn( "Failed to load plugin [%s]: %s", plugin.toString(), ex.getMessage() );
                continue;
            }
            catch ( LinkageError err )
            {
                log.warn( "Failed to load plugin [%s]: %s", plugin.toString(), err.getMessage() );
                continue;
            }
            Pair old = extensions.put( plugin.name, Pair.of( plugin, extender ) );
            if ( old != null )
            {
                log.warn( String.format( "Extension naming conflict \"%s\" between \"%s\" and \"%s\"", plugin.name,
                        old.first().getClass(), plugin.getClass() ) );
            }
        }
        for ( Pair extension : extensions.values() )
        {
            log.info( String.format( "Loaded server plugin \"%s\"", extension.first().name ) );
            for ( PluginPoint point : extension.other().all() )
            {
                log.info( String.format( "  %s.%s: %s", point.forType().getSimpleName(), point.name(),
                        point.getDescription() ) );
            }
            this.extensions.put( extension.first().name, extension.other() );
        }
    }

    @Override
    public Map> getExensionsFor( Class type )
    {
        Map> result = new HashMap>();
        for ( Map.Entry extension : extensions.entrySet() )
        {
            List methods = new ArrayList();
            for ( PluginPoint method : extension.getValue()
                    .getExtensionsFor( type ) )
            {
                methods.add( method.name() );
            }
            if ( !methods.isEmpty() )
            {
                result.put( extension.getKey(), methods );
            }
        }
        return result;
    }

    private PluginPoint extension( String name, Class type, String method ) throws PluginLookupException
    {
        ServerExtender extender = extensions.get( name );
        if ( extender == null )
        {
            throw new PluginLookupException( "No such ServerPlugin: \"" + name + "\"" );
        }
        return extender.getExtensionPoint( type, method );
    }

    @Override
    public ExtensionPointRepresentation describe( String name, Class type, String method )
            throws PluginLookupException
    {
        return describe( extension( name, type, method ) );
    }

    private ExtensionPointRepresentation describe( PluginPoint extension )
    {
        ExtensionPointRepresentation representation = new ExtensionPointRepresentation( extension.name(),
                extension.forType(), extension.getDescription() );
        extension.describeParameters( representation );
        return representation;
    }

    @Override
    public List describeAll( String name ) throws PluginLookupException
    {
        ServerExtender extender = extensions.get( name );
        if ( extender == null )
        {
            throw new PluginLookupException( "No such ServerPlugin: \"" + name + "\"" );
        }
        List result = new ArrayList();
        for ( PluginPoint plugin : extender.all() )
        {
            result.add( describe( plugin ) );
        }
        return result;
    }

    @Override
    public  Representation invoke( GraphDatabaseAPI graphDb, String name, Class type, String method,
            T context, ParameterList params ) throws PluginLookupException, BadInputException,
            PluginInvocationFailureException, BadPluginInvocationException
    {
        PluginPoint plugin = extension( name, type, method );
        try
        {
            return plugin.invoke( graphDb, context, params );
        }
        catch ( BadInputException e )
        {
            throw e;
        }
        catch ( BadPluginInvocationException e )
        {
            throw e;
        }
        catch ( PluginInvocationFailureException e )
        {
            throw e;
        }
        catch ( Exception e )
        {
            throw new PluginInvocationFailureException( e );
        }
    }

    @Override
    public Set extensionNames()
    {
        return Collections.unmodifiableSet( extensions.keySet() );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy