FactoryService.java
01 package net.sf.annocon.examples.configuration.basic;
02 
03 import java.util.Map;
04 
05 /**
06  * Example service, that creates objects. Which object to create is specified by
07  * a key value. The key value is mapped in the configuration to a classname.
08  
09  @author Achim Huegen
10  */
11 public class FactoryService
12 {
13   private Map _configuration;
14 
15   public FactoryService(Map configuration)
16   {
17     _configuration = configuration;
18   }
19 
20   /**
21    * Creates a new instance of the class that is configured for
22    <code>key</code>.
23    
24    @param key
25    @return new instance
26    */
27   public Object createInstance(String key)
28   {
29     String className = (String_configuration.get(key);
30     if (className == null)
31       throw new IllegalArgumentException("Unknown key: '" + key + "'");
32 
33     try {
34       Class clazz = Class.forName(className);
35       return clazz.newInstance();
36     catch (Exception e) {
37       throw new RuntimeException(e);
38     }
39   }
40 }