import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.util.Date;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.velocity.*;
import org.apache.velocity.app.*;
import org.apache.velocity.context.Context;
import org.apache.velocity.servlet.*;
public class Utilities {
private static Properties m_servletConfig = null;
private Utilities() {
}
static {
initJavaMail();
}
public static void debugPrintln(Object o) {
String msg = "proj debug message at " + getNowTimeString() +
" ------------- ";
System.err.println(msg + o);
}
public static Properties initServletEnvironment(VelocityServlet v) {
// init only once
if (m_servletConfig != null) {
return m_servletConfig;
}
//debugPrintln("initServletEnvironment....");
try {
/*
* call the overridable method to allow the
* derived classes a shot at altering the configuration
* before initializing Runtime
*/
Properties p = new Properties();
ServletConfig config = v.getServletConfig();
// Set the Velocity.FILE_RESOURCE_LOADED_PATH property
// to the root directory of the context.
String path = config.getServletContext().getRealPath("/");
//debugPrintln("real path of / is : " + path);
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
// Set the Velocity.RUNTIME_LOG property to be the file
// velocity.log relative to the root directory
// of the context.
p.setProperty(Velocity.RUNTIME_LOG, path +
"velocity.log");
// Return the Properties object.
//return p;
Velocity.init(p);
m_servletConfig = p;
return p;
}
catch (Exception e) {
debugPrintln(e.getMessage());
//throw new ServletException("Error initializing Velocity: " + e);
}
return null;
//this.getServletContext().getRealPath("/");
}
private static void initJavaMail() {
}
public static Connection getDatabaseConnection() {
Connection con = null;
try {
InitialContext initCtx = new InitialContext();
javax.naming.Context context = (javax.naming.Context) initCtx.
lookup("java:comp/env");
javax.sql.DataSource ds = (javax.sql.DataSource) context.lookup(
"jdbc/TestDB");
//Utilities.debugPrintln("ds = " + ds);
con = ds.getConnection();
}
catch (Exception e) {
Utilities.debugPrintln("Exception = " + e.getMessage());
return null;
}
//Utilities.debugPrintln("con = " + con);
return con;
}
public static java.sql.ResultSet excuteDbQuery(Connection con, String sql,
Object[] parameters) {
//Exception err = null;
//Utilities.debugPrintln("excuteDbQuery" + parameters[0] + " ,sql=" + sql);
try {
java.sql.PreparedStatement ps = con.prepareStatement(sql);
for (int i = 0; i < parameters.length; i++) {
processParameter(ps, i + 1, parameters[i]);
}
return ps.executeQuery();
}
catch (Exception e) {
//Utilities.debugPrintln(e.getMessage());
e.printStackTrace();
}
return null;
}
public static void excuteDbUpdate(String sql, Object[] parameters) {
Connection con = Utilities.getDatabaseConnection();
excuteDbUpdate(con, sql, parameters);
closeDbConnection(con);
}
public static void excuteDbUpdate(Connection con, String sql,
Object[] parameters) {
Exception err = null;
try {
java.sql.PreparedStatement ps = con.prepareStatement(sql);
for (int i = 0; i < parameters.length; i++) {
processParameter(ps, i + 1, parameters[i]);
}
ps.execute();
}
catch (Exception e) {
err = e;
//Utilities.debugPrintln(err.getMessage());
e.printStackTrace();
}
}
private static void processParameter(java.sql.PreparedStatement ps,
int index, Object parameter) {
try {
if (parameter instanceof String) {
ps.setString(index, (String) parameter);
}
else {
ps.setObject(index, parameter);
}
}
catch (Exception e) {
//Utilities.debugPrintln(e.getMessage());
e.printStackTrace();
}
}
public static void closeDbConnection(java.sql.Connection con) {
try {
con.close();
}
catch (Exception e) {
Utilities.debugPrintln(e.getMessage());
}
}
public static String getResultPage(
String title, String message, String jumpLink,
VelocityServlet servlet, HttpServletRequest request,
HttpServletResponse response, Context context) {
Template template = null;
context.put("MessageTitle", title);
context.put("ResultMessage", message);
context.put("JumpLink", jumpLink);
try {
template = servlet.getTemplate(
"/templates/Message.htm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
return sw.toString();
}
catch (Exception ex) {
return "error get template Message.htm " + ex.getMessage();
}
}
public static String mergeTemplate(String fileName, VelocityServlet servlet,
Context context) {
Template template = null;
try {
template = servlet.getTemplate(fileName);
StringWriter sw = new StringWriter();
template.merge(context, sw);
return sw.toString();
}
catch (Exception ex) {
return "error get template " + fileName + " " + ex.getMessage();
}
}
}