| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
package org.apache.qpid.proton.engine.impl; |
| 23 |
|
| 24 |
import org.apache.qpid.proton.amqp.transport.ErrorCondition; |
| 25 |
import org.apache.qpid.proton.engine.EndpointState; |
| 26 |
import org.apache.qpid.proton.engine.Event; |
| 27 |
import org.apache.qpid.proton.engine.ProtonJEndpoint; |
| 28 |
import org.apache.qpid.proton.engine.Record; |
| 29 |
|
| 30 |
public abstract class EndpointImpl implements ProtonJEndpoint |
| 31 |
{ |
| 32 |
private EndpointState _localState = EndpointState.UNINITIALIZED; |
| 33 |
private EndpointState _remoteState = EndpointState.UNINITIALIZED; |
| 34 |
private ErrorCondition _localError = new ErrorCondition(); |
| 35 |
private ErrorCondition _remoteError = new ErrorCondition(); |
| 36 |
private boolean _modified; |
| 37 |
private EndpointImpl _transportNext; |
| 38 |
private EndpointImpl _transportPrev; |
| 39 |
private Object _context; |
| 40 |
private Record _attachments = new RecordImpl(); |
| 41 |
|
| 42 |
private int refcount = 1; |
| 43 |
boolean freed = false; |
| 44 |
|
| 45 |
void incref() { |
| 46 |
refcount++; |
| 47 |
} |
| 48 |
|
| 49 |
void decref() { |
| 50 |
refcount--; |
| 51 |
if (refcount == 0) { |
| 52 |
postFinal(); |
| 53 |
} else if (refcount < 0) { |
| 54 |
throw new IllegalStateException(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
abstract void postFinal(); |
| 59 |
|
| 60 |
abstract void localOpen(); |
| 61 |
|
| 62 |
abstract void localClose(); |
| 63 |
|
| 64 |
@Override |
| 65 |
public void open() |
| 66 |
{ |
| 67 |
if (getLocalState() != EndpointState.ACTIVE) |
| 68 |
{ |
| 69 |
_localState = EndpointState.ACTIVE; |
| 70 |
localOpen(); |
| 71 |
modified(); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void close() |
| 77 |
{ |
| 78 |
if (getLocalState() != EndpointState.CLOSED) |
| 79 |
{ |
| 80 |
_localState = EndpointState.CLOSED; |
| 81 |
localClose(); |
| 82 |
modified(); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public EndpointState getLocalState() |
| 88 |
{ |
| 89 |
return _localState; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public EndpointState getRemoteState() |
| 94 |
{ |
| 95 |
return _remoteState; |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public ErrorCondition getCondition() |
| 100 |
{ |
| 101 |
return _localError; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public void setCondition(ErrorCondition condition) |
| 106 |
{ |
| 107 |
if(condition != null) |
| 108 |
{ |
| 109 |
_localError.copyFrom(condition); |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
_localError.clear(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public ErrorCondition getRemoteCondition() |
| 119 |
{ |
| 120 |
return _remoteError; |
| 121 |
} |
| 122 |
|
| 123 |
void setLocalState(EndpointState localState) |
| 124 |
{ |
| 125 |
_localState = localState; |
| 126 |
} |
| 127 |
|
| 128 |
void setRemoteState(EndpointState remoteState) |
| 129 |
{ |
| 130 |
// TODO - check state change legal |
| 131 |
_remoteState = remoteState; |
| 132 |
} |
| 133 |
|
| 134 |
void modified() |
| 135 |
{ |
| 136 |
modified(true); |
| 137 |
} |
| 138 |
|
| 139 |
void modified(boolean emit) |
| 140 |
{ |
| 141 |
if(!_modified) |
| 142 |
{ |
| 143 |
_modified = true; |
| 144 |
getConnectionImpl().addModified(this); |
| 145 |
} |
| 146 |
|
| 147 |
if (emit) { |
| 148 |
ConnectionImpl conn = getConnectionImpl(); |
| 149 |
TransportImpl trans = conn.getTransport(); |
| 150 |
if (trans != null) { |
| 151 |
conn.put(Event.Type.TRANSPORT, trans); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
protected abstract ConnectionImpl getConnectionImpl(); |
| 157 |
|
| 158 |
void clearModified() |
| 159 |
{ |
| 160 |
if(_modified) |
| 161 |
{ |
| 162 |
_modified = false; |
| 163 |
getConnectionImpl().removeModified(this); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
boolean isModified() |
| 168 |
{ |
| 169 |
return _modified; |
| 170 |
} |
| 171 |
|
| 172 |
EndpointImpl transportNext() |
| 173 |
{ |
| 174 |
return _transportNext; |
| 175 |
} |
| 176 |
|
| 177 |
EndpointImpl transportPrev() |
| 178 |
{ |
| 179 |
return _transportPrev; |
| 180 |
} |
| 181 |
|
| 182 |
abstract void doFree(); |
| 183 |
|
| 184 |
@Override |
| 185 |
final public void free() |
| 186 |
{ |
| 187 |
if (freed) return; |
| 188 |
freed = true; |
| 189 |
|
| 190 |
doFree(); |
| 191 |
decref(); |
| 192 |
} |
| 193 |
|
| 194 |
void setTransportNext(EndpointImpl transportNext) |
| 195 |
{ |
| 196 |
_transportNext = transportNext; |
| 197 |
} |
| 198 |
|
| 199 |
void setTransportPrev(EndpointImpl transportPrevious) |
| 200 |
{ |
| 201 |
_transportPrev = transportPrevious; |
| 202 |
} |
| 203 |
|
| 204 |
@Override |
| 205 |
public Object getContext() |
| 206 |
{ |
| 207 |
return _context; |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public void setContext(Object context) |
| 212 |
{ |
| 213 |
_context = context; |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public Record attachments() |
| 218 |
{ |
| 219 |
return _attachments; |
| 220 |
} |
| 221 |
|
| 222 |
} |