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: 555771 $ $Date: 2007-07-12 17:22:48 -0400 (Thu, 12 Jul 2007) $
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            if (name == null) {
070                throw new NullPointerException("No name supplied");
071            }
072            if (type == null) {
073                throw new NullPointerException("No type supplied");
074            }
075            Map nameMap = new HashMap(parentAbstractName.getName());
076    
077            String parentType = (String) nameMap.remove(J2EE_TYPE);
078            String parentName = (String) nameMap.remove(J2EE_NAME);
079            if (parentType == null) {
080                throw new IllegalArgumentException("parent name must have a j2eeType name component");
081            }
082            if (INVALID_GENERIC_PARENT_TYPE.equals(parentType)) {
083                throw new IllegalArgumentException("You can't create a child of a generic typed gbean");
084            }
085            nameMap.put(parentType, parentName);
086            nameMap.put(J2EE_TYPE, type);
087            nameMap.put(J2EE_NAME, name);
088    
089            return new AbstractName(artifact,
090                    nameMap,
091                    createObjectName(nameMap));
092        }
093    
094        /**
095         * @deprecated objectnames are being removed
096         */
097        public static ObjectName createObjectName(Map nameMap) {
098            Hashtable objectNameMap = new Hashtable(nameMap);
099            String type = (String) nameMap.get(J2EE_TYPE);
100            if ("JVM".equals(type)) {
101                objectNameMap.keySet().retainAll(Arrays.asList(new String[] {J2EE_TYPE, J2EE_NAME, "J2EEServer"}));
102                objectNameMap.put("J2EEServer", DEFAULT_SERVER_NAME);
103            } else if ("J2EEDomain".equals(type)) {
104                //special case J2EEDomain gbean
105                objectNameMap.clear();
106                objectNameMap.put(J2EE_TYPE, "J2EEDomain");
107                objectNameMap.put(J2EE_NAME, DEFAULT_DOMAIN_NAME);
108            } else if ("J2EEServer".equals(type)) {
109                //special case J2EEServer gbean
110                objectNameMap.clear();
111                objectNameMap.put(J2EE_TYPE, "J2EEServer");
112                objectNameMap.put(J2EE_NAME, DEFAULT_SERVER_NAME);
113            } else {
114                objectNameMap.put("J2EEServer", DEFAULT_SERVER_NAME);
115            }
116    
117            ObjectName moduleObjectName;
118            try {
119                moduleObjectName = ObjectName.getInstance(DEFAULT_DOMAIN_NAME, objectNameMap);
120            } catch (MalformedObjectNameException e) {
121                throw new AssertionError(e);
122            }
123            return moduleObjectName;
124        }
125    
126    }