Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id$
   *
   * Firebird Open Source JavaEE 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 source control 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
Jmx.mbean:
 
 public class FBManager implements FBManagerMBean {
 
     private static final int DEFAULT_PORT = 3050;
     private final static Logger log = LoggerFactory.getLogger(FBManager.classfalse);
 
     private GDS gds;
     private DatabaseParameterBuffer c;
     private String host = "localhost";
     private Integer port;
     private String fileName;
     private String userName;
     private String password;
     private int dialect = .;
     private boolean forceCreate;
     private boolean createOnStart;
     private boolean dropOnStop;
     private String state = ;
     private static final String STOPPED = "Stopped";
     private static final String STARTED = "Started";
     private GDSType type;
 
     public FBManager() {
         this(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();
         .addArgument(.new byte[] { 120, 10, 0, 0 });
 
          = ;
         if (isCreateOnStart()) {
             createDatabase(getFileName(), getUserName(), getPassword());
         }
     }

    

Jmx.managed:
operation
 
     public void stop() throws Exception {
         if (isDropOnStop()) {
             dropDatabase(getFileName(), getUserName(), getPassword());
         }
 
          = 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 ;
    }

    

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 String getType() {
        return this..toString();
    }
    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;
    }

    
Sets the dialect.

Parameters:
dialect Database dialect (1 or 3)
Throws:
java.lang.IllegalArgumentException if value is not 1 or 3
    public void setDialect(int dialect) {
        if (!(dialect == 1 || dialect == 3)) throw new IllegalArgumentException("Only dialect 1 or 3 allowed");
        this. = dialect;
    }
    public int getDialect() {
        return ;
    }

    
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 passwordthrows Exception {
        IscDbHandle 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 passwordthrows 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;
        }
    }
    public boolean isDatabaseExists(String fileNameString userString passwordthrows Exception {
        IscDbHandle 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 String getConnectString(String filenamethrows GDSException {
        return GDSFactory.getDatabasePath(filename);
    }
New to GrepCode? Check out our FAQ X