View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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.HashSet;
23  import java.util.Arrays;
24  
25  import javax.management.MalformedObjectNameException;
26  import javax.management.ObjectName;
27  
28  import org.apache.geronimo.gbean.AbstractName;
29  import org.apache.geronimo.kernel.repository.Artifact;
30  
31  /**
32   * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
33   */
34  public class Jsr77Naming extends Naming {
35      private static final String DEFAULT_DOMAIN_NAME = "geronimo";
36      private static final String DEFAULT_SERVER_NAME = "geronimo";
37      private static final String J2EE_TYPE = "j2eeType";
38      private static final String J2EE_NAME = "name";
39      private static final String INVALID_GENERIC_PARENT_TYPE = "GBean";
40  
41      public Jsr77Naming() {
42      }
43  
44      public AbstractName createRootName(Artifact artifact, String name, String type) {
45          Map nameMap = new HashMap();
46          nameMap.put(J2EE_TYPE, type);
47          nameMap.put(J2EE_NAME, name);
48  
49          return new AbstractName(artifact,
50                  nameMap,
51                  createObjectName(nameMap));
52      }
53  
54      public AbstractName createChildName(AbstractName parentAbstractName, String name, String type) {
55          return createChildName(parentAbstractName, parentAbstractName.getArtifact(), name, type);
56      }
57  
58      public AbstractName createSiblingName(AbstractName parentAbstractName, String name, String type) {
59          Map nameMap = new HashMap(parentAbstractName.getName());
60  
61          nameMap.put(J2EE_TYPE, type);
62          nameMap.put(J2EE_NAME, name);
63  
64          return new AbstractName(parentAbstractName.getArtifact(),
65                  nameMap,
66                  createObjectName(nameMap));
67      }
68  
69      public AbstractName createChildName(AbstractName parentAbstractName, Artifact artifact, String name, String type) {
70          Map nameMap = new HashMap(parentAbstractName.getName());
71  
72          String parentType = (String) nameMap.remove(J2EE_TYPE);
73          String parentName = (String) nameMap.remove(J2EE_NAME);
74          if (INVALID_GENERIC_PARENT_TYPE.equals(parentType)) {
75              throw new IllegalArgumentException("You can't create a child of a generic typed gbean");
76          }
77          nameMap.put(parentType, parentName);
78          nameMap.put(J2EE_TYPE, type);
79          nameMap.put(J2EE_NAME, name);
80  
81          return new AbstractName(artifact,
82                  nameMap,
83                  createObjectName(nameMap));
84      }
85  
86      /**
87       * @deprecated objectnames are being removed
88       */
89      public static ObjectName createObjectName(Map nameMap) {
90          Hashtable objectNameMap = new Hashtable(nameMap);
91          String type = (String) nameMap.get(J2EE_TYPE);
92          if ("JVM".equals(type)) {
93              objectNameMap.keySet().retainAll(Arrays.asList(new String[] {J2EE_TYPE, J2EE_NAME, "J2EEServer"}));
94              objectNameMap.put("J2EEServer", DEFAULT_SERVER_NAME);
95          } else if ("J2EEDomain".equals(type)) {
96              //special case J2EEDomain gbean
97              objectNameMap.clear();
98              objectNameMap.put(J2EE_TYPE, "J2EEDomain");
99              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 }