📄 emprovider.java
字号:
package net.sourceforge.gjtapi.raw.emulator;
/*
Copyright (c) 2002 8x8 Inc. (www.8x8.com)
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, provided that the above
copyright notice(s) and this permission notice appear in all copies of
the Software and that both the above copyright notice(s) and this
permission notice appear in supporting documentation.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Except as contained in this notice, the name of a copyright holder
shall not be used in advertising or otherwise to promote the sale, use
or other dealings in this Software without prior written authorization
of the copyright holder.
*/
import javax.telephony.media.*;
import net.sourceforge.gjtapi.media.*;
import net.sourceforge.gjtapi.raw.FullJtapiTpi;
import java.io.*;
import java.util.*;
import net.sourceforge.gjtapi.*;
import javax.telephony.*;
/**
* This is a simple JTAPI emulation stub
* Creation date: (2000-02-04 15:04:19)
* @author: Richard Deadman
*/
public class EmProvider implements FullJtapiTpi {
// property to note if I should replace properties loaded from resource file during initialization
public final static String REPLACE = "replace";
// property to note that I shoould operate headless
public final static String DISPLAY = "display";
private final static String RESOURCE_NAME = "Emulator.props";
private final static String ADDRESS_PREFIX = "Address";
private static Set universe = new HashSet(); // globally accessible set of emulators
private TestManager mgr;
private Properties provProps;
/**
* Raw constructor used by the GenericJtapiPeer factory
* Creation date: (2000-02-10 10:28:55)
* @author: Richard Deadman
*/
public EmProvider() {
super();
// read provider details and load the resources, if available
this.setProvProps(this.loadResources("/" + EmProvider.RESOURCE_NAME));
}
/**
* Set the event listener.
*/
public void addListener(TelephonyListener rl) {
this.getMgr().setListener(rl);
}
/**
* We don't have any real resources to manage, but we do have to see if we send detected DTMF
*/
public boolean allocateMedia(java.lang.String terminal, int type, java.util.Dictionary resourceArgs) {
if (resourceArgs == null)
return true; // we don't have any events to send
boolean generate = false;
// a bug in JTAPI 1.3.1 means that ESymbol cannot be instantiated since its parent interface is not in the jar file.
try {
// get the array of events symbols that indicate what needs to be sent
Symbol[] evsToSend = (Symbol[])resourceArgs.get(SignalDetectorConstants.p_EnabledEvents);
if (evsToSend == null)
return true;
// now see if the array contains the RetrieveSignals symbol
Symbol rs = SignalDetectorConstants.ev_RetrieveSignals;
for (int i = 0; i < evsToSend.length; i++)
if (rs.equals(evsToSend[i]))
generate = true;
} catch (NoClassDefFoundError cnfe) {
// assume we should generate signals
generate = true;
}
this.getPhone(terminal).sendDetectedDtmf(generate);
return true;
}
/**
* Tell a call to be answered on a terminal.
* Here we check if the call is Ringing at the terminal
*/
public void answerCall(CallId call, String address, String terminal) throws RawStateException, ResourceUnavailableException {
RawPhone phone = this.getPhone(terminal);
if (phone != null) {
if (!phone.answer(call))
throw new RawStateException(call, terminal, terminal,
javax.telephony.InvalidStateException.TERMINAL_CONNECTION_OBJECT,
javax.telephony.TerminalConnection.UNKNOWN);
} else
throw new ResourceUnavailableException(ResourceUnavailableException.ORIGINATOR_UNAVAILABLE);
}
/**
* Create a call from the given address and terminal to the remote destination address.
*/
public CallId createCall(CallId id, String address, String term, String dest) throws InvalidPartyException, RawStateException {
RawPhone phone = this.getPhone(address);
// Add the new call
int phState = phone.getState();
if (phState == RawPhone.IDLE || phState == RawPhone.HOLD) {
new Leg((RawCall)id, phone, this.getMgr().getListener(), Leg.IDLE);
} else {
throw new RawStateException(id, null, null,
javax.telephony.InvalidStateException.CALL_OBJECT,
javax.telephony.TerminalConnection.UNKNOWN);
}
((TestPhone)phone).getModel().dial(dest);
return id;
}
/**
* Find the provider that supports the given address
* Creation date: (2000-10-04 15:31:06)
* @return net.sourceforge.gjtapi.raw.emulator.EmProvider
* @param address java.lang.String
*/
static EmProvider findProvider(String address) {
// iterating over universe caused a StackOverflowError -- VisualAge class library bug
try {
int provSize = universe.size();
EmProvider[] provs = (EmProvider[])universe.toArray(new EmProvider[provSize]);
for (int i = 0; i < provSize; i++) {
try {
provs[i].getTerminals(address);
return provs[i];
} catch (InvalidArgumentException iae) {
// try next one
}
}
} catch (StackOverflowError soe) {
soe.printStackTrace(System.err);
}
return null;
}
/**
* Null resource freer.
*/
public boolean freeMedia(java.lang.String terminal, int type) {
return true;
}
/**
* Return all the addresses managed by the provider
*/
public String[] getAddresses() {
return (String[])this.getMgr().getPhones().keySet().toArray(new String[0]);
}
/**
* Return the Address names associated with the Terminal name.
*/
public String[] getAddresses(String terminal) throws InvalidArgumentException {
if (this.getMgr().getPhone(terminal) == null)
throw new InvalidArgumentException("Unknown address/phone: " + terminal);
String[] addresses = {terminal};
return addresses;
}
/**
* Return a data snapshot of the call referenced by the call id.
*/
public CallData getCall(CallId id) {
if (id instanceof RawCall && id != null) {
return ((RawCall)id).getCallData();
}
return null;
}
/**
* Return an array of snapshot information for all calls associated with an address.
*/
public CallData[] getCallsOnAddress(String number) {
RawPhone rp = this.getPhone(number);
if (rp != null) {
RawCall[] calls = rp.getCalls();
int size = calls.length;
CallData[] cd = new CallData[size];
for (int i = 0; i < size; i++) {
cd[i] = calls[i].getCallData();
}
}
return null;
}
/**
* Return an array of snapshot information for all calls associated with an terminal.
*/
public CallData[] getCallsOnTerminal(String name) {
return this.getCallsOnAddress(name);
}
/**
* We support all the Generic Capabilities, with the exception of throttling and terminal and terminalConnection PrivateData sending.
* Otherwise we could return null.
*/
public Properties getCapabilities() {
return this.getProvProps();
}
/**
* Gets the Phone Manager for the system.
* Creation date: (2000-02-10 10:07:53)
* @author: Richard Deadman
* @return net.sourceforge.gjtapi.raw.emulator.TestManager
*/
TestManager getMgr() {
return mgr;
}
/**
* Return the phone associated with an address, or null
* Creation date: (2000-02-10 12:23:49)
* @author: Richard Deadman
* @return A RawPhone terminal representation
* @param address An address for a phone (assuming 1:1 corespondence)
*/
RawPhone getPhone(String address) {
return this.getMgr().getPhone(address);
}
/**
* getPrivateData method comment.
*/
public java.lang.Object getPrivateData(net.sourceforge.gjtapi.CallId call, java.lang.String address, java.lang.String terminal) {
return null;
}
/**
* Internal accessor for the properties map
* Creation date: (2000-02-22 14:20:52)
* @author: Richard Deadman
* @return The map that lists the provider properties
*/
private Properties getProvProps() {
return provProps;
}
/**
* Get a list of all the terminal names I manage.
*/
public TermData[] getTerminals() {
String[] addresses = this.getAddresses();
int size = addresses.length;
TermData[] td = new TermData[size];
for (int i = 0; i < size; i++) {
td[i] = new TermData(addresses[i], true);
}
return td;
}
/**
* Get all the terminal names associated with the address
*/
public TermData[] getTerminals(String address) throws InvalidArgumentException {
if (this.getMgr().getPhone(address) == null)
throw new InvalidArgumentException("Unknown address/phone: " + address);
TermData[] terms = {new TermData(address, true)};
return terms;
}
/**
* Hold the given terminal. Here we map address to terminals 1:1
*/
public void hold(CallId call, String address, String term) throws RawStateException {
try {
this.getPhone(term).hold();
} catch (NullPointerException npe) {
// No phone matched
throw new RawStateException(null, term, term,
javax.telephony.InvalidStateException.TERMINAL_CONNECTION_OBJECT,
javax.telephony.TerminalConnection.UNKNOWN);
}
}
/**
* Provide application specific initialization
*/
public void initialize(Map props) throws ProviderUnavailableException {
Map m = null;
Object value = null;
// determine if we need to totally replace the current properties
boolean replace = false;
if (props != null) {
value = props.get(REPLACE);
replace = net.sourceforge.gjtapi.capabilities.Capabilities.resolve(value);
}
if (replace)
m = props;
else {
m = this.getProvProps();
if (props != null)
m.putAll(props);
}
// now look for all addresses
Vector adds = new Vector();
Iterator it = m.keySet().iterator();
while (it.hasNext()) {
String key = (String)it.next();
if (key.startsWith(EmProvider.ADDRESS_PREFIX)) {
adds.add(m.get(key));
}
}
// Now create an instance of the manager
this.setMgr(new TestManager((String[])adds.toArray(new String[0])));
// determine if a view is required
value = m.get(DISPLAY);
if (value instanceof String) {
String display = (String)value;
if (display != null && display.length() > 0 && Character.toLowerCase(display.charAt(0)) == 't')
this.getMgr().show();
}
// add myself to the global set of provider
EmProvider.universe.add(this);
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -