KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > ejbthree655 > AbstractStateChecker


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * 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.ejb3.test.ejbthree655;
23
24 import org.jboss.logging.Logger;
25
26 /**
27  * Comment
28  *
29  * FIXME: This is thing might be useless because all the lifecycle methods are optional.
30  *
31  * @author <a HREF="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
32  * @version $Revision: $
33  */

34 public abstract class AbstractStateChecker
35 {
36    private static final Logger log = Logger.getLogger(AbstractStateChecker.class);
37    
38    public static enum State { INITIATED, CREATED, STARTED, STOPPED, DESTROYED };
39    
40    private State currentState = State.INITIATED;
41    
42    public void create()
43    {
44       log.info("create called on " + this);
45       
46       setState(State.INITIATED, State.CREATED);
47    }
48    
49    public void destroy()
50    {
51       log.info("destroy called on " + this);
52       
53       setState(State.STOPPED, State.DESTROYED);
54    }
55    
56    public State getState()
57    {
58       return currentState;
59    }
60    
61    private void setState(State expectedState, State newState)
62    {
63       //log.info("setState expected = " + expectedState + ", current = " + getState() + ", new = " + newState);
64
if(!this.currentState.equals(expectedState))
65       {
66          // the exception is gobled up somewhere
67
log.warn("state should be " + expectedState + ", not " + currentState);
68          throw new IllegalStateException JavaDoc("state should be " + expectedState + ", not " + currentState);
69       }
70       
71       this.currentState = newState;
72    }
73    
74    public void start()
75    {
76       log.info("start called on " + this);
77       
78       if(currentState.equals(State.STOPPED))
79          setState(State.STOPPED, State.STARTED);
80       else
81          setState(State.CREATED, State.STARTED);
82    }
83    
84    public void stop()
85    {
86       //new Throwable().printStackTrace();
87
log.info("stop called on " + this);
88       
89       setState(State.STARTED, State.STOPPED);
90    }
91 }
92
Popular Tags