001    package org.apache.ojb.broker;
002    
003    /* Copyright 2003-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.commons.lang.builder.ToStringBuilder;
019    
020    /**
021     * The <code>PBStateEvent</code> encapsulates information about
022     * the life-cycle/transaction demarcation of the used {@link org.apache.ojb.broker.PersistenceBroker}
023     * instance.
024     *
025     * @author Armin Waibel
026     * @version $Id: PBStateEvent.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $
027     */
028    public final class PBStateEvent extends PersistenceBrokerEvent
029    {
030        /** Denotes an event that happens before the broker will be closed. */
031        public static final int KEY_BEFORE_CLOSE = 1;
032        /** Denotes an event that happens before a transaction will be started. */
033        public static final int KEY_BEFORE_BEGIN = 2;
034        /** Denotes an event that happens before a transaction will be comitted. */
035        public static final int KEY_BEFORE_COMMIT = 3;
036        /** Denotes an event that happens before a transaction will be rolled back. */
037        public static final int KEY_BEFORE_ROLLBACK = 4;
038        /** Denotes an event that happens after a transaction was started. */
039        public static final int KEY_AFTER_BEGIN = 5;
040        /** Denotes an event that happens after a transaction was comitted. */
041        public static final int KEY_AFTER_COMMIT = 6;
042        /** Denotes an event that happens after a broker was opened. */
043        public static final int KEY_AFTER_OPEN = 7;
044        /** Denotes an event that happens after a transaction was rolled back. */
045        public static final int KEY_AFTER_ROLLBACK = 8;
046    
047        private Type eventType;
048    
049        /**
050         * Creates a new event instance.
051         * 
052         * @param broker    The broker
053         * @param eventType The type of the event
054         */
055        public PBStateEvent(PersistenceBroker broker, Type eventType)
056        {
057            super(broker);
058            this.eventType = eventType;
059        }
060    
061        /**
062         * {@inheritDoc}
063         */
064        public String toString()
065        {
066            ToStringBuilder buf = new ToStringBuilder(this);
067            buf.append("type", eventType.toString()).
068                    append("source object", getSource());
069            return buf.toString();
070        }
071    
072        /**
073         * Returns the event type.
074         * 
075         * @return The event type
076         */
077        public Type getEventType()
078        {
079            return eventType;
080        }
081    
082        /**
083         * Enum-like class for the event types.
084         */
085        public static class Type
086        {
087            /** Denotes an event that happens before a transaction will be started. */
088            public static final Type BEFORE_BEGIN = new Type(KEY_BEFORE_BEGIN);
089            /** Denotes an event that happens after a transaction was started. */
090            public static final Type AFTER_BEGIN = new Type(KEY_AFTER_BEGIN);
091            /** Denotes an event that happens before a transaction will be comitted. */
092            public static final Type BEFORE_COMMIT = new Type(KEY_BEFORE_COMMIT);
093            /** Denotes an event that happens after a transaction was comitted. */
094            public static final Type AFTER_COMMIT = new Type(KEY_AFTER_COMMIT);
095            /** Denotes an event that happens before a transaction will be rolled back. */
096            public static final Type BEFORE_ROLLBACK = new Type(KEY_BEFORE_ROLLBACK);
097            /** Denotes an event that happens after a transaction was rolled back. */
098            public static final Type AFTER_ROLLBACK = new Type(KEY_AFTER_ROLLBACK);
099            /** 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 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 toString()
155            {
156                return this.getClass().getName() + " [type= " + typeAsName(type) + "]";
157            }
158    
159            private String 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    }