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    package org.apache.directory.shared.ldap.schema.loader.ldif;
021    
022    
023    import javax.naming.NamingException;
024    import javax.naming.directory.InvalidAttributeValueException;
025    
026    import org.apache.directory.shared.i18n.I18n;
027    import org.apache.directory.shared.ldap.entry.EntryAttribute;
028    import org.apache.directory.shared.ldap.entry.Value;
029    
030    
031    /**
032     * A class loader that loads classes from an attribute within an entry.
033     * 
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev$ $Date$
036     */
037    public class AttributeClassLoader extends ClassLoader
038    {
039        public EntryAttribute attribute;
040        
041    
042        public AttributeClassLoader()
043        {
044            super( AttributeClassLoader.class.getClassLoader() );
045        }
046        
047        
048        public void setAttribute( EntryAttribute attribute ) throws NamingException
049        {
050            if ( attribute.isHR() )
051            {
052                throw new InvalidAttributeValueException( I18n.err( I18n.ERR_10001 ) );
053            }
054            
055            this.attribute = attribute;
056        }
057    
058        
059        public Class<?> findClass( String name ) throws ClassNotFoundException
060        {
061            byte[] classBytes = null;
062            
063            Value<?> value = attribute.get();
064            
065            if ( value.isBinary() )
066            {
067                classBytes = value.getBytes();
068    
069                return defineClass( name, classBytes, 0, classBytes.length );
070            }
071            else
072            {
073                throw new ClassNotFoundException( I18n.err( I18n.ERR_10002 ) );
074            }
075        }
076    }