1 /*
2 * Copyright (c) 2002-2025, City of Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34 package fr.paris.lutece.portal.service.daemon;
35
36 import java.text.SimpleDateFormat;
37 import java.util.Date;
38 import java.util.Locale;
39
40 import fr.paris.lutece.portal.service.spring.SpringContextService;
41
42 /**
43 * this class is used to manage daemons declaration
44 */
45 public final class DaemonEntry
46 {
47 private SimpleDateFormat _formatterDateTime = new SimpleDateFormat( "dd'/'MM'/'yyyy' 'HH':'mm", Locale.FRANCE );
48 private String _strId;
49 private String _strNameKey;
50 private String _strDescriptionKey;
51 private String _strClassName;
52 private Date _dateLastRunDate;
53 private String _strLastRunLogs;
54 private long _lInterval;
55 private boolean _bOnStartup;
56 private boolean _bIsRunning;
57 private Daemon _daemon;
58 private final DaemonThread _thread;
59 private String _strPluginName;
60
61 // Variables declarations
62
63 /**
64 * Constructor
65 */
66 public DaemonEntry( )
67 {
68 _thread = SpringContextService.getBean( DaemonThread.DEAMON_THREAD_BEAN_NAME );
69 _thread.setDaemonEntry( this );
70 }
71
72 /**
73 * Returns the Id
74 *
75 * @return The Id
76 */
77 public String getId( )
78 {
79 return _strId;
80 }
81
82 /**
83 * Sets the Id
84 *
85 * @param strId
86 * The Id
87 */
88 public void setId( String strId )
89 {
90 _strId = strId;
91 }
92
93 /**
94 * Returns the NameKey
95 *
96 * @return The NameKey
97 */
98 public String getNameKey( )
99 {
100 return _strNameKey;
101 }
102
103 /**
104 * Sets the NameKey
105 *
106 * @param strNameKey
107 * The NameKey
108 */
109 public void setNameKey( String strNameKey )
110 {
111 _strNameKey = strNameKey;
112 }
113
114 /**
115 * Returns the DescriptionKey
116 *
117 * @return The DescriptionKey
118 */
119 public String getDescriptionKey( )
120 {
121 return _strDescriptionKey;
122 }
123
124 /**
125 * Sets the DescriptionKey
126 *
127 * @param strDescriptionKey
128 * The DescriptionKey
129 */
130 public void setDescriptionKey( String strDescriptionKey )
131 {
132 _strDescriptionKey = strDescriptionKey;
133 }
134
135 /**
136 * Returns the ClassName
137 *
138 * @return The ClassName
139 */
140 public String getClassName( )
141 {
142 return _strClassName;
143 }
144
145 /**
146 * Sets the ClassName
147 *
148 * @param strClassName
149 * The ClassName
150 */
151 public void setClassName( String strClassName )
152 {
153 _strClassName = strClassName;
154 }
155
156 /**
157 * Load the daemon
158 *
159 * @throws ClassNotFoundException
160 * If an error occured
161 * @throws InstantiationException
162 * If an error occured
163 * @throws IllegalAccessException
164 * If an error occured
165 */
166 public void loadDaemon( ) throws ClassNotFoundException, InstantiationException, IllegalAccessException
167 {
168 _daemon = (Daemon) Class.forName( _strClassName ).newInstance( );
169 }
170
171 /**
172 * Returns the daemon associated to the entry
173 *
174 * @return The daemon
175 */
176 protected Daemon getDaemon( )
177 {
178 return _daemon;
179 }
180
181 /**
182 * Returns the thread which start the daemon task
183 *
184 * @return The thread
185 */
186 public DaemonThread getDaemonThread( )
187 {
188 return _thread;
189 }
190
191 /**
192 * Returns the Interval of time in seconds between two executions
193 *
194 * @return _lInterval the interval of time
195 */
196 public long getInterval( )
197 {
198 return _lInterval;
199 }
200
201 /**
202 * Indicates if the daemon must be process on system startup
203 *
204 * @return _bOnStartup
205 */
206 public boolean onStartup( )
207 {
208 return _bOnStartup;
209 }
210
211 /**
212 * Checks if the daemon is running
213 *
214 * @return True if the thread is running, otherwise false
215 */
216 public boolean isRunning( )
217 {
218 return _bIsRunning;
219 }
220
221 /**
222 * Set running daemon status
223 *
224 * @param bIsRunning
225 * True if the thread is running, otherwise false
226 */
227 public void setIsRunning( boolean bIsRunning )
228 {
229 _bIsRunning = bIsRunning;
230 }
231
232 /**
233 * Returns the PluginName
234 *
235 * @return The PluginName
236 */
237 public String getPluginName( )
238 {
239 return _strPluginName;
240 }
241
242 /**
243 * Sets the PluginName
244 *
245 * @param strPluginName
246 * The PluginName
247 */
248 public void setPluginName( String strPluginName )
249 {
250 _strPluginName = strPluginName;
251 }
252
253 /**
254 * Returns the LastRunDate
255 *
256 * @return The LastRunDate
257 */
258 public String getLastRunDate( )
259 {
260 if ( _dateLastRunDate == null )
261 {
262 return "";
263 }
264 return _formatterDateTime.format( new Date( _dateLastRunDate.getTime( ) ) );
265 }
266
267 /**
268 * Sets the LastRunDate
269 *
270 * @param dateLastRunDate
271 * The LastRunDate
272 */
273 public void setLastRunDate( Date dateLastRunDate )
274 {
275 _dateLastRunDate = dateLastRunDate;
276 }
277
278 /**
279 * Returns the LastRunLogs
280 *
281 * @return The LastRunLogs
282 */
283 public String getLastRunLogs( )
284 {
285 return ( _strLastRunLogs != null ) ? _strLastRunLogs : "";
286 }
287
288 /**
289 * Sets the LastRunLogs
290 *
291 * @param strLastRunLogs
292 * The LastRunLogs
293 */
294 public void setLastRunLogs( String strLastRunLogs )
295 {
296 _strLastRunLogs = strLastRunLogs;
297 }
298
299 /**
300 * Sets the interval
301 *
302 * @param lInterval
303 * The interval
304 */
305 public void setInterval( long lInterval )
306 {
307 _lInterval = lInterval;
308 }
309
310 /**
311 * Sets the OnStartUp property
312 *
313 * @param bOnStartup
314 * True if the daemon should be launched on startup, otherwise false
315 */
316 public void setOnStartUp( boolean bOnStartup )
317 {
318 _bOnStartup = bOnStartup;
319 }
320 }