001    /**
002     * Copyright (c) 2000-2011 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.osgi;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import org.osgi.framework.Bundle;
021    import org.osgi.framework.FrameworkEvent;
022    
023    /**
024     * @author Raymond Augé
025     */
026    public class FrameworkListener implements org.osgi.framework.FrameworkListener {
027    
028            private static Log _log = LogFactoryUtil.getLog(FrameworkListener.class);
029    
030            public void frameworkEvent(FrameworkEvent frameworkEvent) {
031                    try {
032                            int type = frameworkEvent.getType();
033    
034                            if (type == FrameworkEvent.ERROR) {
035                                    frameworkEventError(frameworkEvent);
036                            }
037                            else if (type == FrameworkEvent.INFO) {
038                                    frameworkEventInfo(frameworkEvent);
039                            }
040                            else if (type == FrameworkEvent.PACKAGES_REFRESHED) {
041                                    frameworkEventPackagesRefreshed(frameworkEvent);
042                            }
043                            else if (type == FrameworkEvent.STARTED) {
044                                    frameworkEventStarted(frameworkEvent);
045                            }
046                            else if (type == FrameworkEvent.STARTLEVEL_CHANGED) {
047                                    frameworkEventStartLevelChanged(frameworkEvent);
048                            }
049                            else if (type == FrameworkEvent.STOPPED) {
050                                    frameworkEventStopped(frameworkEvent);
051                            }
052                            else if (type == FrameworkEvent.STOPPED_BOOTCLASSPATH_MODIFIED) {
053                                    frameworkEventStoppedBootClasspathModified(frameworkEvent);
054                            }
055                            else if (type == FrameworkEvent.STOPPED_UPDATE) {
056                                    frameworkEventStoppedUpdate(frameworkEvent);
057                            }
058                            else if (type == FrameworkEvent.WAIT_TIMEDOUT) {
059                                    frameworkEventWaitTimedout(frameworkEvent);
060                            }
061                            else if (type == FrameworkEvent.WARNING) {
062                                    frameworkEventWarning(frameworkEvent);
063                            }
064                    }
065                    catch (Exception e) {
066                            _log.error(e, e);
067                    }
068            }
069    
070            protected void frameworkEventError(FrameworkEvent frameworkEvent)
071                    throws Exception {
072    
073                    Bundle bundle = frameworkEvent.getBundle();
074    
075                    Log log = LogFactoryUtil.getLog(bundle.getSymbolicName());
076    
077                    if (!log.isErrorEnabled()) {
078                            return;
079                    }
080    
081                    log.error(
082                            "[ERROR] " + frameworkEvent.getSource(),
083                            frameworkEvent.getThrowable());
084            }
085    
086            protected void frameworkEventInfo(FrameworkEvent frameworkEvent)
087                    throws Exception {
088    
089                    Bundle bundle = frameworkEvent.getBundle();
090    
091                    Log log = LogFactoryUtil.getLog(bundle.getSymbolicName());
092    
093                    if (!log.isInfoEnabled()) {
094                            return;
095                    }
096    
097                    log.info("[INFO] " + frameworkEvent.getSource());
098            }
099    
100            protected void frameworkEventPackagesRefreshed(
101                            FrameworkEvent frameworkEvent)
102                    throws Exception {
103    
104                    Bundle bundle = frameworkEvent.getBundle();
105    
106                    Log log = LogFactoryUtil.getLog(bundle.getSymbolicName());
107    
108                    if (!log.isInfoEnabled()) {
109                            return;
110                    }
111    
112                    log.info(
113                            "[PACKAGES_REFRESHED] " + frameworkEvent.getSource(),
114                            frameworkEvent.getThrowable());
115            }
116    
117            protected void frameworkEventStarted(FrameworkEvent frameworkEvent)
118                    throws Exception {
119    
120                    Bundle bundle = frameworkEvent.getBundle();
121    
122                    Log log = LogFactoryUtil.getLog(bundle.getSymbolicName());
123    
124                    if (!log.isInfoEnabled()) {
125                            return;
126                    }
127    
128                    log.info(
129                            "[STARTED] " + frameworkEvent.getSource(),
130                            frameworkEvent.getThrowable());
131            }
132    
133            protected void frameworkEventStartLevelChanged(
134                            FrameworkEvent frameworkEvent)
135                    throws Exception {
136    
137                    Bundle bundle = frameworkEvent.getBundle();
138    
139                    Log log = LogFactoryUtil.getLog(bundle.getSymbolicName());
140    
141                    if (!log.isInfoEnabled()) {
142                            return;
143                    }
144    
145                    log.info(
146                            "[STARTLEVEL_CHANGED] " + frameworkEvent.getSource(),
147                            frameworkEvent.getThrowable());
148            }
149    
150            protected void frameworkEventStopped(FrameworkEvent frameworkEvent)
151                    throws Exception {
152    
153                    Bundle bundle = frameworkEvent.getBundle();
154    
155                    Log log = LogFactoryUtil.getLog(bundle.getSymbolicName());
156    
157                    if (!log.isInfoEnabled()) {
158                            return;
159                    }
160    
161                    log.info(
162                            "[STOPPED] " + frameworkEvent.getSource(),
163                            frameworkEvent.getThrowable());
164            }
165    
166            protected void frameworkEventStoppedBootClasspathModified(
167                            FrameworkEvent frameworkEvent)
168                    throws Exception {
169    
170                    Bundle bundle = frameworkEvent.getBundle();
171    
172                    Log log = LogFactoryUtil.getLog(bundle.getSymbolicName());
173    
174                    if (!log.isInfoEnabled()) {
175                            return;
176                    }
177    
178                    log.info(
179                            "[STOPPED_BOOTCLASSPATH_MODIFIED] " + frameworkEvent.getSource(),
180                            frameworkEvent.getThrowable());
181            }
182    
183            protected void frameworkEventStoppedUpdate(FrameworkEvent frameworkEvent)
184                    throws Exception {
185    
186                    Bundle bundle = frameworkEvent.getBundle();
187    
188                    Log log = LogFactoryUtil.getLog(bundle.getSymbolicName());
189    
190                    if (!log.isInfoEnabled()) {
191                            return;
192                    }
193    
194                    log.info(
195                            "[STOPPED_UPDATE] " + frameworkEvent.getSource(),
196                            frameworkEvent.getThrowable());
197            }
198    
199            protected void frameworkEventWaitTimedout(FrameworkEvent frameworkEvent)
200                    throws Exception {
201    
202                    Bundle bundle = frameworkEvent.getBundle();
203    
204                    Log log = LogFactoryUtil.getLog(bundle.getSymbolicName());
205    
206                    if (!log.isInfoEnabled()) {
207                            return;
208                    }
209    
210                    log.info(
211                            "[WAIT_TIMEDOUT] " + frameworkEvent.getSource(),
212                            frameworkEvent.getThrowable());
213            }
214    
215            protected void frameworkEventWarning(FrameworkEvent frameworkEvent)
216                    throws Exception {
217    
218                    Bundle bundle = frameworkEvent.getBundle();
219    
220                    Log log = LogFactoryUtil.getLog(bundle.getSymbolicName());
221    
222                    if (!log.isWarnEnabled()) {
223                            return;
224                    }
225    
226                    log.warn(
227                            "[WARNING] " + frameworkEvent.getSource(),
228                            frameworkEvent.getThrowable());
229            }
230    
231    }