/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */


package org.apache.geronimo.tomcat;


import java.lang.reflect.InvocationTargetException;

import javax.naming.Context;
import javax.naming.NamingException;

import org.apache.tomcat.InstanceManager;
import org.apache.geronimo.j2ee.annotation.Holder;

/**
 * @version $Rev$ $Date$
 */
public class TomcatInstanceManager implements InstanceManager {

    private final Holder holder;
    private final ClassLoader classLoader;
    private final Context context;


    public TomcatInstanceManager(Holder holder, ClassLoader classLoader, Context context) {
        this.holder = holder;
        this.classLoader = classLoader;
        this.context = context;
    }

    public Object newInstance(String fqcn, ClassLoader classLoader) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException, ClassNotFoundException {
        return holder.newInstance(fqcn, classLoader, context);
    }
	
    public Object newInstance(String className) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException, ClassNotFoundException {
        return holder.newInstance(className, classLoader, context);
    }

    public void destroyInstance(Object o) throws IllegalAccessException, InvocationTargetException {
        try {
            holder.destroyInstance(o);
        } catch (Exception e) {
            throw new InvocationTargetException(e, "Attempted to destroy instance");
        }
    }
    
    public void newInstance(Object o) throws IllegalAccessException,  InvocationTargetException, NamingException {
	throw new UnsupportedOperationException("separate instantiation and injection is not supported");
    }
}

@@ -1650,32 +1650,50 @@
 oundException %7B%0A
+        try %7B%0A    
         return h
@@ -1743,15 +1743,274 @@
 t);%0A
+        %7D catch (IllegalAccessException e) %7B%0A            throw e;%0A        %7D catch (InstantiationException e) %7B%0A            throw e;%0A        %7D catch (Exception e) %7B%0A            throw (InstantiationException) new InstantiationException().initCause(e);%0A        %7D%0A
     %7D%0A
-%09
 %0A   
@@ -2170,32 +2170,50 @@
 oundException %7B%0A
+        try %7B%0A    
         return h
@@ -2260,24 +2260,284 @@
 , context);%0A
+        %7D catch (IllegalAccessException e) %7B%0A            throw e;%0A        %7D catch (InstantiationException e) %7B%0A            throw e;%0A        %7D catch (Exception e) %7B%0A            throw (InstantiationException) new InstantiationException().initCause(e);%0A        %7D%0A
     %7D%0A%0A    p
@@ -2818,20 +2818,16 @@
 %7D%0A    %7D%0A
-    
 %0A    pub
@@ -2887,17 +2887,16 @@
 ception,
- 
  Invocat
@@ -2937,106 +2937,514 @@
 n %7B%0A
-%09throw new UnsupportedOperationException(%22separate instantiation and injection is not supported%22);
+        //Spec 4.4.3.5 from my understanding, there are two scenario that current method is invoked,%0A        //a.  The users use create*** method to create Servlet/Filter/Listener to create the instance, then use add***(String name, ***  instance)%0A        //b. The users create the instances by themselves, then use add***(String name, *** instance)%0A        //For a, we should have done the resource injections, for b, we are not need to do the resource injections%0A        //Correct me if I miss anything !
 %0A   

Convert RuntimeException to InstantiationException, and remove the UnsupportedOperationException thrown by newIntance method