01 package net.sf.annocon.examples.configuration.digester;
02
03 import java.util.HashMap;
04 import java.util.Map;
05
06 import net.sf.annocon.annotations.Configuration;
07 import net.sf.annocon.annotations.ConfigurationParser;
08 import net.sf.annocon.annotations.Context;
09 import net.sf.annocon.annotations.Contribution;
10 import net.sf.annocon.annotations.Service;
11 import net.sf.annocon.configuration.Parser;
12 import net.sf.annocon.configuration.digester.DigesterParser;
13 import net.sf.annocon.examples.configuration.basic.FactoryService;
14
15 import org.apache.commons.digester.Digester;
16
17 /**
18 * Example that demonstrates how to read configuration data using digester. Tdhe
19 * file based data is merged with hard coded data.
20 *
21 * @author Achim Huegen
22 */
23 @Context
24 public class DigesterContext
25 {
26 @Service(id = "FactoryService")
27 public FactoryService getFactoryService()
28 {
29 return new FactoryService(getFactoryConfig());
30 }
31
32 /**
33 * Defines the FactoryConfig. It contains key/class-name combinations.
34 *
35 * @return The configuration container, that contains the merged data of all
36 * contributions.
37 */
38 @Configuration(id = "FactoryConfig")
39 public Map getFactoryConfig()
40 {
41 return new HashMap();
42 }
43
44 /**
45 * Sets up a digester instance that can parse xml configuration data for
46 * FactoryConfig
47 *
48 * @param container the configuration container that receives the parsed data.
49 * @return a parser that can parse xml files
50 */
51 @ConfigurationParser(id = "FactoryConfigParser", configurationId = "FactoryConfig")
52 public Parser setupParserForFactoryConfig(Map container)
53 {
54 Digester digester = new Digester();
55 digester.setValidating(false);
56 digester.push(container);
57 digester.addCallMethod("factory/mapping", "put", 2, new String[] { "java.lang.Object", "java.lang.Object" });
58 digester.addCallParam("factory/mapping", 0, "key");
59 digester.addCallParam("factory/mapping", 1, "class-name");
60
61 Parser parser = new DigesterParser(digester, container, null);
62 return parser;
63 }
64
65 /**
66 * Contributes hard coded data to the FactoryConfig.
67 *
68 * @param container the configuration container.
69 */
70 @Contribution(configurationId = "FactoryConfig")
71 public void contributeToFactory(Map container)
72 {
73 container.put("map", "java.util.HashMap");
74 }
75
76 /**
77 * Contributes the content of an xml file to the FactoryConfig.
78 *
79 * @param parser the parser that can process file based data
80 */
81 @Contribution(configurationId = "FactoryConfig")
82 public void contributeToFactoryConfig(Parser parser)
83 {
84 parser.processResource("net/sf/annocon/examples/configuration/digester/factoryConfig.xml");
85 }
86
87 }
|