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     this.online = online;
229   }
230 
231   /**
232    * {@inheritDoc}
233    *
234    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getPath()
235    */
236   @Override
237   public String getPath() {
238     return path;
239   }
240 
241   /**
242    * @param path
243    *          the path to set
244    */
245   public void setPath(String path) {
246     this.path = path;
247   }
248 
249   /**
250    * {@inheritDoc}
251    *
252    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#isJobProducer()
253    */
254   @Override
255   public boolean isJobProducer() {
256     return jobProducer;
257   }
258 
259   /**
260    * Sets whether this service registration is a job producer.
261    *
262    * @param jobProducer
263    *          the jobProducer to set
264    */
265   public void setJobProducer(boolean jobProducer) {
266     this.jobProducer = jobProducer;
267   }
268 
269   /**
270    *
271    * {@inheritDoc}
272    *
273    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getOnlineFrom()
274    */
275   @Override
276   public Date getOnlineFrom() {
277     return onlineFrom;
278   }
279 
280   /**
281    * Sets the last time the service has been declared online
282    *
283    * @param onlineFrom
284    *          the onlineFrom to set
285    */
286   public void setOnlineFrom(Date onlineFrom) {
287     this.onlineFrom = onlineFrom;
288   }
289 
290   /**
291    *
292    * {@inheritDoc}
293    *
294    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getServiceState()
295    */
296   @Override
297   public ServiceState getServiceState() {
298     return serviceState;
299   }
300 
301   /**
302    * Sets the current state of the service.
303    *
304    * @param state
305    *          current state
306    */
307   public void setServiceState(ServiceState state) {
308     this.serviceState = state;
309   }
310 
311   /**
312    * Sets the current state of the service and the trigger Job. If the state is set to {@link ServiceState#WARNING} or
313    * {@link ServiceState#ERROR} the triggered job will be set.
314    *
315    * @param state
316    *          the service state
317    * @param triggerJobSignature
318    *          the triggered job signature
319    */
320   public void setServiceState(ServiceState state, int triggerJobSignature) {
321 
322     setServiceState(state);
323     setStateChanged(new Date());
324     if (state == ServiceState.WARNING) {
325       setWarningStateTrigger(triggerJobSignature);
326     } else if (state == ServiceState.ERROR) {
327       setErrorStateTrigger(triggerJobSignature);
328     }
329   }
330 
331   /**
332    * {@inheritDoc}
333    *
334    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getStateChanged()
335    */
336   @Override
337   public Date getStateChanged() {
338     return stateChanged;
339   }
340 
341   /**
342    * Sets the last date when the state was changed
343    *
344    * @param stateChanged
345    *          last date
346    */
347   public void setStateChanged(Date stateChanged) {
348     this.stateChanged = stateChanged;
349   }
350 
351   /**
352    * {@inheritDoc}
353    *
354    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getErrorStateTrigger()
355    */
356   @Override
357   public int getErrorStateTrigger() {
358     return errorStateTrigger;
359   }
360 
361   /**
362    * Sets the job which triggered the last error state
363    *
364    * @param jobSignature
365    *          the job
366    */
367   public void setErrorStateTrigger(int jobSignature) {
368     this.errorStateTrigger = jobSignature;
369   }
370 
371   /**
372    * {@inheritDoc}
373    *
374    * @see org.opencastproject.serviceregistry.api.ServiceRegistration#getWarningStateTrigger()
375    */
376   @Override
377   public int getWarningStateTrigger() {
378     return warningStateTrigger;
379   }
380 
381   /**
382    * Sets the job which triggered the last warning state
383    *
384    * @param jobSignature
385    *          the job
386    */
387   public void setWarningStateTrigger(int jobSignature) {
388     this.warningStateTrigger = jobSignature;
389   }
390 
391   /**
392    * {@inheritDoc}
393    *
394    * @see java.lang.Object#hashCode()
395    */
396   @Override
397   public int hashCode() {
398     return toString().hashCode();
399   }
400 
401   /**
402    * {@inheritDoc}
403    *
404    * @see java.lang.Object#equals(java.lang.Object)
405    */
406   @Override
407   public boolean equals(Object obj) {
408     if (!(obj instanceof ServiceRegistration))
409       return false;
410     ServiceRegistration registration = (ServiceRegistration) obj;
411     return getHost().equals(registration.getHost()) && getServiceType().equals(registration.getServiceType());
412   }
413 
414   /**
415    * {@inheritDoc}
416    *
417    * @see java.lang.Object#toString()
418    */
419   @Override
420   public String toString() {
421     return getServiceType() + "@" + getHost();
422   }
423 
424 }