[Asterisk-java-cvs] CVS: asterisk-java/src/java/net/sf/asterisk/fastagi ResourceBundleMappingStrateg
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-03-10 21:31:44
|
Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8941/src/java/net/sf/asterisk/fastagi Modified Files: DefaultAGIServer.java Added Files: ResourceBundleMappingStrategy.java Log Message: Added ResourceBundleMappingStrategy --- NEW FILE: ResourceBundleMappingStrategy.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.fastagi; import java.lang.reflect.Constructor; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.MissingResourceException; import java.util.ResourceBundle; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A MappingStrategy that is configured via a resource bundle.<br> * The resource bundle contains the script part of the url as key and the fully * qualified class name of the corresponding AGIScript as value.<br> * Example: * * <pre> * leastcostdial.agi = com.example.fastagi.LeastCostDialAGIScript * hello.agi = com.example.fastagi.HelloAGIScript * </pre> * * LeastCostDialAGIScript and HelloAGIScript must both implement the AGIScript * interface and have a default constructor with no parameters.<br> * The resource bundle (properties) file must be called * <code>fastagi-mapping.properties</code> and be available on the classpath. * * @author srt * @version $Id: ResourceBundleMappingStrategy.java,v 1.1 2005/03/10 21:31:30 srt Exp $ */ public class ResourceBundleMappingStrategy implements MappingStrategy { private static final String DEFAULT_RESOURCE_BUNDLE_NAME = "fastagi-mapping"; private final Log logger = LogFactory.getLog(getClass()); private String resourceBundleName; private Map mapping; public ResourceBundleMappingStrategy() { this.resourceBundleName = DEFAULT_RESOURCE_BUNDLE_NAME; this.mapping = null; } public void setResourceBundleName(String propertiesName) { this.resourceBundleName = propertiesName; } private void loadResourceBundle() { ResourceBundle resourceBundle; Enumeration keys; mapping = new HashMap(); try { resourceBundle = ResourceBundle.getBundle(resourceBundleName); } catch (MissingResourceException e) { logger.error("Resource bundle " + resourceBundleName + " is missing."); return; } keys = resourceBundle.getKeys(); while (keys.hasMoreElements()) { String scriptName; String className; AGIScript agiScript; scriptName = (String) keys.nextElement(); className = resourceBundle.getString(scriptName); agiScript = createAGIScriptInstance(className); if (agiScript == null) { continue; } mapping.put(scriptName, agiScript); } } private AGIScript createAGIScriptInstance(String className) { Class agiScriptClass; Constructor constructor; AGIScript agiScript; try { agiScriptClass = Class.forName(className); constructor = agiScriptClass.getConstructor(new Class[]{}); agiScript = (AGIScript) constructor.newInstance(new Object[]{}); } catch (Exception e) { logger.error("Unable to create AGIScript instance of type " + className); return null; } return agiScript; } public AGIScript determineScript(AGIRequest request) { if (mapping == null) { loadResourceBundle(); } return (AGIScript) mapping.get(request.getScript()); } } Index: DefaultAGIServer.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi/DefaultAGIServer.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -p -r1.2 -r1.3 --- DefaultAGIServer.java 10 Mar 2005 16:36:43 -0000 1.2 +++ DefaultAGIServer.java 10 Mar 2005 21:31:30 -0000 1.3 @@ -81,6 +81,7 @@ public class DefaultAGIServer implements { this.bindPort = DEFAULT_BIND_PORT; this.poolSize = DEFAULT_POOL_SIZE; + this.mappingStrategy = new ResourceBundleMappingStrategy(); } /** |