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  package org.apache.shiro.io;
20  
21  import org.apache.shiro.util.ClassUtils;
22  import org.apache.shiro.util.UnknownClassException;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectStreamClass;
28  
29  /**
30   * Enables correct ClassLoader lookup in various environments (e.g. JEE Servers, etc).
31   *
32   * @since 1.2
33   * @see <a href="https://issues.apache.org/jira/browse/SHIRO-334">SHIRO-334</a>
34   */
35  public class ClassResolvingObjectInputStream extends ObjectInputStream {
36  
37      public ClassResolvingObjectInputStream(InputStream inputStream) throws IOException {
38          super(inputStream);
39      }
40  
41      /**
42       * Resolves an {@link ObjectStreamClass} by delegating to Shiro's 
43       * {@link ClassUtils#forName(String)} utility method, which is known to work in all ClassLoader environments.
44       * 
45       * @param osc the ObjectStreamClass to resolve the class name.
46       * @return the discovered class
47       * @throws IOException never - declaration retained for subclass consistency
48       * @throws ClassNotFoundException if the class could not be found in any known ClassLoader
49       */
50      @Override
51      protected Class<?> resolveClass(ObjectStreamClass osc) throws IOException, ClassNotFoundException {
52          try {
53              return ClassUtils.forName(osc.getName());
54          } catch (UnknownClassException e) {
55              throw new ClassNotFoundException("Unable to load ObjectStreamClass [" + osc + "]: ", e);
56          }
57      }
58  }