View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.ksb.api.registry;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreConstants;
20  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
21  import org.kuali.rice.core.api.mo.ModelBuilder;
22  import org.kuali.rice.core.api.util.jaxb.QNameAsStringAdapter;
23  import org.w3c.dom.Element;
24  
25  import javax.xml.bind.annotation.XmlAccessType;
26  import javax.xml.bind.annotation.XmlAccessorType;
27  import javax.xml.bind.annotation.XmlAnyElement;
28  import javax.xml.bind.annotation.XmlElement;
29  import javax.xml.bind.annotation.XmlRootElement;
30  import javax.xml.bind.annotation.XmlType;
31  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
32  import javax.xml.namespace.QName;
33  import java.io.Serializable;
34  import java.util.Collection;
35  
36  /**
37   * Immutable implementation of the {@link ServiceInfoContract} interface.
38   * Includes standard configuration information about a service that has been
39   * published to the service registry.
40   * 
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   *
43   */
44  @XmlRootElement(name = ServiceInfo.Constants.ROOT_ELEMENT_NAME)
45  @XmlAccessorType(XmlAccessType.NONE)
46  @XmlType(name = ServiceInfo.Constants.TYPE_NAME, propOrder = {
47      ServiceInfo.Elements.SERVICE_ID,
48      ServiceInfo.Elements.SERVICE_NAME,
49      ServiceInfo.Elements.ENDPOINT_URL,
50      ServiceInfo.Elements.INSTANCE_ID,
51      ServiceInfo.Elements.APPLICATION_ID,
52      ServiceInfo.Elements.SERVER_IP_ADDRESS,
53      ServiceInfo.Elements.TYPE,
54      ServiceInfo.Elements.SERVICE_VERSION,
55      ServiceInfo.Elements.STATUS,
56      ServiceInfo.Elements.SERVICE_DESCRIPTOR_ID,
57      ServiceInfo.Elements.CHECKSUM,
58      CoreConstants.CommonElements.VERSION_NUMBER,
59      CoreConstants.CommonElements.FUTURE_ELEMENTS
60  })
61  public final class ServiceInfo extends AbstractDataTransferObject implements ServiceInfoContract {
62  
63  	private static final long serialVersionUID = 4793306414624564991L;
64  	
65  	@XmlElement(name = Elements.SERVICE_ID, required = false)
66      private final String serviceId;
67  	
68  	@XmlJavaTypeAdapter(QNameAsStringAdapter.class)
69      @XmlElement(name = Elements.SERVICE_NAME, required = true)
70      private final QName serviceName;
71      
72      @XmlElement(name = Elements.ENDPOINT_URL, required = true)
73      private final String endpointUrl;
74      
75      @XmlElement(name = Elements.INSTANCE_ID, required = true)
76      private final String instanceId;
77      
78      @XmlElement(name = Elements.APPLICATION_ID, required = true)
79      private final String applicationId;
80      
81      @XmlElement(name = Elements.SERVER_IP_ADDRESS, required = true)
82      private final String serverIpAddress;
83      
84      @XmlElement(name = Elements.TYPE, required = true)
85      private final String type;
86      
87      @XmlElement(name = Elements.SERVICE_VERSION, required = true)
88      private final String serviceVersion;
89      
90      @XmlJavaTypeAdapter(ServiceEndpointStatus.Adapter.class)
91      @XmlElement(name = Elements.STATUS, required = true)
92      private final String status;
93      
94      @XmlElement(name = Elements.SERVICE_DESCRIPTOR_ID, required = false)
95      private final String serviceDescriptorId;
96      
97      @XmlElement(name = Elements.CHECKSUM, required = true)
98      private final String checksum;
99  
100     @Deprecated
101     @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
102     private final Long versionNumber = Long.valueOf(1);
103     
104     @SuppressWarnings("unused")
105     @XmlAnyElement
106     private final Collection<Element> _futureElements = null;
107 
108     /**
109      * Private constructor used only by JAXB.
110      * 
111      */
112     private ServiceInfo() {
113         this.serviceId = null;
114         this.serviceName = null;
115         this.endpointUrl = null;
116         this.instanceId = null;
117         this.applicationId = null;
118         this.serverIpAddress = null;
119         this.type = null;
120         this.serviceVersion = null;
121         this.status = null;
122         this.serviceDescriptorId = null;
123         this.checksum = null;
124     }
125 
126     private ServiceInfo(Builder builder) {
127         this.serviceId = builder.getServiceId();
128         this.serviceName = builder.getServiceName();
129         this.endpointUrl = builder.getEndpointUrl();
130         this.instanceId = builder.getInstanceId();
131         this.applicationId = builder.getApplicationId();
132         this.serverIpAddress = builder.getServerIpAddress();
133         this.type = builder.getType();
134         this.serviceVersion = builder.getServiceVersion();
135         ServiceEndpointStatus builderStatus = builder.getStatus();
136         this.status = builderStatus == null ? null : builderStatus.getCode();
137         this.serviceDescriptorId = builder.getServiceDescriptorId();
138         this.checksum = builder.getChecksum();
139     }
140 
141     @Override
142     public String getServiceId() {
143         return this.serviceId;
144     }
145 
146     @Override
147     public QName getServiceName() {
148         return this.serviceName;
149     }
150 
151     @Override
152     public String getEndpointUrl() {
153         return this.endpointUrl;
154     }
155     
156     @Override
157     public String getInstanceId() {
158         return this.instanceId;
159     }
160 
161     @Override
162     public String getApplicationId() {
163         return this.applicationId;
164     }
165 
166     @Override
167     public String getServerIpAddress() {
168         return this.serverIpAddress;
169     }
170     
171     @Override
172     public String getType() {
173     	return this.type;
174     }
175     
176     @Override
177     public String getServiceVersion() {
178     	return this.serviceVersion;
179     }
180     
181     @Override
182     public ServiceEndpointStatus getStatus() {
183     	return ServiceEndpointStatus.fromCode(this.status);
184     }
185 
186     @Override
187     public String getServiceDescriptorId() {
188         return this.serviceDescriptorId;
189     }
190     
191     @Override
192     public String getChecksum() {
193         return this.checksum;
194     }
195 
196     @Override
197     public Long getVersionNumber() {
198         return this.versionNumber;
199     }
200 
201     /**
202      * A builder which can be used to construct {@link ServiceInfo} instances.
203      * Enforces the constraints of the {@link ServiceInfoContract}.
204      */
205     public final static class Builder
206         implements Serializable, ModelBuilder, ServiceInfoContract
207     {
208 
209 		private static final long serialVersionUID = 4424090938369742940L;
210 
211 		private String serviceId;
212         private QName serviceName;
213         private String endpointUrl;
214         private String instanceId;
215         private String applicationId;
216         private String serverIpAddress;
217         private String type;
218         private String serviceVersion;
219         private ServiceEndpointStatus status;
220         private String serviceDescriptorId;
221         private String checksum;
222 
223         private Builder() {}
224 
225         public static Builder create() {
226             return new Builder();
227         }
228 
229         public static Builder create(ServiceInfoContract contract) {
230             if (contract == null) {
231                 throw new IllegalArgumentException("contract was null");
232             }
233             Builder builder = create();
234             builder.setServiceId(contract.getServiceId());
235             builder.setServiceName(contract.getServiceName());
236             builder.setEndpointUrl(contract.getEndpointUrl());
237             builder.setInstanceId(contract.getInstanceId());
238             builder.setApplicationId(contract.getApplicationId());
239             builder.setServerIpAddress(contract.getServerIpAddress());
240             builder.setType(contract.getType());
241             builder.setServiceVersion(contract.getServiceVersion());
242             builder.setStatus(contract.getStatus());
243             builder.setServiceDescriptorId(contract.getServiceDescriptorId());
244             builder.setChecksum(contract.getChecksum());
245             return builder;
246         }
247 
248         public ServiceInfo build() {
249         	validateAll();
250             return new ServiceInfo(this);
251         }
252 
253         @Override
254         public String getServiceId() {
255             return this.serviceId;
256         }
257 
258         @Override
259         public QName getServiceName() {
260             return this.serviceName;
261         }
262 
263         @Override
264         public String getEndpointUrl() {
265             return this.endpointUrl;
266         }
267         
268         @Override
269         public String getInstanceId() {
270             return this.instanceId;
271         }
272 
273         @Override
274         public String getApplicationId() {
275             return this.applicationId;
276         }
277 
278         @Override
279         public String getServerIpAddress() {
280             return this.serverIpAddress;
281         }
282         
283         @Override
284         public String getType() {
285         	return this.type;
286         }
287         
288         @Override
289         public String getServiceVersion() {
290         	return this.serviceVersion;
291         }
292 
293         @Override
294         public ServiceEndpointStatus getStatus() {
295         	return this.status;
296         }
297         
298         @Override
299         public String getServiceDescriptorId() {
300             return this.serviceDescriptorId;
301         }
302         
303         @Override
304         public String getChecksum() {
305             return this.checksum;
306         }
307 
308         @Deprecated
309         @Override
310         public Long getVersionNumber() {
311             return Long.valueOf(1);
312         }
313 
314         public void setServiceId(String serviceId) {
315             this.serviceId = serviceId;
316         }
317 
318         public void setServiceName(QName serviceName) {
319             validateServiceName(serviceName);
320             this.serviceName = serviceName;
321         }
322 
323         public void setEndpointUrl(String endpointUrl) {
324             validateEndpointUrl(endpointUrl);
325             this.endpointUrl = endpointUrl;
326         }
327         
328         public void setInstanceId(String instanceId) {
329             validateInstanceId(instanceId);
330             this.instanceId = instanceId;
331         }
332 
333         public void setApplicationId(String applicationId) {
334             validateApplicationId(applicationId);
335             this.applicationId = applicationId;
336         }
337 
338         public void setServerIpAddress(String serverIpAddress) {
339         	validateServerIpAddress(serverIpAddress);
340             this.serverIpAddress = serverIpAddress;
341         }
342         
343         public void setType(String type) {
344         	validateType(type);
345         	this.type = type;
346         }
347         
348         public void setServiceVersion(String serviceVersion) {
349         	validateServiceVersion(serviceVersion);
350         	this.serviceVersion = serviceVersion;
351         }
352         
353         public void setStatus(ServiceEndpointStatus status) {
354         	validateStatus(status);
355         	this.status = status;
356         }
357 
358         public void setServiceDescriptorId(String serviceDescriptorId) {
359             this.serviceDescriptorId = serviceDescriptorId;
360         }
361         
362         public void setChecksum(String checksum) {
363             validateChecksum(checksum);
364             this.checksum = checksum;
365         }
366 
367         /**
368          * Version number is deprecated, so this method does nothing.
369          *
370          * @deprecated version number is no longer used
371          */
372         @Deprecated
373         public void setVersionNumber(Long versionNumber) {
374             // no longer does anything
375         }
376         
377         private void assertNotNull(String name, Object object) {
378         	if (object == null) {
379         		throw new IllegalArgumentException(name + " was null");
380         	}
381         }
382         
383         private void assertNotBlank(String name, String value) {
384         	assertNotNull(name, value);
385         	if (StringUtils.isBlank(value)) {
386         		throw new IllegalArgumentException(name + " was blank");
387         	}
388         }
389         
390         private void validateServiceName(QName serviceName) {
391         	assertNotNull("serviceName", serviceName);
392         }
393         
394         private void validateEndpointUrl(String endpointUrl) {
395         	assertNotBlank("endpointUrl", endpointUrl);
396         }
397         
398         private void validateInstanceId(String instanceId) {
399         	assertNotBlank("instanceId", instanceId);
400         }
401         
402         private void validateApplicationId(String applicationId) {
403         	assertNotBlank("applicationId", applicationId);
404         }
405         
406         private void validateServerIpAddress(String serverIpAddress) {
407         	assertNotBlank("serverIpAddress", serverIpAddress);
408         }
409         
410         private void validateType(String type) {
411         	assertNotBlank("type", type);
412         }
413 
414         private void validateServiceVersion(String serviceVersion) {
415         	assertNotBlank("serviceVersion", serviceVersion);
416         }
417         
418         private void validateStatus(ServiceEndpointStatus status) {
419         	assertNotNull("status", status);
420         }
421         
422         private void validateChecksum(String checksum) {
423         	assertNotBlank("checksum", checksum);
424         }
425         
426         private void validateAll() {
427         	validateServiceName(serviceName);
428             validateEndpointUrl(endpointUrl);
429             validateInstanceId(instanceId);
430             validateApplicationId(applicationId);
431             validateServerIpAddress(serverIpAddress);
432             validateType(type);
433             validateServiceVersion(serviceVersion);
434             validateStatus(status);
435             validateChecksum(checksum);
436         }
437 
438     }
439 
440 
441     /**
442      * Defines some internal constants used on this class.
443      * 
444      */
445     static class Constants {
446 
447         final static String ROOT_ELEMENT_NAME = "serviceInfo";
448         final static String TYPE_NAME = "ServiceInfoType";
449     }
450 
451 
452     /**
453      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
454      * 
455      */
456     static class Elements {
457 
458         final static String SERVICE_ID = "serviceId";
459         final static String SERVICE_NAME = "serviceName";
460         final static String ENDPOINT_URL = "endpointUrl";
461         final static String INSTANCE_ID = "instanceId";
462         final static String APPLICATION_ID = "applicationId";
463         final static String SERVER_IP_ADDRESS = "serverIpAddress";
464         final static String TYPE = "type";
465         final static String SERVICE_VERSION = "serviceVersion";
466         final static String STATUS = "status";
467         final static String SERVICE_DESCRIPTOR_ID = "serviceDescriptorId";
468         final static String CHECKSUM = "checksum";
469 
470     }
471 
472 }
473