📄 connector.java
字号:
/*******************************************************************************
* Copyright (c) 2006 Koji Hisano <hisano@gmail.com> - UBION Inc. Developer
* Copyright (c) 2006 UBION Inc. <http://www.ubion.co.jp/> All rights reserved.
*
* Copyright (c) 2006 Skype Technologies S.A. <http://www.skype.com/>
*
* This program and the accompanying materials are made available under the
* terms of the Common Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Koji Hisano - initial API and implementation
* Bart Lamot - changed package and class of the MacOS to OSX
******************************************************************************/
package com.skype.connector;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.event.EventListenerList;
/**
* Base class for all platform specific connectors.
* A connector connects the Skype Java API with a running Skype client.
*/
public abstract class Connector {
/**
* Enumeration of the connector status.
*/
public enum Status {
/**
* PENDING_AUTHORIZATION - The connector is waiting for the user to accept this app to connect to the Skype client.
* ATTACHED - The connector is attached to the Skype client.
* REFUSED - The user denied the application to connect to the Skype client.
* NOT_AVAILABLE - The is no Skype client available to connect to.
* API_AVAILABLE - Redundant of ATTACHED.
* NOT_RUNNING - Connection can't be established.
*/
PENDING_AUTHORIZATION, ATTACHED, REFUSED, NOT_AVAILABLE, API_AVAILABLE, NOT_RUNNING;
}
/** useJNIConnector if this is true on windows the connection will be made using a dll instead of using swt library. */
private static boolean useJNIConnector;
/** Singleton instance of this class. */
private static Connector instance;
/**
* To use the win32 dll instead of the SWT library please use this method.
* @param on If true the win32 connector will be used.
*/
public static synchronized void useJNIConnector(final boolean on) {
if (instance != null) {
throw new IllegalStateException("You should call this method before calling Connector#getInstance().");
}
useJNIConnector = on;
}
/**
* This method checks if SWT is available in the classpath.
* @return true if SWT is found.
*/
private static boolean isSWTAvailable() {
try {
Class.forName("org.eclipse.swt.SWT");
} catch(ClassNotFoundException e) {
return false;
}
return true;
}
/**
* Initialize a platform specific connection.
* This method will select a connector based on the os.name.
* Windows has two versions see useJNIConnector.
* @return an initialized connection.
*/
public static synchronized Connector getInstance() {
if (instance == null) {
String osName = System.getProperty("os.name");
String connectorClassName = null;
if (!useJNIConnector && !isSWTAvailable()) {
useJNIConnector = true;
}
if (osName.startsWith("Windows")) {
//Todo: add a check to see if swt is in the classpath, if not use the other connector.
if (useJNIConnector) {
connectorClassName = "com.skype.connector.win32.Win32Connector";
} else {
connectorClassName = "com.skype.connector.windows.WindowsConnector";
}
} else if (osName.startsWith("Linux") || osName.startsWith("LINUX")) {
connectorClassName = "com.skype.connector.linux.LinuxConnector";
} else if (osName.startsWith("Mac OS X")) {
connectorClassName = "com.skype.connector.osx.OSXConnector";
}
if (connectorClassName == null) {
throw new IllegalStateException("This platform is not supported by Skype API for Java.");
}
try {
Class connectorClass = Class.forName(connectorClassName);
Method getInstance = connectorClass.getMethod("getInstance");
instance = (Connector) getInstance.invoke(null);
} catch (Exception e) {
throw new IllegalStateException("The connector couldn't be initialized.", e);
}
}
return instance;
}
/**
* Set the instance of the connector.
* @param newInstance The new instance.
* @throws ConnectorException thrown when instance is not valid.
*/
protected static synchronized void setInstance(final Connector newInstance) throws ConnectorException {
if (Connector.instance != null) {
Connector.instance.dispose();
}
Connector.instance = newInstance;
}
/**
* The debug output stream.
* <p>
* This stream is initialized by
* <code>new PrintWriter(System.out, true)</code>.
* </p>
*/
private PrintWriter debugOut = new PrintWriter(System.out, true);
/** debugListener. */
private ConnectorListener debugListener;
/** debug printer lock object. */
private Object debugFieldMutex = new Object();
/** application name to send to Skype client. */
private String applicationName = "SkypeAPI4Java";
/** Initialize the status of the connector. */
private Status status = Status.NOT_RUNNING;
/** Boolean to check if the connector is already initialized. */
private boolean isInitialized;
/** global connector timeout. */
private int connectTimeout = 10000;
/** global command-reply timeout. */
private int commandTimeout = 10000;
/** Collection of event listeners for the connector. */
private EventListenerList listeners = new EventListenerList();
/** Command counter, can be used to identify message and reply pairs. */
private int commandCount;
/** Thread pooled executor */
private ExecutorService executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 20, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactory() {
private final AtomicInteger threadNumber = new AtomicInteger();
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "SkypeMessageSender-" + threadNumber.getAndIncrement());
thread.setDaemon(true);
return thread;
}
});
/**
* Because this object should be a singleton the constructor is protected.
*/
protected Connector() {
}
/**
* Try to get the absolute path to the skype client.
* Should be overridden for each platfrom specific connector.
* Not geranteed to work.
* @return The absolute path to the Skype client executable.
*/
public String getInstalledPath() {
return "skype";
}
/**
* Enable or disable debug printing for more information.
* @param on if true debug output will be written to System.out
* @throws ConnectorException thrown when connection to Skype Client has gone bad.
*/
public final void setDebug(final boolean on) throws ConnectorException {
synchronized (debugFieldMutex) {
if (on) {
if (debugListener == null) {
debugListener = new AbstractConnectorListener() {
@Override
public void messageReceived(final ConnectorMessageEvent event) {
getDebugOut().println("<- " + event.getMessage());
}
@Override
public void messageSent(final ConnectorMessageEvent event) {
getDebugOut().println("-> " + event.getMessage());
}
};
addConnectorListener(debugListener);
}
} else {
if (debugListener != null) {
removeConnectorListener(debugListener);
}
}
}
}
/**
* Sets the debug output stream.
* @param newDebugOut the new debug output stream
* throws NullPointerException if <code>debugOut</code> is null.
* @see #setDebugOut(PrintStream)
* @see #getDebugOut()
*/
public final void setDebugOut(final PrintWriter newDebugOut) {
ConnectorUtils.checkNotNull("debugOut", newDebugOut);
this.debugOut = newDebugOut;
}
/**
* Sets the debug output stream.
* @param newDebugOut the new debug output stream
* throws NullPointerException if <code>debugOut</code> is null.
* @see #setDebugOut(PrintWriter)
* @see #getDebugOut()
*/
public final void setDebugOut(final PrintStream newDebugOut) {
ConnectorUtils.checkNotNull("debugOut", newDebugOut);
setDebugOut(new PrintWriter(newDebugOut, true));
}
/**
* Gets the debug output stream.
* @return the current debug output stream
* @see #setDebugOut(PrintWriter)
* @see #setDebugOut(PrintStream)
*/
public final PrintWriter getDebugOut() {
return debugOut;
}
/**
* Set the application name for this application.
* This is what the User will see in the Allow/Deny dialog.
* @param newApplicationName Name of this application.
*/
public final void setApplicationName(final String newApplicationName) {
ConnectorUtils.checkNotNull("applicationName", newApplicationName);
this.applicationName = newApplicationName;
}
/**
* Return the current application name.
* @return applicationName.
*/
public final String getApplicationName() {
return applicationName;
}
/**
* Set the status of this connector instance.
* @param newValue The new status.
*/
protected final void setStatus(final Status newValue) {
status = newValue;
fireStatusChanged(newValue);
}
/**
* Return the status of this connector instance.
* @return status.
*/
public final Status getStatus() {
return status;
}
/**
* Change the connect timeout of this connector instance.
* @param newValue the new timeout value in milliseconds.
*/
public final void setConnectTimeout(final int newValue) {
connectTimeout = newValue;
}
/**
* Return the current connect timeout settings of the connector instance.
* @return current connect timeout.
*/
public final int getConnectTimeout() {
return connectTimeout;
}
/**
* Change the command timeout value.
* @param newValue The new timeout value in milliseconds.
*/
public final void setCommandTimeout(final int newValue) {
commandTimeout = newValue;
}
/**
* Return the current command timeout setting.
* @return command timeout value.
*/
public final int getCommandTimeout() {
return commandTimeout;
}
/**
* Connect the connector instance to the Skype client.
* @return the status after connecting.
* @throws ConnectorException thrown when a connection could not be made due to technical problems.
*/
public final synchronized Status connect() throws ConnectorException {
int timeout = getConnectTimeout();
if (!isInitialized) {
initialize(timeout);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -