1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }