KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > store > impl > CocoonStoreJanitor


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.store.impl;
17
18 import org.apache.avalon.framework.activity.Disposable;
19 import org.apache.avalon.framework.component.Component;
20 import org.apache.avalon.framework.parameters.ParameterException;
21 import org.apache.avalon.framework.parameters.Parameterizable;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.avalon.framework.service.Serviceable;
26
27 import org.apache.cocoon.components.thread.RunnableManager;
28
29 /**
30  * The CocoonStoreJanitor class just subclasses the {@link StoreJanitorImpl} to
31  * overwrite the start method for background thread creation using the Cocoon
32  * {@link RunnableManager}.
33  *
34  * @author <a HREF="mailto:giacomo.at.apache.org">Giacomo Pati</a>
35  * @version $Id$
36  */

37 public class CocoonStoreJanitor extends StoreJanitorImpl
38                                 implements Parameterizable, Serviceable, Disposable, Component {
39
40     //~ Instance fields --------------------------------------------------------
41

42     /** Name of the thread pool to use. Defaults to 'daemon'. */
43     private String JavaDoc threadPool;
44
45     /** Our {@link ServiceManager} */
46     private ServiceManager serviceManager;
47
48     /** Our {@link RunnableManager} */
49     private RunnableManager runnableManager;
50
51     /** Flags to ignore memory bursts in the startup */
52     private boolean m_firstRun = true;
53
54     /** Flags to ignore memory bursts in the startup */
55     private boolean m_secondRun = false;
56
57     //~ Methods ----------------------------------------------------------------
58

59     public void parameterize(Parameters params) throws ParameterException {
60         super.parameterize(params);
61         this.threadPool = params.getParameter("thread-pool", "daemon");
62     }
63
64     /**
65      * Get the <code>RunnableManager</code>
66      *
67      * @param serviceManager The <code>ServiceManager</code>
68      * @throws ServiceException If RunnableManager is not available
69      */

70     public void service(final ServiceManager serviceManager)
71     throws ServiceException {
72         this.serviceManager = serviceManager;
73         this.runnableManager = (RunnableManager) serviceManager.lookup(RunnableManager.ROLE);
74     }
75
76     /**
77      * Release <code>RunnableManager</code>
78      */

79     public void dispose() {
80         this.serviceManager.release(this.runnableManager);
81         this.runnableManager = null;
82         this.serviceManager = null;
83     }
84
85     /**
86      * The "checker" thread checks if memory is running low in the jvm.
87      */

88     public void run() {
89         // Ignoring memory bursts in the first two invokations
90
if (m_firstRun || m_secondRun) {
91             super.inUse = super.memoryInUse();
92             m_secondRun = m_firstRun;
93             m_firstRun = false;
94         }
95
96         super.checkMemory();
97
98         // Relaunch
99
relaunch(super.interval);
100     }
101
102     /**
103      * Start this instance using a default thread from the
104      * <code>RunnableManager</code>
105      */

106     public void start() {
107         relaunch(0);
108     }
109
110     /**
111      * Does a delayed (re-)start of this instance using a default thread from
112      * the<code>RunnableManager</code> with a delay
113      *
114      * @param delay the delay to apply before next run
115      */

116     private void relaunch(final long delay) {
117         getLogger().debug("(Re-)Start CocoonStoreJanitor");
118         this.runnableManager.execute(this.threadPool, this, delay, 0);
119     }
120 }
121
Popular Tags