Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: FBUserManager.java 56681 2009-10-08 13:48:36Z tsteinmaurer $
   * 
   * Firebird Open Source J2EE Connector - JDBC Driver
   *
   * Distributable under LGPL license.
   * You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * LGPL License for more details.
  *
  * This file was created by members of the firebird development team.
  * All individual contributions remain the Copyright (C) of those
  * individuals.  Contributors to this file are either listed here or
  * can be obtained from a CVS history command.
  *
  * All rights reserved.
  */
 package org.firebirdsql.management;
 
 import java.util.Map;
 
Implements the display/add/delete/modify user functionality of the Firebird Services API.

Author(s):
Steven Jardine
 
 public class FBUserManager extends FBServiceManager implements UserManager {
 
     private int count = 0;
     private String securityDatabase = null;

    
Create a new instance of FBMaintenanceManager based on the default GDSType.
 
     public FBUserManager()
     {
     	super();
     }

    
Create a new instance of FBMaintenanceManager based on a given GDSType.

Parameters:
gdsType type must be PURE_JAVA, EMBEDDED, or NATIVE
 
     public FBUserManager(String gdsType)
     {
     	super(gdsType);
     }

    
Create a new instance of FBMaintenanceManager based on a given GDSType.

Parameters:
gdsType The GDS implementation type to use
 
     public FBUserManager(GDSType gdsType){
         super(gdsType);
     }

    
Parses the displayBuffer and creates a map of users.

Returns:
a map of users parsed from the displayBuffer.
Throws:
java.sql.SQLException
java.io.IOException
 
     private Map getFBUsers() throws SQLExceptionIOException {
         
         User user = null;
 
         Map users = new TreeMap();
 
         byte[] displayBuffer = ((ByteArrayOutputStream)getLogger()).toByteArray();
 
          = 0;
         while ( < (displayBuffer.length)
                 && displayBuffer[] != .) {
 
             switch (displayBuffer[]) {
            case .:
                if (user != null) {
                    users.put(user.getUserName(), user);
                }
                user = new FBUser();
                user.setUserName(getSRBString(displayBuffer));
                break;
            case .:
                user.setFirstName(getSRBString(displayBuffer));
                break;
            case .:
                user.setMiddleName(getSRBString(displayBuffer));
                break;
            case .:
                user.setLastName(getSRBString(displayBuffer));
                break;
            case .:
                user.setUserId(getSRBInteger(displayBuffer));
                break;
            case .:
                user.setGroupId(getSRBInteger(displayBuffer));
                break;
            default:
                ++;
                break;
            }
        }
        users.put(user.getUserName(), user);
        return users;
        
    }

    
Generate the service request buffer for the specified action.

Parameters:
action
username
password
firstname
middlename
lastname
userid
groupid
Returns:
the service request buffer for the specified action.
    private ServiceRequestBuffer getUserSRB(int actionUser user) {
        ServiceRequestBuffer srb = getGds().createServiceRequestBuffer(action);
        if (user.getUserName() != null)
            srb.addArgument(.user
                    .getUserName());
        if (user.getPassword() != null)
            srb.addArgument(.user
                    .getPassword());
        if (user.getFirstName() != null)
            srb.addArgument(.user
                    .getFirstName());
        if (user.getMiddleName() != null)
            srb.addArgument(.user
                    .getMiddleName());
        if (user.getLastName() != null)
            srb.addArgument(.user
                    .getLastName());
        if (user.getUserId() != -1)
            srb.addArgument(.user.getUserId());
        if (user.getGroupId() != -1)
            srb.addArgument(.user
                    .getGroupId());
        return srb;
        
    }

    
Returns an integer from ther service request buffer. Integers are 4 bytes in length.

Parameters:
displayBuffer
Returns:
an integer from ther service request buffer.
    private int getSRBInteger(byte[] displayBuffer) {
        
         += 1;
        int integer = getGds().iscVaxInteger(displayBuffer, 4);
         += 4;
        return integer;
        
    }

    
Returns a string from ther service request buffer.

Parameters:
displayBuffer
Returns:
an string from ther service request buffer.
    private String getSRBString(byte[] displayBuffer) {
        
         += 1;
        int length = getGds().iscVaxInteger(displayBuffer, 2);
         += 2;
        String string = new String(displayBufferlength);
         += length;
        return string;
        
    }
    
    
Sets the security database in the service request buffer, in case it is provided.

Parameters:
srb - ServiceRequestBuffer
        if ( != null)
    }

    
Perform the specified action.

Parameters:
action
username
password
firstname
middlename
lastname
userid
groupid
groupname
sqlrole
Throws:
java.sql.SQLException
java.io.IOException
    private void userAction(int actionUser userthrows SQLException,
            IOException {
    	
    	GDS gds = getGds();
        
        try {
            IscSvcHandle handle = attachServiceManager(gds);
            try {
            	ServiceRequestBuffer srb = getUserSRB(actionuser);
            	setSecurityDatabaseArgument(srb);
                gds.iscServiceStart(handlesrb);
                queueService(gdshandle);
            } finally {
                detachServiceManager(gdshandle);
            }
        } catch (GDSException ex) {
            throw new FBSQLException(ex);
        }
        
    }
    /*
     * (non-Javadoc)
     * 
     * @see org.firebirdsql.management.UserManager#add(org.firebirdsql.management.User)
     */
    public void add(User userthrows SQLExceptionIOException {
        if (user.getUserName() == null)
            throw new FBSQLException("UserName is required.");
    }
    /*
     * (non-Javadoc)
     * 
     * @see org.firebirdsql.management.UserManager#delete(org.firebirdsql.management.User)
     */
    public void delete(User userthrows SQLExceptionIOException {
        if (user.getUserName() == null)
            throw new FBSQLException("UserName is required.");
        // Only parameter for delete action is username. All others should be
        // null.
        User delUser = new FBUser();
        delUser.setUserName(user.getUserName());
    }
    /*
     * (non-Javadoc)
     * 
     * @see org.firebirdsql.management.UserManager#update(org.firebirdsql.management.User)
     */
    public void update(User userthrows SQLExceptionIOException {
        if (user.getUserName() == null)
            throw new FBSQLException("UserName is required.");
    }
    /*
     * (non-Javadoc)
     * 
     * @see org.firebirdsql.management.UserManager#getUsers()
     */
    public Map getUsers() throws SQLExceptionIOException {
        
        OutputStream savedStream = getLogger();
        
        setLogger(new ByteArrayOutputStream());
        try {
            return getFBUsers();
        } finally {
            setLogger(savedStream);
        }
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see org.firebirdsql.management.UserManager#setSecurityDatabase(String securityDatabase)
     */
	public void setSecurityDatabase(String securityDatabase) {
		this. = securityDatabase;
	}


Services API execution for setting and dropping the auto admin role mapping

	private void adminRoleAction(int actionthrows SQLExceptionIOException {
		GDS gds = getGds();
        
        try {
            IscSvcHandle handle = attachServiceManager(gds);
            try {
            	ServiceRequestBuffer srb = getGds().createServiceRequestBuffer(action);
            	setSecurityDatabaseArgument(srb);
                gds.iscServiceStart(handlesrb);
                queueService(gdshandle);
            } finally {
                detachServiceManager(gdshandle);
            }
        } catch (GDSException ex) {
            throw new FBSQLException(ex);
        }
	}
    /*
     * (non-Javadoc)
     * 
     * @see org.firebirdsql.management.UserManager#setAdminRoleMapping()
     */
    public void setAdminRoleMapping() throws SQLExceptionIOException {
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see org.firebirdsql.management.UserManager#dropAdminRoleMapping()
     */
    public void dropAdminRoleMapping() throws SQLExceptionIOException {
    }
New to GrepCode? Check out our FAQ X