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.system.jmx; 018 019 import java.util.ArrayList; 020 import javax.management.MBeanServer; 021 import javax.management.MBeanServerFactory; 022 023 import org.apache.geronimo.gbean.GBeanInfo; 024 import org.apache.geronimo.gbean.GBeanInfoBuilder; 025 026 /** 027 * Creates a real mbean server of finds an existing one with the specified mbeanServerId 028 * @version $Rev: 551483 $ $Date: 2007-06-28 03:29:24 -0400 (Thu, 28 Jun 2007) $ 029 */ 030 public class RealMBeanServerReference implements MBeanServerReference { 031 private static final String GERONIMO_DEFAULT_DOMAIN = "geronimo"; 032 033 private MBeanServer mbeanServer; 034 035 public RealMBeanServerReference(String mbeanServerId) throws MBeanServerNotFound { 036 ArrayList servers = MBeanServerFactory.findMBeanServer(mbeanServerId); 037 if (servers.size() == 0) { 038 mbeanServer = MBeanServerFactory.createMBeanServer(GERONIMO_DEFAULT_DOMAIN); 039 } else if (servers.size() > 1) { 040 throw new MBeanServerNotFound(servers.size() + " MBeanServers were found with the agent id " + mbeanServerId); 041 } else { 042 mbeanServer = (MBeanServer) servers.get(0); 043 } 044 } 045 046 /** 047 * Finds an existing MBeanServer with default domain GERONIMO_DEFAULT_DOMAIN 048 * or creates a new one if there isn't any. 049 */ 050 public RealMBeanServerReference() { 051 // Find all MBeanServers 052 ArrayList<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null); 053 for(MBeanServer server: servers) { 054 // Look for one with default domain GERONIMO_DEFAULT_DOMAIN 055 if (GERONIMO_DEFAULT_DOMAIN.equals(server.getDefaultDomain())) { 056 mbeanServer = server; 057 break; 058 } 059 } 060 if(mbeanServer == null) { 061 // No MBeanServer with default domain GERONIMO_DEFAULT_DOMAIN exists. Create one. 062 mbeanServer = MBeanServerFactory.createMBeanServer(GERONIMO_DEFAULT_DOMAIN); 063 } 064 } 065 066 public MBeanServer getMBeanServer() { 067 return mbeanServer; 068 } 069 070 public static final GBeanInfo GBEAN_INFO; 071 072 static { 073 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(RealMBeanServerReference.class); 074 infoFactory.addAttribute("mbeanServerId", String.class, true); 075 //infoFactory.setConstructor(new String[]{"mbeanServerId"}); 076 GBEAN_INFO = infoFactory.getBeanInfo(); 077 } 078 079 public static GBeanInfo getGBeanInfo() { 080 return GBEAN_INFO; 081 } 082 }