1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.geronimo.kernel; 18 19 import java.util.HashMap; 20 import java.util.Hashtable; 21 import java.util.Map; 22 import java.util.Arrays; 23 24 import javax.management.MalformedObjectNameException; 25 import javax.management.ObjectName; 26 27 import org.apache.geronimo.gbean.AbstractName; 28 import org.apache.geronimo.kernel.repository.Artifact; 29 30 /** 31 * @version $Rev: 674745 $ $Date: 2008-07-08 01:59:46 -0700 (Tue, 08 Jul 2008) $ 32 */ 33 public class Jsr77Naming extends Naming { 34 private static final String DEFAULT_DOMAIN_NAME = "geronimo"; 35 private static final String DEFAULT_SERVER_NAME = "geronimo"; 36 public static final String J2EE_TYPE = "j2eeType"; 37 public static final String J2EE_NAME = "name"; 38 private static final String INVALID_GENERIC_PARENT_TYPE = "GBean"; 39 40 public Jsr77Naming() { 41 } 42 43 public AbstractName createRootName(Artifact artifact, String name, String type) { 44 Map nameMap = new HashMap(); 45 nameMap.put(J2EE_TYPE, type); 46 nameMap.put(J2EE_NAME, name); 47 48 return new AbstractName(artifact, 49 nameMap, 50 createObjectName(nameMap)); 51 } 52 53 public AbstractName createChildName(AbstractName parentAbstractName, String name, String type) { 54 return createChildName(parentAbstractName, parentAbstractName.getArtifact(), name, type); 55 } 56 57 public AbstractName createSiblingName(AbstractName parentAbstractName, String name, String type) { 58 Map nameMap = new HashMap(parentAbstractName.getName()); 59 60 nameMap.put(J2EE_TYPE, type); 61 nameMap.put(J2EE_NAME, name); 62 63 return new AbstractName(parentAbstractName.getArtifact(), 64 nameMap, 65 createObjectName(nameMap)); 66 } 67 68 public AbstractName createChildName(AbstractName parentAbstractName, Artifact artifact, String name, String type) { 69 if (name == null) { 70 throw new NullPointerException("No name supplied"); 71 } 72 if (type == null) { 73 throw new NullPointerException("No type supplied"); 74 } 75 Map nameMap = new HashMap(parentAbstractName.getName()); 76 77 String parentType = (String) nameMap.remove(J2EE_TYPE); 78 String parentName = (String) nameMap.remove(J2EE_NAME); 79 if (parentType == null) { 80 throw new IllegalArgumentException("parent name must have a j2eeType name component"); 81 } 82 if (INVALID_GENERIC_PARENT_TYPE.equals(parentType)) { 83 throw new IllegalArgumentException("You can't create a child of a generic typed gbean"); 84 } 85 nameMap.put(parentType, parentName); 86 nameMap.put(J2EE_TYPE, type); 87 nameMap.put(J2EE_NAME, name); 88 89 return new AbstractName(artifact, 90 nameMap, 91 createObjectName(nameMap)); 92 } 93 94 /** 95 * @deprecated objectnames are being removed 96 */ 97 public static ObjectName createObjectName(Map nameMap) { 98 Hashtable objectNameMap = new Hashtable(nameMap); 99 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 }