Externalize your AGI Configuration
Using a dependency injection framework like the Spring Framework to exernalize your configuration is often a great enhancement for maintainance and deployment of your application.
If you are building an AGI application using Asterisk-Java the following code snippets will show you how do it:
Build your AGI script as before:
public class HelloAgi extends BaseAgiScript
{
private String voicePrompt;
public void setVoicePrompt(String voicePrompt)
{
this.voicePrompt = voicePrompt;
}
public void service(AgiRequest request, AgiChannel channel) throws AgiException
{
streamFile(voicePrompt);
}
}
This is really a very simple one but note the property voicePrompt that we added. This property will be configured using Spring at deploy-time.
Next define an ApplicationContext (usually an XML file somewhere on the classpath):
<beans xmlns="...">
<bean id="agiServer" class="org.asteriskjava.fastagi.DefaultAgiServer"
init-method="startup" destroy-method="shutdown">
<property name="mappingStrategy" ref="mappingStrategy" />
</bean>
<bean id="mappingStrategy" class="org.asteriskjava.fastagi.SimpleMappingStrategy">
<property name="mappings">
<map>
<entry key="hello.agi" value-ref="helloAgi" />
</map>
</property>
</bean>
<bean id="helloAgi" class="HelloAgi">
<property name="voicePrompt" value="tt-monkeys" />
</bean>
</beans>
This context configures your AGI script and defines the mapping of URL to script instance.
Finally provide a main() method somewhere to start the context:
public class Main
{
public static void main(String[] args)
{
new ClassPathXmlApplicationContext("context.xml").start();
}
}
Now you are ready to run the application.
Follow Up: