protected void encryptWithIPWorks(File[] files, File fSendZip, String sPassword) throws IPWorksZipException, IOException
{
Zip zip = new Zip();
zip.setArchiveFile(fSendZip.getAbsolutePath());
System.out.println("Encrypting to strength: " + zipEncrypt);
zip.setEncryptionAlgorithm(zipEncrypt);
zip.setPassword(sPassword);
for ( int i = 0; i < files.length; ++i )
{
zip.includeFiles(files[i].getCanonicalPath());
}
zip.compress();
}
shared items
Friday, September 5, 2008
check dir or create it
protected void checkDir(File dir) throws IOException
{
if (!dir.exists())
dir.mkdir();
if (!dir.isDirectory() )
throw new IOException("Working dir specified is not a directory");
if (!dir.canWrite())
throw new IOException("Working dir is not writable");
}
{
if (!dir.exists())
dir.mkdir();
if (!dir.isDirectory() )
throw new IOException("Working dir specified is not a directory");
if (!dir.canWrite())
throw new IOException("Working dir is not writable");
}
copy File
protected void copyFile(File in, File out) throws IOException
{
byte [] buf = new byte[1024];
int n=0;
try
{
FileInputStream fin = new FileInputStream(in);
FileOutputStream fout = new FileOutputStream(out);
while ((n = fin.read(buf)) > 0)
fout.write(buf, 0, n);
fin.close();
fout.close();
}
catch (IOException e)
{
throw new IOException(e.getMessage());
}
return;
}
{
byte [] buf = new byte[1024];
int n=0;
try
{
FileInputStream fin = new FileInputStream(in);
FileOutputStream fout = new FileOutputStream(out);
while ((n = fin.read(buf)) > 0)
fout.write(buf, 0, n);
fin.close();
fout.close();
}
catch (IOException e)
{
throw new IOException(e.getMessage());
}
return;
}
XML Transformer
protected String transform(String xslFile, Document doc, Request req)
{
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss\tzzzz");
SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
InputSource is = new InputSource(new StringReader(JDOMUtils.serialize(doc)));
Source xml = new SAXSource(is);
StringWriter output=null;
try
{
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer trans = tfactory.newTransformer(new StreamSource(xslFile));
output = new StringWriter();
trans.setParameter("request-date", sdf2.format(req.getCreation()));
trans.transform(xml,new StreamResult(output));
}
catch (TransformerConfigurationException e)
{
System.out.println("Configuration exception " + e.getMessageAndLocation());
}
catch (TransformerException e)
{
System.out.println("Transformer exception " + e.getMessageAndLocation());
System.out.println("Line: " + e.getLocator().getLineNumber() + "Col: " + e.getLocator().getColumnNumber());
}
return output.toString();
}
{
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss\tzzzz");
SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
InputSource is = new InputSource(new StringReader(JDOMUtils.serialize(doc)));
Source xml = new SAXSource(is);
StringWriter output=null;
try
{
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer trans = tfactory.newTransformer(new StreamSource(xslFile));
output = new StringWriter();
trans.setParameter("request-date", sdf2.format(req.getCreation()));
trans.transform(xml,new StreamResult(output));
}
catch (TransformerConfigurationException e)
{
System.out.println("Configuration exception " + e.getMessageAndLocation());
}
catch (TransformerException e)
{
System.out.println("Transformer exception " + e.getMessageAndLocation());
System.out.println("Line: " + e.getLocator().getLineNumber() + "Col: " + e.getLocator().getColumnNumber());
}
return output.toString();
}
Wednesday, September 3, 2008
Exception class
public class NXExceptionReport
{
public static void recordEvent(String strHeading, String strMessage)
{
NXExceptionReport report = new NXExceptionReport(strHeading);
report.appendMessage(strMessage);
report.report();
}
public static void recordException(String strHeading, Throwable t, boolean bTrace)
{
NXExceptionReport report = new NXExceptionReport(strHeading, t);
report.setShowStackTrace(bTrace);
report.report();
}
public static void recordAdminException(String strHeading, Throwable t, boolean bTrace)
{
NXExceptionReport report = new NXExceptionReport(strHeading, t);
report.setShowStackTrace(bTrace);
report.setEmailAdmin(true);
report.report(true);
}
public static void recordAdminEvent(String strHeading, String strMessage)
{
NXExceptionReport report = new NXExceptionReport(strHeading);
report.appendMessage(strMessage);
report.setEmailAdmin(true);
report.report(true);
}
public static void recordException(Throwable t, boolean bTrace)
{
recordException("EXCEPTION", t, bTrace);
}
public NXExceptionReport(String heading, Throwable t)
{
this.heading = heading;
this.throwable = t;
}
public NXExceptionReport(String heading)
{
this.heading = heading;
}
public void appendMessage(Object message)
{
messages.addElement(message);
}
public void appendMessageTimestamp(Object message)
{
java.text.DateFormat tf = java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG);
StringBuffer sb = new StringBuffer(tf.format(new Date()));
sb.append(": ");
sb.append(message);
messages.addElement(sb.toString());
}
public void report()
{
report(false);
}
public void report(boolean bemail)
{
try
{
java.text.DateFormat df = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.FULL, java.text.DateFormat.FULL);
String strDate = df.format(new Date());
java.io.StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println("................................................................................................");
pw.println(". .");
pw.println(". .");
pw.print(strDate);
pw.print(": ");
pw.println(heading);
Iterator iter = messages.iterator();
while ( iter.hasNext() )
{
pw.println(iter.next());
}
if ( getClazz() != null )
{
pw.println("Recording Class: " + getClazz().getName());
}
if ( throwable != null )
{
pw.print("Original Exception ");
pw.print(throwable.getClass().getName());
pw.print(" [");
pw.print(throwable.getMessage());
pw.println("]");
if ( isShowStackTrace() )
{
pw.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Begin Stack Trace <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
throwable.printStackTrace(pw);
pw.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> End Stack Trace <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
}
pw.println(". .");
pw.println(". .");
pw.println("................................................................................................");
pw.close();
String strMessage = sw.toString();
System.err.println(strMessage);
if ( bemail || (throwable != null && this.isEmailException()) )
{
emailException(strMessage);
}
if ( this.isEmailAdmin() )
{
this.emailAdmin(strMessage);
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
/**
* @param strMessage
*/
private void emailException(String strMessage)
{
try
{
ResourceBundle rb = ResourceBundle.getBundle("ospp");
String strList = rb.getString("exception_email_list");
String strOriginator = rb.getString("exception_originator");
StringTokenizer st = new StringTokenizer(strList, ";:");
Vector vSend = new Vector();
while ( st.hasMoreTokens() )
{
dao.beans.NXUser user = new dao.beans.NXUser();
user.setEmail(st.nextToken());
user.setRealName("OSPP Technician");
vSend.addElement(user);
}
if ( !vSend.isEmpty() )
{
NXEmail email = new NXEmail();
email.setRecipients(vSend);
email.setBody(strMessage);
email.setSubject("OSPP Exception");
email.setOriginatorName("OSPP Watch");
email.setOriginatorEmail(strOriginator);
email.send();
}
}
catch ( Exception e )
{
// do nothing
}
}
private void emailAdmin(String strMessage)
{
try
{
ResourceBundle rb = ResourceBundle.getBundle("ospp");
String strList = rb.getString("exception_email_admins");
String strOriginator = rb.getString("exception_originator");
StringTokenizer st = new StringTokenizer(strList, ";:");
Vector vSend = new Vector();
while ( st.hasMoreTokens() )
{
dao.beans.NXUser user = new dao.beans.NXUser();
user.setEmail(st.nextToken());
user.setRealName("OSPP Administrator");
vSend.addElement(user);
}
if ( !vSend.isEmpty() )
{
NXEmail email = new NXEmail();
email.setRecipients(vSend);
email.setBody(strMessage);
email.setSubject("OSPP Exception");
email.setOriginatorName("OSPP Watch");
email.setOriginatorEmail(strOriginator);
email.send();
}
}
catch ( Exception e )
{
// do nothing
}
}
/**
* @return
*/
public boolean isShowStackTrace()
{
return showStackTrace;
}
/**
* @param b
*/
public void setShowStackTrace(boolean b)
{
showStackTrace = b;
}
/**
* @return
*/
public Throwable getThrowable()
{
return throwable;
}
/**
* @param throwable
*/
public void setThrowable(Throwable throwable)
{
this.throwable = throwable;
}
/**
* @return
*/
public Class getClazz()
{
return clazz;
}
/**
* @param class1
*/
public void setClazz(Class class1)
{
clazz = class1;
}
/**
* @return
*/
public boolean isEmailException()
{
return emailException;
}
/**
* @param b
*/
public void setEmailException(boolean b)
{
emailException = b;
}
/**
* @return Returns the emailAdmin.
*/
public boolean isEmailAdmin()
{
return emailAdmin;
}
/**
* @param emailAdmin The emailAdmin to set.
*/
public void setEmailAdmin( boolean emailAdmin )
{
this.emailAdmin = emailAdmin;
}
private String heading;
private Throwable throwable;
private Vector messages = new Vector();
private boolean showStackTrace;
private Class clazz;
private boolean emailException = true;
private boolean emailAdmin;
}
{
public static void recordEvent(String strHeading, String strMessage)
{
NXExceptionReport report = new NXExceptionReport(strHeading);
report.appendMessage(strMessage);
report.report();
}
public static void recordException(String strHeading, Throwable t, boolean bTrace)
{
NXExceptionReport report = new NXExceptionReport(strHeading, t);
report.setShowStackTrace(bTrace);
report.report();
}
public static void recordAdminException(String strHeading, Throwable t, boolean bTrace)
{
NXExceptionReport report = new NXExceptionReport(strHeading, t);
report.setShowStackTrace(bTrace);
report.setEmailAdmin(true);
report.report(true);
}
public static void recordAdminEvent(String strHeading, String strMessage)
{
NXExceptionReport report = new NXExceptionReport(strHeading);
report.appendMessage(strMessage);
report.setEmailAdmin(true);
report.report(true);
}
public static void recordException(Throwable t, boolean bTrace)
{
recordException("EXCEPTION", t, bTrace);
}
public NXExceptionReport(String heading, Throwable t)
{
this.heading = heading;
this.throwable = t;
}
public NXExceptionReport(String heading)
{
this.heading = heading;
}
public void appendMessage(Object message)
{
messages.addElement(message);
}
public void appendMessageTimestamp(Object message)
{
java.text.DateFormat tf = java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG);
StringBuffer sb = new StringBuffer(tf.format(new Date()));
sb.append(": ");
sb.append(message);
messages.addElement(sb.toString());
}
public void report()
{
report(false);
}
public void report(boolean bemail)
{
try
{
java.text.DateFormat df = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.FULL, java.text.DateFormat.FULL);
String strDate = df.format(new Date());
java.io.StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println("................................................................................................");
pw.println(". .");
pw.println(". .");
pw.print(strDate);
pw.print(": ");
pw.println(heading);
Iterator iter = messages.iterator();
while ( iter.hasNext() )
{
pw.println(iter.next());
}
if ( getClazz() != null )
{
pw.println("Recording Class: " + getClazz().getName());
}
if ( throwable != null )
{
pw.print("Original Exception ");
pw.print(throwable.getClass().getName());
pw.print(" [");
pw.print(throwable.getMessage());
pw.println("]");
if ( isShowStackTrace() )
{
pw.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Begin Stack Trace <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
throwable.printStackTrace(pw);
pw.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> End Stack Trace <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
}
pw.println(". .");
pw.println(". .");
pw.println("................................................................................................");
pw.close();
String strMessage = sw.toString();
System.err.println(strMessage);
if ( bemail || (throwable != null && this.isEmailException()) )
{
emailException(strMessage);
}
if ( this.isEmailAdmin() )
{
this.emailAdmin(strMessage);
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
/**
* @param strMessage
*/
private void emailException(String strMessage)
{
try
{
ResourceBundle rb = ResourceBundle.getBundle("ospp");
String strList = rb.getString("exception_email_list");
String strOriginator = rb.getString("exception_originator");
StringTokenizer st = new StringTokenizer(strList, ";:");
Vector vSend = new Vector();
while ( st.hasMoreTokens() )
{
dao.beans.NXUser user = new dao.beans.NXUser();
user.setEmail(st.nextToken());
user.setRealName("OSPP Technician");
vSend.addElement(user);
}
if ( !vSend.isEmpty() )
{
NXEmail email = new NXEmail();
email.setRecipients(vSend);
email.setBody(strMessage);
email.setSubject("OSPP Exception");
email.setOriginatorName("OSPP Watch");
email.setOriginatorEmail(strOriginator);
email.send();
}
}
catch ( Exception e )
{
// do nothing
}
}
private void emailAdmin(String strMessage)
{
try
{
ResourceBundle rb = ResourceBundle.getBundle("ospp");
String strList = rb.getString("exception_email_admins");
String strOriginator = rb.getString("exception_originator");
StringTokenizer st = new StringTokenizer(strList, ";:");
Vector vSend = new Vector();
while ( st.hasMoreTokens() )
{
dao.beans.NXUser user = new dao.beans.NXUser();
user.setEmail(st.nextToken());
user.setRealName("OSPP Administrator");
vSend.addElement(user);
}
if ( !vSend.isEmpty() )
{
NXEmail email = new NXEmail();
email.setRecipients(vSend);
email.setBody(strMessage);
email.setSubject("OSPP Exception");
email.setOriginatorName("OSPP Watch");
email.setOriginatorEmail(strOriginator);
email.send();
}
}
catch ( Exception e )
{
// do nothing
}
}
/**
* @return
*/
public boolean isShowStackTrace()
{
return showStackTrace;
}
/**
* @param b
*/
public void setShowStackTrace(boolean b)
{
showStackTrace = b;
}
/**
* @return
*/
public Throwable getThrowable()
{
return throwable;
}
/**
* @param throwable
*/
public void setThrowable(Throwable throwable)
{
this.throwable = throwable;
}
/**
* @return
*/
public Class getClazz()
{
return clazz;
}
/**
* @param class1
*/
public void setClazz(Class class1)
{
clazz = class1;
}
/**
* @return
*/
public boolean isEmailException()
{
return emailException;
}
/**
* @param b
*/
public void setEmailException(boolean b)
{
emailException = b;
}
/**
* @return Returns the emailAdmin.
*/
public boolean isEmailAdmin()
{
return emailAdmin;
}
/**
* @param emailAdmin The emailAdmin to set.
*/
public void setEmailAdmin( boolean emailAdmin )
{
this.emailAdmin = emailAdmin;
}
private String heading;
private Throwable throwable;
private Vector messages = new Vector();
private boolean showStackTrace;
private Class clazz;
private boolean emailException = true;
private boolean emailAdmin;
}
some more java
try
{
String sclass = ResourceBundle.getBundle("version-index").getString("index.type." + docType);
Class cls = Class.forName(sclass);
Class[] classes = { Properties.class };
Object[] params = { props };
Constructor ct = cls.getConstructor(classes);
return (IVersionIndex) ct.newInstance(params);
}
catch ( Exception any )
{
throw new IllegalArgumentException();
}
{
String sclass = ResourceBundle.getBundle("version-index").getString("index.type." + docType);
Class cls = Class.forName(sclass);
Class[] classes = { Properties.class };
Object[] params = { props };
Constructor ct = cls.getConstructor(classes);
return (IVersionIndex) ct.newInstance(params);
}
catch ( Exception any )
{
throw new IllegalArgumentException();
}
java reflections i think
try
{
// develop class to execute file conversion
Class cls = ( strClass != null && strClass.length() > 0 )
? Class.forName(strClass)
: (nDocType == 1)
? _TypicalFileConverter.class
: _PDFFileConverter.class;
Constructor ct = cls.getConstructor(null);
iConvert = (NX_IFileConvert) ct.newInstance(null);
}
catch ( Exception e )
{
report.appendMessageTimestamp("Invalid File Conversion Class: " + strClass);
throw new IllegalArgumentException("Invalid file conversion class");
}
{
// develop class to execute file conversion
Class cls = ( strClass != null && strClass.length() > 0 )
? Class.forName(strClass)
: (nDocType == 1)
? _TypicalFileConverter.class
: _PDFFileConverter.class;
Constructor ct = cls.getConstructor(null);
iConvert = (NX_IFileConvert) ct.newInstance(null);
}
catch ( Exception e )
{
report.appendMessageTimestamp("Invalid File Conversion Class: " + strClass);
throw new IllegalArgumentException("Invalid file conversion class");
}
Subscribe to:
Posts (Atom)