KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > client > JalistoPropertiesClientImpl


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.client;
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
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.*;
35
36 public class JalistoPropertiesClientImpl implements JalistoProperties {
37
38     private static final String JavaDoc NAME = "jalisto";
39     private static final String JavaDoc SERVER_PROPERTIES_PATH = "jalisto.properties";
40     private static final String JavaDoc CACHE_IMPLEMENTATION = "org.objectweb.jalisto.se.impl.cache.GenericCache";
41     private static final String JavaDoc OBJECT_CACHE_SIZE = "500";
42     private static final String JavaDoc CACHE_CLEAR_POURCENT = "20";
43     private static final String JavaDoc ALLOWS_SPECIAL_FUNCTIONNALITIES = "no";
44     public static final String JavaDoc COMMUNICATION_FACTORY_CLASS =
45             "org.objectweb.jalisto.se.impl.remote.socket.CommunicationFactorySocketImpl";
46     public static final String JavaDoc HOST = "localhost";
47     public static final String JavaDoc PORT = "7777";
48     private static final String JavaDoc QUERY_MANAGER_CLASS = Constants.QUERY_MANAGER;
49     private static final String JavaDoc TRACE = "";
50
51
52     public static final String JavaDoc SERVER_PROPERTIES_PATH_KEY = "serverPropertiesFile";
53     public static final String JavaDoc HOST_KEY = "host";
54     public static final String JavaDoc PORT_KEY = "port";
55
56
57     private File JavaDoc parent;
58     private Properties prop;
59     private String JavaDoc propertiesPath;
60     private JalistoProperties remoteProperties;
61
62
63     static final long serialVersionUID = -7589477064771161459L;
64
65     public JalistoPropertiesClientImpl(String JavaDoc databasePropertiesFilePath) {
66         this.propertiesPath = databasePropertiesFilePath;
67
68         try {
69             prop = new Properties();
70             prop.put(NAME_KEY, NAME);
71             prop.put(SERVER_PROPERTIES_PATH_KEY, SERVER_PROPERTIES_PATH);
72             prop.put(CACHE_IMPLEMENTATION_KEY, CACHE_IMPLEMENTATION);
73             prop.put(OBJECT_CACHE_SIZE_KEY, OBJECT_CACHE_SIZE);
74             prop.put(CACHE_CLEAR_POURCENT_KEY, CACHE_CLEAR_POURCENT);
75             prop.put(ALLOWS_SPECIAL_FUNCTIONNALITIES_KEY, ALLOWS_SPECIAL_FUNCTIONNALITIES);
76             prop.put(COMMUNICATION_FACTORY_CLASS_KEY, COMMUNICATION_FACTORY_CLASS);
77             prop.put(HOST_KEY, HOST);
78             prop.put(PORT_KEY, PORT);
79             prop.put(QUERY_MANAGER_CLASS_KEY, QUERY_MANAGER_CLASS);
80             prop.put(TRACE_KEY, TRACE);
81
82             boolean isLoaded = false;
83             if (!databasePropertiesFilePath.equalsIgnoreCase("")) {
84                 File JavaDoc propfile = new File JavaDoc(databasePropertiesFilePath);
85                 if (propfile.exists() && (!propfile.isDirectory())) {
86                     parent = propfile.getAbsoluteFile().getParentFile();
87                     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(propfile);
88                     prop.load(fis);
89                     isLoaded = true;
90                 }
91                 if (!isLoaded) {
92                     ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
93                     InputStream JavaDoc testIS = classLoader.getResourceAsStream(databasePropertiesFilePath);
94                     URL JavaDoc url = classLoader.getResource(databasePropertiesFilePath);
95                     propfile = new File JavaDoc(url.getFile());
96                     parent = propfile.getAbsoluteFile().getParentFile();
97                     prop.load(testIS);
98                     isLoaded = true;
99                 }
100             } else {
101                 parent = new File JavaDoc(System.getProperty("user.dir"));
102                 System.out.println("Use default properties values with execution path : " + parent.getAbsolutePath());
103                 isLoaded = true;
104             }
105
106             if (!isLoaded) {
107                 parent = new File JavaDoc(System.getProperty("user.dir"));
108                 System.out.println("Exception during reading of Jalisto properties use only default values");
109                 System.out.println("Work in system user.dir directory : " + String.valueOf(parent));
110             }
111
112             truncateProperties();
113         } catch (Exception JavaDoc e) {
114             throw new JalistoException(
115                     "Error during initialisation of JalistoProperties, using " + databasePropertiesFilePath, e);
116         }
117     }
118
119     public void setRemoteProperties(JalistoProperties remoteProperties) {
120         this.remoteProperties = remoteProperties;
121     }
122
123     public String JavaDoc getName() {
124         return getProperty(NAME_KEY);
125     }
126
127     public String JavaDoc getServerPropertiesPath() {
128         return getProperty(SERVER_PROPERTIES_PATH_KEY);
129     }
130
131     public int getObjectCacheSize() {
132         return Integer.parseInt(getProperty(OBJECT_CACHE_SIZE_KEY));
133     }
134
135     public double getCacheClearPourcent() {
136         return Double.parseDouble("0." + getProperty(CACHE_CLEAR_POURCENT_KEY));
137     }
138
139     public String JavaDoc getCommunicationFactoryClassName() {
140         return getProperty(COMMUNICATION_FACTORY_CLASS_KEY);
141     }
142
143     public String JavaDoc getHost() {
144         return getProperty(HOST_KEY);
145     }
146
147     public int getPort() {
148         return Integer.parseInt(getProperty(PORT_KEY));
149     }
150
151     public Collection getKeys() {
152         return prop.keySet();
153     }
154
155     public String JavaDoc getProperty(String JavaDoc key) {
156         return prop.getProperty(key);
157     }
158
159     public void setProperty(String JavaDoc key, String JavaDoc property) {
160         prop.setProperty(key, property);
161     }
162
163     public Collection getTraceModuleNames() {
164         return getStrings(TRACE_KEY, ",");
165     }
166
167     public boolean isTraceEnable() {
168         String JavaDoc modules = getProperty(TRACE_KEY);
169         return ((modules != null) && (!modules.equals("")));
170     }
171
172     private Collection getStrings(Object JavaDoc key, String JavaDoc separator) {
173         String JavaDoc value = prop.getProperty((String JavaDoc) key);
174         List result = new ArrayList();
175         if (value != null) {
176             StringTokenizer tokenizer = new StringTokenizer(value, separator);
177             while (tokenizer.hasMoreTokens()) {
178                 result.add(tokenizer.nextToken().trim());
179             }
180         }
181         return result;
182     }
183
184     public String JavaDoc toString() {
185         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
186         sb.append("JalistoClientProperties[");
187         Iterator keys = prop.keySet().iterator();
188         while (keys.hasNext()) {
189             String JavaDoc key = (String JavaDoc) keys.next();
190             sb.append(key).append("=").append(prop.getProperty(key));
191             if (keys.hasNext()) {
192                 sb.append(",");
193             }
194         }
195         sb.append("]");
196         return sb.toString();
197     }
198
199     public JalistoProperties getClone() {
200         return new JalistoPropertiesClientImpl(propertiesPath);
201     }
202
203     private void truncateProperties() {
204         Iterator keys = prop.keySet().iterator();
205         while (keys.hasNext()) {
206             String JavaDoc key = (String JavaDoc) keys.next();
207             String JavaDoc value = prop.getProperty(key);
208             while (value.endsWith(" ") || value.endsWith("\t")) {
209                 value = value.substring(0, value.length() - 1);
210             }
211             prop.setProperty(key, value);
212         }
213     }
214
215     public String JavaDoc getPropertiesPath() {
216         return propertiesPath;
217     }
218
219     /**
220      * ************************************** FROM INTERFACE ******************************************
221      */

222
223     public void checkProperty(JalistoProperties oldProps, String JavaDoc property) {
224         throw new UnsupportedOperationException JavaDoc();
225     }
226
227     public void compareProperties(JalistoProperties oldProps) {
228         throw new UnsupportedOperationException JavaDoc();
229     }
230
231     public int getAdditionalSpace() {
232         return remoteProperties.getAdditionalSpace();
233     }
234
235     public short getClassPageSize() {
236         return remoteProperties.getClassPageSize();
237     }
238
239     public String JavaDoc getDbFileFullName() {
240         return remoteProperties.getDbFileFullName();
241     }
242
243     public String JavaDoc getImplementation() {
244         return remoteProperties.getImplementation();
245     }
246
247     public int getInitialSize() {
248         return remoteProperties.getInitialSize();
249     }
250
251     public int getInstanceDbFileNumber() {
252         return remoteProperties.getInstanceDbFileNumber();
253     }
254
255     public String JavaDoc getInstanceDbFilePathAt(int index) {
256         return remoteProperties.getInstanceDbFilePathAt(index);
257     }
258
259     public short getInstancePageSize() {
260         return remoteProperties.getInstancePageSize();
261     }
262
263     public int getKeyLength() {
264         return remoteProperties.getKeyLength();
265     }
266
267     public String JavaDoc getLogFileName() {
268         return remoteProperties.getLogFileName();
269     }
270
271     public int getMBeanJmxPort() {
272         return remoteProperties.getMBeanJmxPort();
273     }
274
275     public String JavaDoc getOidDbFilePath() {
276         return remoteProperties.getOidDbFilePath();
277     }
278
279     public int getOidPageSize() {
280         return remoteProperties.getOidPageSize();
281     }
282
283     public int getOidTableSize() {
284         return remoteProperties.getOidTableSize();
285     }
286
287     public int getPageCacheSize() {
288         return remoteProperties.getPageCacheSize();
289     }
290
291     public String JavaDoc[] getPathes() {
292         return remoteProperties.getPathes();
293     }
294
295     public String JavaDoc getSystemDbFilePath() {
296         return remoteProperties.getSystemDbFilePath();
297     }
298
299     public short getSystemPageSize() {
300         return remoteProperties.getSystemPageSize();
301     }
302
303     public boolean isCleanLog() {
304         return remoteProperties.isCleanLog();
305     }
306
307     public boolean allowsSpecialFunctionnalities() {
308         return getProperty(ALLOWS_SPECIAL_FUNCTIONNALITIES_KEY).equalsIgnoreCase("yes");
309     }
310
311     public boolean isKeepingInMemory() {
312         return remoteProperties.isKeepingInMemory();
313     }
314
315     public boolean isMonoImplementation() {
316         return remoteProperties.isMonoImplementation();
317     }
318
319     public boolean isMultiImplementation() {
320         return remoteProperties.isMultiImplementation();
321     }
322
323     public boolean isReadOnlyImplementation() {
324         return remoteProperties.isReadOnlyImplementation();
325     }
326
327     public String JavaDoc getInternalFactoryClass() {
328         return remoteProperties.getInternalFactoryClass();
329     }
330 }
331
Popular Tags