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: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
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            return buildAbstractNameQueryFromPattern(pattern, "car", type, moduleType, interfaceTypes);
054        }
055        
056        public static AbstractNameQuery buildAbstractNameQueryFromPattern(GerPatternType pattern, String artifactType, String type, String moduleType, Set interfaceTypes)  {
057            String groupId = pattern.isSetGroupId() ? pattern.getGroupId().trim() : null;
058            String artifactid = pattern.isSetArtifactId() ? pattern.getArtifactId().trim() : null;
059            String version = pattern.isSetVersion() ? pattern.getVersion().trim() : null;
060            String module = pattern.isSetModule() ? pattern.getModule().trim() : null;
061            String name = pattern.getName().trim();
062    
063            Artifact artifact = artifactid != null ? new Artifact(groupId, artifactid, version, artifactType) : null;
064            Map nameMap = new HashMap();
065            nameMap.put("name", name);
066            if (type != null) {
067                nameMap.put("j2eeType", type);
068            }
069            if (module != null && moduleType != null) {
070                nameMap.put(moduleType, module);
071            }
072            if (interfaceTypes != null) {
073                Set trimmed = new HashSet();
074                for (Iterator it = interfaceTypes.iterator(); it.hasNext();) {
075                    String intf = (String) it.next();
076                    trimmed.add(intf == null ? null : intf.trim());
077                }
078                interfaceTypes = trimmed;
079            }
080            return new AbstractNameQuery(artifact, nameMap, interfaceTypes);
081        }
082    
083        public static AbstractNameQuery buildAbstractNameQuery(Artifact configId, String module, String name, String type, String moduleType) {
084            Map nameMap = new HashMap();
085            nameMap.put("name", name);
086            if (type != null) {
087                nameMap.put("j2eeType", type);
088            }
089            if (module != null && moduleType != null) {        
090                nameMap.put(moduleType, module);
091            }
092            return new AbstractNameQuery(configId, nameMap);
093        }
094    
095    }