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     */
016    package org.kuali.rice.ksb.api.bus.support;
017    
018    import org.kuali.rice.core.api.CoreConstants;
019    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
020    import org.kuali.rice.core.api.security.credentials.CredentialsType;
021    import org.kuali.rice.core.api.util.jaxb.EnumStringAdapter;
022    import org.kuali.rice.core.api.util.jaxb.QNameAsStringAdapter;
023    import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
024    import org.kuali.rice.ksb.api.bus.ServiceDefinition;
025    import org.w3c.dom.Element;
026    
027    import javax.xml.bind.annotation.XmlAccessType;
028    import javax.xml.bind.annotation.XmlAccessorType;
029    import javax.xml.bind.annotation.XmlAnyElement;
030    import javax.xml.bind.annotation.XmlElement;
031    import javax.xml.bind.annotation.XmlType;
032    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
033    import javax.xml.namespace.QName;
034    import java.io.Serializable;
035    import java.net.URL;
036    import java.util.Collection;
037    
038    @XmlAccessorType(XmlAccessType.NONE)
039    @XmlType(name = AbstractServiceConfiguration.Constants.TYPE_NAME, propOrder = {
040                    AbstractServiceConfiguration.Elements.SERVICE_NAME,
041                    AbstractServiceConfiguration.Elements.ENDPOINT_URL,
042                    AbstractServiceConfiguration.Elements.APPLICATION_ID,
043            AbstractServiceConfiguration.Elements.INSTANCE_ID,
044                    AbstractServiceConfiguration.Elements.SERVICE_VERSION,
045                    AbstractServiceConfiguration.Elements.TYPE,
046                    AbstractServiceConfiguration.Elements.QUEUE,
047                    AbstractServiceConfiguration.Elements.PRIORITY,
048                    AbstractServiceConfiguration.Elements.RETRY_ATTEMPTS,
049                    AbstractServiceConfiguration.Elements.MILLIS_TO_LIVE,
050                    AbstractServiceConfiguration.Elements.MESSAGE_EXCEPTION_HANDLER,
051                    AbstractServiceConfiguration.Elements.BUS_SECURITY,
052                    AbstractServiceConfiguration.Elements.CREDENTIALS_TYPE,
053                    AbstractServiceConfiguration.Elements.BASIC_AUTHENTICATION,
054                     CoreConstants.CommonElements.FUTURE_ELEMENTS
055    })
056    public abstract class AbstractServiceConfiguration extends AbstractDataTransferObject implements ServiceConfiguration {
057    
058            private static final long serialVersionUID = 2681595879406587302L;
059    
060            @XmlJavaTypeAdapter(QNameAsStringAdapter.class)
061            @XmlElement(name = Elements.SERVICE_NAME, required = true)
062            private final QName serviceName;
063            
064            @XmlElement(name = Elements.ENDPOINT_URL, required = true)
065            private final URL endpointUrl;
066    
067        @XmlElement(name = Elements.INSTANCE_ID, required = true)
068            private final String instanceId;
069    
070            @XmlElement(name = Elements.APPLICATION_ID, required = true)
071            private final String applicationId;
072            
073            @XmlElement(name = Elements.SERVICE_VERSION, required = true)
074            private final String serviceVersion;
075            
076            @XmlElement(name = Elements.TYPE, required = true)
077            private final String type;
078            
079            @XmlElement(name = Elements.QUEUE, required = false)
080            private final boolean queue;
081            
082            @XmlElement(name = Elements.PRIORITY, required = false)
083            private final Integer priority;
084            
085            @XmlElement(name = Elements.RETRY_ATTEMPTS, required = false)
086            private final Integer retryAttempts;
087            
088            @XmlElement(name = Elements.MILLIS_TO_LIVE, required = false)
089            private final Long millisToLive;
090            
091            @XmlElement(name = Elements.MESSAGE_EXCEPTION_HANDLER, required = false)
092            private final String messageExceptionHandler;
093            
094            @XmlElement(name = Elements.BUS_SECURITY, required = false)
095            private final Boolean busSecurity;
096            
097            @XmlJavaTypeAdapter(CredentialsTypeAdapter.class)
098            @XmlElement(name = Elements.CREDENTIALS_TYPE, required = false)
099            private final String credentialsType;
100    
101            @XmlElement(name = Elements.BASIC_AUTHENTICATION, required = false)
102            private final Boolean basicAuthentication;
103    
104            @SuppressWarnings("unused")
105            @XmlAnyElement
106            private final Collection<Element> _futureElements = null;
107    
108            /**
109         * Constructor intended for use only by JAXB.
110         */
111            protected AbstractServiceConfiguration() {
112                    this.serviceName = null;
113                    this.endpointUrl = null;
114            this.instanceId = null;
115                    this.applicationId = null;
116                    this.serviceVersion = null;
117                    this.type = null;
118                    this.queue = false;
119                    this.priority = null;
120                    this.retryAttempts = null;
121                    this.millisToLive = null;
122                    this.messageExceptionHandler = null;
123                    this.busSecurity = null;
124                    this.credentialsType = null;
125                    this.basicAuthentication = false;
126            }
127            
128            protected AbstractServiceConfiguration(Builder<?> builder) {
129                    this.serviceName = builder.getServiceName();
130                    this.endpointUrl = builder.getEndpointUrl();
131            this.instanceId = builder.getInstanceId();
132                    this.applicationId = builder.getApplicationId();
133                    this.serviceVersion = builder.getServiceVersion();
134                    this.type = builder.getType();
135                    this.queue = builder.isQueue();
136                    this.priority = builder.getPriority();
137                    this.retryAttempts = builder.getRetryAttempts();
138                    this.millisToLive = builder.getMillisToLive();
139                    this.messageExceptionHandler = builder.getMessageExceptionHandler();
140                    this.busSecurity = builder.getBusSecurity();
141                    CredentialsType cred = builder.getCredentialsType();
142                    this.credentialsType = cred == null ? null : cred.name();
143                    this.basicAuthentication = builder.isBasicAuthentication();
144            }
145            
146            public QName getServiceName() {
147                    return serviceName;
148            }
149    
150            public URL getEndpointUrl() {
151                    return endpointUrl;
152            }
153    
154        public String getInstanceId() {
155            return instanceId;
156        }
157    
158            public String getApplicationId() {
159                    return applicationId;
160            }
161            
162            public String getServiceVersion() {
163                    return serviceVersion;
164            }
165            
166            public String getType() {
167                    return type;
168            }
169    
170            public boolean isQueue() {
171                    return queue;
172            }
173    
174            public Integer getPriority() {
175                    return priority;
176            }
177    
178            public Integer getRetryAttempts() {
179                    return retryAttempts;
180            }
181    
182            public Long getMillisToLive() {
183                    return millisToLive;
184            }
185    
186            public String getMessageExceptionHandler() {
187                    return messageExceptionHandler;
188            }
189    
190            public Boolean getBusSecurity() {
191                    return busSecurity;
192            }
193    
194            public CredentialsType getCredentialsType() {
195                    if (credentialsType == null) {
196                            return null;
197                    }
198                    return CredentialsType.valueOf(credentialsType);
199            }
200    
201            @Override
202            public Boolean isBasicAuthentication() {
203                    return basicAuthentication;
204            }
205    
206            protected static abstract class Builder<T> implements Serializable {
207                    
208                    private static final long serialVersionUID = -3002495884401672488L;
209    
210                    private QName serviceName;
211                    private URL endpointUrl;
212            private String instanceId;
213                    private String applicationId;
214                    private String serviceVersion;
215                    private String type;
216                    private boolean queue;
217                    private Integer priority;
218                    private Integer retryAttempts;
219                    private Long millisToLive;
220                    private String messageExceptionHandler;
221                    private Boolean busSecurity;
222                    private CredentialsType credentialsType;
223                    private Boolean basicAuthentication = false;
224    
225                    public abstract T build();
226                    
227                    protected void copyServiceDefinitionProperties(ServiceDefinition serviceDefinition) {
228                            setServiceName(serviceDefinition.getServiceName());
229                            setEndpointUrl(serviceDefinition.getEndpointUrl());
230                            setApplicationId(serviceDefinition.getApplicationId());
231                setInstanceId(serviceDefinition.getInstanceId());
232                            setServiceVersion(serviceDefinition.getServiceVersion());
233                            setType(serviceDefinition.getType());
234                            setQueue(serviceDefinition.isQueue());
235                            setPriority(serviceDefinition.getPriority());
236                            setRetryAttempts(serviceDefinition.getRetryAttempts());
237                            setMillisToLive(serviceDefinition.getMillisToLive());
238                            setMessageExceptionHandler(serviceDefinition.getMessageExceptionHandler());
239                            setBusSecurity(serviceDefinition.getBusSecurity());
240                            setCredentialsType(serviceDefinition.getCredentialsType());
241                            setBasicAuthentication(serviceDefinition.isBasicAuthentication());
242                    }
243                    
244                    public QName getServiceName() {
245                            return serviceName;
246                    }
247                    public void setServiceName(QName serviceName) {
248                            this.serviceName = serviceName;
249                    }
250                    public URL getEndpointUrl() {
251                            return endpointUrl;
252                    }
253                    public void setEndpointUrl(URL endpointUrl) {
254                            this.endpointUrl = endpointUrl;
255                    }
256            public String getInstanceId() {
257               return instanceId;
258            }
259            public void setInstanceId(String instanceId) {
260                this.instanceId = instanceId;
261            }
262                    public String getApplicationId() {
263                            return applicationId;
264                    }
265                    public void setApplicationId(String applicationId) {
266                            this.applicationId = applicationId;
267                    }
268                    public String getServiceVersion() {
269                            return serviceVersion;
270                    }
271                    public void setServiceVersion(String serviceVersion) {
272                            this.serviceVersion = serviceVersion;
273                    }
274                    public String getType() {
275                            return type;
276                    }
277                    public void setType(String type) {
278                            this.type = type;
279                    }
280                    public boolean isQueue() {
281                            return queue;
282                    }
283                    public void setQueue(boolean queue) {
284                            this.queue = queue;
285                    }
286                    public Integer getPriority() {
287                            return priority;
288                    }
289                    public void setPriority(Integer priority) {
290                            this.priority = priority;
291                    }
292                    public Integer getRetryAttempts() {
293                            return retryAttempts;
294                    }
295                    public void setRetryAttempts(Integer retryAttempts) {
296                            this.retryAttempts = retryAttempts;
297                    }
298                    public Long getMillisToLive() {
299                            return millisToLive;
300                    }
301                    public void setMillisToLive(Long millisToLive) {
302                            this.millisToLive = millisToLive;
303                    }
304                    public String getMessageExceptionHandler() {
305                            return messageExceptionHandler;
306                    }
307                    public void setMessageExceptionHandler(String messageExceptionHandler) {
308                            this.messageExceptionHandler = messageExceptionHandler;
309                    }
310                    public Boolean getBusSecurity() {
311                            return busSecurity;
312                    }
313                    public void setBusSecurity(Boolean busSecurity) {
314                            this.busSecurity = busSecurity;
315                    }
316                    public CredentialsType getCredentialsType() {
317                            return credentialsType;
318                    }
319                    public void setCredentialsType(CredentialsType credentialsType) {
320                            this.credentialsType = credentialsType;
321                    }
322                    public Boolean isBasicAuthentication() {
323                            return basicAuthentication;
324                    }
325                    public void setBasicAuthentication(Boolean basicAuthentication) {
326                            this.basicAuthentication = basicAuthentication;
327                    }
328    
329            }
330            
331        /**
332         * Defines some internal constants used on this class.
333         */
334        protected static class Constants {
335            protected final static String TYPE_NAME = "ServiceConfigurationType";
336        }
337    
338        /**
339         * A private class which exposes constants which define the XML element names to use
340         * when this object is marshalled to XML.
341         */
342        protected static class Elements {
343            protected final static String SERVICE_NAME = "serviceName";
344            protected final static String ENDPOINT_URL = "endpointUrl";
345            protected final static String INSTANCE_ID = "instanceId";
346            protected final static String APPLICATION_ID = "applicationId";
347            protected final static String SERVICE_VERSION = "serviceVersion";
348            protected final static String TYPE = "type";
349            protected final static String QUEUE = "queue";
350            protected final static String PRIORITY = "priority";
351            protected final static String RETRY_ATTEMPTS = "retryAttempts";
352            protected final static String MILLIS_TO_LIVE = "millisToLive";
353            protected final static String MESSAGE_EXCEPTION_HANDLER = "messageExceptionHandler";
354            protected final static String BUS_SECURITY = "busSecurity";
355            protected final static String CREDENTIALS_TYPE = "credentialsType";
356            protected final static String BASIC_AUTHENTICATION = "basicAuthentication";
357        }
358        
359        static final class CredentialsTypeAdapter extends EnumStringAdapter<CredentialsType> {
360                    
361                    protected Class<CredentialsType> getEnumClass() {
362                            return CredentialsType.class;
363                    }
364                    
365            }
366            
367    }