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  package org.apache.directory.api.ldap.schemaloader;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.entry.Attribute;
25  import org.apache.directory.api.ldap.model.entry.Value;
26  import org.apache.directory.api.ldap.model.exception.LdapException;
27  import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
28  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
29  
30  
31  /**
32   * A class loader that loads classes from an attribute within an entry.
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class AttributeClassLoader extends ClassLoader
37  {
38  
39      /** The attribute. */
40      private Attribute attribute;
41  
42  
43      /**
44       * Instantiates a new attribute class loader.
45       */
46      public AttributeClassLoader()
47      {
48          super( AttributeClassLoader.class.getClassLoader() );
49      }
50  
51  
52      /**
53       * Sets the attribute.
54       *
55       * @param attribute the new attribute
56       * @throws LdapException if the attribute is not binary.
57       */
58      public void setAttribute( Attribute attribute ) throws LdapException
59      {
60          if ( attribute.isHumanReadable() )
61          {
62              throw new LdapInvalidAttributeValueException( ResultCodeEnum.CONSTRAINT_VIOLATION,
63                  I18n.err( I18n.ERR_10001 ) );
64          }
65  
66          this.attribute = attribute;
67      }
68  
69  
70      /**
71       * {@inheritDoc}
72       */
73      public Class<?> findClass( String name ) throws ClassNotFoundException
74      {
75          byte[] classBytes = null;
76  
77          Value<?> value = attribute.get();
78  
79          if ( value.isHumanReadable() )
80          {
81              throw new ClassNotFoundException( I18n.err( I18n.ERR_10002 ) );
82          }
83  
84          classBytes = value.getBytes();
85  
86          return defineClass( name, classBytes, 0, classBytes.length );
87      }
88  }