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.ServiceEvent;
022    import org.osgi.framework.ServiceReference;
023    
024    /**
025     * @author Raymond Augé
026     */
027    public class ServiceListener implements org.osgi.framework.ServiceListener {
028    
029            public void serviceChanged(ServiceEvent serviceEvent) {
030                    try {
031                            ServiceReference<?> serviceReference =
032                                    serviceEvent.getServiceReference();
033    
034                            Bundle bundle = serviceReference.getBundle();
035    
036                            Log log = LogFactoryUtil.getLog(bundle.getSymbolicName());
037    
038                            if (!log.isInfoEnabled()) {
039                                    return;
040                            }
041    
042                            int type = serviceEvent.getType();
043    
044                            if (type == ServiceEvent.MODIFIED) {
045                                    log.info("[MODIFIED] " + serviceReference.toString());
046                            }
047                            else if (type == ServiceEvent.REGISTERED) {
048                                    log.info("[REGISTERED] " + serviceReference.toString());
049                            }
050                            else if (type == ServiceEvent.UNREGISTERING) {
051                                    log.info("[UNREGISTERING] " + serviceReference.toString());
052                            }
053                    }
054                    catch (Exception e) {
055                            _log.error(e, e);
056                    }
057            }
058    
059            private static Log _log = LogFactoryUtil.getLog(ServiceListener.class);
060    
061    }