001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.FrameworkEvent;
021    
022    /**
023     * @author Raymond Augé
024     */
025    public class FrameworkListener extends BaseListener
026            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                    _log.error(
072                            getLogMessage("[ERROR]", frameworkEvent.getSource()),
073                            frameworkEvent.getThrowable());
074            }
075    
076            protected void frameworkEventInfo(FrameworkEvent frameworkEvent)
077                    throws Exception {
078    
079                    if (!_log.isInfoEnabled()) {
080                            return;
081                    }
082    
083                    _log.info(getLogMessage("[INFO]", frameworkEvent.getSource()));
084            }
085    
086            protected void frameworkEventPackagesRefreshed(
087                            FrameworkEvent frameworkEvent)
088                    throws Exception {
089    
090                    if (!_log.isInfoEnabled()) {
091                            return;
092                    }
093    
094                    _log.info(
095                            getLogMessage("[PACKAGES_REFRESHED]", frameworkEvent.getSource()));
096            }
097    
098            protected void frameworkEventStarted(FrameworkEvent frameworkEvent)
099                    throws Exception {
100    
101                    if (!_log.isInfoEnabled()) {
102                            return;
103                    }
104    
105                    _log.info(getLogMessage("[STARTED]", frameworkEvent.getSource()));
106            }
107    
108            protected void frameworkEventStartLevelChanged(
109                            FrameworkEvent frameworkEvent)
110                    throws Exception {
111    
112                    if (!_log.isInfoEnabled()) {
113                            return;
114                    }
115    
116                    _log.info(
117                            getLogMessage("[STARTLEVEL_CHANGED]", frameworkEvent.getSource()));
118            }
119    
120            protected void frameworkEventStopped(FrameworkEvent frameworkEvent)
121                    throws Exception {
122    
123                    if (!_log.isInfoEnabled()) {
124                            return;
125                    }
126    
127                    _log.info(getLogMessage("[STOPPED]", frameworkEvent.getSource()));
128            }
129    
130            protected void frameworkEventStoppedBootClasspathModified(
131                            FrameworkEvent frameworkEvent)
132                    throws Exception {
133    
134                    if (!_log.isInfoEnabled()) {
135                            return;
136                    }
137    
138                    _log.info(
139                            getLogMessage(
140                                    "[STOPPED_BOOTCLASSPATH_MODIFIED]",
141                                    frameworkEvent.getSource()));
142            }
143    
144            protected void frameworkEventStoppedUpdate(FrameworkEvent frameworkEvent)
145                    throws Exception {
146    
147                    if (!_log.isInfoEnabled()) {
148                            return;
149                    }
150    
151                    _log.info(
152                            getLogMessage("[STOPPED_UPDATE]", frameworkEvent.getSource()));
153            }
154    
155            protected void frameworkEventWaitTimedout(FrameworkEvent frameworkEvent)
156                    throws Exception {
157    
158                    if (!_log.isInfoEnabled()) {
159                            return;
160                    }
161    
162                    _log.info(getLogMessage("[WAIT_TIMEDOUT]", frameworkEvent.getSource()));
163            }
164    
165            protected void frameworkEventWarning(FrameworkEvent frameworkEvent)
166                    throws Exception {
167    
168                    if (!_log.isInfoEnabled()) {
169                            return;
170                    }
171    
172                    _log.info(getLogMessage("[WARNING]", frameworkEvent.getSource()));
173            }
174    
175            private static Log _log = LogFactoryUtil.getLog(FrameworkListener.class);
176    
177    }