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