01: /**********************************************************************
02: Copyright (c) 2005 Andy Jefferson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15:
16: Contributors:
17: ...
18: **********************************************************************/package org.jpox.store.poid;
19:
20: import java.net.InetAddress;
21: import java.util.Properties;
22:
23: import org.jpox.util.TypeConversionHelper;
24:
25: /**
26: * Poid generator for a UUID format. To be extended by implementations
27: * giving the UUID in particular forms.
28: *
29: * @version $Revision: 1.3 $
30: */
31: public abstract class AbstractUUIDPoidGenerator extends
32: AbstractUIDPoidGenerator {
33: /** IP Address of local machine. */
34: protected static final int IP_ADDRESS;
35:
36: static {
37: // Calculate the IP address of this machine for use in the UUID
38: int ipAddr = 0;
39: try {
40: ipAddr = TypeConversionHelper
41: .getIntFromByteArray(InetAddress.getLocalHost()
42: .getAddress());
43: } catch (Exception e) {
44: ipAddr = 0;
45: }
46:
47: IP_ADDRESS = ipAddr;
48: }
49:
50: /** Unique value across JVMs on this machine. */
51: protected static final int JVM_UNIQUE = (int) (System
52: .currentTimeMillis() >>> 8);
53:
54: protected static short counter = (short) 0;
55:
56: /**
57: * Constructor.
58: * @param name Symbolic name for this generator
59: * @param props Properties controlling its behaviour
60: */
61: public AbstractUUIDPoidGenerator(String name, Properties props) {
62: super (name, props);
63: }
64:
65: /**
66: * Simple counter for identities.
67: * @return The next count value
68: */
69: protected short getCount() {
70: // We could move this to the individual implementing classes
71: // so they have their own count
72: synchronized (AbstractUUIDPoidGenerator.class) {
73: if (counter < 0) {
74: counter = 0;
75: }
76: return counter++;
77: }
78: }
79: }
|