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.schema;
23  
24  
25  import javax.naming.NamingException;
26  import javax.naming.directory.InvalidAttributeValueException;
27  
28  import org.apache.directory.server.core.entry.ServerAttribute;
29  import org.apache.directory.server.core.entry.ServerBinaryValue;
30  import org.apache.directory.shared.ldap.entry.EntryAttribute;
31  import org.apache.directory.shared.ldap.entry.Value;
32  
33  
34  /**
35   * A class loader that loads classes from an attribute within an entry.
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev$ $Date$
39   */
40  public class AttributeClassLoader extends ClassLoader
41  {
42      public ServerAttribute attribute;
43      
44  
45      public AttributeClassLoader()
46      {
47          super( AttributeClassLoader.class.getClassLoader() );
48      }
49      
50      
51      public void setAttribute( EntryAttribute attribute ) throws NamingException
52      {
53          if ( ((ServerAttribute)attribute).getAttributeType().getSyntax().isHumanReadable() )
54          {
55              throw new InvalidAttributeValueException( "The attribute must be binary" );
56          }
57          
58          this.attribute = (ServerAttribute)attribute;
59      }
60  
61      
62      public Class<?> findClass( String name ) throws ClassNotFoundException
63      {
64          byte[] classBytes = null;
65          
66          Value<?> value = attribute.get();
67          
68          if ( value instanceof ServerBinaryValue )
69          {
70              classBytes = ((ServerBinaryValue)value).get();
71  
72              return defineClass( name, classBytes, 0, classBytes.length );
73          }
74          else
75          {
76              throw new ClassNotFoundException( "Failed to access attribute bytes." );
77          }
78      }
79  }