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.web;
    21
    22  import com.sun.jersey.api.core.HttpContext;
    23  import com.sun.jersey.api.core.ResourceConfig;
    24  import com.sun.jersey.spi.container.WebApplication;
    25  import com.sun.jersey.spi.container.servlet.ServletContainer;
    26  import com.sun.jersey.spi.container.servlet.WebConfig;
    27  import org.neo4j.server.NeoServer;
    28  import org.neo4j.server.NeoServerProvider;
    29  import org.neo4j.server.configuration.ConfigurationProvider;
    30  import org.neo4j.server.database.AbstractInjectableProvider;
    31  import org.neo4j.server.database.DatabaseProvider;
    32  import org.neo4j.server.database.GraphDatabaseServiceProvider;
    33  import org.neo4j.server.plugins.Injectable;
    34  import org.neo4j.server.plugins.PluginInvocatorProvider;
    35  import org.neo4j.server.rest.repr.InputFormatProvider;
    36  import org.neo4j.server.rest.repr.OutputFormatProvider;
    37  import org.neo4j.server.rest.repr.RepresentationFormatRepository;
    38  import org.neo4j.server.rrd.RrdDbProvider;
    39
    40  import java.util.Collection;
    41  import java.util.Set;
    42
    43  @SuppressWarnings("serial")
    44  public class NeoServletContainer extends ServletContainer {
    45      private final NeoServer server;
    46      private final Collection<Injectable<?>> injectables;
    47
    48      public NeoServletContainer( NeoServer server, Collection<Injectable<?>> injectables ) {
    49          this.server = server;
    50          this.injectables = injectables;
    51      }
    52
    53      @Override
    54      protected void configure(WebConfig wc, ResourceConfig rc, WebApplication wa) {
    55          super.configure(wc, rc, wa);
    56
    57          Set<Object> singletons = rc.getSingletons();
    58          singletons.add( new DatabaseProvider( server.getDatabase() ) );
    59          singletons.add( new GraphDatabaseServiceProvider( server.getDatabase().graph ) );
    60          singletons.add( new NeoServerProvider( server ) );
    61          singletons.add( new ConfigurationProvider( server.getConfiguration() ) );
    62          if(server.getDatabase().rrdDb() != null) {
    63              singletons.add( new RrdDbProvider( server.getDatabase().rrdDb() ) );
    64          }
    65          RepresentationFormatRepository repository = new RepresentationFormatRepository(server.getExtensionManager());
    66          singletons.add( new InputFormatProvider( repository ) );
    67          singletons.add( new OutputFormatProvider( repository ) );
    68          singletons.add( new PluginInvocatorProvider( server.getExtensionManager() ) );
    69
    70          for ( final Injectable injectable : injectables )
    71          {
    72              singletons.add( new InjectableWrapper( injectable ) );
    73          }
    74      }
    75
    76      private static class InjectableWrapper extends AbstractInjectableProvider<Object>
    77      {
    78          private final Injectable injectable;
    79
    80          public InjectableWrapper( Injectable injectable )
    81          {
    82              super( injectable.getType() );
    83              this.injectable = injectable;
    84          }
    85
    86          @Override
    87          public Object getValue( HttpContext c )
    88          {
    89              return injectable.getValue();
    90          }
    91      }
    92  }