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    package org.apache.geronimo.gjndi.binding;
018    
019    import javax.naming.NamingException;
020    import javax.naming.Name;
021    
022    import org.apache.geronimo.gbean.AbstractName;
023    import org.apache.geronimo.gbean.AbstractNameQuery;
024    import org.apache.geronimo.gbean.GBeanInfo;
025    import org.apache.geronimo.gbean.GBeanInfoBuilder;
026    import org.apache.geronimo.kernel.Kernel;
027    import org.apache.geronimo.naming.ResourceSource;
028    
029    /**
030     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
031     */
032    public class ResourceBinding extends GBeanFormatBinding {
033    
034        public ResourceBinding(String format, String namePattern, String nameInNamespace, AbstractNameQuery abstractNameQuery, Kernel kernel) throws NamingException {
035            super(format, namePattern, nameInNamespace, abstractNameQuery, kernel);
036        }
037    
038        /**
039         * Preprocess the value before it is bound.  This is usefult for wrapping values with reference objects.
040         * By default, this method simply return the value.
041         *
042         * @param abstractName the abstract name of the gbean to bind
043         * @param value        the gbean instance
044         * @return the value to bind  or null if there was a problem
045         */
046        @Override
047        protected Object preprocessVaue(AbstractName abstractName, Name name, Object value) {
048            if (!(value instanceof ResourceSource)) {
049                log.info("value at " + abstractName + " is not a ResourceSource: " + value.getClass().getName());
050                return null;
051            }
052            try {
053                return ((ResourceSource) value).$getResource();
054            } catch (Throwable throwable) {
055                log.info("Could not get resource from gbean at " + abstractName,throwable);
056                return null;
057            }
058        }
059    
060        public static final GBeanInfo GBEAN_INFO;
061    
062        static {
063            GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(ResourceBinding.class, GBeanFormatBinding.GBEAN_INFO, "Context");
064            GBEAN_INFO = builder.getBeanInfo();
065        }
066    
067        public static GBeanInfo getGBeanInfo() {
068            return GBEAN_INFO;
069        }
070    }