www.pudn.com > d4j.zip > JMain.java


/*  
 * Copyright (c) 1999 Carlos G Mendioroz. 
 * 
 *  This file is part of D4J. 
 * 
 *  D4J is free software; you can redistribute it and/or 
 *  modify it under the terms of the GNU Lesser General Public 
 *  License as published by the Free Software Foundation; either 
 *  version 2 of the License, or (at your option) any later version. 
 *   
 *  D4J is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 *  Lesser General Public License for more details. 
 *   
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with this library; if not, write to the 
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
 *  Boston, MA  02111-1307, USA. 
 * 
 * Report problems and direct all questions to: 
 * 
 *	tron@acm.org 
 */ 
 
import java.io.*; 
import java.util.*; 
import local.dialogic.*; 
 
public class JMain implements CallHandler 
{ 
 
   	static Properties defaultProp = new Properties(); 
   	static long defaultPropLoaded = 0; 
	 
 
	public void handleCall(Call aCall) { 
	    System.err.println("Serving call to " + aCall.dnis()); 
	    // Find out if defaultProperties changed... 
	    reloadDefaultProp(); 
	    // Make properties for call. Define Home, class. 
	    Properties prop = new Properties(defaultProp); 
	    String config = (String)defaultProp.get(aCall.dnis()); 
	    if (config == null) { 
	        // DNIS no definido... 
	        aCall.reject(0); 
	        return; 
	    } 
	    int dirEnd = config.indexOf(","); 
	    int classEnd = config.indexOf(",", dirEnd+1); 
	    String home = config.substring(0, dirEnd); 
	    prop.put("HOME", home); 
	    String className = ""; 
	    String propsFile = "ewd.cfg"; 
	    if (classEnd >= 0) { 
                className = config.substring(dirEnd+1, classEnd); 
	        propsFile = config.substring(classEnd+1); 
	    } else { 
	        className = config.substring(dirEnd+1); 
	    } 
	    try { 
	        FileInputStream localConfig = new FileInputStream(home + File.separator + propsFile); 
	        prop.load(localConfig); 
	        localConfig.close(); 
	    } catch (IOException ioe) {}; 
	     
	    try { 
	        Voice bdev = aCall.channel().getVoice(); 
	         
	        AppClassLoader loader =  
	            new AppClassLoader(home); 
	        Application handler =  
	            (Application)loader.loadClass(className).newInstance(); 
	        try { 
	            handler.handleCall(aCall, prop); 
	        } catch (RuntimeException rte) {} 
	        aCall.clear(); // Just in case 
	    } catch (Exception e) { 
	        System.err.println("Error in handling call: "+e); 
                aCall.clear(); 
	    } 
	    System.gc(); 
	    Thread.yield(); 
	} 
	 
	     
	private void reloadDefaultProp() { 
	    File props = new File("ewd.cfg"); 
	    if (props.lastModified() != defaultPropLoaded) { 
	        try { 
	            System.err.println("Reloading defaults..."); 
    	        defaultProp.load(new FileInputStream(props)); 
	            defaultPropLoaded = props.lastModified(); 
	        } catch(IOException ioe) {} 
	    } 
	} 
	     
    public static void main(String argv[]) 
    { 
        // instantiate our class 
		String s; 
		int nv = 0, nt = 0; 
		 
	if (argv.length > 0) 
	    try { 
	    Dialogic.debug = Integer.parseInt(argv[0]); 
	} catch (NumberFormatException nfe) {} 
         
	try { 
         
            Channel c1 = new AnalogChannel("dxxxB1C1" , "001"); 
            // Channel c1 = new GCChannel(":N_dtiB1T1:P_ar_r2_io:V_dxxxB1C1"); 
            // Channel c1 = new R2CASChannel("dtiB1T1" , "dxxxB1C1"); 
            Channel c2 = new AnalogChannel("dxxxB1C2" , "002"); 
            c1.setCallHandler(new JMain()); 
            c2.setCallHandler(new JMain()); 
	} catch (Throwable e) { 
           // Do nothing ?// 
            e.printStackTrace(); 
            sleep(5); 
	} 
    } 
 
    private static void sleep(int s) { 
        try { 
            Thread.sleep(1000 * s); 
        } catch (Exception e) {} 
    } 
}