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    
021    
022    package org.apache.directory.server.core.sp.java;
023    
024    
025    import java.lang.reflect.InvocationTargetException;
026    import java.lang.reflect.Method;
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    import javax.naming.NamingException;
031    
032    import org.apache.directory.server.core.CoreSession;
033    import org.apache.directory.server.core.sp.StoredProcEngine;
034    import org.apache.directory.server.core.sp.StoredProcUtils;
035    import org.apache.directory.shared.ldap.entry.EntryAttribute;
036    import org.apache.directory.shared.ldap.entry.ServerEntry;
037    import org.apache.directory.shared.ldap.util.DirectoryClassUtils;
038    
039    
040    /**
041     * A {@link StoredProcEngine} implementation specific to Java stored procedures.
042     * 
043     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044     * @version $Rev$ $Date$
045     */
046    public class JavaStoredProcEngine implements StoredProcEngine
047    {
048    
049        public static final String STORED_PROC_LANG_ID = "Java";
050    
051        private ServerEntry spUnit;
052    
053    
054        /* (non-Javadoc)
055         * @see org.apache.directory.server.core.sp.StoredProcEngine#invokeProcedure(OperationContext, String, Object[])
056         */
057        public Object invokeProcedure( CoreSession session, String fullSPName, Object[] spArgs ) throws Exception
058        {
059            EntryAttribute javaByteCode = spUnit.get( "javaByteCode" );
060            String spName = StoredProcUtils.extractStoredProcName( fullSPName );
061            String className = StoredProcUtils.extractStoredProcUnitName( fullSPName );
062    
063            ClassLoader loader = new LdapJavaStoredProcClassLoader( javaByteCode );
064            Class<?> clazz;
065            
066            try
067            {
068                clazz = loader.loadClass( className );
069            }
070            catch ( ClassNotFoundException e )
071            {
072                NamingException ne = new NamingException();
073                ne.setRootCause( e );
074                throw ne;
075            }
076    
077            Class<?>[] types = getTypesFromValues( spArgs );
078    
079            Method proc;
080            try
081            {
082                proc = DirectoryClassUtils.getAssignmentCompatibleMethod( clazz, spName, types );
083            }
084            catch ( NoSuchMethodException e )
085            {
086                NamingException ne = new NamingException();
087                ne.setRootCause( e );
088                throw ne;
089            }
090            try
091            {
092                return proc.invoke( null, spArgs );
093            }
094            catch ( IllegalArgumentException e )
095            {
096                NamingException ne = new NamingException();
097                ne.setRootCause( e );
098                throw ne;
099            }
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    }