1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.directory.server.core.sp.java;
23
24
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import javax.naming.NamingException;
31
32 import org.apache.directory.server.core.CoreSession;
33 import org.apache.directory.server.core.entry.ServerEntry;
34 import org.apache.directory.server.core.sp.StoredProcEngine;
35 import org.apache.directory.server.core.sp.StoredProcUtils;
36 import org.apache.directory.shared.ldap.entry.EntryAttribute;
37 import org.apache.directory.shared.ldap.util.DirectoryClassUtils;
38
39
40
41
42
43
44
45
46 public class JavaStoredProcEngine implements StoredProcEngine
47 {
48
49 public static final String STORED_PROC_LANG_ID = "Java";
50
51 private ServerEntry spUnit;
52
53
54
55
56
57 public Object invokeProcedure( CoreSession session, String fullSPName, Object[] spArgs ) throws Exception
58 {
59 EntryAttribute javaByteCode = spUnit.get( "javaByteCode" );
60 String spName = StoredProcUtils.extractStoredProcName( fullSPName );
61 String className = StoredProcUtils.extractStoredProcUnitName( fullSPName );
62
63 ClassLoader loader = new LdapJavaStoredProcClassLoader( javaByteCode );
64 Class<?> clazz;
65
66 try
67 {
68 clazz = loader.loadClass( className );
69 }
70 catch ( ClassNotFoundException e )
71 {
72 NamingException ne = new NamingException();
73 ne.setRootCause( e );
74 throw ne;
75 }
76
77 Class<?>[] types = getTypesFromValues( spArgs );
78
79 Method proc;
80 try
81 {
82 proc = DirectoryClassUtils.getAssignmentCompatibleMethod( clazz, spName, types );
83 }
84 catch ( NoSuchMethodException e )
85 {
86 NamingException ne = new NamingException();
87 ne.setRootCause( e );
88 throw ne;
89 }
90 try
91 {
92 return proc.invoke( null, spArgs );
93 }
94 catch ( IllegalArgumentException e )
95 {
96 NamingException ne = new NamingException();
97 ne.setRootCause( e );
98 throw ne;
99 }
100 catch ( IllegalAccessException e )
101 {
102 NamingException ne = new NamingException();
103 ne.setRootCause( e );
104 throw ne;
105 }
106 catch ( InvocationTargetException e )
107 {
108 NamingException ne = new NamingException();
109 ne.setRootCause( e );
110 throw ne;
111 }
112 }
113
114
115
116
117
118 public String getSPLangId()
119 {
120 return STORED_PROC_LANG_ID;
121 }
122
123
124
125
126
127 public void setSPUnitEntry( ServerEntry spUnit )
128 {
129 this.spUnit = spUnit;
130 }
131
132
133 private Class<?>[] getTypesFromValues( Object[] values )
134 {
135 List<Class<?>> types = new ArrayList<Class<?>>();
136
137 for ( Object obj : values )
138 {
139 types.add( obj.getClass() );
140 }
141
142 return types.toArray( EMPTY_CLASS_ARRAY );
143 }
144
145 private static Class<?>[] EMPTY_CLASS_ARRAY = new Class[ 0 ];
146
147 }