View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.tapestry;
14  
15  import java.net.URL;
16  
17  import org.apache.hivemind.ApplicationRuntimeException;
18  import org.apache.hivemind.ClassResolver;
19  
20  /**
21   * {@link ClassResolver} implementation that uses supplied class loader
22   *
23   *
24   * @author Daniel Sendula
25   */
26  public class DanubeClassResolver implements ClassResolver {
27  
28      /** Class loader */
29      protected ClassLoader classLoader;
30  
31      /**
32       * Returns resource as url. It calls {@link ClassLoader#getResource(String)}.
33       * @return resource's url
34       */
35      public URL getResource(String name) {
36          return classLoader.getResource(name);
37      }
38  
39      /**
40       * Finds a class from given name. Calls {@link ClassLoader#loadClass(String)}.
41       * @param name class name
42       * @return class
43       * @throws ApplicationRuntimeException
44       */
45      public Class<?> findClass(String name) {
46          try {
47              return classLoader.loadClass(name);
48          } catch (ClassNotFoundException e) {
49              throw new ApplicationRuntimeException(e);
50          }
51      }
52  
53      /**
54       * Checks if class is available for load. If it is class would be returned.
55       * if not it returns <code>null</code>.
56       * @param name class name
57       * @return class or <code>null</code>
58       */
59      @SuppressWarnings("unchecked")
60      public Class checkForClass(String name) {
61          try {
62              return findClass(name);
63          } catch (ApplicationRuntimeException e) {
64              return null;
65          }
66      }
67  
68      /**
69       * Returns class loader
70       * @return class loader
71       */
72      public ClassLoader getClassLoader() {
73          return classLoader;
74      }
75  
76  }