View Javadoc

1   /*
2    * Copyright  2004-2005 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package net.sf.asterisk.fastagi;
18  
19  import java.util.Map;
20  
21  /***
22   * A MappingStrategy that is configured via a fixed set of properties.<br>
23   * This mapping strategy is most useful when used with the Spring framework.<br>
24   * Example (using Spring):
25   * 
26   * <pre>
27   * &lt;beans&gt;
28   *    &lt;bean id="mapping"
29   *          class="net.sf.asterisk.fastagi.SimpleMappingStrategy"&gt;
30   *        &lt;property name="mappings"&gt;
31   *            &lt;map&gt;
32   *                &lt;entry&gt;
33   *                    &lt;key&gt;&lt;value&gt;hello.agi&lt;/value&gt;&lt;/key&gt;
34   *                    &lt;ref local="hello"/&gt;
35   *                &lt;/entry&gt;
36   *                &lt;entry&gt;
37   *                    &lt;key&gt;&lt;value&gt;leastcostdial.agi&lt;/value&gt;&lt;/key&gt;
38   *                    &lt;ref local="leastCostDial"/&gt;
39   *                &lt;/entry&gt;
40   *            &lt;/map&gt;
41   *        &lt;/property&gt;
42   *    &lt;/bean&gt;
43   *
44   *    &lt;bean id="hello"
45   *          class="com.example.fastagi.HelloAGIScript"/&gt;
46   *
47   *    &lt;bean id="leastCostDial"
48   *          class="com.example.fastagi.LeastCostDialAGIScript"&gt;
49   *        &lt;property name="rates"&gt;&lt;value&gt;rates.txt&lt;/value&gt;&lt;/property&gt;
50   *    &lt;/bean&gt;
51   * &lt;beans&gt;
52   * </pre>
53   * 
54   * LeastCostDialAGIScript and HelloAGIScript must both implement the AGIScript.<br>
55   * 
56   * @author srt
57   * @version $Id: SimpleMappingStrategy.java,v 1.4 2005/10/25 22:29:29 srt Exp $
58   * @since 0.2
59   */
60  public class SimpleMappingStrategy implements MappingStrategy
61  {
62      private Map mappings;
63  
64      /***
65       * Set the path to AGIScript mapping.<br>
66       * Use the path (for example <code>hello.agi</code>) as key and your
67       * AGIScript (for example <code>new HelloAGIScript()</code>) as value of
68       * this map. 
69       * 
70       * @param mappings the path to AGIScript mapping.
71       */
72      public void setMappings(Map mappings)
73      {
74          this.mappings = mappings;
75      }
76  
77      public AGIScript determineScript(AGIRequest request)
78      {
79          if (mappings == null)
80          {
81              return null;
82          }
83  
84          return (AGIScript) mappings.get(request.getScript());
85      }
86  }