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.schema.loader;
21  
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.IOException;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.jar.JarEntry;
28  import java.util.jar.JarInputStream;
29  
30  import org.apache.directory.api.i18n.I18n;
31  import org.apache.directory.api.ldap.model.entry.Attribute;
32  import org.apache.directory.api.ldap.model.entry.Value;
33  import org.apache.directory.api.ldap.model.exception.LdapException;
34  import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
35  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
36  import java.io.ByteArrayOutputStream;
37  import java.io.InputStream;
38  
39  
40  /**
41   * A class loader that loads classes from an attribute within an entry.
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class AttributeClassLoader extends ClassLoader
46  {
47      /** The attribute. */
48      private Attribute attribute;
49  
50  
51      /**
52       * Instantiates a new attribute class loader.
53       */
54      public AttributeClassLoader()
55      {
56          super( AttributeClassLoader.class.getClassLoader() );
57      }
58  
59  
60      /**
61       * Sets the attribute.
62       *
63       * @param attribute the new attribute
64       * @throws LdapException if the attribute is not binary.
65       */
66      public void setAttribute( Attribute attribute ) throws LdapException
67      {
68          if ( attribute.isHumanReadable() )
69          {
70              throw new LdapInvalidAttributeValueException( ResultCodeEnum.CONSTRAINT_VIOLATION,
71                  I18n.err( I18n.ERR_10001 ) );
72          }
73  
74          this.attribute = attribute;
75      }
76  
77      
78      /**
79       * Read data from a jar, and write them into a byte[]
80       */
81      private static byte[] getBytes( InputStream input ) throws IOException 
82      {
83          ByteArrayOutputStream result = new ByteArrayOutputStream();
84  
85          byte[] buf = new byte[2048];
86          int bytesRead = input.read( buf );
87  
88          while ( bytesRead != -1 ) 
89          {
90              result.write( buf, 0, bytesRead );
91              bytesRead = input.read( buf );
92          }
93        
94          result.flush();
95          result.close();
96          
97          return result.toByteArray();
98      }
99  
100     
101     private Map<String, Class<?>> loadClasses( byte[] jarBytes ) throws IOException 
102     {
103         Map<String, Class<?>> map = new HashMap<>();
104         
105         try ( JarInputStream jis = new JarInputStream( new ByteArrayInputStream( jarBytes ) ) ) 
106         {
107             JarEntry entry;
108             boolean isJar = false;
109             
110             while ( ( entry = jis.getNextJarEntry() ) != null ) 
111             {
112                 String fileName = entry.getName();
113                 isJar = true;
114                 
115                 // Just consider the files ending with .class
116                 if ( fileName.endsWith( ".class" ) )
117                 {
118                     String className = fileName.substring( 0,  fileName.length() - ".class".length() ).replace( '/', '.' );
119                     byte[] classBytes = getBytes( jis );
120                     
121                     Class<?> clazz = defineClass( className, classBytes, 0, classBytes.length );
122                     map.put( className, clazz );
123                 }
124             }
125             
126             if ( !isJar )
127             {
128                 return null;
129             }
130         }
131 
132         return map;
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public Class<?> findClass( String name ) throws ClassNotFoundException
140     {
141         byte[] classBytes;
142 
143         Value<?> value = attribute.get();
144 
145         if ( value.isHumanReadable() )
146         {
147             throw new ClassNotFoundException( I18n.err( I18n.ERR_10002 ) );
148         }
149 
150         classBytes = value.getBytes();
151 
152         // May be we are dealing with a JAR ?
153         try 
154         {
155             Map<String, Class<?>> classes = loadClasses( classBytes );
156             
157             if ( classes == null )
158             {
159                 // May be a simple class ?
160                 return defineClass( name, classBytes, 0, classBytes.length );
161             }
162             
163             for ( Map.Entry<String, Class<?>> entry : classes.entrySet() )
164             {
165                 if ( entry.getKey().contains( name ) )
166                 {
167                     return entry.getValue();
168                 }
169             }
170         }
171         catch ( IOException ioe )
172         {
173             // Ok, may be a pure class
174             return defineClass( name, classBytes, 0, classBytes.length );
175         }
176         
177         return null;
178     }
179 }