📄 ipcjmsadminconnection.java
字号:
// implementation of JmsAdminServerIfc.stopServer
public void stopServer() throws JMSException {
Vector v = pack("stopServer", 0);
send(v);
checkReply("stopServer");
}
// implementation of JmsAdminServerIfc.close
public void close() {
if (_connection != null) {
try {
_connection.close();
_mc.finish();
} catch (Exception exception) {
_log.debug("Exception during close", exception);
}
_connection = null;
_mc = null;
}
}
// implementation of JmsAdminServerIfc.addUser
public boolean addUser(String username, String password)
throws JMSException {
Vector v = pack("addUser", 2);
v.add(username);
v.add(password);
send(v);
return checkReply("addUser");
}
// implementation of JmsAdminServerIfc.getAllUsers
public Vector getAllUsers() throws JMSException {
Vector v = pack("getAllUsers", 0);
send(v);
return (Vector) getReply("getAllUsers");
}
// implementation of JmsAdminServerIfc.removeUser
public boolean removeUser(String username) throws JMSException {
Vector v = pack("removeUser", 1);
v.add(username);
send(v);
return checkReply("removeUser");
}
// implementation of JmsAdminServerIfc.changePassword
public boolean changePassword(String username, String password)
throws JMSException {
Vector v = pack("changePassword", 2);
v.add(username);
v.add(password);
send(v);
return checkReply("changePassword");
}
/**
* Returns the server host
*
* @return the server host
*/
protected String getHost() {
return _host;
}
/**
* Returns the server port
*
* @return the server port
*/
protected int getPort() {
return _port;
}
/**
* Returns the underlying connection
*/
protected IpcIfc getConnection() {
return _connection;
}
/**
* Sets the underlying connection
*
* @param connection the underlying connection
*/
protected void setConnection(IpcIfc connection) {
_connection = connection;
}
/**
* Opens a connection to the server.
*
* @throws JMSException if an error occurs
*/
protected void openConnection() throws JMSException {
try {
_mc = createClientConnection(_host, _port);
_connection = new ObjectChannel("server", _mc);
((Thread) _mc).start();
} catch (Exception exception) {
raise(exception);
}
}
/**
* Opens an authenticated connection to the server
*
* @param username the user's name
* @param password the user's password
* @throws JMSException if the connection cannot be established
*/
protected void connect(String username, String password)
throws JMSException {
openConnection();
Vector v = pack("createConnection", 2);
v.add(username);
v.add(password);
send(v);
_connectionId = (String) getReply("createConnection");
}
/**
* Create an MIPC client connection
*
* @param host the host of the server
* @param port the port number to use
* @return a new connection
* @throws IOException if the server fails to initialise the ip service
*/
protected MultiplexConnectionIfc createClientConnection(
String host, int port) throws IOException {
return new MultiplexConnection(host, port);
}
/**
* Pack all the data that is required by the server in a vector.
* Set the size of the vector to be exactly the right size for efficiency.
*
* @param method the function to activate on the server
* @param num the number of additional items to pack
* @return a vector containing all the data
*
*/
private Vector pack(String method, int num) {
Vector v = new Vector(3 + num);
v.add("org.exolab.jms.server.mipc.IpcJmsAdminConnection");
v.add(method);
v.add(_connectionId);
return v;
}
/**
* A convenience method to send a packed command to the server.
* @param v the vector to be sendt
* @throws JMSException for any error
*/
private void send(Vector v) throws JMSException {
try {
_connection.send(v);
} catch (Exception exception) {
raise(exception);
}
}
/**
* A convenience method to check the success of operations which return
* a true on sucess.
*
* @param method the requested server function.
* @return true if the operation was successfull
* @throws JMSException for any error
*/
private boolean checkReply(String method) throws JMSException {
boolean reply = false;
try {
Vector v = (Vector) _connection.receive();
if (v != null) {
Boolean b = (Boolean) v.get(0);
if (!b.booleanValue()) {
if (v.get(1) instanceof JMSException) {
throw (JMSException) v.get(1);
} else {
throw new JMSException("Operation " + method
+ " failed:\n" + v.get(1));
}
}
} else {
throw new JMSException("Unknown connection error for "
+ method);
}
reply = ((Boolean) v.get(1)).booleanValue();
} catch (Exception exception) {
raise(exception);
}
return reply;
}
/**
* A convenience method to check the success of operations which return
* a value
*
* @param method the requested server function
* @return the data returned by the server
* @throws JMSException On any failure.
*/
private Object getReply(String method) throws JMSException {
Object reply = null;
try {
Vector v = (Vector) _connection.receive();
if (v != null) {
Boolean b = (Boolean) v.get(0);
if (!b.booleanValue()) {
if (v.get(1) instanceof JMSException) {
throw (JMSException) v.get(1);
} else {
throw new JMSException("Operation " + method
+ " failed:\n" + v.get(1));
}
} else {
reply = v.get(1);
}
} else {
throw new JMSException("Unknown connection error for "
+ method);
}
} catch (Exception exception) {
raise(exception);
}
return reply;
}
private void raise(Exception exception) throws JMSException {
if (exception instanceof JMSException) {
throw (JMSException) exception;
} else {
JMSException error = new JMSException(exception.getMessage());
error.setLinkedException(exception);
throw error;
}
}
} //-- IpcJmsAdminConnection
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -