KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > JalistoPropertiesImpl


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.impl;
25
26 import org.objectweb.jalisto.se.api.JalistoProperties;
27 import org.objectweb.jalisto.se.api.internal.Constants;
28 import org.objectweb.jalisto.se.exception.JalistoException;
29 import org.objectweb.jalisto.se.impl.util.JalistoUtils;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.*;
34
35 public class JalistoPropertiesImpl implements JalistoProperties {
36
37     private static final String JavaDoc NAME = "jalisto";
38     private static final String JavaDoc IMPLEMENTATION = MONO_IMPLEMENTATION;
39     private static final String JavaDoc DATABASE_FILES_PATHS = "";
40     private static final String JavaDoc DATABASE_INITIAL_SIZE = "1000";
41     private static final String JavaDoc CACHE_IMPLEMENTATION = Constants.CACHE_DIC;
42     private static final String JavaDoc OBJECT_CACHE_SIZE = "0";
43     private static final String JavaDoc PAGE_CACHE_SIZE = "20";
44     private static final String JavaDoc CACHE_CLEAR_POURCENT = "20";
45     private static final String JavaDoc OIDTABLE_SIZE = "10000";
46     private static final String JavaDoc OBJECT_INST_PAGE_SIZE = "200";
47     private static final String JavaDoc OBJECT_SYST_PAGE_SIZE = "10";
48     private static final String JavaDoc OBJECT_CLASS_PAGE_SIZE = "10";
49     private static final String JavaDoc OBJECT_OID_PAGE_SIZE = "50";
50     private static final String JavaDoc CONCURRENCY_MODE = Constants.OPTIMISTIC_MODE;
51     private static final String JavaDoc TIME_OUT = "100";
52     private static final String JavaDoc ADD_SPACE = "15";
53     private static final String JavaDoc INTERNAL_KEY_MAX_SIZE = "12";
54     private static final String JavaDoc ALLOWS_SPECIAL_FUNCTIONNALITIES = "yes";
55     private static final String JavaDoc MBEAN_JMX_SERVER_PORT = "8082";
56     private static final String JavaDoc PHYSICAL_ACCESS_CLASS = Constants.PHYSICAL_FILE_ACCESS_DIC;
57     private static final String JavaDoc INTERNAL_FACTORY_CLASS = Constants.INTERNALFACTORYCLASS_DIC;
58     private static final String JavaDoc COMMUNICATION_FACTORY_CLASS = Constants.COMMUNICATION_FACTORY_DIC;
59     private static final String JavaDoc QUERY_MANAGER_CLASS = Constants.QUERY_MANAGER;
60     private static final String JavaDoc MBEAN_SERVER_CLASS = Constants.JMX_SERVER_DIC;
61     private static final String JavaDoc KEEP_IN_MEMORY = "yes";
62     private static final String JavaDoc CLEAN_LOG = "yes";
63     private static final String JavaDoc LOG_FILE = "log.jalisto";
64     private static final String JavaDoc TRACE = "";
65
66     private File JavaDoc parent;
67     private Properties prop;
68     private String JavaDoc[] pathes;
69     private String JavaDoc propertiesPath;
70
71     static final long serialVersionUID = -7589477069041161459L;
72
73     public JalistoPropertiesImpl(String JavaDoc databasePropertiesFilePath) {
74         this.propertiesPath = databasePropertiesFilePath;
75
76         try {
77             prop = new Properties();
78             prop.put(NAME_KEY, NAME);
79             prop.put(IMPLEMENTATION_KEY, IMPLEMENTATION);
80             prop.put(DATABASE_FILES_PATHS_KEY, DATABASE_FILES_PATHS);
81             prop.put(DATABASE_INITIAL_SIZE_KEY, DATABASE_INITIAL_SIZE);
82             prop.put(CACHE_IMPLEMENTATION_KEY, CACHE_IMPLEMENTATION);
83             prop.put(OBJECT_CACHE_SIZE_KEY, OBJECT_CACHE_SIZE);
84             prop.put(PAGE_CACHE_SIZE_KEY, PAGE_CACHE_SIZE);
85             prop.put(CACHE_CLEAR_POURCENT_KEY, CACHE_CLEAR_POURCENT);
86             prop.put(OIDTABLE_SIZE_KEY, OIDTABLE_SIZE);
87             prop.put(OBJECT_INST_PAGE_SIZE_KEY, OBJECT_INST_PAGE_SIZE);
88             prop.put(OBJECT_SYST_PAGE_SIZE_KEY, OBJECT_SYST_PAGE_SIZE);
89             prop.put(OBJECT_CLASS_PAGE_SIZE_KEY, OBJECT_CLASS_PAGE_SIZE);
90             prop.put(OBJECT_OID_PAGE_SIZE_KEY, OBJECT_OID_PAGE_SIZE);
91             prop.put(CONCURRENCY_MODE_KEY, CONCURRENCY_MODE);
92             prop.put(TIME_OUT_KEY, TIME_OUT);
93             prop.put(ADD_SPACE_KEY, ADD_SPACE);
94             prop.put(INTERNAL_KEY_MAX_SIZE_KEY, INTERNAL_KEY_MAX_SIZE);
95             prop.put(ALLOWS_SPECIAL_FUNCTIONNALITIES_KEY, ALLOWS_SPECIAL_FUNCTIONNALITIES);
96             prop.put(PHYSICAL_ACCESS_CLASS_KEY, PHYSICAL_ACCESS_CLASS);
97             prop.put(INTERNAL_FACTORY_CLASS_KEY, INTERNAL_FACTORY_CLASS);
98             prop.put(COMMUNICATION_FACTORY_CLASS_KEY, COMMUNICATION_FACTORY_CLASS);
99             prop.put(QUERY_MANAGER_CLASS_KEY, QUERY_MANAGER_CLASS);
100             prop.put(MBEAN_SERVER_CLASS_KEY, MBEAN_SERVER_CLASS);
101             prop.put(MBEAN_JMX_SERVER_PORT_KEY, MBEAN_JMX_SERVER_PORT);
102             prop.put(KEEP_IN_MEMORY_KEY, KEEP_IN_MEMORY);
103             prop.put(CLEAN_LOG_KEY, CLEAN_LOG);
104             prop.put(LOG_FILE_KEY, "");
105             prop.put(TRACE_KEY, TRACE);
106
107             File JavaDoc propfile;
108             boolean isLoaded;
109             if (!databasePropertiesFilePath.equalsIgnoreCase("")) {
110                 propfile = JalistoUtils.loadPropertiesFileIn(databasePropertiesFilePath, prop);
111                 parent = propfile.getAbsoluteFile().getParentFile();
112                 isLoaded = true;
113             } else {
114                 parent = new File JavaDoc(System.getProperty("user.dir"));
115                 System.out.println("Use default properties values with execution path : " + parent.getAbsolutePath());
116                 isLoaded = true;
117             }
118
119             if (!isLoaded) {
120                 parent = new File JavaDoc(System.getProperty("user.dir"));
121                 System.out.println("Exception during reading of Jalisto properties. Uses only default values");
122                 System.out.println("Work in system user.dir directory : " + String.valueOf(parent));
123             }
124
125             JalistoUtils.truncateProperties(prop);
126             loadDataFiles();
127         } catch (Exception JavaDoc e) {
128             throw new JalistoException(
129                     "Error during initialisation of JalistoProperties, using " + databasePropertiesFilePath, e);
130         }
131     }
132
133     public String JavaDoc getName() {
134         return getProperty(NAME_KEY);
135     }
136
137     public int getInitialSize() {
138         return Integer.parseInt(getProperty(DATABASE_INITIAL_SIZE_KEY));
139     }
140
141     public int getOidPageSize() {
142         return Integer.parseInt(getProperty(OBJECT_OID_PAGE_SIZE_KEY));
143     }
144
145     public int getOidTableSize() {
146         return Integer.parseInt(getProperty(OIDTABLE_SIZE_KEY));
147     }
148
149     public int getAdditionalSpace() {
150         return Integer.parseInt(getProperty(ADD_SPACE_KEY));
151     }
152
153     public int getObjectCacheSize() {
154         return Integer.parseInt(getProperty(OBJECT_CACHE_SIZE_KEY));
155     }
156
157     public int getPageCacheSize() {
158         return Integer.parseInt(getProperty(PAGE_CACHE_SIZE_KEY));
159     }
160
161     public int getMBeanJmxPort() {
162         return Integer.parseInt(getProperty(MBEAN_JMX_SERVER_PORT_KEY));
163     }
164
165     public double getCacheClearPourcent() {
166         return Double.parseDouble("0." + getProperty(CACHE_CLEAR_POURCENT_KEY));
167     }
168
169     public short getClassPageSize() {
170         return Short.parseShort(getProperty(OBJECT_CLASS_PAGE_SIZE_KEY));
171     }
172
173     public short getSystemPageSize() {
174         return Short.parseShort(getProperty(OBJECT_SYST_PAGE_SIZE_KEY));
175     }
176
177     public String JavaDoc getImplementation() {
178         return getProperty(IMPLEMENTATION_KEY);
179     }
180
181     public String JavaDoc getInternalFactoryClass() {
182         return getProperty(INTERNAL_FACTORY_CLASS_KEY);
183     }
184
185     public boolean isMonoImplementation() {
186         return getProperty(IMPLEMENTATION_KEY).equalsIgnoreCase(MONO_IMPLEMENTATION);
187     }
188
189     public boolean isReadOnlyImplementation() {
190         return getProperty(IMPLEMENTATION_KEY).equalsIgnoreCase(READONLY_IMPLEMENTATION);
191     }
192
193     public boolean isMultiImplementation() {
194         return getProperty(IMPLEMENTATION_KEY).equalsIgnoreCase(MULTI_IMPLEMENTATION);
195     }
196
197     public boolean isKeepingInMemory() {
198         return getProperty(KEEP_IN_MEMORY_KEY).equalsIgnoreCase("yes");
199     }
200
201     public boolean isCleanLog() {
202         return getProperty(CLEAN_LOG_KEY).equalsIgnoreCase("yes");
203     }
204
205     public boolean allowsSpecialFunctionnalities() {
206         return getProperty(ALLOWS_SPECIAL_FUNCTIONNALITIES_KEY).equalsIgnoreCase("yes");
207     }
208
209     public short getInstancePageSize() {
210         return Short.parseShort(getProperty(OBJECT_INST_PAGE_SIZE_KEY));
211     }
212
213     public int getKeyLength() {
214         return Integer.parseInt(getProperty(INTERNAL_KEY_MAX_SIZE_KEY));
215     }
216
217     public String JavaDoc getCommunicationFactoryClassName() {
218         return getProperty(COMMUNICATION_FACTORY_CLASS_KEY);
219     }
220
221     public Collection getKeys() {
222         return prop.keySet();
223     }
224
225     public String JavaDoc getLogFileName() {
226         String JavaDoc logFileName = prop.getProperty(LOG_FILE_KEY);
227         if (logFileName.equals("")) {
228             logFileName = getName() + "-" + LOG_FILE;
229         }
230         return logFileName;
231     }
232
233     public String JavaDoc getProperty(String JavaDoc key) {
234         return prop.getProperty(key);
235     }
236
237     public void setProperty(String JavaDoc key, String JavaDoc property) {
238         prop.setProperty(key, property);
239     }
240
241     public Collection getTraceModuleNames() {
242         return getStrings(TRACE_KEY, ",");
243     }
244
245     public boolean isTraceEnable() {
246         String JavaDoc modules = getProperty(TRACE_KEY);
247         return ((modules != null) && (!modules.equals("")));
248     }
249
250     public JalistoProperties getClone() {
251         return new JalistoPropertiesImpl(propertiesPath);
252     }
253
254     public void compareProperties(JalistoProperties oldProps) {
255         if (oldProps == null) {
256             return;
257         }
258         checkProperty(oldProps, NAME_KEY);
259         checkProperty(oldProps, OBJECT_INST_PAGE_SIZE_KEY);
260         checkProperty(oldProps, OBJECT_SYST_PAGE_SIZE_KEY);
261         checkProperty(oldProps, OBJECT_CLASS_PAGE_SIZE_KEY);
262         checkProperty(oldProps, OBJECT_OID_PAGE_SIZE_KEY);
263         checkProperty(oldProps, INTERNAL_KEY_MAX_SIZE_KEY);
264         checkProperty(oldProps, PHYSICAL_ACCESS_CLASS_KEY);
265     }
266
267     public void checkProperty(JalistoProperties oldProps, String JavaDoc property) {
268         if (!oldProps.getProperty(property).equalsIgnoreCase(prop.getProperty(property))) {
269             throw new JalistoException("Old Jalisto configuration property '" + property + "' with value '" +
270                                            oldProps.getProperty(property) + "'is not equal to new value '" +
271                                            prop.getProperty(property) + "'");
272         }
273     }
274
275     /**
276      * ******************************** PATHS ********************************************
277      */

278
279     public void loadDataFiles() throws IOException JavaDoc {
280         String JavaDoc files = getProperty(DATABASE_FILES_PATHS_KEY);
281         if (files.equals("")) {
282             files = getName() + ".jalisto";
283         }
284         StringTokenizer tokenizer = new StringTokenizer(files, ",;");
285         int size = tokenizer.countTokens();
286         if (size == 2) {
287             throw new JalistoException("Not a valid number of path in property '" + DATABASE_FILES_PATHS_KEY + "'");
288         }
289         pathes = new String JavaDoc[size];
290         int i = 0;
291         while (tokenizer.hasMoreTokens()) {
292             String JavaDoc path = tokenizer.nextToken();
293             File JavaDoc jalistoFile = new File JavaDoc(path);
294
295             if (jalistoFile.isAbsolute()) {
296                 pathes[i] = jalistoFile.getCanonicalPath();
297             } else {
298                 File JavaDoc f = new File JavaDoc(parent, path);
299                 pathes[i] = f.getCanonicalPath();
300             }
301             i++;
302         }
303     }
304
305     public String JavaDoc getPropertiesPath() {
306         return propertiesPath;
307     }
308
309     public String JavaDoc[] getPathes() {
310         return pathes;
311     }
312
313     public String JavaDoc getDbFileFullName() {
314         return pathes[0];
315     }
316
317     public String JavaDoc getOidDbFilePath() {
318         if (pathes.length > 1) {
319             return pathes[OID_INDEX];
320         } else {
321             return pathes[0];
322         }
323     }
324
325     public String JavaDoc getSystemDbFilePath() {
326         if (pathes.length > 1) {
327             return pathes[SYSTEM_INDEX];
328         } else {
329             return pathes[0];
330         }
331     }
332
333     public int getInstanceDbFileNumber() {
334         if (pathes.length > 1) {
335             return (pathes.length - 2);
336         } else {
337             return 1;
338         }
339     }
340
341     public String JavaDoc getInstanceDbFilePathAt(int index) {
342         if (pathes.length > 1) {
343             return pathes[index + 2];
344         } else {
345             return pathes[0];
346         }
347     }
348
349     /******************************* FROM INTERFACE ************************************/
350
351     public String JavaDoc getHost() {
352         throw new UnsupportedOperationException JavaDoc();
353     }
354
355     public int getPort() {
356         throw new UnsupportedOperationException JavaDoc();
357     }
358
359     public String JavaDoc getServerPropertiesPath() {
360         throw new UnsupportedOperationException JavaDoc();
361     }
362
363     /**
364      * ***************************** UTIL ***************************************
365      */

366
367     private Collection getStrings(Object JavaDoc key, String JavaDoc separator) {
368         String JavaDoc value = prop.getProperty((String JavaDoc) key);
369         List result = new ArrayList();
370         if (value != null) {
371             StringTokenizer tokenizer = new StringTokenizer(value, separator);
372             while (tokenizer.hasMoreTokens()) {
373                 result.add(tokenizer.nextToken().trim());
374             }
375         }
376         return result;
377     }
378
379     public String JavaDoc toString() {
380         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
381         sb.append("JalistoProperties[");
382         Iterator keys = prop.keySet().iterator();
383         while (keys.hasNext()) {
384             String JavaDoc key = (String JavaDoc) keys.next();
385             sb.append(key).append("=").append(prop.getProperty(key));
386             if (keys.hasNext()) {
387                 sb.append(",");
388             }
389         }
390         sb.append("]");
391         return sb.toString();
392     }
393 }
394
Popular Tags