View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.neo4j.server.modules;
21  
22  import org.apache.commons.configuration.Configuration;
23  import org.neo4j.helpers.Service;
24  import org.neo4j.kernel.AbstractGraphDatabase;
25  import org.neo4j.server.NeoServer;
26  import org.neo4j.server.plugins.PluginLifecycle;
27  import org.neo4j.server.plugins.Injectable;
28  
29  import java.util.Collection;
30  import java.util.HashSet;
31  
32  /**
33   *  Allows Plugins to provide their own initialization
34   */
35  public class PluginInitializer
36  {
37      private final Iterable<PluginLifecycle> pluginLifecycles;
38      private final NeoServer neoServer;
39  
40      public PluginInitializer(NeoServer neoServer)
41      {
42          this.neoServer = neoServer;
43          pluginLifecycles = Service.load( PluginLifecycle.class );
44      }
45  
46      public Collection<Injectable<?>> intitializePackages( Iterable<String> packageNames )
47      {
48          AbstractGraphDatabase graphDatabaseService = neoServer.getDatabase().graph;
49          Configuration configuration = neoServer.getConfiguration();
50  
51          Collection<Injectable<?>> injectables = new HashSet<Injectable<?>>();
52          for ( PluginLifecycle pluginLifecycle : pluginLifecycles)
53          {
54              if ( hasPackage(pluginLifecycle, packageNames ) )
55              {
56                  Collection<Injectable<?>> start = pluginLifecycle.start( graphDatabaseService, configuration );
57                  injectables.addAll( start );
58              }
59          }
60          return injectables;
61      }
62  
63      private boolean hasPackage( PluginLifecycle pluginLifecycle, Iterable<String> packageNames )
64      {
65          String lifecyclePackageName = pluginLifecycle.getClass().getPackage().getName();
66          for ( String packageName : packageNames )
67          {
68              if ( lifecyclePackageName.startsWith( packageName ) )
69              {
70                  return true;
71              }
72          }
73          return false;
74      }
75  
76      public void stop()
77      {
78          for ( PluginLifecycle pluginLifecycle : pluginLifecycles)
79          {
80              pluginLifecycle.stop();
81          }
82      }
83  }