KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > jackass > daemon > IDManager


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.jackass.daemon;
6
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileNotFoundException JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.Properties JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 /**
17  * IDManager keeps a persistent table of component names and
18  * object IDs to be used to redeploy components when the daemon starts.
19  * This is necessary because the the object ID must always be the same.
20  * <p>
21  * This class is a Singleton. To obtain the singleton object use the static
22  * method getManager.
23  *
24  * The persistance scheme used in this implementation is inadecuate for
25  * real use. It should be replaced with a more adecuate implementation.
26  *
27  * @author Carlos Arévalo
28  */

29 public final class IDManager
30 {
31     private static final Log LOG = LogFactory.getLog(IDManager.class);
32
33     private static final String JavaDoc ID_SEQUENCE_NUMBER = "ID_sequence_number";
34     private static final String JavaDoc OBJECT_ID_FILENAME = "ObjectID.db";
35     private static final String JavaDoc SEPARATOR = ".";
36     private static IDManager manager = null;
37
38     private Properties JavaDoc objectIDDatabase;
39
40     /**
41      * Returns the singlenton IDManager
42      * @return the singleton IDManager
43      */

44     public static IDManager getInstance()
45     {
46         if (manager == null)
47         {
48             manager = new IDManager();
49         }
50         return manager;
51     }
52
53     /**
54      * Class constructor
55      */

56     private IDManager()
57     {
58         try
59         {
60             objectIDDatabase = new Properties JavaDoc();
61             FileInputStream JavaDoc in = new FileInputStream JavaDoc(OBJECT_ID_FILENAME);
62             objectIDDatabase.load(in);
63         }
64         catch (FileNotFoundException JavaDoc e)
65         {
66             LOG.error("Object ID file not found");
67         }
68         catch (IOException JavaDoc e)
69         {
70             LOG.error("Unable to read Object ID file");
71         }
72     }
73
74     /**
75      * Returns the object ID given the application and component name
76      * @param applicationName the application name
77      * @param componentName the component name
78      * @return a byte array representing the object ID
79      */

80     public byte[] getObjectID(String JavaDoc applicationName, String JavaDoc componentName)
81     {
82         String JavaDoc idString = (String JavaDoc) this.objectIDDatabase.get(applicationName + SEPARATOR + componentName);
83         return idString.getBytes();
84     }
85
86     /**
87      * Store an object ID given the application and component name
88      * @param applicationName the application name
89      * @param componentName the component name
90      * @param objectID a byte array representing the object ID
91      */

92     public void storeObjectID(String JavaDoc applicationName, String JavaDoc componentName, byte[] objectID)
93     {
94         String JavaDoc key = applicationName + SEPARATOR + componentName;
95         String JavaDoc id = new String JavaDoc(objectID);
96         this.objectIDDatabase.put(key, id);
97         FileOutputStream JavaDoc out;
98         try
99         {
100             out = new FileOutputStream JavaDoc(OBJECT_ID_FILENAME);
101             this.objectIDDatabase.store(out, "Component ID database. Do not edit");
102         }
103         catch (FileNotFoundException JavaDoc e)
104         {
105             LOG.error("Unable to create file ObjecID.db");
106             e.printStackTrace();
107         }
108         catch (IOException JavaDoc e)
109         {
110             LOG.error("Unable to create file ObjecID.db");
111             e.printStackTrace();
112         }
113     }
114
115     /**
116      * Removes an object ID
117      * @param applicationName the application name
118      * @param componentName the component name
119      */

120     public void removeObjectID(String JavaDoc applicationName, String JavaDoc componentName)
121     {
122         String JavaDoc key = applicationName + SEPARATOR + componentName;
123         this.objectIDDatabase.remove(key);
124         FileOutputStream JavaDoc out;
125         try
126         {
127             out = new FileOutputStream JavaDoc(OBJECT_ID_FILENAME);
128             this.objectIDDatabase.store(out, "Component ID database. Do not edit");
129         }
130         catch (FileNotFoundException JavaDoc e)
131         {
132             LOG.error("Unable to create file ObjecID.db");
133             e.printStackTrace();
134         }
135         catch (IOException JavaDoc e)
136         {
137             LOG.error("Unable to create file ObjecID.db");
138             e.printStackTrace();
139         }
140     }
141
142     /**
143      * Generates an object ID and updates the sequence number used to genrerate the ID
144      * @return a byte array representing the generated ID
145      */

146     public byte[] generateObjectId()
147     {
148         long idSequence = 0;
149         String JavaDoc str = (String JavaDoc) objectIDDatabase.get(ID_SEQUENCE_NUMBER);
150         if (str != null)
151         {
152             idSequence = Long.parseLong(str);
153         }
154         idSequence++;
155         if (idSequence >= Long.MAX_VALUE)
156         {
157             idSequence = 0;
158         }
159         String JavaDoc s = Long.toString(idSequence);
160         this.objectIDDatabase.put(ID_SEQUENCE_NUMBER, s);
161         FileOutputStream JavaDoc out;
162         try
163         {
164             out = new FileOutputStream JavaDoc(OBJECT_ID_FILENAME);
165             this.objectIDDatabase.store(out, "Component ID database. Do not edit");
166         }
167         catch (FileNotFoundException JavaDoc e)
168         {
169             LOG.error("Unable to create file ObjecID.db");
170             e.printStackTrace();
171         }
172         catch (IOException JavaDoc e)
173         {
174             LOG.error("Unable to create file " + OBJECT_ID_FILENAME);
175             e.printStackTrace();
176         }
177         return s.getBytes();
178     }
179
180 }
181
Popular Tags