Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
   * 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;
 
The class FBManager is a simple jmx mbean that allows you to create and drop databases. in particular, they can be created and dropped using the jboss service lifecycle operations start and stop.

Author(s):
David Jencks
Version:
1.0
Jmx.mbean:
 
 public class FBManager implements FBManagerMBean
 	{
     
     private static final int DEFAULT_PORT = 3050;
     private final static Logger log = LoggerFactory.getLogger(FBManager.class,false);
 
     private GDS gds;
 
     private DatabaseParameterBuffer c;
 
 
     private String host = "localhost";
 
     private Integer port;
 
     private String fileName;
 
     private String userName;
 
     private String password;
 
     private boolean forceCreate = false;
 
     private boolean createOnStart = false;
 
     private boolean dropOnStop = false;
 
     private String state = ;
 
     private static final String STOPPED = "Stopped";
     private static final String STARTED = "Started";
 
 
 
     private GDSType type;
 
 
     public FBManager()
         {
         this(((AbstractGDS)GDSFactory.getDefaultGDS()).getType());
         }
 
     public FBManager(GDSType type)
         {
         this. = type;
         }
 
     public FBManager(String type) {
         this. = GDSType.getType(type);
     }
 
 
 
 
 	//Service methods
     

Jmx.managed:
operation
 
     public void start() throws Exception {
          = GDSFactory.getGDSForType();
       
         = .createDatabaseParameterBuffer();
//        c.addArgument(DatabaseParameterBuffer.NUM_BUFFERS, new byte[] {90});
        .addArgument(.new byte[] {120, 10, 0, 0});
        .addArgument(.new byte[] {3, 0, 0, 0});
         = ;
        if (isCreateOnStart())
        {
            createDatabase(getFileName(), getUserName(), getPassword());
        } // end of if ()
    }

    

Jmx.managed:
operation
    public void stop() throws Exception {
        if (isDropOnStop())
        {
            dropDatabase(getFileName(), getUserName(), getPassword());
        } // end of if ()
         = null;
        .close();
         = null;
         = ;
    }


    

Jmx.managed:
attribute
    public String getState()
    {
        return ;
    }


    

Jmx.managed:
attribute
    public String getName() {
        return "Firebird Database manager";
    }
    //Firebird specific methods
    //Which server are we connecting to?

    

Jmx.managed:
attribute
    public void setServer(final String host) {
        this. = host;
    }

    

Jmx.managed:
attribute
    public String getServer() {
        return ;
    }
    public String getType() {
        return this..toString();
    }

    

Jmx.managed:
attribute
    public void setPort(int port) {
        this. = new Integer(port);
    }

    

Jmx.managed:
attribute
    public int getPort() {
        return  != null ? .intValue() : ;
    }



    
mbean get-set pair for field fileName Get the value of fileName

Returns:
value of fileName
Jmx:
managed-attribute
    public String getFileName()
    {
        return ;
    }
    public void setType(String type) {
        final GDSType gdsType = GDSType.getType(type);
        if (gdsType == null)
            throw new RuntimeException("Unrecognized type '"+type+"'");
        this. = gdsType;
    }


    
Set the value of fileName

Parameters:
fileName Value to assign to fileName
Jmx:
managed-attribute
    public void setFileName(final String fileName)
    {
        this. = fileName;
    }




    
mbean get-set pair for field userName Get the value of userName

Returns:
value of userName
Jmx:
managed-attribute
    public String getUserName()
    {
        return ;
    }


    
Set the value of userName

Parameters:
userName Value to assign to userName
Jmx:
managed-attribute
    public void setUserName(final String userName)
    {
        this. = userName;
    }



    
mbean get-set pair for field password Get the value of password

Returns:
value of password
Jmx:
managed-attribute
    public String getPassword()
    {
        return ;
    }


    
Set the value of password

Parameters:
password Value to assign to password
Jmx:
managed-attribute
    public void setPassword(final String password)
    {
        this. = password;
    }




    
mbean get-set pair for field createOnStart Get the value of createOnStart

Returns:
value of createOnStart
Jmx:
managed-attribute
    public boolean isCreateOnStart()
    {
        return ;
    }


    
Set the value of createOnStart

Parameters:
createOnStart Value to assign to createOnStart
Jmx:
managed-attribute
    public void setCreateOnStart(final boolean createOnStart)
    {
        this. = createOnStart;
    }




    
mbean get-set pair for field dropOnStop Get the value of dropOnStop

Returns:
value of dropOnStop
Jmx:
managed-attribute
    public boolean isDropOnStop()
    {
        return ;
    }


    
Set the value of dropOnStop

Parameters:
dropOnStop Value to assign to dropOnStop
Jmx:
managed-attribute
    public void setDropOnStop(final boolean dropOnStop)
    {
        this. = dropOnStop;
    }


    
Get the ForceCreate value.

Returns:
the ForceCreate value.
Jmx:
managed-attribute
    public boolean isForceCreate() {
	return ;
    }

    
Set the ForceCreate value.

Parameters:
forceCreate The new ForceCreate value.
Jmx:
managed-attribute
    public void setForceCreate(boolean forceCreate) {
	this. = forceCreate;
    }
    //Meaningful management methods

    

Jmx.managed:
operation
    public void createDatabase (String fileNameString userString password)
        throws Exception
    {
        IscDbHandle db = null;
        db = .createIscDbHandle();
        try {
            DatabaseParameterBuffer dpb = .deepCopy();
                dpb.addArgument(.user);
                dpb.addArgument(.password);
            .iscAttachDatabase(getConnectString(fileName), dbdpb);
            
            // if forceCreate is set, drop the database correctly
            // otherwise exit, database already exists
            if ()
                .iscDropDatabase(db);
            else {
                .iscDetachDatabase(db);
                return//database exists, don't wipe it out.
            }
	    } catch (GDSException e) {
            // we ignore it
	    }
    	db = .createIscDbHandle();
        try {
            DatabaseParameterBuffer dpb = .deepCopy();
            dpb.addArgument(.user);
            dpb.addArgument(.password);
            .iscCreateDatabase(getConnectString(fileName), dbdpb);
            .iscDetachDatabase(db);
        }
        catch (Exception e) {
    	    if (!=null)
    	    {
    		.error("Exception creating database"e);
    	    }
    	    throw e;
        }
    }

    

Jmx.managed:
operation
    public void dropDatabase(String fileNameString userString password)
        throws Exception
    {
        try {
            IscDbHandle db = .createIscDbHandle();
            DatabaseParameterBuffer dpb = .deepCopy();
            dpb.addArgument(.user);
            dpb.addArgument(.password);
            .iscAttachDatabase(getConnectString(fileName), dbdpb);
            .iscDropDatabase(db);
        }
        catch (Exception e) {
            if (!=null)
            {
                .error("Exception dropping database"e);
            }
            throw e;
        }
    }
    /* (non-Javadoc)
     * @see org.firebirdsql.management.FBManagerMBean#isDatabaseExists(java.lang.String, java.lang.String, java.lang.String)
     */
    public boolean isDatabaseExists(String fileNameString user,
            String passwordthrows Exception {
        IscDbHandle db = null;
        db = .createIscDbHandle();
        try {
            DatabaseParameterBuffer dpb = .deepCopy();
                dpb.addArgument(.user);
                dpb.addArgument(.password);
            .iscAttachDatabase(getConnectString(fileName), dbdpb);
            
            .iscDetachDatabase(db);
            return true;
            
        } catch (GDSException e) {
            return false;
        }
    }
    
    //private methods
    private String getConnectString(String filenamethrows GDSException {
        return GDSFactory.getDatabasePath(filename);
    }
New to GrepCode? Check out our FAQ X