001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020
021package org.apache.directory.server.core.api.sp.java;
022
023
024import java.lang.reflect.InvocationTargetException;
025import java.lang.reflect.Method;
026import java.util.ArrayList;
027import java.util.List;
028
029import org.apache.directory.api.ldap.model.entry.Attribute;
030import org.apache.directory.api.ldap.model.entry.Entry;
031import org.apache.directory.api.ldap.model.exception.LdapException;
032import org.apache.directory.api.util.MethodUtils;
033import org.apache.directory.server.core.api.CoreSession;
034import org.apache.directory.server.core.api.sp.StoredProcEngine;
035import org.apache.directory.server.core.api.sp.StoredProcUtils;
036
037
038/**
039 * A {@link StoredProcEngine} implementation specific to Java stored procedures.
040 * 
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public class JavaStoredProcEngine implements StoredProcEngine
044{
045
046    public static final String STORED_PROC_LANG_ID = "Java";
047
048    private Entry spUnit;
049
050    private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class[0];
051
052    /* (non-Javadoc)
053     * @see org.apache.directory.server.core.sp.StoredProcEngine#invokeProcedure(OperationContext, String, Object[])
054     */
055    @Override
056    public Object invokeProcedure( CoreSession session, String fullSPName, Object[] spArgs ) throws LdapException
057    {
058        Attribute javaByteCode = spUnit.get( "javaByteCode" );
059        String spName = StoredProcUtils.extractStoredProcName( fullSPName );
060        String className = StoredProcUtils.extractStoredProcUnitName( fullSPName );
061
062        ClassLoader loader = new LdapJavaStoredProcClassLoader( javaByteCode );
063        Class<?> clazz;
064
065        try
066        {
067            clazz = loader.loadClass( className );
068        }
069        catch ( ClassNotFoundException e )
070        {
071            throw new LdapException( e );
072        }
073
074        Class<?>[] types = getTypesFromValues( spArgs );
075
076        Method proc;
077        try
078        {
079            proc = MethodUtils.getAssignmentCompatibleMethod( clazz, spName, types );
080        }
081        catch ( NoSuchMethodException e )
082        {
083            throw new LdapException( e );
084        }
085        try
086        {
087            return proc.invoke( null, spArgs );
088        }
089        catch ( IllegalArgumentException | IllegalAccessException | InvocationTargetException e )
090        {
091            throw new LdapException( e );
092        }
093    }
094
095
096    /* (non-Javadoc)
097     * @see org.apache.directory.server.core.sp.StoredProcEngine#getSPLangId()
098     */
099    @Override
100    public String getSPLangId()
101    {
102        return STORED_PROC_LANG_ID;
103    }
104
105
106    /* (non-Javadoc)
107     * @see org.apache.directory.server.core.sp.StoredProcEngine#setSPUnitEntry(javax.naming.directory.Attributes)
108     */
109    @Override
110    public void setSPUnitEntry( Entry spUnit )
111    {
112        this.spUnit = spUnit;
113    }
114
115
116    private Class<?>[] getTypesFromValues( Object[] values )
117    {
118        List<Class<?>> types = new ArrayList<>();
119
120        for ( Object obj : values )
121        {
122            types.add( obj.getClass() );
123        }
124
125        return types.toArray( EMPTY_CLASS_ARRAY );
126    }
127}