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: package org.apache.geronimo.jasper;
021:
022: import java.lang.reflect.InvocationTargetException;
023: import java.util.Map;
024:
025: import javax.naming.Context;
026: import javax.naming.NamingException;
027:
028: import org.apache.geronimo.gbean.GBeanInfo;
029: import org.apache.geronimo.gbean.GBeanInfoBuilder;
030: import org.apache.geronimo.j2ee.RuntimeCustomizer;
031: import org.apache.geronimo.j2ee.annotation.Holder;
032: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
033: import org.apache.InstanceManager;
034:
035: /**
036: * @version $Rev: 543827 $ $Date: 2007-06-02 19:05:28 -0700 (Sat, 02 Jun 2007) $
037: */
038: public class JasperServletContextCustomizer implements
039: RuntimeCustomizer {
040: private final Holder holder;
041:
042: static {
043: try {
044: Class clazz = Class
045: .forName("org.apache.geronimo.jdbc.DataSourceDriver");
046: clazz.newInstance();
047: } catch (Throwable e) {
048: //how can we notify?
049: }
050: }
051:
052: public JasperServletContextCustomizer(Holder holder) {
053: this .holder = holder;
054: }
055:
056: public void customize(Map<Class, Object> context) {
057: Map<String, Object> servletContext = (Map<String, Object>) context
058: .get(Map.class);
059: Context jndiContext = (Context) context.get(Context.class);
060: servletContext.put(InstanceManager.class.getName(),
061: new JasperInstanceManager(holder, jndiContext));
062: }
063:
064: public static class JasperInstanceManager implements
065: InstanceManager {
066: private final Holder holder;
067: private final Context context;
068:
069: public JasperInstanceManager(Holder holder, Context context) {
070: this .holder = holder;
071: this .context = context;
072: }
073:
074: public Object newInstance(String fqcn, ClassLoader classLoader)
075: throws IllegalAccessException,
076: InvocationTargetException, NamingException,
077: InstantiationException, ClassNotFoundException {
078: return holder.newInstance(fqcn, classLoader, context);
079: }
080:
081: public void destroyInstance(Object o)
082: throws IllegalAccessException,
083: InvocationTargetException {
084: try {
085: holder.destroyInstance(o);
086: } catch (Exception e) {
087: throw new InvocationTargetException(e,
088: "Attempted to destroy instance");
089: }
090: }
091:
092: public void newInstance(Object o)
093: throws IllegalAccessException,
094: InvocationTargetException, NamingException {
095: throw new UnsupportedOperationException(
096: "separate instantiation and injection is not supported");
097: }
098:
099: public Object newInstance(String fqcn)
100: throws IllegalAccessException,
101: InvocationTargetException, NamingException,
102: InstantiationException, ClassNotFoundException {
103: throw new UnsupportedOperationException(
104: "separate instantiation and injection is not supported");
105: }
106: }
107:
108: public static final GBeanInfo GBEAN_INFO;
109:
110: static {
111: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
112: JasperServletContextCustomizer.class,
113: NameFactory.GERONIMO_SERVICE);
114: infoBuilder.addAttribute("holder", Holder.class, true, true);
115: infoBuilder.setConstructor(new String[] { "holder" });
116:
117: GBEAN_INFO = infoBuilder.getBeanInfo();
118: }
119:
120: public static GBeanInfo getGBeanInfo() {
121: return GBEAN_INFO;
122: }
123: }
|