KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > repository > ConfigurationImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.repository;
24
25 import java.util.*;
26 import java.io.*;
27 import com.sun.enterprise.util.FileUtil;
28 import com.sun.enterprise.util.Utility;
29 // START OF IASRI 4660742
30
import java.util.logging.*;
31 import com.sun.logging.*;
32 // END OF IASRI 4660742
33

34 /**
35  * A Configuration object stores all the properties that are needed
36  * by various components within the EJB server.
37  * @author Harish Prabandham
38  */

39 public class ConfigurationImpl implements Configuration {
40 // IASRI 4660742 START
41
private static Logger _logger=null;
42     static{
43        _logger=LogDomains.getLogger(LogDomains.ROOT_LOGGER);
44         }
45 // IASRI 4660742 END
46
private static String JavaDoc OBJECT_STORE_DIR = "repository" + File.separator +
47     Utility.getLocalHost() +File.separator + "objects"
48         +File.separator;
49
50     /** IASRI 4672501 -- remove ri directory
51     private static String SERVER_CONFIG_DIR = "repository" + File.separator +
52     Utility.getLocalHost() + File.separator;
53     **/

54    
55     private static final String JavaDoc OBJECT_FILE_EXT = ".ser";
56     private Hashtable table;
57     private Repository defaultRepository;
58     
59     /**
60      * Creates a new instance of a Configuration object.
61      * @param readwrite => true or false
62      */

63     public ConfigurationImpl() {
64         /** IASRI 4672501 -- remove ri directory
65         File dir = new File(FileUtil.getAbsolutePath(OBJECT_STORE_DIR));
66         if(!dir.exists()) {
67         dir.mkdirs();
68         }
69         **/

70
71         table = new Hashtable();
72         defaultRepository = getRepository("default");
73         /** IASRI 4672501 -- remove ri directory
74         getServerRepository();
75         **/

76     }
77
78     private String JavaDoc getIndex(String JavaDoc key) {
79         // Everything before the first .
80
int index = key.indexOf(".");
81         
82         if(index < 0)
83         return "default";
84         
85         return key.substring(0, index);
86     }
87     
88     private String JavaDoc getEffectiveKey(String JavaDoc key) {
89         // Everything after the first .
90
int index = key.indexOf(".");
91         
92         if(index < 0)
93         return key;
94
95         return key.substring(index + 1);
96     }
97     
98     private Repository getRepository(String JavaDoc repName) {
99         Repository rep = (Repository) table.get(repName);
100         if(rep == null) {
101         rep = new Repository(repName);
102         if(rep.getName().equals(repName)) {
103             table.put(repName, rep);
104         } else
105             rep = defaultRepository;
106         }
107
108         return rep;
109     }
110
111     /** IASRI 4672501 -- remove ri directory
112     private Repository getServerRepository() {
113 // IASRI 4660742
114         // System.out.println("getServerRepository.....");
115 // START OF IASRI 4660742
116          _logger.log(Level.FINE,"getServerRepository.....");
117 // END OF IASRI 4660742
118         Repository rep = (Repository) table.get("server");
119         if(rep == null) {
120         File dir = new File(FileUtil.getAbsolutePath(SERVER_CONFIG_DIR));
121         if(!dir.exists()) {
122             dir.mkdirs();
123         }
124
125         File f = new File(dir, "server.properties");
126         if(!f.exists()) {
127             try { f.createNewFile(); } catch(IOException e){
128 //IASRI 4660742 e.printStackTrace();
129 // START OF IASRI 4660742
130             _logger.log(Level.SEVERE,"enterprise.ioexception",e);
131 // END OF IASRI 4660742
132             }
133         }
134         
135         rep = new Repository("server",
136                      FileUtil.getAbsolutePath(SERVER_CONFIG_DIR) + File.separator);
137         table.put("server", rep);
138         }
139     
140         return rep;
141     }
142     **/

143     
144     /**
145      * This method gets a property value associated with the given key.
146      * @return A property value corresponding to the key
147      */

148     public String JavaDoc getProperty(String JavaDoc key)
149         throws java.rmi.RemoteException JavaDoc
150     {
151         String JavaDoc index = getIndex(key);
152         String JavaDoc newKey = getEffectiveKey(key);
153
154         Repository rep = getRepository(index);
155
156         String JavaDoc val = null;
157         
158         if(rep.getName().equals(index)){
159             val = rep.find(newKey);
160         } else {
161             val = rep.find(key);
162         }
163
164
165         return val;
166     }
167
168     /**
169      * This method associates a property value with the given key.
170      */

171     public void setProperty(String JavaDoc key, String JavaDoc value)
172         throws java.rmi.RemoteException JavaDoc
173     {
174         String JavaDoc index = getIndex(key);
175         String JavaDoc newKey = getEffectiveKey(key);
176         Repository rep = getRepository(index);
177
178         if(rep.getName().equals(index)){
179             rep.add(newKey, value);
180         } else {
181             rep.add(key, value);
182         }
183     }
184
185     /**
186      * This method removes a property value given the key.
187      */

188     public void removeProperty(String JavaDoc key)
189         throws java.rmi.RemoteException JavaDoc
190     {
191         String JavaDoc index = getIndex(key);
192         String JavaDoc newKey = getEffectiveKey(key);
193         Repository rep = getRepository(index);
194
195         if(rep.getName().equals(index)){
196             rep.remove(newKey);
197         } else {
198             rep.remove(key);
199         }
200     }
201
202
203     public Object JavaDoc getObject(String JavaDoc key)
204         throws java.rmi.RemoteException JavaDoc
205     {
206         String JavaDoc fname = getProperty(key);
207         Object JavaDoc obj = null;
208
209         // Use this fname to deserialize...
210
if(fname != null) {
211             try{
212                 FileInputStream fstream = new FileInputStream(fname);
213                 ObjectInputStream objstream =
214                 new ObjectInputStream(fstream);
215                 obj = objstream.readObject();
216                 fstream.close();
217             }catch(Exception JavaDoc e){
218 //IASRI 4660742 e.printStackTrace(System.out);
219
// START OF IASRI 4660742
220
_logger.log(Level.SEVERE,"enterprise.file_exception",e);
221 // END OF IASRI 4660742
222
}
223         }
224
225         return obj;
226     }
227
228     /**
229      * This method associates an Object with the given key.
230      */

231     public void setObject(String JavaDoc key, Object JavaDoc obj)
232         throws java.rmi.RemoteException JavaDoc
233     {
234         String JavaDoc className = obj.getClass().getName();
235         String JavaDoc instanceId = String.valueOf(obj.hashCode());
236         String JavaDoc fname = OBJECT_STORE_DIR + className + instanceId +
237                         OBJECT_FILE_EXT;
238
239         // serialize obj and store it in the file .....
240

241         try{
242         String JavaDoc absFileName = FileUtil.getAbsolutePath(fname);
243         FileOutputStream fstream = new FileOutputStream(absFileName);
244         ObjectOutputStream objstream = new ObjectOutputStream(fstream);
245         objstream.writeObject(obj);
246         objstream.flush();
247         fstream.close();
248         setProperty(key, absFileName);
249         }catch(Exception JavaDoc e){
250 //IASRI 4660742 e.printStackTrace(System.out);
251
// START OF IASRI 4660742
252
_logger.log(Level.SEVERE,"enterprise.file_exception",e);
253 // END OF IASRI 4660742
254
}
255     }
256
257     public void removeObject(String JavaDoc key)
258         throws java.rmi.RemoteException JavaDoc
259     {
260         String JavaDoc fname = getProperty(key);
261
262         // Use this fname & delete the file first..
263
if(fname != null) {
264             try{
265             File file = new File(fname);
266             if(file.exists())
267                 file.delete();
268             removeProperty(key);
269             }catch(Exception JavaDoc e){
270 //IASRI 4660742 e.printStackTrace(System.out);
271
// START OF IASRI 4660742
272
_logger.log(Level.SEVERE,"enterprise.file_exception",e);
273 // END OF IASRI 4660742
274
}
275         }
276     }
277
278     /**
279      * This method returns all the keys for the given index.
280      *
281      */

282     public String JavaDoc[] getKeys(String JavaDoc index) throws java.rmi.RemoteException JavaDoc
283     {
284         Repository rep = getRepository(index);
285
286         return rep.keys();
287     }
288 }
289
Popular Tags