001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.felix.framework.searchpolicy;
020    
021    import java.net.URL;
022    import java.util.*;
023    
024    import org.apache.felix.framework.util.Util;
025    import org.apache.felix.moduleloader.*;
026    
027    public class R4WireModule implements IWire
028    {
029        private final IModule m_importer;
030        private final IRequirement m_requirement;
031        private final IModule m_exporter;
032        private final ICapability m_capability;
033        private final Map m_pkgMap;
034    
035        public R4WireModule(IModule importer, IRequirement requirement,
036            IModule exporter, ICapability capability, Map pkgMap)
037        {
038            m_importer = importer;
039            m_requirement = requirement;
040            m_exporter = exporter;
041            m_capability = capability;
042            m_pkgMap = pkgMap;
043        }
044    
045        /* (non-Javadoc)
046         * @see org.apache.felix.framework.searchpolicy.IWire#getImporter()
047         */
048        public IModule getImporter()
049        {
050            return m_importer;
051        }
052    
053        /* (non-Javadoc)
054         * @see org.apache.felix.framework.searchpolicy.IWire#getRequirement()
055         */
056        public IRequirement getRequirement()
057        {
058            return m_requirement;
059        }
060    
061        /* (non-Javadoc)
062         * @see org.apache.felix.framework.searchpolicy.IWire#getExporter()
063         */
064        public IModule getExporter()
065        {
066            return m_exporter;
067        }
068    
069        /* (non-Javadoc)
070         * @see org.apache.felix.framework.searchpolicy.IWire#getCapability()
071         */
072        public ICapability getCapability()
073        {
074            return m_capability;
075        }
076    
077        /* (non-Javadoc)
078         * @see org.apache.felix.framework.searchpolicy.IWire#hasPackage(java.lang.String)
079         */
080        public boolean hasPackage(String pkgName)
081        {
082            return (m_pkgMap.get(pkgName) != null);
083        }
084    
085        /* (non-Javadoc)
086         * @see org.apache.felix.framework.searchpolicy.IWire#getClass(java.lang.String)
087         */
088        public Class getClass(String name) throws ClassNotFoundException
089        {
090            // Get the package of the target class.
091            String pkgName = Util.getClassPackage(name);
092    
093            ResolvedPackage rp = (ResolvedPackage) m_pkgMap.get(pkgName);
094            if (rp != null)
095            {
096                try
097                {
098                    Class clazz = m_exporter.getClassByDelegation(name);
099                    if (clazz != null)
100                    {
101                        return clazz;
102                    }
103                }
104                catch (ClassNotFoundException ex)
105                {
106                    // Do not throw the exception here, since we want
107                    // to continue search other package sources and
108                    // ultimately the module's own content.
109                }
110            }
111    
112            return null;
113        }
114    
115        /* (non-Javadoc)
116         * @see org.apache.felix.framework.searchpolicy.IWire#getResource(java.lang.String)
117         */
118        public URL getResource(String name) throws ResourceNotFoundException
119        {
120            // Get the package of the target class.
121            String pkgName = Util.getResourcePackage(name);
122    
123            ResolvedPackage rp = (ResolvedPackage) m_pkgMap.get(pkgName);
124            if (rp != null)
125            {
126                URL url = m_exporter.getResourceByDelegation(name);
127                if (url != null)
128                {
129                    return url;
130                }
131    
132                // Don't throw ResourceNotFoundException because module
133                // dependencies support split packages.
134            }
135    
136            return null;
137        }
138    
139        /* (non-Javadoc)
140         * @see org.apache.felix.framework.searchpolicy.IWire#getResources(java.lang.String)
141         */
142        public Enumeration getResources(String name) throws ResourceNotFoundException
143        {
144            // Get the package of the target class.
145            String pkgName = Util.getResourcePackage(name);
146    
147            // See if we have a resolved package for the resource's package.
148            // If so, loop through all package sources and aggregate any
149            // matching resource enumerations.
150            ResolvedPackage rp = (ResolvedPackage) m_pkgMap.get(pkgName);
151            if (rp != null)
152            {
153                Enumeration urls = m_exporter.getResourcesByDelegation(name);
154                if (urls != null)
155                {
156                    return urls;
157                }
158    
159                // Don't throw ResourceNotFoundException because module
160                // dependencies support split packages.
161            }
162    
163            return null;
164        }
165    
166        public String toString()
167        {
168            return m_importer + " -> " + m_capability + " -> " + m_exporter;
169        }
170    }