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.jbi.framework.support;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    import org.apache.servicemix.jbi.deployment.Provides;
022    import org.apache.servicemix.jbi.deployment.Services;
023    import org.apache.servicemix.jbi.framework.Registry;
024    import org.apache.servicemix.jbi.framework.ServiceUnitLifeCycle;
025    import org.apache.servicemix.jbi.servicedesc.InternalEndpoint;
026    
027    /**
028     * Retrieve interface implemented by the given endpoint using the SU jbi descriptors.
029     * 
030     * @author gnodet
031     */
032    public class SUDescriptorProcessor implements EndpointProcessor {
033    
034        private static final Log LOG = LogFactory.getLog(SUDescriptorProcessor.class);
035        
036        private Registry registry;
037        
038        public void init(Registry reg) {
039            this.registry = reg;
040        }
041    
042        /**
043         * Retrieve interface implemented by the given endpoint using the SU jbi descriptors.
044         * 
045         * @param serviceEndpoint the endpoint being checked
046         */
047        public void process(InternalEndpoint serviceEndpoint) {
048            ServiceUnitLifeCycle[] sus = registry.getDeployedServiceUnits(serviceEndpoint.getComponentNameSpace().getName());
049            for (int i = 0; i < sus.length; i++) {
050                Services services = sus[i].getServices();
051                if (services != null) {
052                    Provides[] provides = services.getProvides();
053                    if (provides != null) {
054                        for (int j = 0; j < provides.length; j++) {
055                            if (provides[j].getInterfaceName() != null
056                                    && serviceEndpoint.getServiceName().equals(provides[j].getServiceName())
057                                    && serviceEndpoint.getEndpointName().equals(provides[j].getEndpointName())) {
058                                if (LOG.isDebugEnabled()) {
059                                    LOG.debug("Endpoint " + serviceEndpoint + " is provided by SU " + sus[i].getName());
060                                    LOG.debug("Endpoint " + serviceEndpoint + " implements interface " + provides[j].getInterfaceName());
061                                }
062                                serviceEndpoint.addInterface(provides[j].getInterfaceName());
063                            }
064                        }
065                    }
066                }
067            }
068        }
069    
070    }