KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > config > ConfigModule


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 package com.nightlabs.config;
32
33 import java.io.ObjectInputStream JavaDoc;
34 import java.io.ObjectOutputStream JavaDoc;
35 import java.io.Serializable JavaDoc;
36
37 import com.nightlabs.io.DataBuffer;
38 import com.nightlabs.util.RWLockable;
39 import com.nightlabs.util.RWLock;
40 import com.nightlabs.util.DeadLockException;
41
42 /**
43  * The base class for all ConfigModule implementations.
44  * Create a custom config module in this way:
45  * <p><blockquote><pre>
46  * public class MyConfigData extends ConfigModule
47  * {
48  * // a custom config variable:
49  * private String myConfigValue;
50  *
51  * // provide a public default constructor:
52  * public GlobalL10nSettings() {
53  * }
54  *
55  * // initialize custom config variables with
56  * // default values if they are not already
57  * // initialized:
58  * public void init() throws InitException {
59  * if(myConfigValue == null)
60  * myConfigValue = "My default config data";
61  * }
62  *
63  * // provide getters and setters for your
64  * // custom config variables:
65  * public String getMyConfigValue() {
66  * return myConfigValue;
67  * }
68  * public void setMyConfigValue(String myConfigValue) {
69  * this.myConfigValue = myConfigValue;
70  * }
71  * }
72  * </pre></blockquote>
73  *
74  * @author Alexander Bieber, Marc Klinger, Marco Schulze, Niklas Schiffler
75  */

76 public abstract class ConfigModule
77     implements Serializable JavaDoc, Initializable, RWLockable, Cloneable JavaDoc
78 {
79     private transient Config config;
80
81   private String JavaDoc identifier;
82   private String JavaDoc searchClass;
83
84   public ConfigModule()
85   {
86   }
87   
88   public void _setConfig(Config _config)
89   {
90     this.config = _config;
91   }
92   public Config _getConfig()
93   {
94     return config;
95   }
96
97   public void init()
98   throws InitException
99   {
100   }
101
102   protected transient RWLock rwLock = new RWLock(this.getClass().getName(), true);
103
104   public void acquireReadLock() throws DeadLockException
105   {
106     rwLock.acquireReadLock();
107   }
108
109   public void acquireWriteLock() throws DeadLockException
110   {
111     rwLock.acquireWriteLock();
112   }
113
114   public void releaseLock()
115   {
116     rwLock.releaseLock();
117   }
118
119   
120   private volatile boolean changed = false;
121   
122   /**
123    * This method controls, whether this config module is written to disk.
124    * <br/>
125    * Note, that this works only, if the Config is in multi files mode. In single
126    * file mode, the whole config is always written. But, because the single file
127    * mode is deprecated anyway, this should not cause head-aches.
128    */

129   public boolean _isChanged()
130   {
131     return changed;
132   }
133   
134   public void setChanged() {
135     setChanged(true);
136   }
137   
138   public void setChanged(boolean changed) {
139     this.changed = changed;
140   }
141   
142   /**
143    * Accessor method for property {@link #identifier}.
144    * @return {@link #identifier}.
145    */

146   public String JavaDoc getIdentifier()
147   {
148     return identifier;
149   }
150
151   /**
152    * Accessor method for property {@link #identifier}.
153    * @param identifier The new value.
154    */

155   public void setIdentifier(String JavaDoc identifier)
156   {
157     if ("null".equals(identifier))
158         throw new IllegalArgumentException JavaDoc("null is a reserved word and should not be used as identifier!");
159     this.identifier = identifier;
160   }
161
162   /**
163    * Accessor method for property {@link #searchClass}.
164    * <p>
165    * // TODO Marco: Who has added this? It seems to me, the work isn't complete!
166    *
167    * @return {@link #searchClass}.
168    */

169   public String JavaDoc getSearchClass()
170   {
171     return searchClass;
172   }
173
174   /**
175    * Accessor method for property {@link #searchClass}.
176    * @param searchClass The new value.
177    */

178   public void setSearchClass(String JavaDoc searchClass)
179   {
180     if ("null".equals(searchClass))
181         throw new IllegalArgumentException JavaDoc("null is a reserved word and should not be used as searchClassName!");
182     this.searchClass = searchClass;
183   }
184
185     /**
186      * Warning: The member config is transient and will be null at the clone!
187      *
188      * @see java.lang.Object#clone()
189      */

190     public Object JavaDoc clone()
191     {
192         try {
193             DataBuffer buf = new DataBuffer();
194             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buf.createOutputStream());
195             out.writeObject(this);
196             out.close();
197             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(buf.createInputStream());
198             Object JavaDoc n = in.readObject();
199             in.close();
200             return n;
201         } catch (Exception JavaDoc e) {
202             throw new RuntimeException JavaDoc(e);
203         }
204     }
205
206 }
Popular Tags