sessionId产生方法参考
Posted onsessionId产生方法参考 - balaschen - JavaEye技术网站
balaschen
永久域名 http://balaschen.javaeye.com/
rad rail plugin | Spring Hibernate SessionFactory配置
2007-05-28
sessionId产生方法参考
tomcat java 代码
- protected synchronized String generateSessionId() {
- byte random[] = new byte[16];
- // Render the result as a String of hexadecimal digits
- StringBuffer result = new StringBuffer();
- int resultLenBytes = 0;
- while (resultLenBytes < this.sessionIdLength) {
- getRandomBytes(random);
- random = getDigest().digest(random);
- for (int j = 0;
- j < random.length && resultLenBytes < this.sessionIdLength;
- j++) {
- byte b1 = (byte) ((random[j] & 0xf0) >> 4);
- byte b2 = (byte) (random[j] & 0x0f);
- if (b1 < 10)
- result.append((char) ('0' + b1));
- else
- result.append((char) ('A' + (b1 - 10)));
- if (b2 < 10)
- result.append((char) ('0' + b2));
- else
- result.append((char) ('A' + (b2 - 10)));
- resultLenBytes++;
- }
- }
- return (result.toString());
- }
hibernate uuid: java 代码
- public abstract class AbstractUUIDGenerator implements IdentifierGenerator {
- private static final int IP;
- static {
- int ipadd;
- try {
- ipadd = BytesHelper.toInt( InetAddress.getLocalHost().getAddress() );
- }
- catch (Exception e) {
- ipadd = 0;
- }
- IP = ipadd;
- }
- private static short counter = (short) 0;
- private static final int JVM = (int) ( System.currentTimeMillis() >>> 8 );
- public AbstractUUIDGenerator() {
- }
- ///
- /* Unique across JVMs on this machine (unless they load this class
- /* in the same quater second - very unlikely)
- /*/
- protected int getJVM() {
- return JVM;
- }
- ///
- /* Unique in a millisecond for this JVM instance (unless there
- /* are > Short.MAX_VALUE instances created in a millisecond)
- /*/
- protected short getCount() {
- synchronized(AbstractUUIDGenerator.class) {
- if (counter<0) counter=0;
- return counter++;
- }
- }
- ///
- /* Unique in a local network
- /*/
- protected int getIP() {
- return IP;
- }
- ///
- /* Unique down to millisecond
- /*/
- protected short getHiTime() {
- return (short) ( System.currentTimeMillis() >>> 32 );
- }
- protected int getLoTime() {
- return (int) System.currentTimeMillis();
- }
- }
- public class UUIDHexGenerator extends AbstractUUIDGenerator implements Configurable {
- private String sep = "";
- protected String format(int intval) {
- String formatted = Integer.toHexString(intval);
- StringBuffer buf = new StringBuffer("00000000");
- buf.replace( 8-formatted.length(), 8, formatted );
- return buf.toString();
- }
- protected String format(short shortval) {
- String formatted = Integer.toHexString(shortval);
- StringBuffer buf = new StringBuffer("0000");
- buf.replace( 4-formatted.length(), 4, formatted );
- return buf.toString();
- }
- public Serializable generate(SessionImplementor session, Object obj) {
- return new StringBuffer(36)
- .append( format( getIP() ) ).append(sep)
- .append( format( getJVM() ) ).append(sep)
- .append( format( getHiTime() ) ).append(sep)
- .append( format( getLoTime() ) ).append(sep)
- .append( format( getCount() ) )
- .toString();
- }
- public void configure(Type type, Properties params, Dialect d) {
- sep = PropertiesHelper.getString("separator", params, "");
- }
- public static void main( String[] args ) throws Exception {
- Properties props = new Properties();
- props.setProperty("separator", "/");
- IdentifierGenerator gen = new UUIDHexGenerator();
- ( (Configurable) gen ).configure(Hibernate.STRING, props, null);
- IdentifierGenerator gen2 = new UUIDHexGenerator();
- ( (Configurable) gen2 ).configure(Hibernate.STRING, props, null);
- for ( int i=0; i<10; i++) {
- String id = (String) gen.generate(null, null);
- System.out.println(id);
- String id2 = (String) gen2.generate(null, null);
- System.out.println(id2);
- }
- }
- }
rad rail plugin | Spring Hibernate SessionFactory配置
发表评论
您还没有登录,请登录后发表评论(快捷键 Alt+S / Ctrl+Enter)
balaschen
搜索本博客
最近访客 >>更多访客
博客分类
- 全部博客 (51)
- 生活 (0)
- ror (3)
- 综合 (18)
- spring (2)
- MVC (3)
- 测试 (3)
- hibernate (1)
- 系统(安装配置) (3)
-
其他分类
我的收藏 (156)
- 我的论坛主题贴 (63)
- 我的所有论坛贴 (203)
- 我的精华良好贴 (3)
最近加入圈子
最新评论
- FreeMarker解析字符串模板 Map valuesMap = HashMap(); valuesMap.put( ... -- by 抛出异常的爱
- 添加用户、修改ad密码 是的,确实是证书没正确导入。后来我解决了。请问,我能不能添加或删除一个Group ... -- by Leecupn
- 添加用户、修改ad密码 应该是证书没正确导入到keyStord指定的位置 -- by balaschen
- 添加用户、修改ad密码 你这明显是SSL没配置好。 -- by balaschen
添加用户、修改ad密码 你好,我用你的方法,但是不知道怎么搞的,报以下异常: Exception in ... -- by Leecupn
评论排行榜
- [什么是RSS?]
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。若作者同意转载,必须以超链接形式标明文章原始出处和作者。 © 2003-2009 JavaEye.com. All rights reserved. 上海炯耐计算机软件有限公司 [ 沪ICP备05023328号 ]