nz.co.senanque.perspectivemanager.BundleListenerImpl.java Source code

Java tutorial

Introduction

Here is the source code for nz.co.senanque.perspectivemanager.BundleListenerImpl.java

Source

/*******************************************************************************
 * Copyright (c)2014 Prometheus Consulting
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package nz.co.senanque.perspectivemanager;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import nz.co.senanque.madura.bundle.BundleListener;
import nz.co.senanque.madura.bundle.BundleRoot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.MessageSource;
import org.springframework.context.support.AbstractMessageSource;

/**
 * 
 * Simple example implementation of a bundle listener
 * 
 * @author Roger Parkinson
 * @version $Revision:$
 */
public class BundleListenerImpl implements BundleListener, Serializable {
    private static final long serialVersionUID = 7147632840139484367L;
    private Logger m_logger = LoggerFactory.getLogger(this.getClass());

    private MessageSource m_messageSource;

    private Map<String, SubApplication> m_subApplications = Collections
            .synchronizedMap(new HashMap<String, SubApplication>());

    /* (non-Javadoc)
     * @see nz.co.senanque.madura.bundle.BundleListener#add(java.lang.String, nz.co.senanque.madura.bundle.BundleRoot)
     */
    public void add(String bundleName, BundleRoot root) {
        m_logger.debug("added bundle {}", root.getProperties().get("bundle.name"));
        String bundleNameNoVersion = (String) root.getProperties().get("Bundle-Name");
        try {
            SubApplication subApplication = (SubApplication) root.getApplicationContext().getBean("SubApplication");
            SubApplication existing = m_subApplications.get(bundleNameNoVersion);
            if (existing == null || (existing.getVersion().compareToIgnoreCase(subApplication.getVersion()) < 0)) {
                // If this is the latest version of this bundle then set the app message source as the parent message source
                // and add the sub application to the map.
                AbstractMessageSource messageSource = (AbstractMessageSource) subApplication.getMessageSource();
                messageSource.setParentMessageSource(getMessageSource());
                m_subApplications.put(bundleNameNoVersion, subApplication);
            }
        } catch (BeansException e) {
            // ignore bundles with missing bean, assume they do something else.
        }
    }

    public void remove(String bundleName, BundleRoot root) {
        m_logger.debug("removed bundle {}", root.getProperties().get("bundle.name"));
        m_subApplications.remove(bundleName);
    }

    public Collection<SubApplication> getSubApplicationValues() {
        return m_subApplications.values();
    }

    public Map<String, SubApplication> getSubApplications() {
        return m_subApplications;
    }

    public void setSubApplications(Map<String, SubApplication> subApplications) {
        m_subApplications = subApplications;
    }

    public MessageSource getMessageSource() {
        return m_messageSource;
    }

    public void setMessageSource(MessageSource messageSource) {
        m_messageSource = messageSource;
    }

}