001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.jasper;
022    
023    import java.lang.reflect.InvocationTargetException;
024    import java.util.Map;
025    
026    import javax.naming.Context;
027    import javax.naming.NamingException;
028    
029    import org.apache.geronimo.gbean.GBeanInfo;
030    import org.apache.geronimo.gbean.GBeanInfoBuilder;
031    import org.apache.geronimo.j2ee.RuntimeCustomizer;
032    import org.apache.geronimo.j2ee.annotation.Holder;
033    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
034    import org.apache.InstanceManager;
035    
036    /**
037     * @version $Rev: 543827 $ $Date: 2007-06-02 22:05:28 -0400 (Sat, 02 Jun 2007) $
038     */
039    public class JasperServletContextCustomizer implements RuntimeCustomizer {
040        private final Holder holder;
041    
042        static {
043            try {
044                Class clazz = Class.forName("org.apache.geronimo.jdbc.DataSourceDriver");
045                clazz.newInstance();
046            } catch (Throwable e) {
047                //how can we notify?
048            }
049        }
050    
051        public JasperServletContextCustomizer(Holder holder) {
052            this.holder = holder;
053        }
054    
055        public void customize(Map<Class, Object> context) {
056            Map<String, Object> servletContext = (Map<String, Object>) context.get(Map.class);
057            Context jndiContext = (Context) context.get(Context.class);
058            servletContext.put(InstanceManager.class.getName(), new JasperInstanceManager(holder, jndiContext));
059        }
060    
061    
062        public static class JasperInstanceManager implements InstanceManager {
063            private final Holder holder;
064            private final Context context;
065    
066            public JasperInstanceManager(Holder holder, Context context) {
067                this.holder = holder;
068                this.context = context;
069            }
070    
071            public Object newInstance(String fqcn, ClassLoader classLoader) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException, ClassNotFoundException {
072                return holder.newInstance(fqcn, classLoader, context);
073            }
074    
075            public void destroyInstance(Object o) throws IllegalAccessException, InvocationTargetException {
076                try {
077                    holder.destroyInstance(o);
078                } catch (Exception e) {
079                    throw new InvocationTargetException(e, "Attempted to destroy instance");
080                }
081            }
082            
083            public void newInstance(Object o) throws IllegalAccessException, InvocationTargetException, NamingException {
084                throw new UnsupportedOperationException("separate instantiation and injection is not supported");
085            }
086    
087            public Object newInstance(String fqcn) throws IllegalAccessException, InvocationTargetException, NamingException,
088                    InstantiationException, ClassNotFoundException {
089                throw new UnsupportedOperationException("separate instantiation and injection is not supported");
090            }
091        }
092    
093        public static final GBeanInfo GBEAN_INFO;
094    
095        static {
096            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(JasperServletContextCustomizer.class, NameFactory.GERONIMO_SERVICE);
097            infoBuilder.addAttribute("holder", Holder.class, true, true);
098            infoBuilder.setConstructor(new String[] {"holder"});
099    
100            GBEAN_INFO = infoBuilder.getBeanInfo();
101        }
102    
103        public static GBeanInfo getGBeanInfo() {
104            return GBEAN_INFO;
105        }
106    }