1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.deploy.hot;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.List;
29  import java.util.Vector;
30  
31  /**
32   * <a href="HotDeployUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Ivica Cardic
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class HotDeployUtil {
39  
40      public static void fireDeployEvent(HotDeployEvent event) {
41          _instance._fireDeployEvent(event);
42      }
43  
44      public static void fireUndeployEvent(HotDeployEvent event) {
45          _instance._fireUndeployEvent(event);
46      }
47  
48      public static void flushEvents() {
49          _instance._flushEvents();
50      }
51  
52      public static void registerListener(HotDeployListener listener) {
53          _instance._registerListener(listener);
54      }
55  
56      public static void unregisterListeners() {
57          _instance._unregisterListeners();
58      }
59  
60      private HotDeployUtil() {
61          if (_log.isInfoEnabled()) {
62              _log.info("Initializing hot deploy manager " + this.hashCode());
63          }
64  
65          _listeners = new Vector<HotDeployListener>();
66          _events = new Vector<HotDeployEvent>();
67      }
68  
69      private synchronized void _fireDeployEvent(HotDeployEvent event) {
70  
71          // Capture events that are fired before the portal initialized. These
72          // events are later fired by flushEvents.
73  
74          if (_events != null) {
75              _events.add(event);
76  
77              return;
78          }
79  
80          // Fire current event
81  
82          for (HotDeployListener listener : _listeners) {
83              try {
84                  listener.invokeDeploy(event);
85              }
86              catch (HotDeployException hde) {
87                  _log.error(hde, hde);
88              }
89          }
90      }
91  
92      private void _fireUndeployEvent(HotDeployEvent event) {
93          for (HotDeployListener listener : _listeners) {
94              try {
95                  listener.invokeUndeploy(event);
96              }
97              catch (HotDeployException hde) {
98                  _log.error(hde, hde);
99              }
100         }
101     }
102 
103     private synchronized void _flushEvents() {
104         for (HotDeployEvent event : _events) {
105             for (HotDeployListener listener : _listeners) {
106                 try {
107                     listener.invokeDeploy(event);
108                 }
109                 catch (HotDeployException hde) {
110                     _log.error(hde, hde);
111                 }
112             }
113         }
114 
115         _events = null;
116     }
117 
118     private void _registerListener(HotDeployListener listener) {
119         _listeners.add(listener);
120     }
121 
122     private void _unregisterListeners() {
123         _listeners.clear();
124     }
125 
126     private static Log _log = LogFactoryUtil.getLog(HotDeployUtil.class);
127 
128     private static HotDeployUtil _instance = new HotDeployUtil();
129 
130     private List<HotDeployListener> _listeners;
131     private List<HotDeployEvent> _events;
132 
133 }