KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > pm > AttributePersistenceService


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.system.pm;
23
24 import org.jboss.mx.persistence.AttributePersistenceManager;
25 import org.jboss.system.ServiceMBean;
26 import org.jboss.system.ServiceMBeanSupport;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * AttributePersistenceService
31  *
32  * Works in conjuction with
33  * org.jboss.mx.persistence.DelegatingPersistenceManager
34  * that upon creation, consults this service for an
35  * implementations of the interface
36  * org.jboss.mx.persistence.AttributePersistenceManager
37  *
38  * The service will instantiate and initialize the actual
39  * persistence manager and return it whenever asked.
40  *
41  * It also introduces the notion of a 'version', when
42  * creating the persistent manager, so that persisted
43  * data from different versions are kept separately.
44  *
45  * The service can be stopped, the version can be changed
46  * and the service re-started, resulting in a new
47  * persistent manager being instantiated and all
48  * XMBeans created thereafter using this instead.
49  *
50  * So the goal is really to have a way to plug-in
51  * external and manageable persistent managers that
52  * that support versioning, too.
53  *
54  * @jmx:mbean
55  * extends="org.jboss.system.ServiceMBean"
56  *
57  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
58  * @version $Revision: 57108 $
59 **/

60 public class AttributePersistenceService
61    extends ServiceMBeanSupport
62    implements AttributePersistenceServiceMBean
63 {
64    // Constants -----------------------------------------------------
65

66    /** The default AttributePersistenceManager implementation */
67    public static final String JavaDoc DEFAULT_APM = "org.jboss.system.pm.XMLAttributePersistenceManager";
68    
69    /** The default behaviour for destroying the APM when stopping */
70    public static final boolean DEFAULT_DESTROY_APM_ON_STOP = false;
71    
72    // Private Data --------------------------------------------------
73

74    /** the actual AttributePersistenceManager (APM) implementation */
75    private AttributePersistenceManager apm = null;
76    
77    /** the APM XML configuration */
78    private Element JavaDoc apmConfig = null;
79    
80    /** the name of the APM implementation class */
81    private String JavaDoc apmClass = DEFAULT_APM;
82    
83    /** whether to destroy() the APM upon stop, or leave it servicing XMBeans that use it */
84    private boolean apmDestroyOnStop = DEFAULT_DESTROY_APM_ON_STOP;
85    
86    /** indicate the active version configured to the APM */
87    private String JavaDoc versionTag = null;
88    
89    // Constructors -------------------------------------------------
90

91    /**
92     * Constructs a <tt>AttributePersistenceService</tt>.
93     */

94    public AttributePersistenceService()
95    {
96       // setup logger
97
super(AttributePersistenceService.class);
98    }
99
100    // Attributes ----------------------------------------------------
101

102    /**
103     * @jmx:managed-attribute
104     * @return Returns the versionTag.
105     */

106    public String JavaDoc getVersionTag()
107    {
108       return versionTag;
109    }
110    
111    /**
112     * @jmx:managed-attribute
113     * @param versionTag The versionTag to set.
114     */

115    public void setVersionTag(String JavaDoc versionTag)
116    {
117       checkNotStarted();
118       this.versionTag = versionTag;
119    }
120    
121    /**
122     * @jmx:managed-attribute
123     * @return Returns the apmClass.
124     */

125    public String JavaDoc getAttributePersistenceManagerClass()
126    {
127       return apmClass;
128    }
129    
130    /**
131     * @jmx:managed-attribute
132     * @param apmClass The apmClass to set.
133     */

134    public void setAttributePersistenceManagerClass(String JavaDoc apmClass)
135    {
136       checkNotStarted();
137       this.apmClass = apmClass;
138    }
139    
140    /**
141     * @jmx:managed-attribute
142     * @return Returns the apmConfig.
143     */

144    public Element JavaDoc getAttributePersistenceManagerConfig()
145    {
146       return apmConfig;
147    }
148    
149    /**
150     * @jmx:managed-attribute
151     * @param apmConfig The apmConfig to set.
152     */

153    public void setAttributePersistenceManagerConfig(Element JavaDoc apmConfig)
154    {
155       checkNotStarted();
156       this.apmConfig = apmConfig;
157    }
158    
159    /**
160     * @jmx:managed-attribute
161     * @return Returns the apmDestroyOnStop.
162     */

163    public boolean getApmDestroyOnServiceStop()
164    {
165       return apmDestroyOnStop;
166    }
167    /**
168     * @jmx:managed-attribute
169     * @param apmDestroyOnStop The apmDestroyOnStop to set.
170     */

171    public void setApmDestroyOnServiceStop(boolean apmDestroyOnStop)
172    {
173       checkNotStarted();
174       this.apmDestroyOnStop = apmDestroyOnStop;
175    }
176    
177    // ServiceMBeanSupport overrides ---------------------------------
178

179    public void startService()
180       throws Exception JavaDoc
181    {
182       // Instantiate the APM trough the MBeanServer
183
this.apm = (AttributePersistenceManager)this.getServer().instantiate(this.apmClass);
184       
185       // Initialize it
186
this.apm.create(this.versionTag, this.apmConfig);
187    }
188
189    public void stopService()
190    {
191       // if we destroy it non can be used it
192
if (this.apmDestroyOnStop) {
193          this.apm.destroy();
194       }
195       
196       // forget about this apm
197
this.apm = null;
198    }
199    
200    // Operations ----------------------------------------------------
201

202    /**
203     * @jmx:managed-operation
204     */

205    public AttributePersistenceManager apmCreate()
206    {
207       checkStarted();
208       
209       return this.apm;
210    }
211    
212    /**
213     * @jmx:managed-operation
214     */

215    public boolean apmExists(String JavaDoc id)
216       throws Exception JavaDoc
217    {
218       checkStarted();
219       
220       return this.apm.exists(id);
221    }
222    
223    /**
224     * @jmx:managed-operation
225     */

226    public void apmRemove(String JavaDoc id)
227       throws Exception JavaDoc
228    {
229       checkStarted();
230       
231       this.apm.remove(id);
232    }
233    
234    /**
235     * @jmx:managed-operation
236     */

237    public void apmRemoveAll()
238       throws Exception JavaDoc
239    {
240       checkStarted();
241       
242       this.apm.removeAll();
243    }
244    
245    /**
246     * @jmx:managed-operation
247     */

248    public String JavaDoc[] apmListAll()
249       throws Exception JavaDoc
250    {
251       checkStarted();
252       
253       return this.apm.listAll();
254    }
255
256    /**
257     * @jmx:managed-operation
258     */

259    public String JavaDoc apmListAllAsString()
260       throws Exception JavaDoc
261    {
262       checkStarted();
263       
264       StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(1024);
265       String JavaDoc[] list = this.apm.listAll();
266       
267       for (int i = 0; i < list.length; i++) {
268          sbuf.append(list[i]).append("\n");
269       }
270       return sbuf.toString();
271    }
272    
273    // Private -------------------------------------------------------
274

275    private void checkStarted()
276    {
277       int state = this.getState();
278       
279       if (state != ServiceMBean.STARTED) {
280          throw new IllegalStateException JavaDoc("Cannot perform operations unless service is started");
281       }
282    }
283    
284    private void checkNotStarted()
285    {
286       int state = this.getState();
287       
288       if (state == ServiceMBean.STARTING || state == ServiceMBean.STARTED) {
289          throw new IllegalStateException JavaDoc("Cannot modify attributes while service is started");
290       }
291    }
292 }
Popular Tags