|
7 | 7 |
|
8 | 8 | package org.red5.server.adapter; |
9 | 9 |
|
| 10 | +import java.util.List; |
10 | 11 | import java.util.Map; |
| 12 | +import java.util.Set; |
11 | 13 |
|
| 14 | +import org.red5.server.api.IAttributeStore; |
| 15 | +import org.red5.server.api.ICastingAttributeStore; |
12 | 16 | import org.red5.server.api.IClient; |
13 | 17 | import org.red5.server.api.IConnection; |
14 | 18 | import org.red5.server.api.Red5; |
15 | 19 | import org.red5.server.api.event.IEvent; |
16 | 20 | import org.red5.server.api.scope.IBasicScope; |
17 | 21 | import org.red5.server.api.scope.IScope; |
| 22 | +import org.red5.server.api.scope.IScopeAware; |
18 | 23 | import org.red5.server.api.scope.IScopeHandler; |
19 | 24 | import org.red5.server.api.service.IServiceCall; |
20 | 25 |
|
|
23 | 28 | * |
24 | 29 | * @author mondain |
25 | 30 | */ |
26 | | -public abstract class AbstractScopeAdapter implements IScopeHandler { |
| 31 | +public abstract class AbstractScopeAdapter implements IScopeAware, IScopeHandler, ICastingAttributeStore { |
27 | 32 |
|
28 | | - //private static Logger log = LoggerFactory.getLogger(AbstractScopeAdapter.class); |
| 33 | + /** |
| 34 | + * Wrapped scope |
| 35 | + */ |
| 36 | + protected volatile IScope scope; |
29 | 37 |
|
30 | 38 | /** |
31 | 39 | * Can start flag. |
@@ -129,6 +137,27 @@ public abstract class AbstractScopeAdapter implements IScopeHandler { |
129 | 137 | */ |
130 | 138 | private boolean canHandleEvent = true; |
131 | 139 |
|
| 140 | + /** |
| 141 | + * Setter for wrapped scope |
| 142 | + * |
| 143 | + * @param scope |
| 144 | + * Scope to wrap |
| 145 | + */ |
| 146 | + @Override |
| 147 | + public void setScope(IScope scope) { |
| 148 | + this.scope = scope; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Getter for wrapped scope |
| 153 | + * |
| 154 | + * @return Wrapped scope |
| 155 | + */ |
| 156 | + @Override |
| 157 | + public IScope getScope() { |
| 158 | + return scope; |
| 159 | + } |
| 160 | + |
132 | 161 | /** |
133 | 162 | * Setter for can start flag. |
134 | 163 | * |
@@ -297,11 +326,186 @@ public Map<String, Object> checkBandwidthUp(Object[] params) { |
297 | 326 | return null; |
298 | 327 | } |
299 | 328 |
|
| 329 | + /** {@inheritDoc} */ |
| 330 | + @Override |
| 331 | + public Object getAttribute(String name) { |
| 332 | + return scope.getAttribute(name); |
| 333 | + } |
| 334 | + |
| 335 | + /** {@inheritDoc} */ |
| 336 | + @Override |
| 337 | + public Object getAttribute(Enum<?> enm) { |
| 338 | + return getAttribute(enm.name()); |
| 339 | + } |
| 340 | + |
| 341 | + /** {@inheritDoc} */ |
| 342 | + @Override |
| 343 | + public Object getAttribute(String name, Object defaultValue) { |
| 344 | + // this sets the attribute if it does not exist and returns the default value |
| 345 | + // if it already existed, the existing value is returned |
| 346 | + Object value = scope.setAttributeIfAbsent(name, defaultValue); |
| 347 | + if (value == null) { |
| 348 | + // if the value was not set, return the default value |
| 349 | + return defaultValue; |
| 350 | + } |
| 351 | + return value; |
| 352 | + } |
| 353 | + |
| 354 | + /** {@inheritDoc} */ |
| 355 | + @Override |
| 356 | + public Set<String> getAttributeNames() { |
| 357 | + return scope.getAttributeNames(); |
| 358 | + } |
| 359 | + |
| 360 | + /** {@inheritDoc} */ |
| 361 | + @Override |
| 362 | + public Map<String, Object> getAttributes() { |
| 363 | + return scope.getAttributes(); |
| 364 | + } |
| 365 | + |
| 366 | + /** {@inheritDoc} */ |
| 367 | + @Override |
| 368 | + public boolean hasAttribute(String name) { |
| 369 | + return scope.hasAttribute(name); |
| 370 | + } |
| 371 | + |
| 372 | + /** {@inheritDoc} */ |
| 373 | + @Override |
| 374 | + public boolean hasAttribute(Enum<?> enm) { |
| 375 | + return hasAttribute(enm.name()); |
| 376 | + } |
| 377 | + |
| 378 | + /** {@inheritDoc} */ |
| 379 | + @Override |
| 380 | + public boolean removeAttribute(String name) { |
| 381 | + return scope.removeAttribute(name); |
| 382 | + } |
| 383 | + |
| 384 | + /** {@inheritDoc} */ |
| 385 | + @Override |
| 386 | + public boolean removeAttribute(Enum<?> enm) { |
| 387 | + return removeAttribute(enm.name()); |
| 388 | + } |
| 389 | + |
| 390 | + /** {@inheritDoc} */ |
| 391 | + @Override |
| 392 | + public void removeAttributes() { |
| 393 | + scope.removeAttributes(); |
| 394 | + } |
| 395 | + |
| 396 | + /** {@inheritDoc} */ |
| 397 | + @Override |
| 398 | + public boolean setAttribute(String name, Object value) { |
| 399 | + return scope.setAttribute(name, value); |
| 400 | + } |
| 401 | + |
| 402 | + /** {@inheritDoc} */ |
| 403 | + @Override |
| 404 | + public boolean setAttribute(Enum<?> enm, Object value) { |
| 405 | + return setAttribute(enm.name(), value); |
| 406 | + } |
| 407 | + |
| 408 | + /** {@inheritDoc} */ |
| 409 | + @Override |
| 410 | + public boolean setAttributes(IAttributeStore attributes) { |
| 411 | + int successes = 0; |
| 412 | + for (Map.Entry<String, Object> entry : attributes.getAttributes().entrySet()) { |
| 413 | + if (scope.setAttribute(entry.getKey(), entry.getValue())) { |
| 414 | + successes++; |
| 415 | + } |
| 416 | + } |
| 417 | + // expect every value to have been added |
| 418 | + return (successes == attributes.size()); |
| 419 | + } |
| 420 | + |
300 | 421 | /** |
301 | | - * <p>getScope.</p> |
| 422 | + * {@inheritDoc} |
302 | 423 | * |
303 | | - * @return a {@link org.red5.server.api.scope.IScope} object |
| 424 | + * @param attributes a {@link java.util.Map} object |
| 425 | + * @return a boolean |
304 | 426 | */ |
305 | | - public abstract IScope getScope(); |
| 427 | + @Override |
| 428 | + public boolean setAttributes(Map<String, Object> attributes) { |
| 429 | + int successes = 0; |
| 430 | + for (Map.Entry<String, Object> entry : attributes.entrySet()) { |
| 431 | + if (scope.setAttribute(entry.getKey(), entry.getValue())) { |
| 432 | + successes++; |
| 433 | + } |
| 434 | + } |
| 435 | + // expect every value to have been added |
| 436 | + return (successes == attributes.size()); |
| 437 | + } |
| 438 | + |
| 439 | + /** {@inheritDoc} */ |
| 440 | + @Override |
| 441 | + public Object setAttributeIfAbsent(String name, Object value) { |
| 442 | + return scope.setAttributeIfAbsent(name, value); |
| 443 | + } |
| 444 | + |
| 445 | + /** {@inheritDoc} */ |
| 446 | + @Override |
| 447 | + public Boolean getBoolAttribute(String name) { |
| 448 | + return scope.getBoolAttribute(name); |
| 449 | + } |
| 450 | + |
| 451 | + /** {@inheritDoc} */ |
| 452 | + @Override |
| 453 | + public Byte getByteAttribute(String name) { |
| 454 | + return scope.getByteAttribute(name); |
| 455 | + } |
| 456 | + |
| 457 | + /** {@inheritDoc} */ |
| 458 | + @Override |
| 459 | + public Double getDoubleAttribute(String name) { |
| 460 | + return scope.getDoubleAttribute(name); |
| 461 | + } |
| 462 | + |
| 463 | + /** {@inheritDoc} */ |
| 464 | + @Override |
| 465 | + public Integer getIntAttribute(String name) { |
| 466 | + return scope.getIntAttribute(name); |
| 467 | + } |
| 468 | + |
| 469 | + /** {@inheritDoc} */ |
| 470 | + @Override |
| 471 | + public List<?> getListAttribute(String name) { |
| 472 | + return scope.getListAttribute(name); |
| 473 | + } |
| 474 | + |
| 475 | + /** {@inheritDoc} */ |
| 476 | + @Override |
| 477 | + public Long getLongAttribute(String name) { |
| 478 | + return scope.getLongAttribute(name); |
| 479 | + } |
| 480 | + |
| 481 | + /** {@inheritDoc} */ |
| 482 | + @Override |
| 483 | + public Map<?, ?> getMapAttribute(String name) { |
| 484 | + return scope.getMapAttribute(name); |
| 485 | + } |
| 486 | + |
| 487 | + /** {@inheritDoc} */ |
| 488 | + @Override |
| 489 | + public Set<?> getSetAttribute(String name) { |
| 490 | + return scope.getSetAttribute(name); |
| 491 | + } |
| 492 | + |
| 493 | + /** {@inheritDoc} */ |
| 494 | + @Override |
| 495 | + public Short getShortAttribute(String name) { |
| 496 | + return scope.getShortAttribute(name); |
| 497 | + } |
| 498 | + |
| 499 | + /** {@inheritDoc} */ |
| 500 | + @Override |
| 501 | + public String getStringAttribute(String name) { |
| 502 | + return scope.getStringAttribute(name); |
| 503 | + } |
| 504 | + |
| 505 | + /** {@inheritDoc} */ |
| 506 | + @Override |
| 507 | + public int size() { |
| 508 | + return scope != null ? scope.getAttributeNames().size() : 0; |
| 509 | + } |
306 | 510 |
|
307 | 511 | } |
0 commit comments