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.plugins;
21  
22  import java.lang.reflect.InvocationTargetException;
23  import java.lang.reflect.Method;
24  
25  import org.neo4j.kernel.AbstractGraphDatabase;
26  import org.neo4j.server.rest.repr.BadInputException;
27  import org.neo4j.server.rest.repr.Representation;
28  
29  class PluginMethod extends PluginPoint
30  {
31      private final ServerPlugin plugin;
32      private final Method method;
33      private final DataExtractor[] extractors;
34      private final ResultConverter result;
35  
36      PluginMethod( String name, Class<?> discovery, ServerPlugin plugin,
37                    ResultConverter result, Method method, DataExtractor[] extractors,
38                    Description description )
39      {
40          super( discovery, name, description == null ? "" : description.value() );
41          this.plugin = plugin;
42          this.result = result;
43          this.method = method;
44          this.extractors = extractors;
45      }
46  
47  
48  
49  
50  
51      @Override
52      public Representation invoke( AbstractGraphDatabase graphDb, Object source, ParameterList params )
53              throws BadPluginInvocationException, PluginInvocationFailureException,
54              BadInputException
55      {
56          Object[] arguments = new Object[extractors.length];
57          for ( int i = 0; i < arguments.length; i++ )
58          {
59              arguments[i] = extractors[i].extract( graphDb, source, params );
60          }
61          try
62          {
63              Object returned = method.invoke( plugin, arguments );
64  
65              if ( returned == null )
66              {
67                  return Representation.emptyRepresentation();
68              } else
69              {
70                  return result.convert( returned );
71              }
72          }
73          catch ( InvocationTargetException exc )
74          {
75              Throwable targetExc = exc.getTargetException();
76              for ( Class<?> excType : method.getExceptionTypes() )
77              {
78                  if ( excType.isInstance( targetExc ) )
79                      throw new BadPluginInvocationException( targetExc );
80              }
81              throw new PluginInvocationFailureException( targetExc );
82          }
83          catch ( IllegalArgumentException e )
84          {
85              throw new PluginInvocationFailureException( e );
86          }
87          catch ( IllegalAccessException e )
88          {
89              throw new PluginInvocationFailureException( e );
90          }
91      }
92  
93      @Override
94      protected void describeParameters( ParameterDescriptionConsumer consumer )
95      {
96          for ( DataExtractor extractor : extractors )
97          {
98              extractor.describe( consumer );
99          }
100     }
101 
102 }