~kirkland/eucalyptus/label-metadata

« back to all changes in this revision

Viewing changes to clc/modules/core/src/edu/ucsb/eucalyptus/cloud/entities/Counters.java

  • Committer: graziano obertelli
  • Date: 2009-01-07 03:32:35 UTC
  • Revision ID: graziano@cs.ucsb.edu-20090107033235-oxhuexp18v8hg0pw
Tags: 1.4
from CVS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Software License Agreement (BSD License)
 
3
 *
 
4
 * Copyright (c) 2008, Regents of the University of California
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use of this software in source and binary forms, with or
 
8
 * without modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * * Redistributions of source code must retain the above
 
12
 *   copyright notice, this list of conditions and the
 
13
 *   following disclaimer.
 
14
 *
 
15
 * * Redistributions in binary form must reproduce the above
 
16
 *   copyright notice, this list of conditions and the
 
17
 *   following disclaimer in the documentation and/or other
 
18
 *   materials provided with the distribution.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
21
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
23
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
24
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
25
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
26
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
27
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
28
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
29
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
30
 * POSSIBILITY OF SUCH DAMAGE.
 
31
 *
 
32
 * Author: Chris Grzegorczyk grze@cs.ucsb.edu
 
33
 */
 
34
 
 
35
package edu.ucsb.eucalyptus.cloud.entities;
 
36
 
 
37
import edu.ucsb.eucalyptus.keys.Hashes;
 
38
import org.hibernate.*;
 
39
import org.hibernate.annotations.*;
 
40
 
 
41
import javax.persistence.*;
 
42
import javax.persistence.Entity;
 
43
import javax.persistence.Table;
 
44
import java.util.List;
 
45
import java.util.zip.Adler32;
 
46
 
 
47
@Entity
 
48
@Table( name = "counters" )
 
49
@Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
 
50
public class Counters {
 
51
 
 
52
  @Id
 
53
  @Column( name = "id" )
 
54
  private Long id = -1l;
 
55
  @Column( name = "msg_count" )
 
56
  private Long messageId;
 
57
 
 
58
  public Counters()
 
59
  {
 
60
    this.id = 0l;
 
61
    this.messageId = 0l;
 
62
  }
 
63
 
 
64
  public Long getMessageId()
 
65
  {
 
66
    return messageId;
 
67
  }
 
68
 
 
69
  public void setMessageId( Long messageId )
 
70
  {
 
71
    this.messageId = messageId;
 
72
  }
 
73
 
 
74
  @Transient
 
75
  private static long tempId = -1;
 
76
  @Transient
 
77
  private static Adler32 digest = new Adler32();
 
78
  public static synchronized long getIdBlock( int length )
 
79
  {
 
80
    ensurePersistence( );
 
81
    long oldTemp = tempId;
 
82
    tempId+=length;
 
83
    return oldTemp;
 
84
  }
 
85
 
 
86
  public synchronized static String getNextId()
 
87
  {
 
88
    ensurePersistence( );
 
89
    tempId++;
 
90
    return Hashes.getDigestBase64( Long.toString( tempId ), Hashes.Digest.SHA512, false ).replaceAll( "\\.","" );
 
91
  }
 
92
 
 
93
  private static void ensurePersistence( )
 
94
  {
 
95
    long modulus = 100l;
 
96
    if ( tempId < 0 )
 
97
    {
 
98
      Counters find = null;
 
99
      EntityManager em = EntityWrapper.getEntityManagerFactory().createEntityManager(  );
 
100
      Session session = (Session) em.getDelegate();
 
101
      List<Counters> found = ( List<Counters> ) session.createSQLQuery( "select * from COUNTERS" ).addEntity( Counters.class ).list();
 
102
      if( found.isEmpty() )
 
103
      {
 
104
        EntityTransaction tx = em.getTransaction();
 
105
        tx.begin();
 
106
        Counters newCounters = new Counters();
 
107
        em.persist(newCounters);
 
108
        em.flush();
 
109
        tx.commit();
 
110
        find = newCounters;
 
111
      }
 
112
      else
 
113
        find = found.get(0);
 
114
      tempId = find.getMessageId() + modulus;
 
115
      em.close();
 
116
    }
 
117
    else if ( tempId % modulus == 0 )
 
118
    {
 
119
      EntityManager em = EntityWrapper.getEntityManagerFactory().createEntityManager(  );
 
120
      Session session = (Session) em.getDelegate();
 
121
      Transaction tx = session.beginTransaction();
 
122
      tx.begin();
 
123
      Counters find = ( Counters ) session.createSQLQuery( "select * from COUNTERS" ).addEntity( Counters.class ).list().get( 0 );
 
124
      tempId += modulus;
 
125
      find.setMessageId( tempId );
 
126
      session.save( find );
 
127
      tx.commit();
 
128
      em.close();
 
129
    }
 
130
  }
 
131
}