001 /*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014 package org.apache.commons.classscan.builtin;
015
016 import java.io.File;
017 import java.io.IOException;
018 import java.util.ArrayList;
019 import java.util.Collections;
020 import java.util.List;
021 import java.util.Map;
022 import java.util.regex.Pattern;
023
024 import org.apache.commons.classscan.ClassPathElement;
025 import org.apache.commons.classscan.ResourceFile;
026 import org.apache.commons.classscan.spi.model.SpiClassPath;
027 import org.apache.commons.classscan.spi.model.SpiClassPathElement;
028 import org.apache.commons.classscan.spi.model.SpiMetaClassLoader;
029 import org.apache.commons.classscan.spi.model.SpiMetaRegistry;
030 import org.slf4j.Logger;
031 import org.slf4j.LoggerFactory;
032
033 public class BootstrapClassPath implements SpiClassPath {
034
035 private static final Logger logger = LoggerFactory.getLogger(BootstrapClassPath.class);
036
037 static Pattern PATH_SPLITTER = Pattern.compile(File.pathSeparator);
038
039 private final SpiMetaRegistry registry;
040
041 public BootstrapClassPath(SpiMetaRegistry registry) {
042 this.registry = registry;
043 }
044
045 @Override
046 public List<? extends ClassPathElement> getClassPathElements() {
047 return getBootstrapLocations();
048 }
049
050 private List<SpiClassPathElement> getBootstrapLocations() {
051 List<SpiClassPathElement> locations= new ArrayList<SpiClassPathElement>();
052 for(String element : parseBootstrapClassPathValues()) {
053 try {
054 SpiClassPathElement location = checkLocation(element);
055 if(location!=null) {
056 locations.add(location);
057 }
058 } catch (IOException e) {
059 logger.info("Bootstrap Classpath element "+element+" ignored", e);
060 }
061 }
062 return locations;
063 }
064
065 String[] parseBootstrapClassPathValues() {
066 return PATH_SPLITTER.split(System.getProperty("sun.boot.class.path"));
067 }
068
069 private SpiClassPathElement checkLocation(String element) throws IOException {
070 File location = new File(element);
071 return registry.createClassPathElement(location.toURI().toURL());
072 }
073
074 @Override
075 public SpiMetaClassLoader createMetaClassLoader(SpiMetaRegistry registry, ClassLoader classLoader) {
076 if(classLoader!=null) {
077 throw new IllegalArgumentException("BootStrap ClassLoader should be null");
078 }
079
080 return new BootstrapMetaClassLoader(registry);
081 }
082
083 @Override
084 public Map<ClassPathElement, ResourceFile> getResources(String fileName) {
085 return Collections.emptyMap();
086 }
087 }