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.Enumeration;
023
024 import org.apache.felix.framework.util.Util;
025 import org.apache.felix.framework.util.manifestparser.Capability;
026 import org.apache.felix.moduleloader.*;
027
028 public class R4Wire implements IWire
029 {
030 private final IModule m_importer;
031 private final IRequirement m_requirement;
032 private final IModule m_exporter;
033 private final ICapability m_capability;
034
035 public R4Wire(IModule importer, IRequirement requirement,
036 IModule exporter, ICapability capability)
037 {
038 m_importer = importer;
039 m_requirement = requirement;
040 m_exporter = exporter;
041 m_capability = capability;
042 }
043
044 /* (non-Javadoc)
045 * @see org.apache.felix.framework.searchpolicy.IWire#getImporter()
046 */
047 public IModule getImporter()
048 {
049 return m_importer;
050 }
051
052 /* (non-Javadoc)
053 * @see org.apache.felix.framework.searchpolicy.IWire#getRequirement()
054 */
055 public IRequirement getRequirement()
056 {
057 return m_requirement;
058 }
059
060 /* (non-Javadoc)
061 * @see org.apache.felix.framework.searchpolicy.IWire#getExporter()
062 */
063 public IModule getExporter()
064 {
065 return m_exporter;
066 }
067
068 /* (non-Javadoc)
069 * @see org.apache.felix.framework.searchpolicy.IWire#getCapability()
070 */
071 public ICapability getCapability()
072 {
073 return m_capability;
074 }
075
076 /* (non-Javadoc)
077 * @see org.apache.felix.framework.searchpolicy.IWire#getClass(java.lang.String)
078 */
079 public boolean hasPackage(String pkgName)
080 {
081 return (m_capability.getNamespace().equals(ICapability.PACKAGE_NAMESPACE) &&
082 m_capability.getProperties().get(ICapability.PACKAGE_PROPERTY).equals(pkgName));
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 Class clazz = null;
091
092 // Get the package of the target class.
093 String pkgName = Util.getClassPackage(name);
094
095 // Only check when the package of the target class is
096 // the same as the package for the wire.
097 if (m_capability.getNamespace().equals(ICapability.PACKAGE_NAMESPACE) &&
098 m_capability.getProperties().get(ICapability.PACKAGE_PROPERTY).equals(pkgName))
099 {
100 // Check the include/exclude filters from the target package
101 // to make sure that the class is actually visible. We delegate
102 // to the exporting module, rather than its content, so it can
103 // it can follow any internal wires it may have (e.g., if the
104 // package has multiple sources).
105 if (m_capability.getNamespace().equals(ICapability.PACKAGE_NAMESPACE)
106 && ((Capability) m_capability).isIncluded(name))
107 {
108 clazz = m_exporter.getClassByDelegation(name);
109 }
110
111 // If no class was found, then we must throw an exception
112 // since the exporter for this package did not contain the
113 // requested class.
114 if (clazz == null)
115 {
116 throw new ClassNotFoundException(name);
117 }
118 }
119
120 return clazz;
121 }
122
123 /* (non-Javadoc)
124 * @see org.apache.felix.framework.searchpolicy.IWire#getResource(java.lang.String)
125 */
126 public URL getResource(String name) throws ResourceNotFoundException
127 {
128 URL url = null;
129
130 // Get the package of the target class.
131 String pkgName = Util.getResourcePackage(name);
132
133 // Only check when the package of the target resource is
134 // the same as the package for the wire.
135 if (m_capability.getNamespace().equals(ICapability.PACKAGE_NAMESPACE) &&
136 m_capability.getProperties().get(ICapability.PACKAGE_PROPERTY).equals(pkgName))
137 {
138 // Delegate to the exporting module, rather than its
139 // content, so that it can follow any internal wires it may have
140 // (e.g., if the package has multiple sources).
141 url = m_exporter.getResourceByDelegation(name);
142
143 // If no resource was found, then we must throw an exception
144 // since the exporter for this package did not contain the
145 // requested class.
146 if (url == null)
147 {
148 throw new ResourceNotFoundException(name);
149 }
150 }
151
152 return url;
153 }
154
155 /* (non-Javadoc)
156 * @see org.apache.felix.framework.searchpolicy.IWire#getResources(java.lang.String)
157 */
158 public Enumeration getResources(String name) throws ResourceNotFoundException
159 {
160 Enumeration urls = null;
161
162 // Get the package of the target class.
163 String pkgName = Util.getResourcePackage(name);
164
165 // Only check when the package of the target resource is
166 // the same as the package for the wire.
167 if (m_capability.getNamespace().equals(ICapability.PACKAGE_NAMESPACE) &&
168 m_capability.getProperties().get(ICapability.PACKAGE_PROPERTY).equals(pkgName))
169 {
170 urls = m_exporter.getResourcesByDelegation(name);
171
172 // If no resource was found, then we must throw an exception
173 // since the exporter for this package did not contain the
174 // requested class.
175 if (urls == null)
176 {
177 throw new ResourceNotFoundException(name);
178 }
179 }
180
181 return urls;
182 }
183
184 public String toString()
185 {
186 if (m_capability.getNamespace().equals(ICapability.PACKAGE_NAMESPACE))
187 {
188 return m_importer + " -> "
189 + m_capability.getProperties().get(ICapability.PACKAGE_PROPERTY)
190 + " -> " + m_exporter;
191 }
192 return m_importer + " -> " + m_capability + " -> " + m_exporter;
193 }
194 }