/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/timerservice/TimerHandleImpl.java

# · Java · 138 lines · 70 code · 16 blank · 52 comment · 22 complexity · ff7e52cf8e9a216489184adbae763af7 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.ejb3.timerservice;
  23. import javax.ejb.EJBException;
  24. import javax.ejb.Timer;
  25. import javax.ejb.TimerHandle;
  26. import org.jboss.as.ejb3.timerservice.spi.TimedObjectInvoker;
  27. import org.jboss.as.server.CurrentServiceContainer;
  28. import org.jboss.msc.service.ServiceName;
  29. import static org.jboss.as.ejb3.EjbMessages.MESSAGES;
  30. /**
  31. * @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
  32. * @version $Revision: $
  33. */
  34. public class TimerHandleImpl implements TimerHandle {
  35. private static final long serialVersionUID = 1L;
  36. /**
  37. * Id of the target {@link TimedObjectInvoker}
  38. */
  39. private final String timedObjectId;
  40. /**
  41. * The service name of the timer service
  42. */
  43. private final String serviceName;
  44. /**
  45. * Each {@link TimedObjectInvoker} can have multiple timer instances.
  46. * This id corresponds to one such <i>instance</i>
  47. */
  48. private final String id;
  49. /**
  50. * The {@link TimerServiceImpl} to which this timer handle belongs to
  51. */
  52. private transient TimerServiceImpl service;
  53. /**
  54. * Creates a {@link TimerHandleImpl}
  55. *
  56. * @param id The id of the timer instance
  57. * @param timedObjectId The id of the target {@link TimedObjectInvoker}
  58. * @param service The timer service to which this timer handle belongs to
  59. * @throws IllegalArgumentException If either of the passed parameters is null
  60. */
  61. public TimerHandleImpl(final String id, final String timedObjectId, final TimerServiceImpl service) throws IllegalArgumentException {
  62. if (id == null) {
  63. throw MESSAGES.idIsNull();
  64. }
  65. if (timedObjectId == null) {
  66. throw MESSAGES.timedObjectNull();
  67. }
  68. if (service == null) {
  69. throw MESSAGES.timerServiceIsNull();
  70. }
  71. this.timedObjectId = timedObjectId;
  72. this.id = id;
  73. this.service = service;
  74. this.serviceName = service.getServiceName().getCanonicalName();
  75. }
  76. /**
  77. * Returns the {@link javax.ejb.Timer} corresponding to this timer handle
  78. * <p/>
  79. * {@inheritDoc}
  80. */
  81. public Timer getTimer() throws IllegalStateException, EJBException {
  82. if (service == null) {
  83. // get hold of the timer service through the use of timed object id
  84. service = (TimerServiceImpl) CurrentServiceContainer.getServiceContainer().getRequiredService(ServiceName.parse(serviceName)).getValue();
  85. if (service == null) {
  86. throw MESSAGES.timerServiceWithIdNotRegistered(timedObjectId);
  87. }
  88. }
  89. final TimerImpl timer = this.service.getTimer(this);
  90. if (timer != null && timer.isActive() == false) {
  91. throw MESSAGES.timerHandleIsNotActive(this);
  92. }
  93. return timer;
  94. }
  95. public String getId() {
  96. return this.id;
  97. }
  98. public String getTimedObjectId() {
  99. return this.timedObjectId;
  100. }
  101. @Override
  102. public boolean equals(Object obj) {
  103. if (obj == null) {
  104. return false;
  105. }
  106. if (obj instanceof TimerHandleImpl == false) {
  107. return false;
  108. }
  109. TimerHandleImpl other = (TimerHandleImpl) obj;
  110. if (this == other) {
  111. return true;
  112. }
  113. if (this.id.equals(other.id) && this.timedObjectId.equals(other.timedObjectId)) {
  114. return true;
  115. }
  116. return false;
  117. }
  118. @Override
  119. public int hashCode() {
  120. return this.id.hashCode();
  121. }
  122. }