View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.serviceregistry.api;
23  
24  import java.util.Date;
25  
26  import javax.xml.bind.annotation.XmlAccessType;
27  import javax.xml.bind.annotation.XmlAccessorType;
28  import javax.xml.bind.annotation.XmlElement;
29  import javax.xml.bind.annotation.XmlRootElement;
30  import javax.xml.bind.annotation.XmlType;
31  
32  /**
33   * A record of a service that creates and manages receipts.
34   */
35  @XmlAccessorType(XmlAccessType.NONE)
36  @XmlType(name = "service", namespace = "http://serviceregistry.opencastproject.org")
37  @XmlRootElement(name = "service", namespace = "http://serviceregistry.opencastproject.org")
38  public class JaxbServiceRegistration implements ServiceRegistration {
39  
40    @XmlElement(name = "type")
41    protected String serviceType;
42  
43    @XmlElement(name = "host")
44    protected String host;
45  
46    @XmlElement(name = "path")
47    protected String path;
48  
49    @XmlElement(name = "active")
50    protected boolean active;
51  
52    @XmlElement(name = "online")
53    protected boolean online;
54  
55    @XmlElement(name = "maintenance")
56    protected boolean maintenanceMode;
57  
58    @XmlElement(name = "jobproducer")
59    protected boolean jobProducer;
60  
61    /** The last time the service has been declared online */
62    @XmlElement(name = "onlinefrom")
63    protected Date onlineFrom;
64  
65    @XmlElement(name = "service_state")
66    protected ServiceState serviceState;
67  
68    @XmlElement(name = "state_changed")
69    protected Date stateChanged;
70  
71    @XmlElement(name = "error_state_trigger")
72    protected int errorStateTrigger;
73  
74    @XmlElement(name = "warning_state_trigger")
75    protected int warningStateTrigger;
76  
77    /**
78     * Creates a new service registration which is online and not in maintenance mode.
79     */
80    public JaxbServiceRegistration() {
81      this.online = true;
82      this.active = true;
83      this.maintenanceMode = false;
84      this.onlineFrom = new Date();
85      this.serviceState = ServiceState.NORMAL;
86      this.stateChanged = new Date();
87    }
88  
89    /**
90     * Creates a new JAXB annotated service registration based on an existing service registration
91     *
92     * @param serviceRegistration
93     */
94    public JaxbServiceRegistration(ServiceRegistration serviceRegistration) {
95      this.host = serviceRegistration.getHost();
96      this.jobProducer = serviceRegistration.isJobProducer();
97      this.maintenanceMode = serviceRegistration.isInMaintenanceMode();
98      this.active = serviceRegistration.isActive();
99      this.online = serviceRegistration.isOnline();
100     this.onlineFrom = serviceRegistration.getOnlineFrom();
101     this.path = serviceRegistration.getPath();
102     this.serviceType = serviceRegistration.getServiceType();
103     this.serviceState = serviceRegistration.getServiceState();
104     this.stateChanged = serviceRegistration.getStateChanged();
105     this.warningStateTrigger = serviceRegistration.getWarningStateTrigger();
106     this.errorStateTrigger = serviceRegistration.getErrorStateTrigger();
107   }
108 
109   /**
110    * Creates a new service registration which is online and not in maintenance mode.
111    *
112    * @param host
113    *          the host
114    * @param serviceType
115    *          the job type
116    */
117   public JaxbServiceRegistration(String serviceType, String host, String path) {
118     this();
119     this.serviceType = serviceType;
120     this.host = host;
121     this.path = path;
122   }
123 
124   /**
125    * Creates a new service registration which is online and not in maintenance mode.
126    *
127    * @param host
128    *          the host
129    * @param serviceType
130    *          the job type
131    * @param jobProducer
132    */
133   public JaxbServiceRegistration(String serviceType, String host, String path, boolean jobProducer) {
134     this();
135     this.serviceType = serviceType;
136     this.host = host;
137     this.path = path;
138     this.jobProducer = jobProducer;
139   }
140 
141   /**
142    * {@inheritDoc}
143    *
144    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getHost()
145    */
146   @Override
147   public String getHost() {
148     return host;
149   }
150 
151   /**
152    * @param host
153    *          the host to set
154    */
155   public void setHost(String host) {
156     this.host = host;
157   }
158 
159   /**
160    * {@inheritDoc}
161    *
162    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getServiceType()
163    */
164   @Override
165   public String getServiceType() {
166     return serviceType;
167   }
168 
169   /**
170    * @param serviceType
171    *          the serviceType to set
172    */
173   public void setServiceType(String serviceType) {
174     this.serviceType = serviceType;
175   }
176 
177   /**
178    * {@inheritDoc}
179    *
180    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#isInMaintenanceMode()
181    */
182   @Override
183   public boolean isInMaintenanceMode() {
184     return maintenanceMode;
185   }
186 
187   /**
188    * Sets the maintenance status of this service registration
189    *
190    * @param maintenanceMode
191    */
192   public void setInMaintenanceMode(boolean maintenanceMode) {
193     this.maintenanceMode = maintenanceMode;
194   }
195 
196   /**
197    * {@inheritDoc}
198    *
199    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#isActive()
200    */
201   @Override
202   public boolean isActive() {
203     return active;
204   }
205 
206   public void setActive(boolean active) {
207     this.active = active;
208   }
209 
210   /**
211    * {@inheritDoc}
212    *
213    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#isOnline()
214    */
215   @Override
216   public boolean isOnline() {
217     return online;
218   }
219 
220   /**
221    * Sets the online status of this service registration
222    *
223    * @param online
224    */
225   public void setOnline(boolean online) {
226     if (online && !isOnline()) {
227       setOnlineFrom(new Date());
228     }
229     this.online = online;
230   }
231 
232   /**
233    * {@inheritDoc}
234    *
235    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getPath()
236    */
237   @Override
238   public String getPath() {
239     return path;
240   }
241 
242   /**
243    * @param path
244    *          the path to set
245    */
246   public void setPath(String path) {
247     this.path = path;
248   }
249 
250   /**
251    * {@inheritDoc}
252    *
253    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#isJobProducer()
254    */
255   @Override
256   public boolean isJobProducer() {
257     return jobProducer;
258   }
259 
260   /**
261    * Sets whether this service registration is a job producer.
262    *
263    * @param jobProducer
264    *          the jobProducer to set
265    */
266   public void setJobProducer(boolean jobProducer) {
267     this.jobProducer = jobProducer;
268   }
269 
270   /**
271    *
272    * {@inheritDoc}
273    *
274    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getOnlineFrom()
275    */
276   @Override
277   public Date getOnlineFrom() {
278     return onlineFrom;
279   }
280 
281   /**
282    * Sets the last time the service has been declared online
283    *
284    * @param onlineFrom
285    *          the onlineFrom to set
286    */
287   public void setOnlineFrom(Date onlineFrom) {
288     this.onlineFrom = onlineFrom;
289   }
290 
291   /**
292    *
293    * {@inheritDoc}
294    *
295    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getServiceState()
296    */
297   @Override
298   public ServiceState getServiceState() {
299     return serviceState;
300   }
301 
302   /**
303    * Sets the current state of the service.
304    *
305    * @param state
306    *          current state
307    */
308   public void setServiceState(ServiceState state) {
309     this.serviceState = state;
310   }
311 
312   /**
313    * Sets the current state of the service and the trigger Job. If the state is set to {@link ServiceState#WARNING} or
314    * {@link ServiceState#ERROR} the triggered job will be set.
315    *
316    * @param state
317    *          the service state
318    * @param triggerJobSignature
319    *          the triggered job signature
320    */
321   public void setServiceState(ServiceState state, int triggerJobSignature) {
322 
323     setServiceState(state);
324     setStateChanged(new Date());
325     if (state == ServiceState.WARNING) {
326       setWarningStateTrigger(triggerJobSignature);
327     } else if (state == ServiceState.ERROR) {
328       setErrorStateTrigger(triggerJobSignature);
329     }
330   }
331 
332   /**
333    * {@inheritDoc}
334    *
335    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getStateChanged()
336    */
337   @Override
338   public Date getStateChanged() {
339     return stateChanged;
340   }
341 
342   /**
343    * Sets the last date when the state was changed
344    *
345    * @param stateChanged
346    *          last date
347    */
348   public void setStateChanged(Date stateChanged) {
349     this.stateChanged = stateChanged;
350   }
351 
352   /**
353    * {@inheritDoc}
354    *
355    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getErrorStateTrigger()
356    */
357   @Override
358   public int getErrorStateTrigger() {
359     return errorStateTrigger;
360   }
361 
362   /**
363    * Sets the job which triggered the last error state
364    *
365    * @param jobSignature
366    *          the job
367    */
368   public void setErrorStateTrigger(int jobSignature) {
369     this.errorStateTrigger = jobSignature;
370   }
371 
372   /**
373    * {@inheritDoc}
374    *
375    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getWarningStateTrigger()
376    */
377   @Override
378   public int getWarningStateTrigger() {
379     return warningStateTrigger;
380   }
381 
382   /**
383    * Sets the job which triggered the last warning state
384    *
385    * @param jobSignature
386    *          the job
387    */
388   public void setWarningStateTrigger(int jobSignature) {
389     this.warningStateTrigger = jobSignature;
390   }
391 
392   /**
393    * {@inheritDoc}
394    *
395    * @see java.lang.Object#hashCode()
396    */
397   @Override
398   public int hashCode() {
399     return toString().hashCode();
400   }
401 
402   /**
403    * {@inheritDoc}
404    *
405    * @see java.lang.Object#equals(java.lang.Object)
406    */
407   @Override
408   public boolean equals(Object obj) {
409     if (!(obj instanceof ServiceRegistration)) {
410       return false;
411     }
412     ServiceRegistration registration = (ServiceRegistration) obj;
413     return getHost().equals(registration.getHost()) && getServiceType().equals(registration.getServiceType());
414   }
415 
416   /**
417    * {@inheritDoc}
418    *
419    * @see java.lang.Object#toString()
420    */
421   @Override
422   public String toString() {
423     return getServiceType() + "@" + getHost();
424   }
425 
426 }