View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
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   * A {@link StoredProcEngine} implementation specific to Java stored procedures.
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   * @version $Rev$ $Date$
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      /* (non-Javadoc)
55       * @see org.apache.directory.server.core.sp.StoredProcEngine#invokeProcedure(OperationContext, String, Object[])
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     /* (non-Javadoc)
116      * @see org.apache.directory.server.core.sp.StoredProcEngine#getSPLangId()
117      */
118     public String getSPLangId()
119     {
120         return STORED_PROC_LANG_ID;
121     }
122 
123 
124     /* (non-Javadoc)
125      * @see org.apache.directory.server.core.sp.StoredProcEngine#setSPUnitEntry(javax.naming.directory.Attributes)
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 }