//package com.java2s;/* **********************************************************************
/*
* NOTE: This copyright does *not* cover user programs that use Hyperic
* program services by normal system calls through the application
* program interfaces provided as part of the Hyperic Plug-in Development
* Kit or the Hyperic Client Development Kit - this is merely considered
* normal use of the program, and does *not* fall under the heading of
* "derived work"./*www.java2s.com*/
*
* Copyright (C) [2004-2012], VMware, Inc.
* This file is part of Hyperic.
*
* Hyperic is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. 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 GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
publicclass Main {
publicstaticfinalint NOOP_INSTRUCTION_FLAG = 0;
/**
* Fully reads the given file content and returns it (to be used be small files only).
* @param file
* @return full content of the file
* @throws IOException
*/publicstaticfinalString getFileContent(finalFile file) throwsIOException {FileInputStream fis = null;
String content = null;
try {
fis = newFileInputStream(file);
finalbyte arrBytes[] = newbyte[fis.available()];
fis.read(arrBytes);
content = newString(arrBytes);
} finally {
close(fis);
} //EO catch block return content;
}
/**
* Closes all formal argument objects with no special operations.<br/>
* Supported types:<br/>
* - {@link Connection}<br/>
* - {@link PreparedStatement}<br/>
* - {@link OutputStream}<br/>
* - {@link InputStream}<br/>
* <p>
* <b>Note:</b> Exceptions are silenced.
* </p>
* @param closeables list of closeables
*/publicstaticfinalvoid close(finalObject... closeables) {
close(NOOP_INSTRUCTION_FLAG, closeables);
}
/**
* Closes all formal argument objects with no special operations.<br/>
* Supported types:<br/>
* - {@link Connection}<br/>
* - Special Instructions:<br/>
* - {@value #COMMIT_INSTRUCTION_FLAG}<br/>
* - {@value #ROLLBACK_INSTRUCTION_FLAG}<br/>
* - {@link PreparedStatement}<br/>
* - {@link OutputStream}<br/>
* - {@link InputStream}<br/>
* <p>
* <b>Note:</b> Exceptions are silenced.
* </p>
* @param closeables list of closeables
*/publicstaticfinalvoid close(finalint specialInstructionsMask, finalObject... closeables) {
for (Object closeable : closeables) {
if (closeables == null)
continue;
try {
if ((closeable instanceofConnection)) {
Connection conn = (Connection) closeable;
try {
if ((specialInstructionsMask & 0x4) == 4)
conn.commit();
elseif ((specialInstructionsMask & 0x8) == 8)
conn.rollback();
} catch (Throwable t2) {
printStackTrace(t2);
} //EO inner catch block
((Connection) closeable).close();
} elseif ((closeable instanceofPreparedStatement)) {
((PreparedStatement) closeable).close();
} elseif ((closeable instanceofResultSet)) {
((ResultSet) closeable).close();
} elseif ((closeable instanceofWriter)) {
finalWriter writer = (Writer) closeable;
writer.flush();
writer.close();
} elseif ((closeable instanceofReader)) {
((Reader) closeable).close();
} elseif ((closeable instanceofOutputStream)) {
finalOutputStream os = (OutputStream) closeable;
os.flush();
os.close();
} elseif ((closeable instanceofInputStream)) {
finalInputStream is = (InputStream) closeable;
is.close();
} //EO else if inputstreamelseif ((closeable instanceofCloseable)) {
((Closeable) closeable).close();
} //EO else if Closeable
} catch (Throwable t) {
printStackTrace(t);
} //EO catch block
} //EO while there are more closeables
}
/**
* @param exception to print
*/publicstaticfinalvoid printStackTrace(finalThrowable exception) {
printStackTrace(exception, null);
}
/**
* Prints the exception to the {@link System#err} stream with an optional prefixMessage<br/>
* <b>Note:</b> If {@link SQLException}, prints all its linked exceptions as well.
* @param exception to print
* @param prefixMessage optional message to output to {@link System#err} stream
*/publicstaticfinalvoid printStackTrace(finalThrowable exception, finalString prefixMessage) {
if (prefixMessage != null)
System.err.println(prefixMessage);
if ((exception instanceofSQLException)) {
SQLException sqle = (SQLException) exception;
Throwable t = null;
do {
t = sqle;
t.printStackTrace();
} while (((sqle = sqle.getNextException()) != null) && (sqle != t));
} else {
exception.printStackTrace();
} //EO if instanceof SQLException
}
}