1 8 package org.apache.avalon.phoenix.components.manager; 9 10 import java.util.HashMap ; 11 import org.apache.avalon.excalibur.i18n.ResourceManager; 12 import org.apache.avalon.excalibur.i18n.Resources; 13 import org.apache.avalon.phoenix.interfaces.ManagerException; 14 import org.apache.avalon.phoenix.interfaces.SystemManager; 15 16 23 class SubContext 24 implements SystemManager 25 { 26 private static final Resources REZ = 27 ResourceManager.getPackageResources( SubContext.class ); 28 29 private static final String EMPTY_STRING = ""; 30 31 private final HashMap m_subcontexts = new HashMap (); 32 private final SystemManager m_parent; 33 private final String m_name; 34 private final String m_type; 35 36 44 public SubContext( final SystemManager parent, 45 final String name, 46 final String type ) 47 { 48 if( null == parent ) 49 { 50 throw new NullPointerException ( "parent" ); 51 } 52 m_parent = parent; 53 m_name = name; 54 m_type = type; 55 } 56 57 71 public void register( String name, Object object, Class [] interfaces ) 72 throws ManagerException, IllegalArgumentException 73 { 74 m_parent.register( jmxName( name ), object, interfaces ); 75 } 76 77 88 public void register( String name, Object object ) 89 throws ManagerException, IllegalArgumentException 90 { 91 m_parent.register( jmxName( name ), object ); 92 } 93 94 100 public void unregister( String name ) 101 throws ManagerException 102 { 103 m_parent.unregister( jmxName( name ) ); 104 } 105 106 113 public SystemManager getSubContext( final String name, 114 final String type ) 115 throws ManagerException 116 { 117 if( null == type || EMPTY_STRING.equals( type ) ) 118 { 119 final String message = 120 REZ.getString( "subcontext.error.no.subcontext" ); 121 throw new ManagerException( message ); 122 } 123 else if( null != name && this.m_type == null ) 124 { 125 final String message = 126 REZ.getString( "subcontext.error.no.subcontext" ); 127 throw new ManagerException( message ); 128 } 129 130 final String key = contextKey( name, type ); 132 SystemManager subcontext = 133 (SystemManager)m_subcontexts.get( key ); 134 135 if( subcontext == null ) 137 { 138 subcontext = new SubContext( this, name, type ); 139 m_subcontexts.put( key, subcontext ); 140 } 141 142 return subcontext; 143 } 144 145 149 private String jmxName( final String name ) 150 { 151 final StringBuffer sb = new StringBuffer (); 152 if( null != m_name ) 153 { 154 sb.append( m_name ); 155 sb.append( ',' ); 156 } 157 if( null != m_type ) 158 { 159 sb.append( m_type ); 160 sb.append( '=' ); 161 } 162 sb.append( name ); 163 164 return sb.toString(); 165 } 166 167 171 private String contextKey( final String parent, 172 final String type ) 173 { 174 return parent + "|" + type; 175 } 176 } 177 | Popular Tags |