View Javadoc

1   package org.apache.commons.digester3.binder;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static java.lang.System.getSecurityManager;
23  import static java.security.AccessController.doPrivileged;
24  
25  import java.net.URL;
26  import java.security.PrivilegedAction;
27  import java.util.Collections;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  final class BinderClassLoader
32      extends ClassLoader
33  {
34  
35      private static final Map<String, Class<?>> PRIMITIVE_TYPES;
36      static
37      {
38          HashMap<String, Class<?>> primitiveTypes = new HashMap<String, Class<?>>();
39          primitiveTypes.put( "boolean", boolean.class );
40          primitiveTypes.put( "byte", byte.class );
41          primitiveTypes.put( "short", short.class );
42          primitiveTypes.put( "int", int.class );
43          primitiveTypes.put( "char", char.class );
44          primitiveTypes.put( "long", long.class );
45          primitiveTypes.put( "float", float.class );
46          primitiveTypes.put( "double", double.class );
47          PRIMITIVE_TYPES = Collections.unmodifiableMap( primitiveTypes );
48      }
49  
50      public static BinderClassLoader createBinderClassLoader( final ClassLoader adaptedClassLoader )
51      {
52          PrivilegedAction<BinderClassLoader> action = new PrivilegedAction<BinderClassLoader>()
53          {
54  
55              public BinderClassLoader run()
56              {
57                  return new BinderClassLoader( adaptedClassLoader );
58              }
59  
60          };
61  
62          if ( getSecurityManager() != null )
63          {
64              return doPrivileged( action );
65          }
66          return action.run();
67      }
68  
69      private BinderClassLoader( ClassLoader adaptedClassLoader )
70      {
71          super( adaptedClassLoader );
72      }
73  
74      public ClassLoader getAdaptedClassLoader()
75      {
76          return getParent();
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      protected synchronized Class<?> loadClass( String name, boolean resolve )
84          throws ClassNotFoundException
85      {
86          if ( PRIMITIVE_TYPES.containsKey( name ) )
87          {
88              return PRIMITIVE_TYPES.get( name );
89          }
90          return getParent().loadClass( name );
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public URL getResource( String name )
98      {
99          return getAdaptedClassLoader().getResource( name );
100     }
101 
102 }