KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jass > as > ActivityIdGenerator


1 /**
2  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3  -
4  - JASS: Java Advanced tranSaction Support
5  -
6  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7  -
8  - This module was originally developed by
9  -
10  - LSD (Distributed Systems Lab, http://lsd.ls.fi.upm.es/lsd/lsd.htm)
11  - at Universidad Politecnica de Madrid (UPM) as an ObjectWeb Consortium
12  - (http://www.objectweb.org) project.
13  -
14  - This project has been partially funded by the European Commission under
15  - the IST programme of V FP grant IST-2001-37126 and by the Spanish
16  - Ministry of Science & Technology (MCyT) grants TIC2002-10376-E and
17  - TIC2001-1586-C03-02
18  -
19  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20  - The original code and portions created by LSD are
21  - Copyright (c) 2004 LSD (UPM)
22  - All rights reserved.
23  -
24  - Redistribution and use in source and binary forms, with or without
25  - modification, are permitted provided that the following conditions are met:
26  -
27  - -Redistributions of source code must retain the above copyright notice, this
28  - list of conditions and the following disclaimer.
29  -
30  - -Redistributions in binary form must reproduce the above copyright notice,
31  - this list of conditions and the following disclaimer in the documentation
32  - and/or other materials provided with the distribution.
33  -
34  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35  - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
38  - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39  - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40  - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41  - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42  - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43  - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44  - POSSIBILITY OF SUCH DAMAGE.
45  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46  -
47  - Author: Francisco Perez Sorrosal (frperezs)
48  -
49  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50 */

51
52 package org.objectweb.jass.as;
53
54 import java.net.InetAddress JavaDoc;
55 import java.net.UnknownHostException JavaDoc;
56
57 import org.apache.log4j.Logger;
58
59 /**
60  * Implements the Id Generator service.
61  * @author fran
62  * Date: Feb 16, 2004
63  * org.objectweb.jass.asActivityIdGenerator.java
64  */

65 public class ActivityIdGenerator implements java.io.Serializable JavaDoc {
66
67     private static Logger log = Logger.getLogger(ActivityIdGenerator.class);
68     private static ActivityIdGenerator singleton = null;
69
70     // Attributes -------------------------------------------------------------
71

72     private String JavaDoc baseGlobalId;
73     private long globalIdNumber = 0;
74     private ActivityIdImpl lastId = null;
75
76     // Constructors -----------------------------------------------------------
77

78     /**
79      * Default private constructor
80      */

81     private ActivityIdGenerator() {
82
83         try {
84             baseGlobalId = InetAddress.getLocalHost().getHostName() + "/";
85         } catch (UnknownHostException JavaDoc e) {
86             baseGlobalId = "localhost/";
87         }
88     }
89
90     // My Public --------------------------------------------------------------
91

92     /**
93      * Returns the unique id generator service instance.
94      * @return The unique instance.
95      */

96     public static ActivityIdGenerator getSingleton() {
97
98         if (singleton == null)
99             singleton = new ActivityIdGenerator();
100
101         return singleton;
102     }
103
104     /**
105      * Creates a new activity identifier.
106      * @return the new activity identifier.
107      */

108     public ActivityIdImpl newActivityId() {
109
110         String JavaDoc id = baseGlobalId + Long.toString(getNextId());
111         byte[] globalId = id.getBytes();
112         ActivityIdImpl aid = new ActivityIdImpl(globalId);
113         log.debug("New Activity Id Created: " + aid.print());
114         lastId = aid;
115         
116         return aid;
117     }
118
119     /**
120      * Returns the last generated activity id.
121      * @return the last generated id.
122      */

123     public ActivityIdImpl getLastId() {
124         return lastId;
125     }
126     
127     // My Protected -----------------------------------------------------------
128

129     // My Private -------------------------------------------------------------
130

131     /**
132      * Returns the next id number.
133      * @return the next id number.
134      */

135     private synchronized long getNextId() {
136
137         return globalIdNumber++;
138     }
139
140 }
141
Popular Tags