001    /*
002     * Copyright 2002-2004 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.clazz.bean;
017    
018    import org.apache.commons.clazz.Clazz;
019    import org.apache.commons.clazz.ClazzInstanceFactory;
020    import org.apache.commons.clazz.ClazzLoader;
021    import org.apache.commons.clazz.common.ClazzFeatureSupport;
022    
023    /**
024     * 
025     * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
026     * @version $Id: BeanClazzInstanceFactory.java 155436 2005-02-26 13:17:48Z dirkv $
027     */
028    public abstract class BeanClazzInstanceFactory
029        extends ClazzFeatureSupport
030        implements ClazzInstanceFactory 
031    {
032        private String name;
033        private static final String[] EMPTY_STRING_ARRAY = new String[0];
034        private String[] parameterClazzNames;
035        private Clazz[] parameterClazzes;
036        private String signature;
037        
038        public BeanClazzInstanceFactory(
039                Clazz declaringClazz,
040                String parameterClazzNames[])
041        {
042            this(declaringClazz, null, parameterClazzNames);
043        }
044        
045        /**
046         * Constructor for BeanClazzInstanceFactory.
047         * @param declaringClazz
048         */
049        public BeanClazzInstanceFactory(
050                Clazz declaringClazz,
051                String name,
052                String parameterClazzNames[]) 
053        {
054            super(declaringClazz);
055            this.name = name;
056            this.parameterClazzNames = parameterClazzNames;
057            this.parameterClazzNames =
058                (parameterClazzNames != null
059                    ? parameterClazzNames
060                    : EMPTY_STRING_ARRAY);
061        }
062    
063        /**
064         * @see org.apache.commons.clazz.ClazzInstanceFactory#getName()
065         */
066        public String getName() {
067            return name;
068        }
069    
070        /**
071         * @see org.apache.commons.clazz.ClazzInstanceFactory#getParameterClazzes()
072         */
073        public Clazz[] getParameterClazzes() {
074            if (parameterClazzes == null) {
075                parameterClazzes = new Clazz[parameterClazzNames.length];
076                ClazzLoader loader = getDeclaringClazz().getClazzLoader();
077                for (int i = 0; i < parameterClazzes.length; i++) {
078                    parameterClazzes[i] =
079                        loader.getClazzForName(parameterClazzNames[i]);
080                    if (parameterClazzes[i] == null) {
081                        throw new BeanClazzConfigurationException(
082                            "Invalid argument type: "
083                                + parameterClazzNames[i]
084                                + ". Clazz not found.");
085                    }
086                }
087            }
088    
089            return parameterClazzes;
090        }
091    
092        /**
093         * @see org.apache.commons.clazz.ClazzInstanceFactory#getSignature()
094         */
095        public String getSignature() {
096            if (signature == null) {
097                signature = Clazz.constructSignature(name, getParameterClazzes());
098            }
099            return signature;
100        }
101    }