001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-11-26 22:21:34 +0000 (Mon, 26 Nov 2012) $' 
007 * '$Revision: 31119 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.objectmanager.cache;
031
032import java.io.File;
033import java.io.IOException;
034import java.io.InputStream;
035import java.io.OutputStream;
036import java.io.Reader;
037import java.io.Writer;
038import java.sql.Connection;
039import java.sql.SQLException;
040import java.sql.Statement;
041
042import org.kepler.util.sql.DatabaseFactory;
043
044/**
045 * Class that represents an object in the ObjectCache. This class should be
046 * extended by each type of object that wants to control its own lifecycle
047 * events and serialization events.
048 */
049public class CacheUtil {
050        /**
051         * execute a SQL command against the hsql database
052         */
053        public static synchronized void executeSQLCommand(String sql)
054                        throws SQLException, CacheException {
055                Connection conn = null;
056                Statement st = null;
057                try {
058                        conn = DatabaseFactory.getDBConnection();
059                        st = conn.createStatement();
060                        st.execute(sql);
061                } catch (ClassNotFoundException cnfe) {
062                        throw new CacheException("Could not get the database connection: "
063                                        + cnfe.getMessage());
064                } catch (SQLException sqle) {
065                        // sqle.printStackTrace();
066                        throw sqle;
067                } finally {
068                        st.close();
069                        conn.close();
070                }
071        }
072
073        /**
074         * execute a SQL query against the hsql database
075         */
076        //comment this method out since returning a ResultSet is not good way for invocation 
077        // since it is hard to close connections. 
078//      public static synchronized ResultSet executeSQLQuery(String sql)
079//                      throws SQLException, CacheException {
080//              Connection conn = null;
081//              Statement st = null;
082//              try {
083//                      conn = DatabaseFactory.getDBConnection();
084//                      // set up the statement so that it's scrollable and not updateable
085//                      // by other resultsets
086//                      st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
087//                                      ResultSet.CONCUR_UPDATABLE);
088//                      ResultSet rs = st.executeQuery(sql);
089//                      return rs;
090//              } catch (ClassNotFoundException cnfe) {
091//                      // cnfe.printStackTrace();
092//                      throw new CacheException("Could not get the database connection: "
093//                                      + cnfe.getMessage());
094//              } catch (SQLException sqle) {
095//                      // sqle.printStackTrace();
096//                      throw sqle;
097//              }
098//      }
099
100        /**
101         * recursively cleans up the work dir, deleting old files that are no longer
102         * needed.
103         */
104        public static void cleanUpDir(File dir) {
105                String[] dirList = dir.list();
106                if (dirList != null)
107                        for (int i = 0; i < dirList.length; i++) {
108                                File f = new File(dir, dirList[i]);
109                                if (f.isDirectory()) {
110                                        cleanUpDir(f);
111                                        f.delete();
112                                } else {
113                                        f.delete();
114                                }
115                        }
116        }
117
118        /**
119         * reads bytes from in InputStream and writes them to an OutputStream.
120         * 
121         *@param is
122         *@param os
123         *@throws IOException
124         */
125        public static void writeInputStreamToOutputStream(InputStream is,
126                        OutputStream os) throws IOException {
127                byte[] b = new byte[1024];
128                int numread = is.read(b, 0, 1024);
129
130                while (numread != -1) {
131                        os.write(b, 0, numread);
132                        numread = is.read(b, 0, 1024);
133                }
134                os.flush();
135        }
136
137        /**
138         * writes the bytes in the reader to the writer.
139         * 
140         * @param r
141         *            the reader
142         * @param w
143         *            the writer
144         */
145        public static void writeReaderToWriter(Reader r, Writer w)
146                        throws IOException {
147                char[] c = new char[1024];
148                int numread = r.read(c, 0, 1024);
149                while (numread != -1) {
150                        w.write(c, 0, numread);
151                        numread = r.read(c, 0, 1024);
152                }
153                w.flush();
154        }
155}