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.gjndi.binding;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    import java.util.regex.Matcher;
026    import java.util.regex.Pattern;
027    
028    import javax.naming.Name;
029    import javax.naming.NamingException;
030    
031    import org.apache.commons.logging.Log;
032    import org.apache.commons.logging.LogFactory;
033    import org.apache.geronimo.gbean.AbstractName;
034    import org.apache.geronimo.gbean.AbstractNameQuery;
035    import org.apache.geronimo.gbean.GBeanInfo;
036    import org.apache.geronimo.gbean.GBeanInfoBuilder;
037    import org.apache.geronimo.gjndi.KernelContextGBean;
038    import org.apache.geronimo.kernel.Kernel;
039    import org.apache.geronimo.kernel.repository.Artifact;
040    
041    /**
042     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
043     */
044    public class GBeanFormatBinding extends KernelContextGBean {
045        protected static final Log log = LogFactory.getLog(GBeanFormatBinding.class);
046        private static final Pattern PATTERN = Pattern.compile("(\\{)(\\w+)(})");
047    
048        protected final String format;
049        protected final Pattern namePattern;
050    
051        public GBeanFormatBinding(String format, String namePattern, String nameInNamespace, AbstractNameQuery abstractNameQuery, Kernel kernel) throws NamingException {
052            super(nameInNamespace, abstractNameQuery, kernel);
053            this.format = format;
054            if (namePattern != null && namePattern.length() > 0) {
055                this.namePattern = Pattern.compile(namePattern);
056            } else {
057                this.namePattern = null;
058            }
059        }
060    
061        @Override
062        protected Name createBindingName(AbstractName abstractName, Object value) throws NamingException {
063            String name = abstractName.getNameProperty("name");
064            if (namePattern != null) {
065                Matcher matcher = namePattern.matcher(name);
066                if (!matcher.matches()) {
067                    return null;
068                }
069            }
070            Map<String, String> map = new HashMap<String, String>(abstractName.getName());
071            Artifact artifact = abstractName.getArtifact();
072            map.put("groupId", artifact.getGroupId());
073            map.put("artifactId", artifact.getArtifactId());
074            map.put("version", artifact.getVersion().toString());
075            map.put("type", artifact.getType());
076            String fullName = format(format, map);
077            return getNameParser().parse(fullName);
078        }
079    
080        static String format(String input, Map<String, String> map) {
081            Matcher matcher = PATTERN.matcher(input);
082            StringBuffer buf = new StringBuffer();
083            while (matcher.find()) {
084                String key = matcher.group(2);
085                String value = map.get(key);
086                matcher.appendReplacement(buf, value);
087            }
088            matcher.appendTail(buf);
089            return buf.toString();
090        }
091    
092        public static final GBeanInfo GBEAN_INFO;
093    
094        static {
095            GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(GBeanFormatBinding.class, KernelContextGBean.GBEAN_INFO, "Context");
096            builder.addAttribute("format", String.class, true);
097            builder.addAttribute("namePattern", String.class, true);
098            builder.setConstructor(new String[]{"format", "namePattern", "nameInNamespace", "abstractNameQuery", "kernel"});
099            GBEAN_INFO = builder.getBeanInfo();
100        }
101    
102        public static GBeanInfo getGBeanInfo() {
103            return GBEAN_INFO;
104        }
105    
106    }