/sun/src/ak/webcontrol/util/UserException.java |
---|
0,0 → 1,28 |
package ak.webcontrol.util; |
public class UserException |
extends Exception |
{ |
private Object[] values; |
public UserException() |
{ |
this(null, null); |
} |
public UserException(String message) |
{ |
this(message, null); |
} |
public UserException(String message, Object[] values) |
{ |
super(message); |
this.values = values; |
} |
public Object[] getValues() |
{ |
return values; |
} |
} |
/sun/src/ak/webcontrol/util/StringConverter.java |
---|
0,0 → 1,42 |
package ak.webcontrol.util; |
public abstract class StringConverter |
{ |
public static Long parseLong(Object o) |
throws NumberFormatException |
{ |
if(o instanceof String) { |
String s = (String)o; |
if(s == null || s.equals("")) |
return null; |
else |
return new Long(s); |
} |
else { |
throw new ClassCastException("String is expected, but it is " + o.getClass()); |
} |
} |
public static Integer parseInteger(Object o) |
throws NumberFormatException |
{ |
if(o instanceof String) { |
String s = (String)o; |
if(s == null || s.equals("")) |
return null; |
else |
return new Integer(s); |
} |
else { |
throw new ClassCastException("String is expected, but it is " + o.getClass()); |
} |
} |
public static String toString(Object o) |
{ |
if(o == null) |
return null; |
else |
return o.toString(); |
} |
} |
/sun/src/ak/webcontrol/util/Digest.java |
---|
0,0 → 1,51 |
package ak.webcontrol.util; |
import java.security.MessageDigest; |
import java.security.NoSuchAlgorithmException; |
public class Digest |
{ |
/** |
* digest to encode passwords |
*/ |
protected static MessageDigest digest = null; |
private static final char[] hexDigits = { |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
}; |
public static String encode(String password) |
{ |
return bytesToHex(digest.digest(password.getBytes())); |
} |
/** |
* converts password bytes to hex string |
*/ |
protected static String bytesToHex(byte[] bytes) |
{ |
char[] buffer = new char[bytes.length * 2]; |
for (int i = 0; i < bytes.length; i++) { |
int low = (int)( bytes[i] & 0x0f); |
int high = (int)((bytes[i] & 0xf0) >> 4); |
buffer[i * 2] = hexDigits[high]; |
buffer[i * 2 + 1] = hexDigits[low]; |
} |
return new String(buffer); |
} |
/** |
* digest initialization |
*/ |
static { |
try { |
digest = MessageDigest.getInstance("MD5"); |
} |
catch(NoSuchAlgorithmException ex) { |
ex.printStackTrace(); |
} |
} |
} |
/sun/src/ak/webcontrol/util/HibernateUtil.java |
---|
0,0 → 1,109 |
package ak.webcontrol.util; |
import net.sf.hibernate.*; |
import net.sf.hibernate.cfg.*; |
public class HibernateUtil |
{ |
private static Configuration configuration; |
private static SessionFactory sessionFactory; |
private static final ThreadLocal hibernateBean = new ThreadLocal(); |
public static Configuration getConfiguration() |
throws HibernateException |
{ |
if(configuration == null) |
configuration = new Configuration(); |
return configuration; |
} |
public static SessionFactory getSessionFactory() |
throws HibernateException |
{ |
if(sessionFactory == null) |
sessionFactory = getConfiguration().configure().buildSessionFactory(); |
return sessionFactory; |
} |
private static HibernateBean currentBean() |
throws HibernateException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
if(hb == null) { |
hb = new HibernateBean(); |
hb.session = getSessionFactory().openSession(); |
hibernateBean.set(hb); |
} |
return hb; |
} |
public static Session currentSession() |
throws HibernateException |
{ |
return currentBean().session; |
} |
public static void closeSession() |
throws HibernateException, ModelException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
if(hb == null) |
throw new ModelException("No session found for this thread"); |
hibernateBean.set(null); |
hb.session.close(); |
} |
public static void beginTransaction() |
throws HibernateException, ModelException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
if(hb != null && hb.transaction != null) |
throw new ModelException("Transaction is already open"); |
currentBean().transaction = currentSession().beginTransaction(); |
} |
public static boolean isTransactionOpen() |
throws HibernateException, ModelException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
return (hb != null) && (hb.transaction != null); |
} |
public static void commitTransaction() |
throws HibernateException, ModelException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
if(hb == null || hb.transaction == null) |
throw new ModelException("No open transaction"); |
hb.transaction.commit(); |
hb.transaction = null; |
} |
public static void rollbackTransaction() |
throws HibernateException, ModelException |
{ |
HibernateBean hb = (HibernateBean)hibernateBean.get(); |
if(hb == null || hb.transaction == null) |
throw new ModelException("No open transaction"); |
hb.transaction.rollback(); |
hb.transaction = null; |
} |
static class HibernateBean |
{ |
public Session session; |
public Transaction transaction; |
} |
} |
/sun/src/ak/webcontrol/util/ModelException.java |
---|
0,0 → 1,44 |
package ak.webcontrol.util; |
public class ModelException |
extends Exception |
{ |
private Exception chainedException; |
public ModelException() |
{ |
this(null, null); |
} |
public ModelException(String message) |
{ |
this(message, null); |
} |
public ModelException(Exception chainedException) |
{ |
this(null, chainedException); |
} |
public ModelException(String message, Exception chainedException) |
{ |
super(message); |
this.chainedException = chainedException; |
} |
public Exception getChainedException() |
{ |
return chainedException; |
} |
public void setChainedException(Exception chainedException) |
{ |
this.chainedException = chainedException; |
} |
public String toString() |
{ |
return super.toString() |
+ (chainedException == null ? "" : "\n" + chainedException.toString()); |
} |
} |
/sun/src/ak/webcontrol/util/FormException.java |
---|
0,0 → 1,39 |
package ak.webcontrol.util; |
public class FormException |
extends UserException |
{ |
private String property; |
public FormException() |
{ |
this(null, null, null); |
} |
public FormException(String message) |
{ |
this(message, null, null); |
} |
public FormException(String message, String property) |
{ |
this(message, property, null); |
} |
public FormException(String message, String property, Object[] values) |
{ |
super(message, values); |
this.property = property; |
} |
public String getProperty() |
{ |
return property; |
} |
public String toString() |
{ |
return super.toString() |
+ (property == null ? "" : " for " + property); |
} |
} |