1 87 package org.codehaus.loom.components.manager; 88 89 import java.util.HashMap ; 90 91 import org.codehaus.loom.interfaces.LoomException; 92 import org.codehaus.loom.interfaces.SystemManager; 93 import org.codehaus.spice.salt.i18n.ResourceManager; 94 import org.codehaus.spice.salt.i18n.Resources; 95 96 103 class SubContext 104 implements SystemManager 105 { 106 private static final Resources REZ = 107 ResourceManager.getPackageResources( SubContext.class ); 108 109 private static final String EMPTY_STRING = ""; 110 111 private final HashMap m_subcontexts = new HashMap (); 112 private final SystemManager m_parent; 113 private final String m_name; 114 private final String m_type; 115 116 124 public SubContext( final SystemManager parent, 125 final String name, 126 final String type ) 127 { 128 if( null == parent ) 129 { 130 throw new NullPointerException ( "parent" ); 131 } 132 m_parent = parent; 133 m_name = name; 134 m_type = type; 135 } 136 137 149 public void register( final String name, final Object object ) 150 throws LoomException, IllegalArgumentException  151 { 152 m_parent.register( jmxName( name ), object ); 153 } 154 155 162 public void unregister( final String name ) 163 throws LoomException 164 { 165 m_parent.unregister( jmxName( name ) ); 166 } 167 168 175 public SystemManager getSubContext( final String name, 176 final String type ) 177 throws LoomException 178 { 179 if( null == type || EMPTY_STRING.equals( type ) ) 180 { 181 final String message = 182 REZ.getString( "subcontext.error.no.subcontext" ); 183 throw new LoomException( message ); 184 } 185 else if( null != name && this.m_type == null ) 186 { 187 final String message = 188 REZ.getString( "subcontext.error.no.subcontext" ); 189 throw new LoomException( message ); 190 } 191 192 final String key = contextKey( name, type ); 194 SystemManager subcontext = 195 (SystemManager)m_subcontexts.get( key ); 196 197 if( subcontext == null ) 199 { 200 subcontext = new SubContext( this, name, type ); 201 m_subcontexts.put( key, subcontext ); 202 } 203 204 return subcontext; 205 } 206 207 211 private String jmxName( final String name ) 212 { 213 final StringBuffer sb = new StringBuffer (); 214 if( null != m_name ) 215 { 216 sb.append( m_name ); 217 sb.append( ',' ); 218 } 219 if( null != m_type ) 220 { 221 sb.append( m_type ); 222 sb.append( '=' ); 223 } 224 sb.append( name ); 225 226 return sb.toString(); 227 } 228 229 232 private String contextKey( final String parent, 233 final String type ) 234 { 235 return parent + "|" + type; 236 } 237 } 238 | Popular Tags |