001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.proxy.provider;
019
020 import org.apache.commons.proxy.ObjectProvider;
021 import org.apache.commons.proxy.exception.ObjectProviderException;
022
023 import java.io.Serializable;
024
025 /**
026 * Uses <code>Class.newInstance()</code> to instantiate an object.
027 *
028 * @author James Carman
029 * @since 1.0
030 */
031 public class BeanProvider implements ObjectProvider, Serializable
032 {
033 //**********************************************************************************************************************
034 // Fields
035 //**********************************************************************************************************************
036
037 private Class beanClass;
038
039 //**********************************************************************************************************************
040 // Constructors
041 //**********************************************************************************************************************
042
043 public BeanProvider()
044 {
045 }
046
047 /**
048 * Constructs a provider which instantiates objects of the specified bean class.
049 *
050 * @param beanClass the bean class
051 */
052 public BeanProvider( Class beanClass )
053 {
054 this.beanClass = beanClass;
055 }
056
057 //**********************************************************************************************************************
058 // ObjectProvider Implementation
059 //**********************************************************************************************************************
060
061 public Object getObject()
062 {
063 try
064 {
065 if( beanClass == null )
066 {
067 throw new ObjectProviderException("No bean class provided.");
068 }
069 return beanClass.newInstance();
070 }
071 catch( InstantiationException e )
072 {
073 throw new ObjectProviderException("Class " + beanClass.getName() + " is not concrete.", e);
074 }
075 catch( IllegalAccessException e )
076 {
077 throw new ObjectProviderException("Constructor for class " + beanClass.getName() + " is not accessible.",
078 e);
079 }
080 }
081
082 //**********************************************************************************************************************
083 // Getter/Setter Methods
084 //**********************************************************************************************************************
085
086 public void setBeanClass( Class beanClass )
087 {
088 this.beanClass = beanClass;
089 }
090 }
091