View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.discovery.tools;
18  
19  import org.apache.commons.discovery.ResourceClass;
20  import org.apache.commons.discovery.ResourceClassIterator;
21  import org.apache.commons.discovery.resource.classes.DiscoverClasses;
22  import org.apache.commons.discovery.resource.ClassLoaders;
23  
24  
25  /***
26   * Holder for a default class.
27   * 
28   * Class may be specified by name (String) or class (Class).
29   * Using the holder complicates the users job, but minimized # of API's.
30   * 
31   * @author Richard A. Sitze
32   */
33  public class DefaultClassHolder {
34      private Class        defaultClass;
35      private final String defaultName;
36      
37      public DefaultClassHolder(Class defaultClass) {
38          this.defaultClass = defaultClass;
39          this.defaultName = defaultClass.getName();
40      }
41      
42      public DefaultClassHolder(String defaultName) {
43          this.defaultClass = null;
44          this.defaultName = defaultName;
45      }
46  
47      /***
48       * @param spi non-null SPI
49       * @param loaders Used only if class needs to be loaded.
50       * 
51       * @return Default Class.  Load the class if necessary,
52       *         and verify that it implements the SPI.
53       *         (this forces the check, no way out..).
54       */
55      public Class getDefaultClass(SPInterface spi, ClassLoaders loaders) {
56          if (defaultClass == null) {
57              DiscoverClasses classDiscovery = new DiscoverClasses(loaders);
58              ResourceClassIterator classes = classDiscovery.findResourceClasses(getDefaultName());
59              if (classes.hasNext()) {
60                  ResourceClass info = classes.nextResourceClass();
61                  try {
62                      defaultClass = info.loadClass();
63                  } catch (Exception e) {
64                      // ignore
65                  }
66              }
67          }
68          
69          if (defaultClass != null) {
70              spi.verifyAncestory(defaultClass);
71          }
72  
73          return defaultClass;
74      }
75  
76      public String getDefaultName() {
77          return defaultName;
78      }
79  }