KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > PBStateEvent


1 package org.apache.ojb.broker;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.commons.lang.builder.ToStringBuilder;
19
20 /**
21  * The <code>PBStateEvent</code> encapsulates information about
22  * the life-cycle/transaction demarcation of the used {@link org.apache.ojb.broker.PersistenceBroker}
23  * instance.
24  *
25  * @author Armin Waibel
26  * @version $Id: PBStateEvent.java,v 1.5.2.1 2005/12/21 22:22:08 tomdz Exp $
27  */

28 public final class PBStateEvent extends PersistenceBrokerEvent
29 {
30     /** Denotes an event that happens before the broker will be closed. */
31     public static final int KEY_BEFORE_CLOSE = 1;
32     /** Denotes an event that happens before a transaction will be started. */
33     public static final int KEY_BEFORE_BEGIN = 2;
34     /** Denotes an event that happens before a transaction will be comitted. */
35     public static final int KEY_BEFORE_COMMIT = 3;
36     /** Denotes an event that happens before a transaction will be rolled back. */
37     public static final int KEY_BEFORE_ROLLBACK = 4;
38     /** Denotes an event that happens after a transaction was started. */
39     public static final int KEY_AFTER_BEGIN = 5;
40     /** Denotes an event that happens after a transaction was comitted. */
41     public static final int KEY_AFTER_COMMIT = 6;
42     /** Denotes an event that happens after a broker was opened. */
43     public static final int KEY_AFTER_OPEN = 7;
44     /** Denotes an event that happens after a transaction was rolled back. */
45     public static final int KEY_AFTER_ROLLBACK = 8;
46
47     private Type eventType;
48
49     /**
50      * Creates a new event instance.
51      *
52      * @param broker The broker
53      * @param eventType The type of the event
54      */

55     public PBStateEvent(PersistenceBroker broker, Type eventType)
56     {
57         super(broker);
58         this.eventType = eventType;
59     }
60
61     /**
62      * {@inheritDoc}
63      */

64     public String JavaDoc toString()
65     {
66         ToStringBuilder buf = new ToStringBuilder(this);
67         buf.append("type", eventType.toString()).
68                 append("source object", getSource());
69         return buf.toString();
70     }
71
72     /**
73      * Returns the event type.
74      *
75      * @return The event type
76      */

77     public Type getEventType()
78     {
79         return eventType;
80     }
81
82     /**
83      * Enum-like class for the event types.
84      */

85     public static class Type
86     {
87         /** Denotes an event that happens before a transaction will be started. */
88         public static final Type BEFORE_BEGIN = new Type(KEY_BEFORE_BEGIN);
89         /** Denotes an event that happens after a transaction was started. */
90         public static final Type AFTER_BEGIN = new Type(KEY_AFTER_BEGIN);
91         /** Denotes an event that happens before a transaction will be comitted. */
92         public static final Type BEFORE_COMMIT = new Type(KEY_BEFORE_COMMIT);
93         /** Denotes an event that happens after a transaction was comitted. */
94         public static final Type AFTER_COMMIT = new Type(KEY_AFTER_COMMIT);
95         /** Denotes an event that happens before a transaction will be rolled back. */
96         public static final Type BEFORE_ROLLBACK = new Type(KEY_BEFORE_ROLLBACK);
97         /** Denotes an event that happens after a transaction was rolled back. */
98         public static final Type AFTER_ROLLBACK = new Type(KEY_AFTER_ROLLBACK);
99         /** Denotes an event that happens after a broker was opened. */
100         public static final Type AFTER_OPEN = new Type(KEY_AFTER_OPEN);
101         /** Denotes an event that happens before the broker will be closed. */
102         public static final Type BEFORE_CLOSE = new Type(KEY_BEFORE_CLOSE);
103
104         private int type;
105
106         /**
107          * Creates a new instance.
108          *
109          * @param type The type value
110          */

111         protected Type(int type)
112         {
113             this.type = type;
114         }
115
116         /**
117          * {@inheritDoc}
118          */

119         public final boolean equals(Object JavaDoc obj)
120         {
121             if (obj == this)
122             {
123                 return true;
124             }
125             if (!(obj instanceof PBStateEvent))
126             {
127                 return false;
128             }
129
130             return type == ((Type) obj).type;
131         }
132
133         /**
134          * {@inheritDoc}
135          */

136         public final int hashCode()
137         {
138             return type;
139         }
140         
141         /**
142          * Returns the type id.
143          *
144          * @return The type id
145          */

146         public final int typeId()
147         {
148             return type;
149         }
150
151         /**
152          * {@inheritDoc}
153          */

154         public String JavaDoc toString()
155         {
156             return this.getClass().getName() + " [type= " + typeAsName(type) + "]";
157         }
158
159         private String JavaDoc typeAsName(int aType)
160         {
161             if (aType == KEY_AFTER_BEGIN)
162             {
163                 return "AFTER_BEGIN";
164             }
165             else if (aType == KEY_AFTER_COMMIT)
166             {
167                 return "AFTER_COMMIT";
168             }
169             else if (aType == KEY_AFTER_OPEN)
170             {
171                 return "AFTER_OPEN";
172             }
173             else if (aType == KEY_AFTER_ROLLBACK)
174             {
175                 return "AFTER_ROLLBACK";
176             }
177             else if (aType == KEY_BEFORE_BEGIN)
178             {
179                 return "BEFORE_BEGIN";
180             }
181             else if (aType == KEY_BEFORE_CLOSE)
182             {
183                 return "BEFORE_CLOSE";
184             }
185             else if (aType == KEY_BEFORE_COMMIT)
186             {
187                 return "BEFORE_COMMIT";
188             }
189             else if (aType == KEY_BEFORE_ROLLBACK)
190             {
191                 return "BEFORE_ROLLBACK";
192             }
193             else
194             {
195                 throw new OJBRuntimeException("Could not find type " + aType);
196             }
197         }
198     }
199 }
200
Popular Tags