001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.ksb.api.registry;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.CoreConstants;
020    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021    import org.kuali.rice.core.api.mo.ModelBuilder;
022    import org.kuali.rice.core.api.util.jaxb.QNameAsStringAdapter;
023    import org.w3c.dom.Element;
024    
025    import javax.xml.bind.annotation.XmlAccessType;
026    import javax.xml.bind.annotation.XmlAccessorType;
027    import javax.xml.bind.annotation.XmlAnyElement;
028    import javax.xml.bind.annotation.XmlElement;
029    import javax.xml.bind.annotation.XmlRootElement;
030    import javax.xml.bind.annotation.XmlType;
031    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
032    import javax.xml.namespace.QName;
033    import java.io.Serializable;
034    import 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    })
061    public 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        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
101        private final Long versionNumber;
102        
103        @SuppressWarnings("unused")
104        @XmlAnyElement
105        private final Collection<Element> _futureElements = null;
106    
107        /**
108         * Private constructor used only by JAXB.
109         * 
110         */
111        private ServiceInfo() {
112            this.serviceId = null;
113            this.serviceName = null;
114            this.endpointUrl = null;
115            this.instanceId = null;
116            this.applicationId = null;
117            this.serverIpAddress = null;
118            this.type = null;
119            this.serviceVersion = null;
120            this.status = null;
121            this.serviceDescriptorId = null;
122            this.checksum = null;
123            this.versionNumber = 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            this.versionNumber = builder.getVersionNumber();
140        }
141    
142        @Override
143        public String getServiceId() {
144            return this.serviceId;
145        }
146    
147        @Override
148        public QName getServiceName() {
149            return this.serviceName;
150        }
151    
152        @Override
153        public String getEndpointUrl() {
154            return this.endpointUrl;
155        }
156        
157        @Override
158        public String getInstanceId() {
159            return this.instanceId;
160        }
161    
162        @Override
163        public String getApplicationId() {
164            return this.applicationId;
165        }
166    
167        @Override
168        public String getServerIpAddress() {
169            return this.serverIpAddress;
170        }
171        
172        @Override
173        public String getType() {
174            return this.type;
175        }
176        
177        @Override
178        public String getServiceVersion() {
179            return this.serviceVersion;
180        }
181        
182        @Override
183        public ServiceEndpointStatus getStatus() {
184            return ServiceEndpointStatus.fromCode(this.status);
185        }
186    
187        @Override
188        public String getServiceDescriptorId() {
189            return this.serviceDescriptorId;
190        }
191        
192        @Override
193        public String getChecksum() {
194            return this.checksum;
195        }
196    
197        @Override
198        public Long getVersionNumber() {
199            return this.versionNumber;
200        }
201    
202        /**
203         * A builder which can be used to construct {@link ServiceInfo} instances.
204         * Enforces the constraints of the {@link ServiceInfoContract}.
205         */
206        public final static class Builder
207            implements Serializable, ModelBuilder, ServiceInfoContract
208        {
209    
210                    private static final long serialVersionUID = 4424090938369742940L;
211    
212                    private String serviceId;
213            private QName serviceName;
214            private String endpointUrl;
215            private String instanceId;
216            private String applicationId;
217            private String serverIpAddress;
218            private String type;
219            private String serviceVersion;
220            private ServiceEndpointStatus status;
221            private String serviceDescriptorId;
222            private String checksum;
223            private Long versionNumber;
224    
225            private Builder() {}
226    
227            public static Builder create() {
228                return new Builder();
229            }
230    
231            public static Builder create(ServiceInfoContract contract) {
232                if (contract == null) {
233                    throw new IllegalArgumentException("contract was null");
234                }
235                Builder builder = create();
236                builder.setServiceId(contract.getServiceId());
237                builder.setServiceName(contract.getServiceName());
238                builder.setEndpointUrl(contract.getEndpointUrl());
239                builder.setInstanceId(contract.getInstanceId());
240                builder.setApplicationId(contract.getApplicationId());
241                builder.setServerIpAddress(contract.getServerIpAddress());
242                builder.setType(contract.getType());
243                builder.setServiceVersion(contract.getServiceVersion());
244                builder.setStatus(contract.getStatus());
245                builder.setServiceDescriptorId(contract.getServiceDescriptorId());
246                builder.setChecksum(contract.getChecksum());
247                builder.setVersionNumber(contract.getVersionNumber());
248                return builder;
249            }
250    
251            public ServiceInfo build() {
252                    validateAll();
253                return new ServiceInfo(this);
254            }
255    
256            @Override
257            public String getServiceId() {
258                return this.serviceId;
259            }
260    
261            @Override
262            public QName getServiceName() {
263                return this.serviceName;
264            }
265    
266            @Override
267            public String getEndpointUrl() {
268                return this.endpointUrl;
269            }
270            
271            @Override
272            public String getInstanceId() {
273                return this.instanceId;
274            }
275    
276            @Override
277            public String getApplicationId() {
278                return this.applicationId;
279            }
280    
281            @Override
282            public String getServerIpAddress() {
283                return this.serverIpAddress;
284            }
285            
286            @Override
287            public String getType() {
288                    return this.type;
289            }
290            
291            @Override
292            public String getServiceVersion() {
293                    return this.serviceVersion;
294            }
295    
296            @Override
297            public ServiceEndpointStatus getStatus() {
298                    return this.status;
299            }
300            
301            @Override
302            public String getServiceDescriptorId() {
303                return this.serviceDescriptorId;
304            }
305            
306            @Override
307            public String getChecksum() {
308                return this.checksum;
309            }
310    
311            @Override
312            public Long getVersionNumber() {
313                return this.versionNumber;
314            }
315    
316            public void setServiceId(String serviceId) {
317                this.serviceId = serviceId;
318            }
319    
320            public void setServiceName(QName serviceName) {
321                validateServiceName(serviceName);
322                this.serviceName = serviceName;
323            }
324    
325            public void setEndpointUrl(String endpointUrl) {
326                validateEndpointUrl(endpointUrl);
327                this.endpointUrl = endpointUrl;
328            }
329            
330            public void setInstanceId(String instanceId) {
331                validateInstanceId(instanceId);
332                this.instanceId = instanceId;
333            }
334    
335            public void setApplicationId(String applicationId) {
336                validateApplicationId(applicationId);
337                this.applicationId = applicationId;
338            }
339    
340            public void setServerIpAddress(String serverIpAddress) {
341                    validateServerIpAddress(serverIpAddress);
342                this.serverIpAddress = serverIpAddress;
343            }
344            
345            public void setType(String type) {
346                    validateType(type);
347                    this.type = type;
348            }
349            
350            public void setServiceVersion(String serviceVersion) {
351                    validateServiceVersion(serviceVersion);
352                    this.serviceVersion = serviceVersion;
353            }
354            
355            public void setStatus(ServiceEndpointStatus status) {
356                    validateStatus(status);
357                    this.status = status;
358            }
359    
360            public void setServiceDescriptorId(String serviceDescriptorId) {
361                this.serviceDescriptorId = serviceDescriptorId;
362            }
363            
364            public void setChecksum(String checksum) {
365                validateChecksum(checksum);
366                this.checksum = checksum;
367            }
368    
369            public void setVersionNumber(Long versionNumber) {
370                this.versionNumber = versionNumber;
371            }
372            
373            private void assertNotNull(String name, Object object) {
374                    if (object == null) {
375                            throw new IllegalArgumentException(name + " was null");
376                    }
377            }
378            
379            private void assertNotBlank(String name, String value) {
380                    assertNotNull(name, value);
381                    if (StringUtils.isBlank(value)) {
382                            throw new IllegalArgumentException(name + " was blank");
383                    }
384            }
385            
386            private void validateServiceName(QName serviceName) {
387                    assertNotNull("serviceName", serviceName);
388            }
389            
390            private void validateEndpointUrl(String endpointUrl) {
391                    assertNotBlank("endpointUrl", endpointUrl);
392            }
393            
394            private void validateInstanceId(String instanceId) {
395                    assertNotBlank("instanceId", instanceId);
396            }
397            
398            private void validateApplicationId(String applicationId) {
399                    assertNotBlank("applicationId", applicationId);
400            }
401            
402            private void validateServerIpAddress(String serverIpAddress) {
403                    assertNotBlank("serverIpAddress", serverIpAddress);
404            }
405            
406            private void validateType(String type) {
407                    assertNotBlank("type", type);
408            }
409    
410            private void validateServiceVersion(String serviceVersion) {
411                    assertNotBlank("serviceVersion", serviceVersion);
412            }
413            
414            private void validateStatus(ServiceEndpointStatus status) {
415                    assertNotNull("status", status);
416            }
417            
418            private void validateChecksum(String checksum) {
419                    assertNotBlank("checksum", checksum);
420            }
421            
422            private void validateAll() {
423                    validateServiceName(serviceName);
424                validateEndpointUrl(endpointUrl);
425                validateInstanceId(instanceId);
426                validateApplicationId(applicationId);
427                validateServerIpAddress(serverIpAddress);
428                validateType(type);
429                validateServiceVersion(serviceVersion);
430                validateStatus(status);
431                validateChecksum(checksum);
432            }
433    
434        }
435    
436    
437        /**
438         * Defines some internal constants used on this class.
439         * 
440         */
441        static class Constants {
442    
443            final static String ROOT_ELEMENT_NAME = "serviceInfo";
444            final static String TYPE_NAME = "ServiceInfoType";
445        }
446    
447    
448        /**
449         * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
450         * 
451         */
452        static class Elements {
453    
454            final static String SERVICE_ID = "serviceId";
455            final static String SERVICE_NAME = "serviceName";
456            final static String ENDPOINT_URL = "endpointUrl";
457            final static String INSTANCE_ID = "instanceId";
458            final static String APPLICATION_ID = "applicationId";
459            final static String SERVER_IP_ADDRESS = "serverIpAddress";
460            final static String TYPE = "type";
461            final static String SERVICE_VERSION = "serviceVersion";
462            final static String STATUS = "status";
463            final static String SERVICE_DESCRIPTOR_ID = "serviceDescriptorId";
464            final static String CHECKSUM = "checksum";
465    
466        }
467    
468    }
469