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