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    
018    package org.apache.geronimo.naming.deployment;
019    
020    import java.util.HashMap;
021    import java.util.HashSet;
022    import java.util.Iterator;
023    import java.util.Map;
024    import java.util.Set;
025    
026    import org.apache.geronimo.gbean.AbstractNameQuery;
027    import org.apache.geronimo.kernel.repository.Artifact;
028    import org.apache.geronimo.xbeans.geronimo.naming.GerGbeanLocatorType;
029    import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
030    
031    /**
032     * @version $Rev:385232 $ $Date: 2006-11-19 18:55:25 -0500 (Sun, 19 Nov 2006) $
033     */
034    public class ENCConfigBuilder {
035    
036        public static AbstractNameQuery getGBeanQuery(String j2eeType, GerGbeanLocatorType gerGbeanLocator) {
037            AbstractNameQuery abstractNameQuery;
038            if (gerGbeanLocator.isSetGbeanLink()) {
039                //exact match
040                String linkName = gerGbeanLocator.getGbeanLink().trim();
041                abstractNameQuery = buildAbstractNameQuery(null, null, linkName, j2eeType, null);
042    
043            } else {
044                GerPatternType patternType = gerGbeanLocator.getPattern();
045                //construct name from components
046                abstractNameQuery = buildAbstractNameQuery(patternType, j2eeType, null, null);
047            }
048            //TODO check that the query is satisfied.
049            return abstractNameQuery;
050        }
051    
052        public static AbstractNameQuery buildAbstractNameQuery(GerPatternType pattern, String type, String moduleType, Set interfaceTypes) {
053            String groupId = pattern.isSetGroupId() ? pattern.getGroupId().trim() : null;
054            String artifactid = pattern.isSetArtifactId() ? pattern.getArtifactId().trim() : null;
055            String version = pattern.isSetVersion() ? pattern.getVersion().trim() : null;
056            String module = pattern.isSetModule() ? pattern.getModule().trim() : null;
057            String name = pattern.getName().trim();
058    
059            Artifact artifact = artifactid != null ? new Artifact(groupId, artifactid, version, "car") : null;
060            Map nameMap = new HashMap();
061            nameMap.put("name", name);
062            if (type != null) {
063                nameMap.put("j2eeType", type);
064            }
065            if (module != null && moduleType != null) {
066                nameMap.put(moduleType, module);
067            }
068            if(interfaceTypes != null) {
069                Set trimmed = new HashSet();
070                for (Iterator it = interfaceTypes.iterator(); it.hasNext();) {
071                    String intf = (String) it.next();
072                    trimmed.add(intf == null ? null : intf.trim());
073                }
074                interfaceTypes = trimmed;
075            }
076            return new AbstractNameQuery(artifact, nameMap, interfaceTypes);
077        }
078    
079        public static AbstractNameQuery buildAbstractNameQuery(Artifact configId, String module, String name, String type, String moduleType) {
080            Map nameMap = new HashMap();
081            nameMap.put("name", name);
082            if (type != null) {
083                nameMap.put("j2eeType", type);
084            }
085            if (module != null) {
086                nameMap.put(moduleType, module);
087            }
088            return new AbstractNameQuery(configId, nameMap);
089        }
090    
091    }