1 50 package org.apache.excalibur.configuration; 51 52 import java.lang.reflect.Constructor ; 53 import java.util.Hashtable ; 54 import java.util.Map ; 55 56 import org.apache.avalon.framework.configuration.Configuration; 57 import org.apache.avalon.framework.configuration.ConfigurationException; 58 import org.apache.avalon.framework.context.Context; 59 import org.apache.avalon.framework.logger.Logger; 60 61 68 public class ContextFactory 69 { 70 71 75 81 public static Context createContextFromConfiguration( Configuration config ) 82 throws ConfigurationException 83 { 84 return createContextFromConfiguration( null, config ); 85 } 86 87 94 public static Context createContextFromConfiguration( 95 Context parent, Configuration config ) 96 throws ConfigurationException 97 { 98 return createContextFromConfiguration( parent, config, null ); 99 } 100 101 109 public static Context createContextFromConfiguration( 110 Context parent, Configuration config, Logger log ) 111 throws ConfigurationException 112 { 113 114 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 115 String contextClassName = config.getAttribute( 116 "class", 117 "org.apache.avalon.framework.context.DefaultContext" ); 118 119 Class contextClass = null; 120 121 try 122 { 123 contextClass = loader.loadClass( contextClassName ); 124 } 125 catch( ClassNotFoundException cnfe ) 126 { 127 throw new ConfigurationException( 128 "Could not find context class " + contextClassName, cnfe ); 129 } 130 131 Map map = new Hashtable (); 132 Context context = null; 133 try 134 { 135 Constructor constructor = contextClass.getConstructor( 136 new Class []{Map .class, Context.class} ); 137 context = (Context)constructor.newInstance( new Object []{map, parent} ); 138 } 139 catch( Throwable e ) 140 { 141 throw new ConfigurationException( 142 "Unexpected exception while creating custom context form " 143 + contextClassName, e ); 144 } 145 146 final Configuration[] entrys = config.getChildren( "entry" ); 147 148 for( int i = 0; i < entrys.length; i++ ) 149 { 150 final String className = entrys[ i ].getAttribute( 151 "type", "java.lang.String" ); 152 final String paramName = entrys[ i ].getAttribute( 153 "name", null ); 154 155 if( paramName == null ) 156 { 157 throw new ConfigurationException( 158 "missing name for context-entry" ); 159 } 160 161 try 162 { 163 Class [] params; 164 Object [] values; 165 Configuration entry = entrys[ i ]; 166 167 if( entry.getAttribute( "value", null ) != null ) 168 { 169 params = new Class [ 1 ]; 171 params[ 0 ] = Class.forName( "java.lang.String" ); 172 Class [] consObjects = {Class.forName( "java.lang.String" )}; 173 Constructor cons = params[ 0 ].getConstructor( consObjects ); 174 values = new Object [ 1 ]; 175 Object [] consValues = { 176 getContextValue( map, entry.getAttribute( "value" ) ) 177 }; 178 values[ 0 ] = cons.newInstance( consValues ); 179 180 if( log != null ) 181 { 182 log.debug( "add context-attr '" + paramName 183 + "' class '" + className 184 + "' with value '" + consValues[ 0 ] + "'" ); 185 } 186 } 187 else 188 { 189 Configuration[] entryChilds = entry.getChildren( "parameter" ); 191 192 params = new Class [ entryChilds.length ]; 193 values = new Object [ entryChilds.length ]; 194 195 if( log != null ) 196 { 197 log.debug( "add context-attr '" + paramName 198 + "' class '" + className + "' with " 199 + entryChilds.length + " values" ); 200 } 201 202 for( int p = 0; p < entryChilds.length; p++ ) 203 { 204 String paramClassName = entryChilds[ p ].getAttribute( 205 "type", "java.lang.String" ); 206 String paramValue = entryChilds[ p ].getAttribute( "value", null ); 207 208 if( paramValue == null ) 209 { 210 if( log != null ) 211 { 212 log.debug( "value" + ( p + 1 ) + ": class '" 213 + paramClassName + "' no value" ); 214 } 215 } 216 else 217 { 218 paramValue = getContextValue( map, paramValue ); 219 if( log != null ) 220 { 221 log.debug( "value" + ( p + 1 ) + ": class '" 222 + paramClassName + "' value '" + paramValue + "'" ); 223 } 224 } 225 226 try 227 { 228 params[ p ] = loader.loadClass( paramClassName ); 229 230 if( paramValue == null ) 231 { 232 values[ p ] = params[ p ].newInstance(); 233 } 234 else 235 { 236 Class [] consObjects = {Class.forName( "java.lang.String" )}; 237 Constructor cons = params[ p ].getConstructor( consObjects ); 238 Object [] consValues = {paramValue}; 239 values[ p ] = cons.newInstance( consValues ); 240 } 241 } 242 catch( ClassNotFoundException e ) 243 { 244 if( paramClassName.equals( "int" ) ) 247 { 248 params[ p ] = int.class; 249 values[ p ] = new Integer ( paramValue ); 250 } 251 else if( paramClassName.equals( "short" ) ) 252 { 253 params[ p ] = short.class; 254 values[ p ] = new Short ( paramValue ); 255 } 256 else if( paramClassName.equals( "long" ) ) 257 { 258 params[ p ] = long.class; 259 values[ p ] = new Long ( paramValue ); 260 } 261 else if( paramClassName.equals( "byte" ) ) 262 { 263 params[ p ] = byte.class; 264 values[ p ] = new Byte ( paramValue ); 265 } 266 else if( paramClassName.equals( "double" ) ) 267 { 268 params[ p ] = double.class; 269 values[ p ] = new Double ( paramValue ); 270 } 271 else if( paramClassName.equals( "float" ) ) 272 { 273 params[ p ] = float.class; 274 values[ p ] = new Float ( paramValue ); 275 } 276 else if( paramClassName.equals( "char" ) ) 277 { 278 params[ p ] = char.class; 279 values[ p ] = new Character ( paramValue.charAt( 0 ) ); 280 } 281 else if( paramClassName.equals( "boolean" ) ) 282 { 283 params[ p ] = boolean.class; 284 values[ p ] = new Boolean ( paramValue ); 285 } 286 else 287 { 288 throw new ConfigurationException( 289 "incorrect type '" + paramClassName 290 + "' for context-attribute '" + paramName + "'", e ); 291 } 292 } 293 } 294 } 295 296 Class paramClass; 297 try 298 { 299 paramClass = loader.loadClass( className ); 300 } 301 catch( final ClassNotFoundException e ) 302 { 303 throw new ConfigurationException( 304 "incorrect type '" + className 305 + "' for context-attribute '" + paramName + "'", 306 e ); 307 } 308 309 Object paramInstance; 310 311 if( params.length > 0 ) 312 { 313 Constructor cons = paramClass.getConstructor( params ); 315 paramInstance = cons.newInstance( values ); 316 } 317 else 318 { 319 paramInstance = paramClass.newInstance(); 321 } 322 323 map.put( paramName, paramInstance ); 324 } 325 catch( ConfigurationException e ) 326 { 327 throw e; 328 } 329 catch( Exception e ) 330 { 331 throw new ConfigurationException( 332 "Error add context-attribute '" + paramName 333 + "' from Configuration", e ); 334 } 335 } 336 return context; 337 } 338 339 347 private static String getContextValue( Map map, String rawValue ) 348 throws ConfigurationException 349 { 350 StringBuffer result = new StringBuffer ( "" ); 351 int i = 0; 352 int j = -1; 353 while( ( j = rawValue.indexOf( "${", i ) ) > -1 ) 354 { 355 if( i < j ) 356 { 357 result.append( rawValue.substring( i, j ) ); 358 } 359 int k = rawValue.indexOf( '}', j ); 360 final String ctxName = rawValue.substring( j + 2, k ); 361 final Object ctx = map.get( ctxName ); 362 if( ctx == null ) 363 { 364 throw new ConfigurationException( 365 "missing entry '" + ctxName + "' in Context" ); 366 } 367 result.append( ctx.toString() ); 368 i = k + 1; 369 } 370 if( i < rawValue.length() ) 371 { 372 result.append( rawValue.substring( i, rawValue.length() ) ); 373 } 374 return result.toString(); 375 } 376 377 } 378 379 380 381 | Popular Tags |