001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.enhance;
016
017 import java.lang.reflect.Constructor;
018
019 import org.apache.hivemind.ApplicationRuntimeException;
020 import org.apache.hivemind.Location;
021 import org.apache.hivemind.util.Defense;
022 import org.apache.tapestry.services.ComponentConstructor;
023
024 /**
025 * @author Howard M. Lewis Ship
026 * @since 4.0
027 */
028 public class ComponentConstructorImpl implements ComponentConstructor
029 {
030 private Location _location;
031
032 private Constructor _constructor;
033
034 private Object[] _parameters;
035
036 private String _classFabString;
037
038 /**
039 * News instance of this class.
040 *
041 * @param constructor
042 * the constructor method to invoke
043 * @param parameters
044 * the parameters to pass to the constructor. These are retained, not copied.
045 * @param classFabString
046 * a string representing the generated class information, used for exception
047 * reporting
048 * @param location
049 * the location, used for exception reporting
050 */
051
052 public ComponentConstructorImpl(Constructor constructor, Object[] parameters,
053 String classFabString, Location location)
054 {
055 Defense.notNull(constructor, "constructor");
056 _constructor = constructor;
057 _parameters = parameters;
058 _classFabString = classFabString;
059 _location = location;
060 }
061
062 public Class getComponentClass()
063 {
064 return _constructor.getDeclaringClass();
065 }
066
067 public Object newInstance()
068 {
069 try
070 {
071 Object result = _constructor.newInstance(_parameters);
072
073 // Unlikely to generate an error if we can get through it once, so it's
074 // safe to release the big classFabString
075
076 _classFabString = null;
077
078 return result;
079 }
080 catch (Throwable ex)
081 {
082 throw new ApplicationRuntimeException(EnhanceMessages.instantiationFailure(
083 _constructor,
084 _parameters,
085 _classFabString,
086 ex), null, _location, ex);
087 }
088 }
089
090 }