Home » geronimo-2.2-source-release » org.apache.geronimo.gjndi.binding » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *  http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   
   21   package org.apache.geronimo.gjndi.binding;
   22   
   23   import java.util.HashMap;
   24   import java.util.Map;
   25   import java.util.regex.Matcher;
   26   import java.util.regex.Pattern;
   27   
   28   import javax.naming.Name;
   29   import javax.naming.NamingException;
   30   
   31   import org.slf4j.Logger;
   32   import org.slf4j.LoggerFactory;
   33   import org.apache.geronimo.gbean.AbstractName;
   34   import org.apache.geronimo.gbean.AbstractNameQuery;
   35   import org.apache.geronimo.gbean.GBeanInfo;
   36   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   37   import org.apache.geronimo.gjndi.KernelContextGBean;
   38   import org.apache.geronimo.kernel.Kernel;
   39   import org.apache.geronimo.kernel.repository.Artifact;
   40   
   41   /**
   42    * @version $Rev: 651912 $ $Date: 2008-04-27 03:20:35 -0700 (Sun, 27 Apr 2008) $
   43    */
   44   public class GBeanFormatBinding extends KernelContextGBean {
   45       protected static final Logger log = LoggerFactory.getLogger(GBeanFormatBinding.class);
   46       private static final Pattern PATTERN = Pattern.compile("(\\{)(\\w+)(})");
   47   
   48       protected final String format;
   49       protected final Pattern namePattern;
   50   
   51       public GBeanFormatBinding(String format, String namePattern, String nameInNamespace, AbstractNameQuery abstractNameQuery, Kernel kernel) throws NamingException {
   52           super(nameInNamespace, abstractNameQuery, kernel);
   53           this.format = format;
   54           if (namePattern != null && namePattern.length() > 0) {
   55               this.namePattern = Pattern.compile(namePattern);
   56           } else {
   57               this.namePattern = null;
   58           }
   59       }
   60   
   61       @Override
   62       protected Name createBindingName(AbstractName abstractName, Object value) throws NamingException {
   63           String name = abstractName.getNameProperty("name");
   64           if (namePattern != null) {
   65               Matcher matcher = namePattern.matcher(name);
   66               if (!matcher.matches()) {
   67                   return null;
   68               }
   69           }
   70           Map<String, String> map = new HashMap<String, String>(abstractName.getName());
   71           Artifact artifact = abstractName.getArtifact();
   72           map.put("groupId", artifact.getGroupId());
   73           map.put("artifactId", artifact.getArtifactId());
   74           map.put("version", artifact.getVersion().toString());
   75           map.put("type", artifact.getType());
   76           String fullName = format(format, map);
   77           return getNameParser().parse(fullName);
   78       }
   79   
   80       static String format(String input, Map<String, String> map) {
   81           Matcher matcher = PATTERN.matcher(input);
   82           StringBuffer buf = new StringBuffer();
   83           while (matcher.find()) {
   84               String key = matcher.group(2);
   85               String value = map.get(key);
   86               matcher.appendReplacement(buf, value);
   87           }
   88           matcher.appendTail(buf);
   89           return buf.toString();
   90       }
   91   
   92       public static final GBeanInfo GBEAN_INFO;
   93   
   94       static {
   95           GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(GBeanFormatBinding.class, KernelContextGBean.GBEAN_INFO, "Context");
   96           builder.addAttribute("format", String.class, true);
   97           builder.addAttribute("namePattern", String.class, true);
   98           builder.setConstructor(new String[]{"format", "namePattern", "nameInNamespace", "abstractNameQuery", "kernel"});
   99           GBEAN_INFO = builder.getBeanInfo();
  100       }
  101   
  102       public static GBeanInfo getGBeanInfo() {
  103           return GBEAN_INFO;
  104       }
  105   
  106   }

Home » geronimo-2.2-source-release » org.apache.geronimo.gjndi.binding » [javadoc | source]