import javax.telephony.InvalidArgumentException;
import javax.telephony.JtapiPeer;
import javax.telephony.JtapiPeerFactory;
import javax.telephony.JtapiPeerUnavailableException;
import javax.telephony.MethodNotSupportedException;
import javax.telephony.Provider;
import javax.telephony.ProviderObserver;
import javax.telephony.ResourceUnavailableException;
import javax.telephony.Terminal;
import javax.telephony.events.ProvEv;
import javax.telephony.events.ProvInServiceEv;
import com.cisco.cti.util.Condition;
public class CTIExample implements ProviderObserver {
private static final String PROVIDER_STRING = “127.0.0.1;login=loginName;passwd=password”;
Condition conditionInService = new Condition();
Provider provider;
public static void main (String args[]) {
CTIExample example = new CTIExample();
if (example.provider == null) {
JtapiPeer peer;
try {
peer = JtapiPeerFactory.getJtapiPeer(null);
// we now connect to the provider
example.provider = (Provider) peer.getProvider(PROVIDER_STRING);
// cause we need 2 have the condition set, we have to
// add the ProviderObserver here
example.provider.addObserver(example);
// thats only cisco, we have to wait until the condition is set,
// to know that everything works fine
example.conditionInService.waitTrue();
// you know, that we do not write any stacktrace … we log them, but
// it is just an example 🙂
} catch (JtapiPeerUnavailableException e) {
e.printStackTrace();
} catch (ResourceUnavailableException e) {
e.printStackTrace();
} catch (MethodNotSupportedException e) {
e.printStackTrace();
}
}
try {
// thats how we connect a terminal ( the phone(s) with the number 100 )
Address address = example.provider.getAddress(“100”);
Terminal[] terminals = address.getTerminals();
// we may got more than one phone with the 100 as number
// believe me, it is possible 🙂
for (int j = 0; j < terminals.length; j++) {
// you may do not know whats that good for .. see the faq,
// we explain that later
CTIEventListener.getInstance().initialize(address, terminals[j]);
}
} catch (InvalidArgumentException e) {
e.printStackTrace();
}
}
/* (non-Javadoc)
* this event is fired, everytime something changed with the provider.
* f.e. if you are connected, you get the ProvInServiceEv
*/
public void providerChangedEvent(ProvEv[] eventList) {
if (eventList != null) {
// you do not need the for loop, for just this event.
// but maybe you want to add some more later ..
for (int i = 0; i < eventList.length; i++) {
if (eventList[i] instanceof ProvInServiceEv) {
conditionInService.set();
}
}
}
}
}
Hi Jörg,
great stuff. I just found this posts and tried to “use” the code.
After I loaded the code to eclipse I get an error:
CTIEventListener.getInstance().initialize(address, terminals[j]);
-> CTIEventListener -> cannot be resolved.
Any idea?
Marcus