KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > repl > newjvm > ClassPathManager


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.repl.newjvm;
35
36 import java.net.URL JavaDoc;
37 import java.util.LinkedList JavaDoc;
38 import java.util.List JavaDoc;
39 import java.lang.ClassLoader JavaDoc;
40 import edu.rice.cs.drjava.model.ClassPathEntry;
41 import edu.rice.cs.drjava.model.DeadClassLoader;
42 import edu.rice.cs.drjava.model.BrainClassLoader;
43
44 import edu.rice.cs.util.ClassPathVector;
45
46 /* This class runs in the Main JVM, but it accessed from the Slave JVM via RMI. All public methods are synchronzed. */
47 public class ClassPathManager{
48   
49   private LinkedList JavaDoc<ClassPathEntry> projectCP; /* The custom project classpath. */
50   private LinkedList JavaDoc<ClassPathEntry> buildCP; /* The build directory. */
51   private LinkedList JavaDoc<ClassPathEntry> projectFilesCP; /* The open project files. */
52   private LinkedList JavaDoc<ClassPathEntry> externalFilesCP; /* The open external files. */
53   private LinkedList JavaDoc<ClassPathEntry> extraCP; /* The extra preferences classpath. */
54   
55 // private volatile LinkedList<ClassPathEntry> systemCP; /* The system classpath. */
56
// private List<ClasspathEntry> openFilesCP; /* Open files classpath (for nonproject mode) */
57

58   public ClassPathManager() {
59     projectCP = new LinkedList JavaDoc<ClassPathEntry>();
60     buildCP = new LinkedList JavaDoc<ClassPathEntry>();
61     projectFilesCP = new LinkedList JavaDoc<ClassPathEntry>();
62     externalFilesCP = new LinkedList JavaDoc<ClassPathEntry>();
63     extraCP = new LinkedList JavaDoc<ClassPathEntry>();
64 // systemCP = new LinkedList<ClassPathEntry>();
65
// openFilesCP = new LinkedList<ClasspathEntry>();
66
}
67   
68   /** Adds the entry to the front of the project classpath
69    * (this is the classpath specified in project properties)
70    */

71   public synchronized void addProjectCP(URL JavaDoc f) { projectCP.add(0, new ClassPathEntry(f)); }
72   
73   public synchronized ClassPathEntry[] getProjectCP() {
74     return projectCP.toArray(new ClassPathEntry[projectCP.size()]);
75   }
76   
77   /** Adds the entry to the front of the build classpath. */
78   public synchronized void addBuildDirectoryCP(URL JavaDoc f) {
79     buildCP.addFirst(new ClassPathEntry(f));
80   }
81
82   public synchronized ClassPathEntry[] getBuildDirectoryCP() {
83     return buildCP.toArray(new ClassPathEntry[buildCP.size()]);
84   }
85   
86   /** Adds the entry to the front of the project files classpath (this is the classpath for all open project files). */
87   public synchronized void addProjectFilesCP(URL JavaDoc f) { projectFilesCP.addFirst(new ClassPathEntry(f)); }
88   
89   public synchronized ClassPathEntry[] getProjectFilesCP() {
90     return projectFilesCP.toArray(new ClassPathEntry[projectFilesCP.size()]);
91   }
92   
93   /** Adds new entry containing f to the front of the external classpath. */
94   public void addExternalFilesCP(URL JavaDoc f) { externalFilesCP.add(0, new ClassPathEntry(f)); }
95   
96   public ClassPathEntry[] getExternalFilesCP() {
97     return externalFilesCP.toArray(new ClassPathEntry[externalFilesCP.size()]);
98   }
99   
100   /** Adds the entry to the front of the extra classpath. */
101   public synchronized void addExtraCP(URL JavaDoc f) { extraCP.addFirst(new ClassPathEntry(f)); }
102   
103   public ClassPathEntry[] getExtraCP() { return extraCP.toArray(new ClassPathEntry[extraCP.size()]); }
104   
105   /** Returns a new classloader that represents the custom classpath. */
106   public synchronized ClassLoader JavaDoc getClassLoader() {
107     return new BrainClassLoader(buildClassLoader(projectCP),
108                                 buildClassLoader(buildCP),
109                                 buildClassLoader(projectFilesCP),
110                                 buildClassLoader(externalFilesCP),
111                                 buildClassLoader(extraCP));
112   }
113   
114   /** Builds a new classloader for the list of classpath entries. */
115   private ClassLoader JavaDoc buildClassLoader(List JavaDoc<ClassPathEntry>locpe) {
116     ClassLoader JavaDoc c = new DeadClassLoader();
117     for(ClassPathEntry cpe: locpe) { c = cpe.getClassLoader(c); }
118     return c;
119   }
120
121   /** Returns a copy of the list of unique entries on the classpath. */
122   public synchronized ClassPathVector getAugmentedClassPath() {
123     ClassPathVector ret = new ClassPathVector();
124   
125     for (ClassPathEntry e: getProjectCP()) { ret.add(e.getEntry()); }
126
127     for (ClassPathEntry e: getBuildDirectoryCP()) { ret.add(e.getEntry()); }
128
129     for (ClassPathEntry e: getProjectFilesCP()) { ret.add(e.getEntry()); }
130
131     for (ClassPathEntry e: getExternalFilesCP()) { ret.add(e.getEntry()); }
132
133     for (ClassPathEntry e: getExtraCP()) { ret.add(e.getEntry()); }
134     return ret;
135   }
136
137 }
138
139
Popular Tags