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  package org.apache.directory.server.core.sp;
22  
23  
24  import org.apache.directory.server.core.DirectoryService;
25  import org.apache.directory.server.core.integ.CiRunner;
26  import org.apache.directory.server.core.integ.Level;
27  import org.apache.directory.server.core.integ.annotations.CleanupLevel;
28  
29  import static org.apache.directory.server.core.integ.IntegrationUtils.getRootContext;
30  import org.apache.directory.server.core.jndi.ServerLdapContext;
31  import org.apache.directory.shared.ldap.util.Base64;
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertNotNull;
34  import org.junit.Test;
35  import org.junit.runner.RunWith;
36  
37  import javax.naming.directory.Attribute;
38  import javax.naming.directory.Attributes;
39  import javax.naming.directory.BasicAttribute;
40  import javax.naming.directory.BasicAttributes;
41  import javax.naming.ldap.LdapContext;
42  
43  
44  /**
45   * Test case for LdapClassLoader.
46   *
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   * @version $Rev$ $Date$
49   */
50  @RunWith ( CiRunner.class )
51  @CleanupLevel(Level.CLASS)
52  public class LdapClassLoaderIT
53  {
54      private static final String HELLOWORLD_CLASS_BASE64 = "yv66vgAAADEAHQoABgAPCQAQABEIABIKABMAFAcAFQcAFgEABjxpbml0PgEAAygpV"
55          + "gEABENvZGUBAA9MaW5lTnVtYmVyVGFibGUBAARtYWluAQAWKFtMamF2YS9sYW5nL1N0cmluZzsp"
56          + "VgEAClNvdXJjZUZpbGUBAA9IZWxsb1dvcmxkLmphdmEMAAcACAcAFwwAGAAZAQAMSGVsbG8gV29"
57          + "ybGQhBwAaDAAbABwBAApIZWxsb1dvcmxkAQAQamF2YS9sYW5nL09iamVjdAEAEGphdmEvbGFuZy"
58          + "9TeXN0ZW0BAANvdXQBABVMamF2YS9pby9QcmludFN0cmVhbTsBABNqYXZhL2lvL1ByaW50U3RyZ"
59          + "WFtAQAHcHJpbnRsbgEAFShMamF2YS9sYW5nL1N0cmluZzspVgAhAAUABgAAAAAAAgABAAcACAAB"
60          + "AAkAAAAdAAEAAQAAAAUqtwABsQAAAAEACgAAAAYAAQAAAAEACQALAAwAAQAJAAAAJQACAAEAAAA"
61          + "JsgACEgO2AASxAAAAAQAKAAAACgACAAAABQAIAAYAAQANAAAAAgAO";
62  
63      private static final byte[] HELLOWORLD_CLASS_BYTES = Base64.decode( HELLOWORLD_CLASS_BASE64.toCharArray() );
64  
65  
66      public static DirectoryService service;
67  
68  
69      @Test
70      public void testLdapClassLoaderWithClassLoadedAnywhere() throws Exception
71      {
72          LdapContext root = getRootContext( service );
73  
74          // get default naming context to work on
75          ServerLdapContext defaultContext = ( ServerLdapContext ) root.lookup( "ou=system" );
76  
77          // set up
78          Attributes attributes = new BasicAttributes( "objectClass", "top", true );
79          attributes.get( "objectClass" ).add( "javaClass" );
80          attributes.put( "fullyQualifiedJavaClassName", "HelloWorld" );
81          attributes.put( "javaClassByteCode", HELLOWORLD_CLASS_BYTES );
82          defaultContext.createSubcontext( "fullyQualifiedJavaClassName=HelloWorld", attributes );
83  
84          // assert set up successfull
85          assertNotNull( defaultContext.lookup( "fullyQualifiedJavaClassName=HelloWorld" ) );
86  
87          // load the class
88          LdapClassLoader loader = new LdapClassLoader( service );
89          Class<?> clazz = loader.loadClass( "HelloWorld" );
90  
91          // assert class loaded successfully
92          assertEquals( clazz.getName(), "HelloWorld" );
93      }
94  
95  
96      @Test
97      public void testLdapClassLoaderWithClassLoadedAtDefaultSearchSubtree() throws Exception
98      {
99          LdapContext root = getRootContext( service );
100 
101         // get default naming context to work on
102         ServerLdapContext defaultContext = ( ServerLdapContext ) root.lookup( "ou=system" );
103 
104         // create an extensible object for holding custom config data
105         Attributes classLoaderDefaultSearchContextConfig = new BasicAttributes( true );
106         Attribute objectClass = new BasicAttribute( "objectClass" );
107         objectClass.add( "top" );
108         objectClass.add( "javaContainer" );
109         
110         // We need to ad this extensibleObject OC in order to avoid an error
111         // with the classLoaderDefaultSearchContext AT used later (no OC contains
112         // this AT)
113         objectClass.add( "extensibleObject" );
114 
115         // create custom config entry
116         classLoaderDefaultSearchContextConfig.put( objectClass );
117         classLoaderDefaultSearchContextConfig.put( new BasicAttribute( "cn", "classLoaderDefaultSearchContext" ) );
118 
119         // add a default search context to the configuration
120         classLoaderDefaultSearchContextConfig.put( new BasicAttribute( "classLoaderDefaultSearchContext", "ou=system" ) );
121 
122         // add the configuration entry to the DIT
123         ServerLdapContext configContext = ( ServerLdapContext ) defaultContext.lookup( "ou=configuration" );
124         configContext.createSubcontext( "cn=classLoaderDefaultSearchContext", classLoaderDefaultSearchContextConfig );
125 
126         // create a class holder entry and add it to the DIT
127         Attributes attributes = new BasicAttributes( "objectClass", "top", true );
128         attributes.get( "objectClass" ).add( "javaClass" );
129         attributes.put( "fullyQualifiedJavaClassName", "HelloWorld" );
130         attributes.put( "javaClassByteCode", HELLOWORLD_CLASS_BYTES );
131         defaultContext.createSubcontext( "fullyQualifiedJavaClassName=HelloWorld", attributes );
132 
133         // assert set up successfull
134         assertNotNull( defaultContext.lookup( "fullyQualifiedJavaClassName=HelloWorld" ) );
135 
136         // load the class
137         LdapClassLoader loader = new LdapClassLoader( service );
138         Class<?> clazz = loader.loadClass( "HelloWorld" );
139 
140         // assert class loaded successfully
141         assertEquals( clazz.getName(), "HelloWorld" );
142     }
143 }