KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > classfile > impl > ClassPathImpl


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.classfile.impl;
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import edu.umd.cs.findbugs.classfile.IClassPath;
32 import edu.umd.cs.findbugs.classfile.ICodeBase;
33 import edu.umd.cs.findbugs.classfile.ICodeBaseEntry;
34 import edu.umd.cs.findbugs.classfile.IScannableCodeBase;
35 import edu.umd.cs.findbugs.classfile.ResourceNotFoundException;
36
37 /**
38  * Implementation of IClassPath.
39  *
40  * @author David Hovemeyer
41  */

42 public class ClassPathImpl implements IClassPath {
43     private List JavaDoc<IScannableCodeBase> appCodeBaseList;
44     private List JavaDoc<ICodeBase> auxCodeBaseList;
45     private Map JavaDoc<String JavaDoc, ICodeBaseEntry> codeBaseEntryMap;
46     
47     public ClassPathImpl() {
48         this.appCodeBaseList = new LinkedList JavaDoc<IScannableCodeBase>();
49         this.auxCodeBaseList = new LinkedList JavaDoc<ICodeBase>();
50         this.codeBaseEntryMap = new HashMap JavaDoc<String JavaDoc, ICodeBaseEntry>();
51     }
52
53     /* (non-Javadoc)
54      * @see edu.umd.cs.findbugs.classfile.IClassPath#addCodeBase(edu.umd.cs.findbugs.classfile.ICodeBase)
55      */

56     public void addCodeBase(ICodeBase codeBase) {
57         if (codeBase.isApplicationCodeBase()) {
58             if (!(codeBase instanceof IScannableCodeBase)) {
59                 throw new IllegalStateException JavaDoc();
60             }
61             appCodeBaseList.add((IScannableCodeBase) codeBase);
62         } else {
63             auxCodeBaseList.add(codeBase);
64         }
65     }
66     
67     /* (non-Javadoc)
68      * @see edu.umd.cs.findbugs.classfile.IClassPath#appCodeBaseIterator()
69      */

70     public Iterator JavaDoc<? extends ICodeBase> appCodeBaseIterator() {
71         return appCodeBaseList.iterator();
72     }
73     
74     /* (non-Javadoc)
75      * @see edu.umd.cs.findbugs.classfile.IClassPath#auxCodeBaseIterator()
76      */

77     public Iterator JavaDoc<? extends ICodeBase> auxCodeBaseIterator() {
78         return auxCodeBaseList.iterator();
79     }
80     
81     /* (non-Javadoc)
82      * @see edu.umd.cs.findbugs.classfile.IClassPath#close()
83      */

84     public void close() {
85         for (ICodeBase codeBase : appCodeBaseList) {
86             codeBase.close();
87         }
88         for (ICodeBase codeBase : auxCodeBaseList) {
89             codeBase.close();
90         }
91     }
92     
93     /* (non-Javadoc)
94      * @see edu.umd.cs.findbugs.classfile.IClassPath#lookupResource(java.lang.String)
95      */

96     public ICodeBaseEntry lookupResource(String JavaDoc resourceName) throws ResourceNotFoundException {
97         // See if we have cached the codebase entry for this resource
98
ICodeBaseEntry result = codeBaseEntryMap.get(resourceName);
99
100         if (result == null) {
101             // No previously resolved entry - look up the resources in the codebases
102

103             // First try application codebases
104
result = search(appCodeBaseList, resourceName);
105             if (result == null) {
106                 // Next try aux codebases
107
result = search(auxCodeBaseList, resourceName);
108             }
109             
110             // If not found in any codebase, then throw ResourceNotFoundException
111
if (result == null) {
112                 throw new ResourceNotFoundException(resourceName);
113             }
114             
115             // Cache the entry for future lookups
116
codeBaseEntryMap.put(resourceName, result);
117         }
118
119         return result;
120     }
121
122     /**
123      * Search list of codebases for named resource.
124      *
125      * @param codeBaseList list of codebases to search
126      * @param resourceName name of resourse
127      * @return codebase entry for the named resource, or null if
128      * the named resource cannot be found
129      */

130     private ICodeBaseEntry search(List JavaDoc<? extends ICodeBase> codeBaseList, String JavaDoc resourceName) {
131         for (ICodeBase codeBase : codeBaseList) {
132             try {
133                 return codeBase.lookupResource(resourceName);
134             } catch (ResourceNotFoundException e) {
135                 // Ignore, continue trying other codebases
136
}
137         }
138         return null;
139     }
140     
141     /* (non-Javadoc)
142      * @see edu.umd.cs.findbugs.classfile.IClassPath#mapResourceNameToCodeBaseEntry(java.lang.String, edu.umd.cs.findbugs.classfile.ICodeBaseEntry)
143      */

144     public void mapResourceNameToCodeBaseEntry(String JavaDoc resourceName, ICodeBaseEntry codeBaseEntry) {
145         codeBaseEntryMap.put(resourceName, codeBaseEntry);
146     }
147 }
148
Popular Tags