001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  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, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.naming.deployment;
020    
021    import java.net.URI;
022    import java.net.URL;
023    import java.util.HashMap;
024    import java.util.HashSet;
025    import java.util.Iterator;
026    import java.util.Map;
027    import java.util.Set;
028    
029    import org.apache.geronimo.common.DeploymentException;
030    import org.apache.geronimo.gbean.AbstractNameQuery;
031    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
032    import org.apache.geronimo.kernel.repository.Artifact;
033    import org.apache.geronimo.xbeans.geronimo.naming.GerGbeanLocatorType;
034    import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
035    import org.apache.geronimo.xbeans.geronimo.naming.GerResourceRefType;
036    import org.apache.geronimo.xbeans.j2ee.ResourceRefType;
037    
038    /**
039     * @version $Rev:385232 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
040     */
041    public class ENCConfigBuilder {
042    
043        public static AbstractNameQuery getGBeanQuery(String j2eeType, GerGbeanLocatorType gerGbeanLocator) {
044            AbstractNameQuery abstractNameQuery;
045            if (gerGbeanLocator.isSetGbeanLink()) {
046                //exact match
047                String linkName = gerGbeanLocator.getGbeanLink().trim();
048                abstractNameQuery = buildAbstractNameQuery(null, null, linkName, j2eeType, null);
049    
050            } else {
051                GerPatternType patternType = gerGbeanLocator.getPattern();
052                //construct name from components
053                abstractNameQuery = buildAbstractNameQuery(patternType, j2eeType, null, null);
054            }
055            //TODO check that the query is satisfied.
056            return abstractNameQuery;
057        }
058    
059        public static AbstractNameQuery buildAbstractNameQuery(GerPatternType pattern, String type, String moduleType, Set interfaceTypes) {
060            String groupId = pattern.isSetGroupId() ? pattern.getGroupId().trim() : null;
061            String artifactid = pattern.isSetArtifactId() ? pattern.getArtifactId().trim() : null;
062            String version = pattern.isSetVersion() ? pattern.getVersion().trim() : null;
063            String module = pattern.isSetModule() ? pattern.getModule().trim() : null;
064            String name = pattern.getName().trim();
065    
066            Artifact artifact = artifactid != null ? new Artifact(groupId, artifactid, version, "car") : null;
067            Map nameMap = new HashMap();
068            nameMap.put("name", name);
069            if (type != null) {
070                nameMap.put("j2eeType", type);
071            }
072            if (module != null && moduleType != null) {
073                nameMap.put(moduleType, module);
074            }
075            if(interfaceTypes != null) {
076                Set trimmed = new HashSet();
077                for (Iterator it = interfaceTypes.iterator(); it.hasNext();) {
078                    String intf = (String) it.next();
079                    trimmed.add(intf == null ? null : intf.trim());
080                }
081                interfaceTypes = trimmed;
082            }
083            return new AbstractNameQuery(artifact, nameMap, interfaceTypes);
084        }
085    
086        public static AbstractNameQuery buildAbstractNameQuery(Artifact configId, String module, String name, String type, String moduleType) {
087            Map nameMap = new HashMap();
088            nameMap.put("name", name);
089            if (type != null) {
090                nameMap.put("j2eeType", type);
091            }
092            if (module != null) {
093                nameMap.put(moduleType, module);
094            }
095            return new AbstractNameQuery(configId, nameMap);
096        }
097    
098    }