001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.lwcontainer;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.List;
022    import java.util.Map;
023    
024    import javax.jbi.management.DeploymentException;
025    
026    import org.apache.servicemix.common.BaseComponent;
027    import org.apache.servicemix.common.Endpoint;
028    import org.apache.servicemix.common.ServiceUnit;
029    import org.apache.servicemix.common.xbean.AbstractXBeanDeployer;
030    import org.apache.servicemix.jbi.container.ActivationSpec;
031    import org.apache.servicemix.jbi.container.SpringServiceUnitContainer;
032    import org.springframework.context.support.AbstractXmlApplicationContext;
033    
034    public class LwContainerXBeanDeployer extends AbstractXBeanDeployer {
035    
036        public LwContainerXBeanDeployer(BaseComponent component) {
037            super(component);
038        }
039    
040        protected String getXBeanFile() {
041            return "servicemix";
042        }
043    
044        @Override
045        protected Collection<Endpoint> getServices(AbstractXmlApplicationContext applicationContext) throws Exception {
046            try {
047                List<Endpoint> services = new ArrayList<Endpoint>();
048                Map<String, SpringServiceUnitContainer> containers = applicationContext.getBeansOfType(SpringServiceUnitContainer.class);
049                for (SpringServiceUnitContainer suContainer : containers.values()) { 
050                    ActivationSpec[] specs = suContainer.getActivationSpecs();
051                    if (specs != null) {
052                        for (int i = 0; i < specs.length; i++) {
053                            services.add(new LwContainerEndpoint(specs[i]));
054                        }
055                    }
056                    if (suContainer.getComponents() != null || suContainer.getEndpoints() != null
057                            || suContainer.getListeners() != null || suContainer.getServices() != null) {
058                        services.add(new LwContainerExtra(suContainer.getComponents(), suContainer.getEndpoints(),
059                                                      suContainer.getListeners(), suContainer.getServices()));
060                    }
061                }
062                return services;
063            } catch (Exception e) {
064                throw new RuntimeException("Can not find 'jbi' bean", e);
065            }
066        }
067    
068        protected void validate(ServiceUnit su) throws DeploymentException {
069        }
070    
071    }