Java tutorial
/** * Copyright 2013 Jee Vang Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package demo.learn.shiro.util; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import demo.learn.shiro.constant.C; /** * SQL util. * @author Jee Vang * */ public class SqlUtil { /** * Semicolon constant. */ private static final String SEMICOLON = ";"; /** * JNDI {@link DataSource}. Created via JNDI. */ private static DataSource _jndiDataSource = null; /** * Basic {@link DataSource}. Created via a real database. */ private static DataSource _basicDataSource = null; /** * HSQL {@link DataSource}. Created in-memory. */ private static DataSource _hsqlDataSource = null; /** * A boolean indicated if HSQL database has been initialized. */ private static boolean isHsqlInitialized = false; static { if (null == _hsqlDataSource) { BasicDataSource ds = new BasicDataSource(); ds.setUsername("sa"); ds.setPassword(""); ds.setDriverClassName("org.hsqldb.jdbc.JDBCDriver"); ds.setUrl("jdbc:hsqldb:mem:secure"); ds.setMaxActive(10); _hsqlDataSource = ds; } } /** * Gets the default {@link DataSource}, which is * uses HSQL. * @return {@link DataSource}. */ public static DataSource getDefaultDataSource() { return getHsqlDataSource(); } /** * Gets the JNDI {@link DataSource}. Look at * %webroot%/META-INF/context.xml. * @return {@link DataSource}. */ public static DataSource getJndiDataSource() { if (null == _jndiDataSource) { try { Context context = new InitialContext(); _jndiDataSource = (DataSource) context.lookup(C.JNDI_DATASOURCE_NAME); } catch (Exception ex) { ex.printStackTrace(); } } return _jndiDataSource; } /** * Gets the basic {@link DataSource}. Hard-coded * in this util. There is a script, mysql.sql, to * create the MySQL database. * @return {@link DataSource}. */ public static DataSource getBasicDataSource() { if (null == _basicDataSource) { BasicDataSource ds = new BasicDataSource(); ds.setUsername("secure"); ds.setPassword("secure#123}{"); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setUrl("jdbc:mysql://mysql:3306/secure?profileSQL=false"); ds.setMaxActive(10); _basicDataSource = ds; } return _basicDataSource; } /** * Gets the HSQL {@link DataSource}. * @return {@link DataSource}. */ public synchronized static DataSource getHsqlDataSource() { if (!isHsqlInitialized) { try { String content = FileUtil.getContent(C.HSQL_RESOURCE); List<String> sqls = SqlUtil.getSqlStatements(content); Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = _hsqlDataSource.getConnection(); stmt = conn.createStatement(); for (String sql : sqls) { stmt.execute(sql); } isHsqlInitialized = true; } catch (Exception ex) { throw new RuntimeException(ex); } finally { SqlUtil.closeQuietly(conn, stmt, rs); } } catch (Exception ex) { throw new RuntimeException(ex); } } return _hsqlDataSource; } /** * Parses and gets all SQL statements from * the content. Assumes SQL statements in * content are delimited by semi-colons. * @param content Content of SQL statements. * @return {@link List} of SQL statements. */ public static List<String> getSqlStatements(String content) { List<String> list = new ArrayList<String>(); String[] tokens = content.split(SEMICOLON); for (String token : tokens) { if (null != token && !"".equals(token)) { String t = token.trim(); if (t.length() > 0) list.add(t); } } return list; } /** * Quietly closes JDBC objects (exceptions are swallowed). * @param conn {@link Connection}. * @param stmt {@link Statement}. * @param rs {@link ResultSet}. */ public static void closeQuietly(Connection conn, Statement stmt, ResultSet rs) { if (null != rs) { try { rs.close(); rs = null; } catch (Exception ex) { } } if (null != stmt) { try { stmt.close(); stmt = null; } catch (Exception ex) { } } if (null != conn) { try { conn.close(); conn = null; } catch (Exception ex) { } } } }