Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: FBServiceManager.java 56918 2012-03-10 17:38:21Z mrotteveel $
   * 
   * 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;
 
 
An implementation of the basic Firebird Service API functionality.

Author(s):
Roman Rokytskyy
 
 public class FBServiceManager implements ServiceManager {
 
     private String user;
     private String password;
 
     private String database;
     
     private String host;
     private int port = 3050;
     
     private OutputStream logger;
 
     private GDS gds;
     
     public final static int BUFFER_SIZE = 1024; //1K
 
    
Create a new instance of FBServiceManager based on the default GDSType.
 
     protected FBServiceManager() {
     	this. = GDSFactory.getGDSForType(GDSFactory.getDefaultGDSType());
     }

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

Parameters:
gdsType type must be PURE_JAVA, EMBEDDED, or NATIVE
 
     protected FBServiceManager(String gdsType) {
     	this. = GDSFactory.getGDSForType(GDSType.getType(gdsType));
     }

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

Parameters:
gdsType The GDS implementation type to use
 
     protected FBServiceManager(GDSType gdsType) {
         this. = GDSFactory.getGDSForType(gdsType);
     }

    
Set the name of the user that performs the operation.

Parameters:
user name of the user.
 
     public void setUser(String user) {
         this. = user;
     }

    
Get name of the user that performs the operation.

Returns:
name of the user that performs the operation.
 
     public String getUser() {
        return ;
    }

    

Parameters:
password The password to set.
    public void setPassword(String password) {
        this. = password;
    }

    

Returns:
Returns the password.
    public String getPassword() {
        return ;
    }
    public void setDatabase(String database) {
        this. = database;
    }
    public String getDatabase() {
        return ;
    }
    
    

Returns:
Returns the host.
    public String getHost() {
        return ;
    }
    

Parameters:
host The host to set.
    public void setHost(String host) {
        this. = host;
    }
    

Returns:
Returns the port.
    public int getPort() {
        return ;
    }
    

Parameters:
port The port to set.
    public void setPort(int port) {
        this. = port;
    }
    

Returns:
Returns the out.
    public synchronized OutputStream getLogger() {
        return ;
    }
    
    

Parameters:
logger The out to set.
    public synchronized void setLogger(OutputStream logger) {
        this. = logger;
    }

    
Get org.firebirdsql.gds.GDS implementation depending on the type specified during instantiation.

Returns:
instance of org.firebirdsql.gds.GDS.
    public GDS getGds() {
        return ;
    }
    
    public String getServiceName() {
        StringBuffer sb = new StringBuffer();
        if (getHost() != null) {
            
            sb.append(getHost());
            
            if (getPort() != 3050) {
                sb.append("/");
                sb.append(getPort());
            }
            
            sb.append(":");
        }
        sb.append("service_mgr");
        return sb.toString();
    }
    
    public IscSvcHandle attachServiceManager(GDS gdsthrows GDSException {
        ServiceParameterBuffer serviceParameterBuffer = 
            gds.createServiceParameterBuffer();
    
        if (getUser() != null)
            serviceParameterBuffer.addArgument(
                .getUser());
        
        if (getPassword() != null)
            serviceParameterBuffer.addArgument(
                .getPassword());
        serviceParameterBuffer.addArgument(
            .new byte[]{120, 10, 0, 0});
        final IscSvcHandle handle = gds.createIscSvcHandle();
        gds.iscServiceAttach(getServiceName(), handleserviceParameterBuffer);
        
        return handle;
    }
    
    public void detachServiceManager(GDS gdsIscSvcHandle handlethrows GDSException {
        gds.iscServiceDetach(handle);
    }
    public void queueService(GDS gdsIscSvcHandle handlethrows GDSExceptionFBSQLExceptionIOException {
        
        OutputStream currentLogger = getLogger();
        
        int bufferSize = ;        
        byte[] buffer = new byte[bufferSize];
        
        boolean processing = true;
        while (processing) {
            gds.iscServiceQuery(handlegds.createServiceParameterBuffer() , infoSRBbuffer);
    
            switch(buffer[0]) {
            
                case . :
                
                    int dataLength = (buffer[1] & 0xff) | ((buffer[2] & 0xff) << 8);
                    if (dataLength == 0) {
                        if (buffer[3] != .)
                            throw new FBSQLException("Unexpected end of stream reached.");
                        else {
                            processing = false;
                            break;
                        }
                    }
                    if (currentLogger != null)
                        currentLogger.write(buffer, 3, dataLength);
                    
                    break;
                    
                case . :
                    bufferSize = bufferSize * 2;
                    buffer = new byte[bufferSize];
                    break;
                    
                case . : 
                    processing = false;
                    break;
            }
        }
    }

    
Execute a Services API operation in the database. All output from the operation is sent to this ServiceManager's logger.

Parameters:
srb The buffer containing the task request
Throws:
org.firebirdsql.jdbc.FBSQLException if a database access error occurs or incorrect parameters are supplied
    protected void executeServicesOperation(ServiceRequestBuffer srb)
            throws FBSQLException {
        try {
            IscSvcHandle svcHandle = attachServiceManager();
            try {
                .iscServiceStart(svcHandlesrb);
                queueService(svcHandle);
            } finally {
                detachServiceManager(svcHandle);
            }
        } catch (GDSException gdse){
            throw new FBSQLException(gdse);
        } catch (IOException ioe){
            throw new FBSQLException(ioe);
        }
    }

    
Build up a request buffer for the specified operation.

Parameters:
operation The isc_action_svc_* operation
options The options bitmask for the request buffer
    protected ServiceRequestBuffer createRequestBuffer(int operation
                                                        int options){
        ServiceRequestBuffer srb = .createServiceRequestBuffer(operation);
        srb.addArgument(.options);
        return srb;
    }
New to GrepCode? Check out our FAQ X