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 org.apache.commons.clazz.Clazz;
19  import org.apache.commons.clazz.ClazzInstanceFactory;
20  import org.apache.commons.clazz.ClazzLoader;
21  import org.apache.commons.clazz.common.ClazzFeatureSupport;
22  
23  /**
24   * 
25   * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
26   * @version $Id: BeanClazzInstanceFactory.java 155436 2005-02-26 13:17:48Z dirkv $
27   */
28  public abstract class BeanClazzInstanceFactory
29      extends ClazzFeatureSupport
30      implements ClazzInstanceFactory 
31  {
32      private String name;
33      private static final String[] EMPTY_STRING_ARRAY = new String[0];
34      private String[] parameterClazzNames;
35      private Clazz[] parameterClazzes;
36      private String signature;
37      
38      public BeanClazzInstanceFactory(
39              Clazz declaringClazz,
40              String parameterClazzNames[])
41      {
42          this(declaringClazz, null, parameterClazzNames);
43      }
44      
45      /**
46       * Constructor for BeanClazzInstanceFactory.
47       * @param declaringClazz
48       */
49      public BeanClazzInstanceFactory(
50              Clazz declaringClazz,
51              String name,
52              String parameterClazzNames[]) 
53      {
54          super(declaringClazz);
55          this.name = name;
56          this.parameterClazzNames = parameterClazzNames;
57          this.parameterClazzNames =
58              (parameterClazzNames != null
59                  ? parameterClazzNames
60                  : EMPTY_STRING_ARRAY);
61      }
62  
63      /**
64       * @see org.apache.commons.clazz.ClazzInstanceFactory#getName()
65       */
66      public String getName() {
67          return name;
68      }
69  
70      /**
71       * @see org.apache.commons.clazz.ClazzInstanceFactory#getParameterClazzes()
72       */
73      public Clazz[] getParameterClazzes() {
74          if (parameterClazzes == null) {
75              parameterClazzes = new Clazz[parameterClazzNames.length];
76              ClazzLoader loader = getDeclaringClazz().getClazzLoader();
77              for (int i = 0; i < parameterClazzes.length; i++) {
78                  parameterClazzes[i] =
79                      loader.getClazzForName(parameterClazzNames[i]);
80                  if (parameterClazzes[i] == null) {
81                      throw new BeanClazzConfigurationException(
82                          "Invalid argument type: "
83                              + parameterClazzNames[i]
84                              + ". Clazz not found.");
85                  }
86              }
87          }
88  
89          return parameterClazzes;
90      }
91  
92      /**
93       * @see org.apache.commons.clazz.ClazzInstanceFactory#getSignature()
94       */
95      public String getSignature() {
96          if (signature == null) {
97              signature = Clazz.constructSignature(name, getParameterClazzes());
98          }
99          return signature;
100     }
101 }