Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.console.internaldb » [javadoc | source]
    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package org.apache.geronimo.console.internaldb;
   19   
   20   import org.slf4j.Logger;
   21   import org.slf4j.LoggerFactory;
   22   
   23   import java.io.File;
   24   import java.sql.Connection;
   25   import java.sql.SQLException;
   26   import java.sql.Statement;
   27   
   28   import javax.portlet.PortletRequest;
   29   
   30   import org.apache.geronimo.console.BasePortlet;
   31   
   32   import org.apache.geronimo.kernel.util.InputUtils;
   33   
   34   public class RunSQLHelper {
   35   
   36       private static final Logger log = LoggerFactory.getLogger(RunSQLHelper.class);
   37   
   38       private static final String DERBY_BACKUP_FOLDER = "derby.backup";
   39   
   40       private static final String PARENT_FOLDER = "..";
   41   
   42       private static final String BAK_EXTENSION = ".bak";
   43   
   44       private static final String BAK_PREFIX = "BAK_";
   45       
   46       private final BasePortlet portlet;
   47   
   48       public RunSQLHelper (BasePortlet portlet) {
   49           this.portlet = portlet;
   50       }
   51       
   52       public boolean createDB(String dbName, PortletRequest request) {
   53   
   54           // ensure there are no illegal chars in DB name
   55           InputUtils.validateSafeInput(dbName);
   56   
   57           Connection conn = null;
   58           try {
   59               conn = DerbyConnectionUtil.getDerbyConnection(dbName,
   60                       DerbyConnectionUtil.CREATE_DB_PROP);
   61               portlet.addInfoMessage(request, portlet.getLocalizedString(request, "sysdb.infoMsg01", dbName));
   62               return true;
   63           } catch (Throwable e) {
   64               portlet.addErrorMessage(request, portlet.getLocalizedString(request, "sysdb.errorMsg01"), e.getMessage());
   65               return false;
   66           } finally {
   67               // close DB connection
   68               try {
   69                   if (conn != null) {
   70                       conn.close();
   71                   }
   72               } catch (SQLException e) {
   73                   portlet.addErrorMessage(request, portlet.getLocalizedString(request, "sysdb.errorMsg02"), e.getMessage());
   74               }
   75           }        
   76       }
   77   
   78       public boolean backupDB(String derbyHome, String dbName, PortletRequest request) {
   79           return false;
   80       }
   81   
   82       public boolean restoreDB(String derbyHome, String dbName, PortletRequest request) {
   83           return false;
   84       }
   85   
   86       public boolean deleteDB(String derbyHome, String dbName, PortletRequest request) {
   87           // shutdown database before deleting it
   88           if (!shutdownDB(dbName)) {
   89               portlet.addErrorMessage(request, portlet.getLocalizedString(request, "sysdb.errorMsg03"));
   90               return false;
   91           }
   92   
   93           try {
   94               // create backup folder if not created
   95               File derbyBackupFolder = new File(derbyHome + File.separatorChar
   96                       + PARENT_FOLDER + File.separatorChar + DERBY_BACKUP_FOLDER);
   97               if (!derbyBackupFolder.exists()) {
   98                   if (!derbyBackupFolder.mkdirs()) {
   99                       portlet.addErrorMessage(request, portlet.getLocalizedString(request, "sysdb.errorMsg04"));
  100                       return false;
  101                   }
  102               }
  103   
  104               File oldDBFolder = new File(derbyHome + File.separatorChar + dbName);
  105               if (oldDBFolder.exists()) {
  106                   // Need to add a prefix because File.createTempFile's first
  107                   // argument must be a String at least three characters long.
  108                   File tmpFile = File.createTempFile(BAK_PREFIX + dbName,
  109                           BAK_EXTENSION, derbyBackupFolder);
  110                   File newDBFolder = new File(tmpFile.getAbsolutePath());
  111                   /*
  112                    * Delete temp file and create a temp folder using the temp
  113                    * filename
  114                    */
  115                   if (tmpFile.delete()) {
  116                       if (newDBFolder.mkdirs()) {
  117                           if (!oldDBFolder.renameTo(new File(newDBFolder,
  118                                   oldDBFolder.getName()))) {
  119                               portlet.addErrorMessage(request, portlet.getLocalizedString(request, "sysdb.errorMsg05"));
  120                               return false;
  121                           }
  122                       }
  123                   }
  124               }
  125               portlet.addInfoMessage(request, portlet.getLocalizedString(request, "sysdb.infoMsg02", dbName));
  126               return true;
  127           } catch (Exception e) {
  128               portlet.addErrorMessage(request, portlet.getLocalizedString(request, "sysdb.errorMsg06"), e.getMessage());
  129               return false;
  130           }
  131       }
  132   
  133       public boolean runSQL(String connName, String sql, Boolean dsConn, PortletRequest request) {
  134           if ((sql == null) || (sql.trim().length() == 0)) {
  135               portlet.addErrorMessage(request, portlet.getLocalizedString(request, "sysdb.errorMsg07"));
  136               return false;
  137           }
  138           
  139           Connection conn = null;
  140           Statement s = null;
  141           try {
  142               if (dsConn) {
  143                   conn = DerbyConnectionUtil.getDataSourceConnection(connName);
  144               } else {
  145                   conn = DerbyConnectionUtil.getDerbyConnection(connName);
  146               }
  147               conn.setAutoCommit(false);
  148   
  149               s = conn.createStatement();
  150               String[] sqlCmds = sql.split(";");
  151               for (int i = 0; i < sqlCmds.length; i++) {
  152                   if (sqlCmds[i].trim().length() > 0) {
  153                       // debug printout (remove later)
  154                       log.debug("SQL" + i + ": <" + sqlCmds[i].trim() + ">");
  155                       s.execute(sqlCmds[i]);
  156                   }
  157               }
  158               conn.commit();
  159               portlet.addInfoMessage(request, portlet.getLocalizedString(request, "sysdb.infoMsg03"));
  160               return true;
  161           } catch (SQLException e) {
  162               try {
  163                   conn.rollback();
  164               } catch (Exception unHandledException) {
  165               }
  166               portlet.addErrorMessage(request, portlet.getLocalizedString(request, "sysdb.errorMsg08"), e.getMessage());
  167               return false;
  168           } catch (Throwable e) {
  169               portlet.addErrorMessage(request, portlet.getLocalizedString(request, "sysdb.errorMsg08"), e.getMessage());
  170               return false;
  171           } finally {
  172               // close DB connection
  173               try {
  174                   if (s != null) {
  175                       s.close();
  176                   }
  177                   if (conn != null) {
  178                       conn.setAutoCommit(true);
  179                       conn.close();
  180                   }
  181               } catch (SQLException e) {
  182                   portlet.addErrorMessage(request, portlet.getLocalizedString(request, "sysdb.errorMsg02"), e.getMessage());
  183               }
  184           }
  185       }
  186   
  187       private boolean shutdownDB(String dbName) {
  188           boolean ok = true;
  189   
  190           boolean gotSQLExc = false;
  191           try {
  192               DerbyConnectionUtil.getDerbyConnection(dbName,
  193                       DerbyConnectionUtil.SHUTDOWN_DB_PROP);
  194           } catch (SQLException se) {
  195               gotSQLExc = true;
  196           }
  197   
  198           if (!gotSQLExc) {
  199               ok = false;
  200           }
  201   
  202           return ok;
  203       }
  204   
  205   }

Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.console.internaldb » [javadoc | source]