View Javadoc

1   /*
2    * Copyright 2002-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.clazz.bean;
17  
18  import java.lang.reflect.Constructor;
19  
20  import org.apache.commons.clazz.Clazz;
21  import org.apache.commons.clazz.ClazzLoader;
22  import org.apache.commons.clazz.ModelClazzLoader;
23  
24  /***
25   * 
26   * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
27   * @version $Id: BeanClazzLoader.java,v 1.6 2004/02/19 23:58:38 scolebourne Exp $
28   */
29  public class BeanClazzLoader extends ClazzLoader {
30      
31      public BeanClazzLoader(ModelClazzLoader modelClazzLoader) {
32          super(modelClazzLoader);
33      }
34  
35      /***
36       * @see ClazzLoader#isMember(Object)
37       */
38      public boolean isMember(Object instance) {
39          return instance instanceof Bean;
40      }
41  
42      public String getClazzName(Object instance) {
43          if (!isMember(instance)) {
44              return null;
45          }
46          
47          return ((Bean) instance).getClazz().getName();
48      }
49          
50      /***
51       * BeanClazzLoader does not cache clazzes, its parent, BeanGroupClazzLoader,
52       * is responsible for caching
53       */
54      public Clazz getClazzForName(String clazzName) {
55          return null;
56      }
57      
58      /***
59       * @see ClazzLoader#defineClazz(String, Class, Class)
60       */
61      public Clazz defineClazz(String name, Class clazzType, Class instanceClass) 
62      {
63          if (!BeanClazz.class.isAssignableFrom(clazzType)) {
64              return null;
65          }
66          try {
67              Constructor constructor =
68                  clazzType.getConstructor(
69                      new Class[] {
70                          ClazzLoader.class,
71                          String.class,
72                          Class.class });
73              return (Clazz) constructor.newInstance(
74                  new Object[] { getModelClazzLoader(), name, instanceClass });
75          }
76          catch (Exception e) {
77              e.printStackTrace();
78              throw new BeanClazzConfigurationException(
79                  "Cannot define clazz "
80                      + name
81                      + " of clazz type "
82                      + clazzType.getName(),
83                  e);
84          }
85      }
86  }