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.xbean.finder.archive;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    import java.util.ArrayList;
022    import java.util.Arrays;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.NoSuchElementException;
026    
027    /**
028     * @version $Rev$ $Date$
029     */
030    public class CompositeArchive implements Archive {
031    
032        private final List<Archive> archives = new ArrayList<Archive>();
033    
034        public CompositeArchive(Archive... archives) {
035            this(Arrays.asList(archives));
036        }
037    
038        public CompositeArchive(Iterable<Archive> archives) {
039            for (Archive archive : archives) {
040                this.archives.add(archive);
041            }
042        }
043    
044        @Override
045        public InputStream getBytecode(String className) throws IOException, ClassNotFoundException {
046            for (Archive archive : archives) {
047                try {
048                    return archive.getBytecode(className);
049                } catch (ClassNotFoundException e) {
050                }
051            }
052    
053            throw new ClassNotFoundException(className);
054        }
055    
056        @Override
057        public Class<?> loadClass(String className) throws ClassNotFoundException {
058            for (Archive archive : archives) {
059                try {
060                    return archive.loadClass(className);
061                } catch (ClassNotFoundException e) {
062                }
063            }
064    
065            throw new ClassNotFoundException(className);
066        }
067    
068        @Override
069        public Iterator<String> iterator() {
070            return new CompositeIterator(archives);
071        }
072    
073        private static class CompositeIterator implements Iterator<String> {
074    
075            private Iterator<Archive> archives;
076            private Iterator<String> current;
077    
078            private CompositeIterator(Iterable<Archive> archives) {
079                this.archives = archives.iterator();
080                if (this.archives.hasNext()) {
081                    current = this.archives.next().iterator();
082                }
083            }
084    
085            @Override
086            public boolean hasNext() {
087                if (current == null) return false;
088                if (current.hasNext()) return true;
089                
090                if (archives.hasNext()) {
091                    current = archives.next().iterator();
092                    return hasNext();
093                }
094                return false;
095            }
096    
097            @Override
098            public String next() {
099                if (!hasNext()) throw new NoSuchElementException();
100    
101                return current.next();
102            }
103    
104            @Override
105            public void remove() {
106                throw new UnsupportedOperationException();
107            }
108        }
109    }