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.Bundle;
021    import org.osgi.framework.BundleEvent;
022    
023    /**
024     * @author Raymond Augé
025     */
026    public class BundleListener extends BaseListener
027            implements org.osgi.framework.BundleListener {
028    
029            public void bundleChanged(BundleEvent bundleEvent) {
030                    try {
031                            int type = bundleEvent.getType();
032    
033                            if (type == BundleEvent.INSTALLED) {
034                                    bundleEventInstalled(bundleEvent);
035                            }
036                            else if (type == BundleEvent.LAZY_ACTIVATION) {
037                                    bundleEventLazyActivation(bundleEvent);
038                            }
039                            else if (type == BundleEvent.RESOLVED) {
040                                    bundleEventResolved(bundleEvent);
041                            }
042                            else if (type == BundleEvent.STARTED) {
043                                    bundleEventStarted(bundleEvent);
044                            }
045                            else if (type == BundleEvent.STARTING) {
046                                    bundleEventStarting(bundleEvent);
047                            }
048                            else if (type == BundleEvent.STOPPED) {
049                                    bundleEventStopped(bundleEvent);
050                            }
051                            else if (type == BundleEvent.STOPPING) {
052                                    bundleEventStopped(bundleEvent);
053                            }
054                            else if (type == BundleEvent.UNINSTALLED) {
055                                    bundleEventUninstalled(bundleEvent);
056                            }
057                            else if (type == BundleEvent.UNRESOLVED) {
058                                    bundleEventUnresolved(bundleEvent);
059                            }
060                            else if (type == BundleEvent.UPDATED) {
061                                    bundleEventUpdated(bundleEvent);
062                            }
063                    }
064                    catch (Exception e) {
065                            _log.error(e, e);
066                    }
067            }
068    
069            protected void bundleEventInstalled(BundleEvent bundleEvent)
070                    throws Exception {
071    
072                    if (!_log.isInfoEnabled()) {
073                            return;
074                    }
075    
076                    _log.info(getLogMessage("[INSTALLED]", bundleEvent));
077            }
078    
079            protected void bundleEventLazyActivation(BundleEvent bundleEvent)
080                    throws Exception {
081    
082                    if (!_log.isInfoEnabled()) {
083                            return;
084                    }
085    
086                    _log.info(getLogMessage("[LAZY_ACTIVATION]", bundleEvent));
087            }
088    
089            protected void bundleEventResolved(BundleEvent bundleEvent)
090                    throws Exception {
091    
092                    if (!_log.isInfoEnabled()) {
093                            return;
094                    }
095    
096                    _log.info(getLogMessage("[RESOLVED]", bundleEvent));
097            }
098    
099            protected void bundleEventStarted(BundleEvent bundleEvent)
100                    throws Exception {
101    
102                    if (!_log.isInfoEnabled()) {
103                            return;
104                    }
105    
106                    _log.info(getLogMessage("[STARTED]", bundleEvent));
107            }
108    
109            protected void bundleEventStarting(BundleEvent bundleEvent)
110                    throws Exception {
111    
112                    if (!_log.isInfoEnabled()) {
113                            return;
114                    }
115    
116                    _log.info(getLogMessage("[STARTING]", bundleEvent));
117            }
118    
119            protected void bundleEventStopped(BundleEvent bundleEvent)
120                    throws Exception {
121    
122                    if (!_log.isInfoEnabled()) {
123                            return;
124                    }
125    
126                    _log.info(getLogMessage("[STOPPED]", bundleEvent));
127            }
128    
129            protected void bundleEventUninstalled(BundleEvent bundleEvent)
130                    throws Exception {
131    
132                    if (!_log.isInfoEnabled()) {
133                            return;
134                    }
135    
136                    _log.info(getLogMessage("[UNINSTALLED]", bundleEvent));
137            }
138    
139            protected void bundleEventUnresolved(BundleEvent bundleEvent)
140                    throws Exception {
141    
142                    if (!_log.isInfoEnabled()) {
143                            return;
144                    }
145    
146                    _log.info(getLogMessage("[UNRESOLVED]", bundleEvent));
147            }
148    
149            protected void bundleEventUpdated(BundleEvent bundleEvent)
150                    throws Exception {
151    
152                    if (!_log.isInfoEnabled()) {
153                            return;
154                    }
155    
156                    _log.info(getLogMessage("[UPDATED]", bundleEvent));
157            }
158    
159            protected String getLogMessage(String state, BundleEvent bundleEvent) {
160                    Bundle bundle = bundleEvent.getBundle();
161    
162                    return getLogMessage(state, bundle.getSymbolicName());
163            }
164    
165            private static Log _log = LogFactoryUtil.getLog(BundleListener.class);
166    
167    }