summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/pre-spec-changes/kernel/spi/src/main/java/org/apache/tuscany/spi/model/ServiceContract.java
blob: b199de720cecf011edf8dc5e64a723960e29c1ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.spi.model;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Base class representing service contract information
 *
 * @version $Rev$ $Date$
 */
public abstract class ServiceContract<T> extends ModelObject implements Cloneable {
    protected InteractionScope interactionScope;
    protected boolean remotable;
    protected Class<?> interfaceClass;
    protected String interfaceName;
    protected String callbackName;
    protected Class<?> callbackClass;
    protected Map<String, Operation<T>> operations;
    protected Map<String, Operation<T>> callbackOperations;
    protected String dataBinding;
    protected Map<String, Object> metaData;

    protected ServiceContract() {
    }

    protected ServiceContract(Class<?> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }

    protected ServiceContract(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    /**
     * Returns the interface name for the contract
     *
     * @return the interface name for the contract
     */
    public String getInterfaceName() {
        return interfaceName;
    }

    /**
     * Sets the interface name for the contract
     */
    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    /**
     * Returns the class used to represent the service contract
     */
    public Class<?> getInterfaceClass() {
        return interfaceClass;
    }

    /**
     * Sets the class used to represent the service contract
     */
    public void setInterfaceClass(Class<?> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }

    /**
     * Returns the service interaction scope
     */
    public InteractionScope getInteractionScope() {
        return interactionScope;
    }

    /**
     * Sets the service interaction scope
     */
    public void setInteractionScope(InteractionScope interactionScope) {
        this.interactionScope = interactionScope;
    }

    /**
     * @return the remotable
     */
    public boolean isRemotable() {
        return remotable;
    }

    /**
     * @param remotable the remotable to set
     */
    public void setRemotable(boolean remotable) {
        this.remotable = remotable;
    }

    /**
     * Returns the name of the callback or null if the contract is unidirectional
     */
    public String getCallbackName() {
        return callbackName;
    }

    /**
     * Sets the name of the callback service
     */
    public void setCallbackName(String callbackName) {
        this.callbackName = callbackName;
    }

    /**
     * Returns the name of the callback service
     */
    public Class<?> getCallbackClass() {
        return callbackClass;
    }

    public void setCallbackClass(Class<?> callbackClass) {
        this.callbackClass = callbackClass;
    }

    public Map<String, Operation<T>> getOperations() {
        if (operations == null) {
            return Collections.emptyMap();
        }
        return Collections.unmodifiableMap(operations);
    }

    public void setOperations(Map<String, Operation<T>> operations) {
        for (Operation<T> operation : operations.values()) {
            operation.setServiceContract(this);
        }
        this.operations = operations;
    }

    public Map<String, Operation<T>> getCallbackOperations() {
        if (callbackOperations == null) {
            return Collections.emptyMap();
        }
        return Collections.unmodifiableMap(callbackOperations);
    }

    public void setCallbackOperations(Map<String, Operation<T>> callbacksOperations) {
        for (Operation<T> operation : callbacksOperations.values()) {
            operation.setServiceContract(this);
            operation.setCallback(true);
        }
        this.callbackOperations = callbacksOperations;
    }

    public String getDataBinding() {
        return dataBinding;
    }

    public void setDataBinding(String dataBinding) {
        this.dataBinding = dataBinding;
    }

    /**
     * Returns a map of metadata key to value mappings for the operation.
     *
     * @return a map of metadata key to value mappings for the operation.
     */
    public Map<String, Object> getMetaData() {
        if (metaData == null) {
            return Collections.emptyMap();
        }
        return metaData;
    }

    /**
     * Adds metadata associated with the operation.
     *
     * @param key the metadata key
     * @param val the metadata value
     */
    public void setMetaData(String key, Object val) {
        if (metaData == null) {
            metaData = new HashMap<String, Object>();
        }
        metaData.put(key, val);
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        final ServiceContract that = (ServiceContract) o;

        if (callbackName != null ? !callbackName.equals(that.callbackName) : that.callbackName != null) {
            return false;
        }
        if (callbackOperations != null ? !callbackOperations.equals(that.callbackOperations)
            : that.callbackOperations != null) {
            return false;
        }
        if (interfaceClass != null ? !interfaceClass.equals(that.interfaceClass) : that.interfaceClass != null) {
            return false;
        }
        if (interfaceName != null ? !interfaceName.equals(that.interfaceName) : that.interfaceName != null) {
            return false;
        }
        return !(operations != null ? !operations.equals(that.operations) : that.operations != null);

    }

    public int hashCode() {
        int result;
        result = interfaceClass != null ? interfaceClass.hashCode() : 0;
        result = 29 * result + (interfaceName != null ? interfaceName.hashCode() : 0);
        result = 29 * result + (callbackName != null ? callbackName.hashCode() : 0);
        result = 29 * result + (operations != null ? operations.hashCode() : 0);
        result = 29 * result + (callbackOperations != null ? callbackOperations.hashCode() : 0);
        return result;
    }

    public String toString() {
        if (interfaceName != null) {
            return new StringBuilder().append("ServiceContract[").append(interfaceName).append("]").toString();
        } else if (interfaceClass != null) {
            return new StringBuilder().append("ServiceContract[").append(interfaceClass.getName()).append("]")
                .toString();
        } else {
            return super.toString();
        }

    }

    @SuppressWarnings("unchecked")
    @Override
    public Object clone() throws CloneNotSupportedException {
        ServiceContract<T> copy = (ServiceContract<T>) super.clone();

        if (operations != null) {
            Map<String, Operation<T>> clonedOperations = new HashMap<String, Operation<T>>();
            for (Operation<T> o : operations.values()) {
                clonedOperations.put(o.getName(), o);
            }
            copy.setOperations(clonedOperations);
        }

        if (callbackOperations != null) {
            Map<String, Operation<T>> clonedCallbackOperations = new HashMap<String, Operation<T>>();
            for (Operation<T> o : callbackOperations.values()) {
                clonedCallbackOperations.put(o.getName(), (Operation<T>) o.clone());
            }
            copy.setCallbackOperations(clonedCallbackOperations);
        }

        if (this.metaData != null) {
            assert this.metaData instanceof HashMap;
            copy.metaData = (HashMap) ((HashMap) this.metaData).clone();
        }

        return copy;
    }
}