001    /**
002     *
003     * Copyright 2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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    package org.apache.geronimo.kernel;
018    
019    import java.util.HashMap;
020    import java.util.Hashtable;
021    import java.util.Map;
022    import java.util.HashSet;
023    import java.util.Arrays;
024    
025    import javax.management.MalformedObjectNameException;
026    import javax.management.ObjectName;
027    
028    import org.apache.geronimo.gbean.AbstractName;
029    import org.apache.geronimo.kernel.repository.Artifact;
030    
031    /**
032     * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
033     */
034    public class Jsr77Naming extends Naming {
035        private static final String DEFAULT_DOMAIN_NAME = "geronimo";
036        private static final String DEFAULT_SERVER_NAME = "geronimo";
037        private static final String J2EE_TYPE = "j2eeType";
038        private static final String J2EE_NAME = "name";
039        private static final String INVALID_GENERIC_PARENT_TYPE = "GBean";
040    
041        public Jsr77Naming() {
042        }
043    
044        public AbstractName createRootName(Artifact artifact, String name, String type) {
045            Map nameMap = new HashMap();
046            nameMap.put(J2EE_TYPE, type);
047            nameMap.put(J2EE_NAME, name);
048    
049            return new AbstractName(artifact,
050                    nameMap,
051                    createObjectName(nameMap));
052        }
053    
054        public AbstractName createChildName(AbstractName parentAbstractName, String name, String type) {
055            return createChildName(parentAbstractName, parentAbstractName.getArtifact(), name, type);
056        }
057    
058        public AbstractName createSiblingName(AbstractName parentAbstractName, String name, String type) {
059            Map nameMap = new HashMap(parentAbstractName.getName());
060    
061            nameMap.put(J2EE_TYPE, type);
062            nameMap.put(J2EE_NAME, name);
063    
064            return new AbstractName(parentAbstractName.getArtifact(),
065                    nameMap,
066                    createObjectName(nameMap));
067        }
068    
069        public AbstractName createChildName(AbstractName parentAbstractName, Artifact artifact, String name, String type) {
070            Map nameMap = new HashMap(parentAbstractName.getName());
071    
072            String parentType = (String) nameMap.remove(J2EE_TYPE);
073            String parentName = (String) nameMap.remove(J2EE_NAME);
074            if (INVALID_GENERIC_PARENT_TYPE.equals(parentType)) {
075                throw new IllegalArgumentException("You can't create a child of a generic typed gbean");
076            }
077            nameMap.put(parentType, parentName);
078            nameMap.put(J2EE_TYPE, type);
079            nameMap.put(J2EE_NAME, name);
080    
081            return new AbstractName(artifact,
082                    nameMap,
083                    createObjectName(nameMap));
084        }
085    
086        /**
087         * @deprecated objectnames are being removed
088         */
089        public static ObjectName createObjectName(Map nameMap) {
090            Hashtable objectNameMap = new Hashtable(nameMap);
091            String type = (String) nameMap.get(J2EE_TYPE);
092            if ("JVM".equals(type)) {
093                objectNameMap.keySet().retainAll(Arrays.asList(new String[] {J2EE_TYPE, J2EE_NAME, "J2EEServer"}));
094                objectNameMap.put("J2EEServer", DEFAULT_SERVER_NAME);
095            } else if ("J2EEDomain".equals(type)) {
096                //special case J2EEDomain gbean
097                objectNameMap.clear();
098                objectNameMap.put(J2EE_TYPE, "J2EEDomain");
099                objectNameMap.put(J2EE_NAME, DEFAULT_DOMAIN_NAME);
100            } else if ("J2EEServer".equals(type)) {
101                //special case J2EEServer gbean
102                objectNameMap.clear();
103                objectNameMap.put(J2EE_TYPE, "J2EEServer");
104                objectNameMap.put(J2EE_NAME, DEFAULT_SERVER_NAME);
105            } else {
106                objectNameMap.put("J2EEServer", DEFAULT_SERVER_NAME);
107            }
108    
109            ObjectName moduleObjectName;
110            try {
111                moduleObjectName = ObjectName.getInstance(DEFAULT_DOMAIN_NAME, objectNameMap);
112            } catch (MalformedObjectNameException e) {
113                throw new AssertionError(e);
114            }
115            return moduleObjectName;
116        }
117    
118    }