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    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.Arrays;
023    
024    import javax.management.MalformedObjectNameException;
025    import javax.management.ObjectName;
026    
027    import org.apache.geronimo.gbean.AbstractName;
028    import org.apache.geronimo.kernel.repository.Artifact;
029    
030    /**
031     * @version $Rev: 487175 $ $Date: 2006-12-14 03:10:31 -0800 (Thu, 14 Dec 2006) $
032     */
033    public class Jsr77Naming extends Naming {
034        private static final String DEFAULT_DOMAIN_NAME = "geronimo";
035        private static final String DEFAULT_SERVER_NAME = "geronimo";
036        private static final String J2EE_TYPE = "j2eeType";
037        private static final String J2EE_NAME = "name";
038        private static final String INVALID_GENERIC_PARENT_TYPE = "GBean";
039    
040        public Jsr77Naming() {
041        }
042    
043        public AbstractName createRootName(Artifact artifact, String name, String type) {
044            Map nameMap = new HashMap();
045            nameMap.put(J2EE_TYPE, type);
046            nameMap.put(J2EE_NAME, name);
047    
048            return new AbstractName(artifact,
049                    nameMap,
050                    createObjectName(nameMap));
051        }
052    
053        public AbstractName createChildName(AbstractName parentAbstractName, String name, String type) {
054            return createChildName(parentAbstractName, parentAbstractName.getArtifact(), name, type);
055        }
056    
057        public AbstractName createSiblingName(AbstractName parentAbstractName, String name, String type) {
058            Map nameMap = new HashMap(parentAbstractName.getName());
059    
060            nameMap.put(J2EE_TYPE, type);
061            nameMap.put(J2EE_NAME, name);
062    
063            return new AbstractName(parentAbstractName.getArtifact(),
064                    nameMap,
065                    createObjectName(nameMap));
066        }
067    
068        public AbstractName createChildName(AbstractName parentAbstractName, Artifact artifact, String name, String type) {
069            Map nameMap = new HashMap(parentAbstractName.getName());
070    
071            String parentType = (String) nameMap.remove(J2EE_TYPE);
072            String parentName = (String) nameMap.remove(J2EE_NAME);
073            if (INVALID_GENERIC_PARENT_TYPE.equals(parentType)) {
074                throw new IllegalArgumentException("You can't create a child of a generic typed gbean");
075            }
076            nameMap.put(parentType, parentName);
077            nameMap.put(J2EE_TYPE, type);
078            nameMap.put(J2EE_NAME, name);
079    
080            return new AbstractName(artifact,
081                    nameMap,
082                    createObjectName(nameMap));
083        }
084    
085        /**
086         * @deprecated objectnames are being removed
087         */
088        public static ObjectName createObjectName(Map nameMap) {
089            Hashtable objectNameMap = new Hashtable(nameMap);
090            String type = (String) nameMap.get(J2EE_TYPE);
091            if ("JVM".equals(type)) {
092                objectNameMap.keySet().retainAll(Arrays.asList(new String[] {J2EE_TYPE, J2EE_NAME, "J2EEServer"}));
093                objectNameMap.put("J2EEServer", DEFAULT_SERVER_NAME);
094            } else if ("J2EEDomain".equals(type)) {
095                //special case J2EEDomain gbean
096                objectNameMap.clear();
097                objectNameMap.put(J2EE_TYPE, "J2EEDomain");
098                objectNameMap.put(J2EE_NAME, DEFAULT_DOMAIN_NAME);
099            } else if ("J2EEServer".equals(type)) {
100                //special case J2EEServer gbean
101                objectNameMap.clear();
102                objectNameMap.put(J2EE_TYPE, "J2EEServer");
103                objectNameMap.put(J2EE_NAME, DEFAULT_SERVER_NAME);
104            } else {
105                objectNameMap.put("J2EEServer", DEFAULT_SERVER_NAME);
106            }
107    
108            ObjectName moduleObjectName;
109            try {
110                moduleObjectName = ObjectName.getInstance(DEFAULT_DOMAIN_NAME, objectNameMap);
111            } catch (MalformedObjectNameException e) {
112                throw new AssertionError(e);
113            }
114            return moduleObjectName;
115        }
116    
117    }