Package org.apache.servicemix.web.http

Source Code of org.apache.servicemix.web.http.HttpComponentListener

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.servicemix.web.http;

import java.lang.reflect.Method;

import javax.jbi.component.Component;

import org.apache.servicemix.jbi.container.JBIContainer;
import org.apache.servicemix.jbi.event.ComponentEvent;
import org.apache.servicemix.jbi.event.ComponentListener;
import org.apache.servicemix.jbi.event.ContainerAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This ComponentListener is a hack to automatically configure
* the servicemix-http component in managed mode while avoiding
* to embed the component itself.
*/
public class HttpComponentListener implements ComponentListener, ContainerAware {

    private static final transient Logger LOGGER = LoggerFactory.getLogger(HttpComponentListener.class);
   
    private String name = "servicemix-http";
    private JBIContainer container;
   
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the container
     */
    public JBIContainer getContainer() {
        return container;
    }

    /**
     * @param container the container to set
     */
    public void setContainer(JBIContainer container) {
        this.container = container;
    }

    public void componentInstalled(ComponentEvent event) {
    }

    public void componentShutDown(ComponentEvent event) {
    }

    public void componentInitialized(ComponentEvent event) {
        if (getName().equals(event.getComponent().getName())) {
            try {
                Component component = event.getComponent().getComponent();
                Method m = component.getClass().getMethod("getConfiguration", (Class[]) null);
                Object cfg = m.invoke(component, (Object[]) null);
                m = cfg.getClass().getMethod("isManaged", (Class[]) null);
                Boolean b = (Boolean) m.invoke(cfg, (Object[]) null);
                if (!b.booleanValue()) {
                    m = cfg.getClass().getMethod("setManaged", new Class[] { boolean.class });
                    m.invoke(cfg, new Object[] { Boolean.TRUE });
                }
                LOGGER.info("Component {} configured", getName());
            } catch (Exception e) {
                LOGGER.error("Unable to update component configuration", e);
            }
        }
    }

    public void componentStarted(ComponentEvent event) {
    }

    public void componentStopped(ComponentEvent event) {
    }

    public void componentUninstalled(ComponentEvent event) {
    }

}
TOP

Related Classes of org.apache.servicemix.web.http.HttpComponentListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.