com.nmt.mgp
Class MgpSession

java.lang.Object
  extended by com.nmt.mgp.MgpSession
All Implemented Interfaces:
MgpSessionI

public class MgpSession
extends java.lang.Object
implements MgpSessionI

An implementation of the MgpSessionI interface.


Constructor Summary
MgpSession()
           
MgpSession(java.lang.String host, int port, boolean useSsl)
          Construct a new MgpSession object, and connect to the server.
 
Method Summary
 void close()
          Clears the session by logging off from the server and closing the current connection.
 void connect(java.lang.String host, int port, boolean useSsl)
          Connect to the server.
 java.sql.ResultSet dbexec(java.lang.String profile, java.lang.String sql)
          Send an SQL string to be executed on the EMG server.
 void deleteMessage(int id)
          Delete a message.
 OptionHolder execute(int operation, OptionHolder options)
          Send an operation with arguments to the EMG server, and wait for the response.
 int getAdminFlags()
          Gets the full administration flags for the currently logged in user.
 java.lang.String getClientConfig()
          Gets the client configuration string.
 OptionHolder getLoginOptions()
          Gets the current login options.
 int getPermissions()
          Gets the permissions for the currently logged in user.
 java.lang.String getRemoteIp()
          The IP number of this client, as seen by the server.
 boolean havePermission(int functions)
          Checks if the current user has all of the given permissions.
 boolean havePermission(int mask, boolean all)
          Checks if the current user has any or all of the given permissions.
 boolean isAdmin()
          Returns whether user in session is an administrator.
 void login(java.lang.String username, java.lang.String password)
          Login to the server using username and password for authentication.
 void login(java.lang.String username, java.lang.String password, java.lang.String servicetype)
          Login to the server using username and password for authentication.
 void logoff()
          Logoff from the server.
protected  javax.net.ssl.SSLSocket openSSL()
          Open an SSL connection, allowing any certificate that the server might offer.
 int queryMessage(int id)
          Query the server for status of a message, identified by the specified id.
 int readOperation(OptionHolder options)
          Read an operation request from the server and store the options in the provided option holder.
 int readResponse()
          Read the response to the last command sent to the EMG server.
 void reload()
          Reload the configuration on the server, while keeping the connection open.
 int sendMessage(Message msg)
          Send a message.
 void sendOperation(int operation, OptionHolder options)
          Send an operation to the EMG server.
 void sendOperation(int operation, OptionHolder options, boolean doFlush)
          Send an operation to the EMG server.
 void setDebug(boolean on)
          Enables/disables runtime debug output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MgpSession

public MgpSession()

MgpSession

public MgpSession(java.lang.String host,
                  int port,
                  boolean useSsl)
           throws MgpException
Construct a new MgpSession object, and connect to the server.

Parameters:
host - the host name to connect to
port - the port number to connect to
useSsl - true if and only if the communication should use SSL
Throws:
MgpException - if a communication error occurs
See Also:
connect(java.lang.String, int, boolean)
Method Detail

connect

public void connect(java.lang.String host,
                    int port,
                    boolean useSsl)
             throws MgpException
Description copied from interface: MgpSessionI
Connect to the server.

Specified by:
connect in interface MgpSessionI
Parameters:
host - the host name to connect to
port - the port number to connect to
useSsl - true if and only if the communication should use SSL
Throws:
MgpException - if a communication error occurs

openSSL

protected javax.net.ssl.SSLSocket openSSL()
                                   throws java.security.NoSuchAlgorithmException,
                                          java.security.KeyManagementException,
                                          java.io.IOException
Open an SSL connection, allowing any certificate that the server might offer.

Throws:
java.security.NoSuchAlgorithmException - if no matching algorithm can be found
java.security.KeyManagementException - if no key can be agreed upon
java.io.IOException - if no socket can be opened on the connection

close

public void close()
           throws MgpException
Description copied from interface: MgpSessionI
Clears the session by logging off from the server and closing the current connection.

Specified by:
close in interface MgpSessionI
Throws:
MgpException

sendOperation

public void sendOperation(int operation,
                          OptionHolder options,
                          boolean doFlush)
                   throws MgpException
Description copied from interface: MgpSessionI
Send an operation to the EMG server.

Specified by:
sendOperation in interface MgpSessionI
Parameters:
operation - the operation number, one of Mgp.MGP_OP_*
options - the list of operation parameters
doFlush - true if and only if the socket stream should be flushed after the operation
Throws:
MgpException
See Also:
Mgp

sendOperation

public void sendOperation(int operation,
                          OptionHolder options)
                   throws MgpException
Description copied from interface: MgpSessionI
Send an operation to the EMG server.

This is a simple overload to MgpSessionI.sendOperation(int,OptionHolder,boolean), with true used as the third argument.

Specified by:
sendOperation in interface MgpSessionI
Parameters:
operation - the operation number, one of Mgp.MGP_OP_*
options - the list of operation parameters
Throws:
MgpException

readOperation

public int readOperation(OptionHolder options)
                  throws MgpException
Description copied from interface: MgpSessionI
Read an operation request from the server and store the options in the provided option holder.

Specified by:
readOperation in interface MgpSessionI
Parameters:
options - an empty OptionHolder, which will get filled with the parameters to the operation
Throws:
MgpException - if a communication error occurs

readResponse

public int readResponse()
                 throws MgpException
Description copied from interface: MgpSessionI
Read the response to the last command sent to the EMG server.

Calls MgpSessionI.readOperation(OptionHolder), extracts the return code (Mgp.MGP_OPTION_RETCODE) and returns the value as an int.

Specified by:
readResponse in interface MgpSessionI
Throws:
MgpException - if an error occurs when reading from the stream
See Also:
Mgp

execute

public OptionHolder execute(int operation,
                            OptionHolder options)
                     throws MgpException
Description copied from interface: MgpSessionI
Send an operation with arguments to the EMG server, and wait for the response. The implementation of this method should be synchronized to provide a simple multithread safe access point.

Specified by:
execute in interface MgpSessionI
Parameters:
operation - the operation number, as one of MGP_OP_* from Mgp
options - the input parameters, to be sent to the server
Returns:
the returned parameters from the server
Throws:
MgpException - if a communication error occurs

login

public void login(java.lang.String username,
                  java.lang.String password,
                  java.lang.String servicetype)
           throws MgpException
Description copied from interface: MgpSessionI
Login to the server using username and password for authentication.

Specified by:
login in interface MgpSessionI
Parameters:
username - the EMG username, sent as Mgp.MGP_OPTION_USERNAME
password - the EMG password, sent as Mgp.MGP_OPTION_PASSWORD
servicetype - the systemtype, sent as Mgp.MGP_OPTION_SYSTEMTYPE
Throws:
MgpException - if a communication error occurs

login

public void login(java.lang.String username,
                  java.lang.String password)
           throws MgpException
Login to the server using username and password for authentication. Overload for login(String,String,String), using null as the system type.

Specified by:
login in interface MgpSessionI
Throws:
MgpException

logoff

public void logoff()
            throws MgpException
Logoff from the server.

Sends the operation Mgp.MGP_OP_LOGOFF to the EMG server.

Specified by:
logoff in interface MgpSessionI
Throws:
MgpException

reload

public void reload()
            throws MgpException
Reload the configuration on the server, while keeping the connection open.

Sends the operation Mgp.MGP_OP_RELOAD to the EMG server.

Specified by:
reload in interface MgpSessionI
Throws:
MgpException

sendMessage

public int sendMessage(Message msg)
                throws MgpException
Description copied from interface: MgpSessionI
Send a message.

Destination address and message data is mandatory. This is checked before the message is sent. If the message is not valid an exception is thrown.

Returns the message id on success, otherwise an MgpException is thrown.

Specified by:
sendMessage in interface MgpSessionI
Parameters:
msg - A message with message text, destination address etc set
Throws:
MgpException - if a communication error occurs

queryMessage

public int queryMessage(int id)
                 throws MgpException
Description copied from interface: MgpSessionI
Query the server for status of a message, identified by the specified id.

Returns message status on success, otherwise an MgpException is thrown.

Specified by:
queryMessage in interface MgpSessionI
Parameters:
id - the unique identifier of the message, returned by MgpSessionI.sendMessage(Message)
Throws:
MgpException - if the message isn't found on the server, or a communication error occurs

deleteMessage

public void deleteMessage(int id)
                   throws MgpException
Description copied from interface: MgpSessionI
Delete a message.

Shortcut for deleting the message identified by the specified id.

Returns message status on success, otherwise an MgpException is thrown.

Specified by:
deleteMessage in interface MgpSessionI
Parameters:
id - the unique identifier of the message, returned by MgpSessionI.sendMessage(Message)
Throws:
MgpException - if the message isn't found on the server, or a communication error occurs

dbexec

public java.sql.ResultSet dbexec(java.lang.String profile,
                                 java.lang.String sql)
                          throws MgpException,
                                 java.sql.SQLException
Description copied from interface: MgpSessionI
Send an SQL string to be executed on the EMG server.

Specified by:
dbexec in interface MgpSessionI
Parameters:
profile - the database profile name
sql - the SQL string
Returns:
the selected data, or an empty ResultSet if the SQL didn't produce any data (such as an INSERT or UPDATE).
Throws:
MgpException
java.sql.SQLException

isAdmin

public boolean isAdmin()
Description copied from interface: MgpSessionI
Returns whether user in session is an administrator.

Specified by:
isAdmin in interface MgpSessionI

getAdminFlags

public int getAdminFlags()
Description copied from interface: MgpSessionI
Gets the full administration flags for the currently logged in user.

Specified by:
getAdminFlags in interface MgpSessionI

getPermissions

public int getPermissions()
Description copied from interface: MgpSessionI
Gets the permissions for the currently logged in user.

The value is a combined bitmask of MGP_ACCESS_* from Mgp.

Specified by:
getPermissions in interface MgpSessionI

havePermission

public boolean havePermission(int functions)
Description copied from interface: MgpSessionI
Checks if the current user has all of the given permissions.

Specified by:
havePermission in interface MgpSessionI
See Also:
MgpSessionI.havePermission(int, boolean)

havePermission

public boolean havePermission(int mask,
                              boolean all)
Description copied from interface: MgpSessionI
Checks if the current user has any or all of the given permissions. The permissions should be from the MGP_ACCESS_* constants in Mgp.

Specified by:
havePermission in interface MgpSessionI
Parameters:
mask - the bitmask of permissions
all - true if all permissions must be present

getClientConfig

public java.lang.String getClientConfig()
Description copied from interface: MgpSessionI
Gets the client configuration string.

Specified by:
getClientConfig in interface MgpSessionI

setDebug

public void setDebug(boolean on)
Description copied from interface: MgpSessionI
Enables/disables runtime debug output.

Specified by:
setDebug in interface MgpSessionI
Parameters:
on - true for debug out, false if not

getLoginOptions

public OptionHolder getLoginOptions()
Description copied from interface: MgpSessionI
Gets the current login options.

Specified by:
getLoginOptions in interface MgpSessionI

getRemoteIp

public java.lang.String getRemoteIp()
Description copied from interface: MgpSessionI
The IP number of this client, as seen by the server.

Specified by:
getRemoteIp in interface MgpSessionI