001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.transaction;
016    
017    import com.liferay.registry.Registry;
018    import com.liferay.registry.RegistryUtil;
019    import com.liferay.registry.ServiceReference;
020    import com.liferay.registry.ServiceTracker;
021    import com.liferay.registry.ServiceTrackerCustomizer;
022    
023    import java.util.Set;
024    import java.util.concurrent.CopyOnWriteArraySet;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class TransactionLifecycleNotifier {
030    
031            public static final TransactionLifecycleListener
032                    TRANSACTION_LIFECYCLE_LISTENER = new NewTransactionLifecycleListener() {
033    
034                            @Override
035                            protected void doCreated(
036                                    TransactionAttribute transactionAttribute,
037                                    TransactionStatus transactionStatus) {
038    
039                                    fireTransactionCreatedEvent(
040                                            transactionAttribute, transactionStatus);
041                            }
042    
043                            @Override
044                            protected void doCommitted(
045                                    TransactionAttribute transactionAttribute,
046                                    TransactionStatus transactionStatus) {
047    
048                                    fireTransactionCommittedEvent(
049                                            transactionAttribute, transactionStatus);
050                            }
051    
052                            @Override
053                            protected void doRollbacked(
054                                    TransactionAttribute transactionAttribute,
055                                    TransactionStatus transactionStatus, Throwable throwable) {
056    
057                                    fireTransactionRollbackedEvent(
058                                            transactionAttribute, transactionStatus, throwable);
059                            }
060    
061                    };
062    
063            protected static void fireTransactionCommittedEvent(
064                    TransactionAttribute transactionAttribute,
065                    TransactionStatus transactionStatus) {
066    
067                    for (TransactionLifecycleListener transactionLifecycleListener :
068                                    _instance._transactionLifecycleListeners) {
069    
070                            transactionLifecycleListener.committed(
071                                    transactionAttribute, transactionStatus);
072                    }
073            }
074    
075            protected static void fireTransactionCreatedEvent(
076                    TransactionAttribute transactionAttribute,
077                    TransactionStatus transactionStatus) {
078    
079                    for (TransactionLifecycleListener transactionLifecycleListener :
080                                    _instance._transactionLifecycleListeners) {
081    
082                            transactionLifecycleListener.created(
083                                    transactionAttribute, transactionStatus);
084                    }
085            }
086    
087            protected static void fireTransactionRollbackedEvent(
088                    TransactionAttribute transactionAttribute,
089                    TransactionStatus transactionStatus, Throwable throwable) {
090    
091                    for (TransactionLifecycleListener transactionLifecycleListener :
092                                    _instance._transactionLifecycleListeners) {
093    
094                            transactionLifecycleListener.rollbacked(
095                                    transactionAttribute, transactionStatus, throwable);
096                    }
097            }
098    
099            private TransactionLifecycleNotifier() {
100                    Registry registry = RegistryUtil.getRegistry();
101    
102                    _serviceTracker = registry.trackServices(
103                            TransactionLifecycleListener.class,
104                            new TransactionLifecycleListenerServiceTrackerCustomizer());
105    
106                    _serviceTracker.open();
107            }
108    
109            private static final TransactionLifecycleNotifier _instance =
110                    new TransactionLifecycleNotifier();
111    
112            private final ServiceTracker
113                    <TransactionLifecycleListener, TransactionLifecycleListener>
114                            _serviceTracker;
115            private final Set<TransactionLifecycleListener>
116                    _transactionLifecycleListeners = new CopyOnWriteArraySet<>();
117    
118            private class TransactionLifecycleListenerServiceTrackerCustomizer
119                    implements ServiceTrackerCustomizer
120                            <TransactionLifecycleListener, TransactionLifecycleListener> {
121    
122                    @Override
123                    public TransactionLifecycleListener addingService(
124                            ServiceReference<TransactionLifecycleListener> serviceReference) {
125    
126                            Registry registry = RegistryUtil.getRegistry();
127    
128                            TransactionLifecycleListener transactionLifecycleListener =
129                                    registry.getService(serviceReference);
130    
131                            _transactionLifecycleListeners.add(transactionLifecycleListener);
132    
133                            return transactionLifecycleListener;
134                    }
135    
136                    @Override
137                    public void modifiedService(
138                            ServiceReference<TransactionLifecycleListener> serviceReference,
139                            TransactionLifecycleListener transactionLifecycleListener) {
140                    }
141    
142                    @Override
143                    public void removedService(
144                            ServiceReference<TransactionLifecycleListener> serviceReference,
145                            TransactionLifecycleListener transactionLifecycleListener) {
146    
147                            _transactionLifecycleListeners.remove(transactionLifecycleListener);
148                    }
149    
150            }
151    
152    }