001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ksb.api.registry;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.CoreConstants;
020import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021import org.kuali.rice.core.api.mo.ModelBuilder;
022import org.kuali.rice.core.api.util.jaxb.QNameAsStringAdapter;
023import org.w3c.dom.Element;
024
025import javax.xml.bind.annotation.XmlAccessType;
026import javax.xml.bind.annotation.XmlAccessorType;
027import javax.xml.bind.annotation.XmlAnyElement;
028import javax.xml.bind.annotation.XmlElement;
029import javax.xml.bind.annotation.XmlRootElement;
030import javax.xml.bind.annotation.XmlType;
031import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
032import javax.xml.namespace.QName;
033import java.io.Serializable;
034import java.util.Collection;
035
036/**
037 * Immutable implementation of the {@link ServiceInfoContract} interface.
038 * Includes standard configuration information about a service that has been
039 * published to the service registry.
040 * 
041 * @author Kuali Rice Team (rice.collab@kuali.org)
042 *
043 */
044@XmlRootElement(name = ServiceInfo.Constants.ROOT_ELEMENT_NAME)
045@XmlAccessorType(XmlAccessType.NONE)
046@XmlType(name = ServiceInfo.Constants.TYPE_NAME, propOrder = {
047    ServiceInfo.Elements.SERVICE_ID,
048    ServiceInfo.Elements.SERVICE_NAME,
049    ServiceInfo.Elements.ENDPOINT_URL,
050    ServiceInfo.Elements.INSTANCE_ID,
051    ServiceInfo.Elements.APPLICATION_ID,
052    ServiceInfo.Elements.SERVER_IP_ADDRESS,
053    ServiceInfo.Elements.TYPE,
054    ServiceInfo.Elements.SERVICE_VERSION,
055    ServiceInfo.Elements.STATUS,
056    ServiceInfo.Elements.SERVICE_DESCRIPTOR_ID,
057    ServiceInfo.Elements.CHECKSUM,
058    CoreConstants.CommonElements.VERSION_NUMBER,
059    CoreConstants.CommonElements.FUTURE_ELEMENTS
060})
061public final class ServiceInfo extends AbstractDataTransferObject implements ServiceInfoContract {
062
063        private static final long serialVersionUID = 4793306414624564991L;
064        
065        @XmlElement(name = Elements.SERVICE_ID, required = false)
066    private final String serviceId;
067        
068        @XmlJavaTypeAdapter(QNameAsStringAdapter.class)
069    @XmlElement(name = Elements.SERVICE_NAME, required = true)
070    private final QName serviceName;
071    
072    @XmlElement(name = Elements.ENDPOINT_URL, required = true)
073    private final String endpointUrl;
074    
075    @XmlElement(name = Elements.INSTANCE_ID, required = true)
076    private final String instanceId;
077    
078    @XmlElement(name = Elements.APPLICATION_ID, required = true)
079    private final String applicationId;
080    
081    @XmlElement(name = Elements.SERVER_IP_ADDRESS, required = true)
082    private final String serverIpAddress;
083    
084    @XmlElement(name = Elements.TYPE, required = true)
085    private final String type;
086    
087    @XmlElement(name = Elements.SERVICE_VERSION, required = true)
088    private final String serviceVersion;
089    
090    @XmlJavaTypeAdapter(ServiceEndpointStatus.Adapter.class)
091    @XmlElement(name = Elements.STATUS, required = true)
092    private final String status;
093    
094    @XmlElement(name = Elements.SERVICE_DESCRIPTOR_ID, required = false)
095    private final String serviceDescriptorId;
096    
097    @XmlElement(name = Elements.CHECKSUM, required = true)
098    private final String checksum;
099
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