001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.repl.newjvm;
038:
039: import java.io.File;
040: import java.net.URL;
041: import java.net.URLClassLoader;
042: import java.net.MalformedURLException;
043: import java.util.LinkedList;
044: import java.util.List;
045: import java.lang.ClassLoader;
046: import edu.rice.cs.plt.iter.IterUtil;
047: import edu.rice.cs.drjava.model.DeadClassLoader;
048: import edu.rice.cs.drjava.model.BrainClassLoader;
049:
050: import static edu.rice.cs.plt.debug.DebugUtil.error;
051:
052: /* This class runs in the Main JVM, but it accessed from the Slave JVM via RMI. All public methods are synchronzed. */
053: public class ClassPathManager {
054:
055: private LinkedList<File> projectCP; /* The custom project classpath. */
056: private LinkedList<File> buildCP; /* The build directory. */
057: private LinkedList<File> projectFilesCP; /* The open project files. */
058: private LinkedList<File> externalFilesCP; /* The open external files. */
059: private LinkedList<File> extraCP; /* The extra preferences classpath. */
060:
061: // private volatile LinkedList<File> systemCP; /* The system classpath. */
062: // private List<File> openFilesCP; /* Open files classpath (for nonproject mode) */
063:
064: public ClassPathManager() {
065: projectCP = new LinkedList<File>();
066: buildCP = new LinkedList<File>();
067: projectFilesCP = new LinkedList<File>();
068: externalFilesCP = new LinkedList<File>();
069: extraCP = new LinkedList<File>();
070: // systemCP = new LinkedList<File>();
071: // openFilesCP = new LinkedList<File>();
072: }
073:
074: /** Adds the entry to the front of the project classpath
075: * (this is the classpath specified in project properties)
076: */
077: public synchronized void addProjectCP(File f) {
078: projectCP.add(f);
079: }
080:
081: public synchronized Iterable<File> getProjectCP() {
082: return IterUtil.reverse(projectCP);
083: }
084:
085: /** Adds the entry to the front of the build classpath. */
086: public synchronized void addBuildDirectoryCP(File f) {
087: buildCP.add(f);
088: }
089:
090: public synchronized Iterable<File> getBuildDirectoryCP() {
091: return IterUtil.reverse(buildCP);
092: }
093:
094: /** Adds the entry to the front of the project files classpath (this is the classpath for all open project files). */
095: public synchronized void addProjectFilesCP(File f) {
096: projectFilesCP.add(f);
097: }
098:
099: public synchronized Iterable<File> getProjectFilesCP() {
100: return IterUtil.reverse(projectFilesCP);
101: }
102:
103: /** Adds new entry containing f to the front of the external classpath. */
104: public void addExternalFilesCP(File f) {
105: externalFilesCP.add(f);
106: }
107:
108: public Iterable<File> getExternalFilesCP() {
109: return IterUtil.reverse(externalFilesCP);
110: }
111:
112: /** Adds the entry to the front of the extra classpath. */
113: public synchronized void addExtraCP(File f) {
114: extraCP.add(f);
115: }
116:
117: public Iterable<File> getExtraCP() {
118: return IterUtil.reverse(extraCP);
119: }
120:
121: /** Returns a new classloader that represents the custom classpath. */
122: public synchronized ClassLoader getClassLoader() {
123: return new BrainClassLoader(buildClassLoader(projectCP),
124: buildClassLoader(buildCP),
125: buildClassLoader(projectFilesCP),
126: buildClassLoader(externalFilesCP),
127: buildClassLoader(extraCP));
128: }
129:
130: /** Builds a new classloader for the list of classpath entries. */
131: private ClassLoader buildClassLoader(List<File> path) {
132: List<URL> urls = new LinkedList<URL>();
133: for (File f : path) {
134: try {
135: URL u = f.toURI().toURL();
136: urls.add(u);
137: } catch (MalformedURLException e) {
138: error.log("Can't convert file to URL", e);
139: }
140: }
141: return new URLClassLoader(urls.toArray(new URL[urls.size()]),
142: new DeadClassLoader());
143: }
144:
145: }
|