PageRenderTime 39ms CodeModel.GetById 26ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): LGPL-2.1, Apache-2.0
  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 */
 22package org.jboss.as.ejb3.timerservice;
 23
 24import javax.ejb.EJBException;
 25import javax.ejb.Timer;
 26import javax.ejb.TimerHandle;
 27
 28import org.jboss.as.ejb3.timerservice.spi.TimedObjectInvoker;
 29import org.jboss.as.server.CurrentServiceContainer;
 30import org.jboss.msc.service.ServiceName;
 31
 32import static org.jboss.as.ejb3.EjbMessages.MESSAGES;
 33
 34/**
 35 * @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
 36 * @version $Revision: $
 37 */
 38public class TimerHandleImpl implements TimerHandle {
 39    private static final long serialVersionUID = 1L;
 40
 41    /**
 42     * Id of the target {@link TimedObjectInvoker}
 43     */
 44    private final String timedObjectId;
 45
 46    /**
 47     * The service name of the timer service
 48     */
 49    private final String serviceName;
 50
 51    /**
 52     * Each {@link TimedObjectInvoker} can have multiple timer instances.
 53     * This id corresponds to one such <i>instance</i>
 54     */
 55    private final String id;
 56
 57    /**
 58     * The {@link TimerServiceImpl} to which this timer handle belongs to
 59     */
 60    private transient TimerServiceImpl service;
 61
 62    /**
 63     * Creates a {@link TimerHandleImpl}
 64     *
 65     * @param id            The id of the timer instance
 66     * @param timedObjectId The id of the target {@link TimedObjectInvoker}
 67     * @param service       The timer service to which this timer handle belongs to
 68     * @throws IllegalArgumentException If either of the passed parameters is null
 69     */
 70    public TimerHandleImpl(final String id, final String timedObjectId, final TimerServiceImpl service) throws IllegalArgumentException {
 71        if (id == null) {
 72            throw MESSAGES.idIsNull();
 73        }
 74        if (timedObjectId == null) {
 75            throw MESSAGES.timedObjectNull();
 76        }
 77        if (service == null) {
 78            throw MESSAGES.timerServiceIsNull();
 79        }
 80
 81        this.timedObjectId = timedObjectId;
 82        this.id = id;
 83        this.service = service;
 84        this.serviceName = service.getServiceName().getCanonicalName();
 85    }
 86
 87    /**
 88     * Returns the {@link javax.ejb.Timer} corresponding to this timer handle
 89     * <p/>
 90     * {@inheritDoc}
 91     */
 92    public Timer getTimer() throws IllegalStateException, EJBException {
 93        if (service == null) {
 94            // get hold of the timer service through the use of timed object id
 95            service = (TimerServiceImpl) CurrentServiceContainer.getServiceContainer().getRequiredService(ServiceName.parse(serviceName)).getValue();
 96            if (service == null) {
 97                throw MESSAGES.timerServiceWithIdNotRegistered(timedObjectId);
 98            }
 99        }
100        final TimerImpl timer = this.service.getTimer(this);
101        if (timer != null && timer.isActive() == false) {
102            throw MESSAGES.timerHandleIsNotActive(this);
103        }
104        return timer;
105    }
106
107    public String getId() {
108        return this.id;
109    }
110
111    public String getTimedObjectId() {
112        return this.timedObjectId;
113    }
114
115    @Override
116    public boolean equals(Object obj) {
117        if (obj == null) {
118            return false;
119        }
120        if (obj instanceof TimerHandleImpl == false) {
121            return false;
122        }
123        TimerHandleImpl other = (TimerHandleImpl) obj;
124        if (this == other) {
125            return true;
126        }
127        if (this.id.equals(other.id) && this.timedObjectId.equals(other.timedObjectId)) {
128            return true;
129        }
130        return false;
131    }
132
133    @Override
134    public int hashCode() {
135        return this.id.hashCode();
136    }
137
138}