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: */package org.apache.geronimo.kernel;
017:
018: import java.util.HashMap;
019: import java.util.Hashtable;
020: import java.util.Map;
021: import java.util.Arrays;
022:
023: import javax.management.MalformedObjectNameException;
024: import javax.management.ObjectName;
025:
026: import org.apache.geronimo.gbean.AbstractName;
027: import org.apache.geronimo.kernel.repository.Artifact;
028:
029: /**
030: * @version $Rev: 555771 $ $Date: 2007-07-12 14:22:48 -0700 (Thu, 12 Jul 2007) $
031: */
032: public class Jsr77Naming extends Naming {
033: private static final String DEFAULT_DOMAIN_NAME = "geronimo";
034: private static final String DEFAULT_SERVER_NAME = "geronimo";
035: private static final String J2EE_TYPE = "j2eeType";
036: private static final String J2EE_NAME = "name";
037: private static final String INVALID_GENERIC_PARENT_TYPE = "GBean";
038:
039: public Jsr77Naming() {
040: }
041:
042: public AbstractName createRootName(Artifact artifact, String name,
043: 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, nameMap,
049: createObjectName(nameMap));
050: }
051:
052: public AbstractName createChildName(
053: AbstractName parentAbstractName, String name, String type) {
054: return createChildName(parentAbstractName, parentAbstractName
055: .getArtifact(), name, type);
056: }
057:
058: public AbstractName createSiblingName(
059: AbstractName parentAbstractName, String name, String type) {
060: Map nameMap = new HashMap(parentAbstractName.getName());
061:
062: nameMap.put(J2EE_TYPE, type);
063: nameMap.put(J2EE_NAME, name);
064:
065: return new AbstractName(parentAbstractName.getArtifact(),
066: nameMap, createObjectName(nameMap));
067: }
068:
069: public AbstractName createChildName(
070: AbstractName parentAbstractName, Artifact artifact,
071: String name, String type) {
072: if (name == null) {
073: throw new NullPointerException("No name supplied");
074: }
075: if (type == null) {
076: throw new NullPointerException("No type supplied");
077: }
078: Map nameMap = new HashMap(parentAbstractName.getName());
079:
080: String parentType = (String) nameMap.remove(J2EE_TYPE);
081: String parentName = (String) nameMap.remove(J2EE_NAME);
082: if (parentType == null) {
083: throw new IllegalArgumentException(
084: "parent name must have a j2eeType name component");
085: }
086: if (INVALID_GENERIC_PARENT_TYPE.equals(parentType)) {
087: throw new IllegalArgumentException(
088: "You can't create a child of a generic typed gbean");
089: }
090: nameMap.put(parentType, parentName);
091: nameMap.put(J2EE_TYPE, type);
092: nameMap.put(J2EE_NAME, name);
093:
094: return new AbstractName(artifact, nameMap,
095: createObjectName(nameMap));
096: }
097:
098: /**
099: * @deprecated objectnames are being removed
100: */
101: public static ObjectName createObjectName(Map nameMap) {
102: Hashtable objectNameMap = new Hashtable(nameMap);
103: String type = (String) nameMap.get(J2EE_TYPE);
104: if ("JVM".equals(type)) {
105: objectNameMap.keySet().retainAll(
106: Arrays.asList(new String[] { J2EE_TYPE, J2EE_NAME,
107: "J2EEServer" }));
108: objectNameMap.put("J2EEServer", DEFAULT_SERVER_NAME);
109: } else if ("J2EEDomain".equals(type)) {
110: //special case J2EEDomain gbean
111: objectNameMap.clear();
112: objectNameMap.put(J2EE_TYPE, "J2EEDomain");
113: objectNameMap.put(J2EE_NAME, DEFAULT_DOMAIN_NAME);
114: } else if ("J2EEServer".equals(type)) {
115: //special case J2EEServer gbean
116: objectNameMap.clear();
117: objectNameMap.put(J2EE_TYPE, "J2EEServer");
118: objectNameMap.put(J2EE_NAME, DEFAULT_SERVER_NAME);
119: } else {
120: objectNameMap.put("J2EEServer", DEFAULT_SERVER_NAME);
121: }
122:
123: ObjectName moduleObjectName;
124: try {
125: moduleObjectName = ObjectName.getInstance(
126: DEFAULT_DOMAIN_NAME, objectNameMap);
127: } catch (MalformedObjectNameException e) {
128: throw new AssertionError(e);
129: }
130: return moduleObjectName;
131: }
132:
133: }
|