1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.asterisk.fastagi;
18
19 import java.lang.reflect.Constructor;
20 import java.util.Enumeration;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.MissingResourceException;
24 import java.util.ResourceBundle;
25
26 import net.sf.asterisk.util.Log;
27 import net.sf.asterisk.util.LogFactory;
28
29 /***
30 * A MappingStrategy that is configured via a resource bundle.<br>
31 * The resource bundle contains the script part of the url as key and the fully
32 * qualified class name of the corresponding AGIScript as value.<br>
33 * Example:
34 *
35 * <pre>
36 * leastcostdial.agi = com.example.fastagi.LeastCostDialAGIScript
37 * hello.agi = com.example.fastagi.HelloAGIScript
38 * </pre>
39 *
40 * LeastCostDialAGIScript and HelloAGIScript must both implement the AGIScript
41 * interface and have a default constructor with no parameters.<br>
42 * The resource bundle (properties) file must be called
43 * <code>fastagi-mapping.properties</code> and be available on the classpath.
44 *
45 * @author srt
46 * @version $Id: ResourceBundleMappingStrategy.java,v 1.4 2005/11/04 21:49:01 srt Exp $
47 */
48 public class ResourceBundleMappingStrategy implements MappingStrategy
49 {
50 private static final String DEFAULT_RESOURCE_BUNDLE_NAME = "fastagi-mapping";
51 private final Log logger = LogFactory.getLog(getClass());
52 private String resourceBundleName;
53 private Map mappings;
54
55 public ResourceBundleMappingStrategy()
56 {
57 this.resourceBundleName = DEFAULT_RESOURCE_BUNDLE_NAME;
58 this.mappings = null;
59 }
60
61 public void setResourceBundleName(String propertiesName)
62 {
63 this.resourceBundleName = propertiesName;
64 }
65
66 private void loadResourceBundle()
67 {
68 ResourceBundle resourceBundle;
69 Enumeration keys;
70
71 mappings = new HashMap();
72
73 try
74 {
75 resourceBundle = ResourceBundle.getBundle(resourceBundleName);
76 }
77 catch (MissingResourceException e)
78 {
79 logger.error("Resource bundle '" + resourceBundleName + "' is missing.");
80 return;
81 }
82
83 keys = resourceBundle.getKeys();
84
85 while (keys.hasMoreElements())
86 {
87 String scriptName;
88 String className;
89 AGIScript agiScript;
90
91 scriptName = (String) keys.nextElement();
92 className = resourceBundle.getString(scriptName);
93
94 agiScript = createAGIScriptInstance(className);
95 if (agiScript == null)
96 {
97 continue;
98 }
99
100 mappings.put(scriptName, agiScript);
101 logger.info("Added mapping for '" + scriptName + "' to class " + agiScript.getClass());
102 }
103 }
104
105 private AGIScript createAGIScriptInstance(String className)
106 {
107 Class agiScriptClass;
108 Constructor constructor;
109 AGIScript agiScript;
110
111 try
112 {
113 agiScriptClass = Class.forName(className);
114 constructor = agiScriptClass.getConstructor(new Class[]{});
115 agiScript = (AGIScript) constructor.newInstance(new Object[]{});
116 }
117 catch (Exception e)
118 {
119 logger.error("Unable to create AGIScript instance of type "
120 + className);
121 return null;
122 }
123
124 return agiScript;
125 }
126
127 public AGIScript determineScript(AGIRequest request)
128 {
129 if (mappings == null)
130 {
131 loadResourceBundle();
132 }
133
134 return (AGIScript) mappings.get(request.getScript());
135 }
136 }