001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.projectimport.eclipse;
043:
044: import java.io.File;
045: import java.util.Collection;
046: import java.util.Collections;
047: import java.util.HashMap;
048: import java.util.HashSet;
049: import java.util.Iterator;
050: import java.util.Map;
051: import java.util.Set;
052: import java.util.logging.Logger;
053: import org.netbeans.modules.projectimport.LoggerFactory;
054: import org.openide.ErrorManager;
055:
056: /**
057: * Provides access to an eclipse workspace.
058: *
059: * @author mkrauskopf
060: */
061: public final class Workspace {
062:
063: /** Logger for this class. */
064: private static final Logger logger = LoggerFactory.getDefault()
065: .createLogger(Workspace.class);
066:
067: /** Represents variable in Eclipse project's classpath. */
068: static class Variable {
069: private String name;
070: private String location;
071:
072: String getName() {
073: return name;
074: }
075:
076: void setName(String name) {
077: this .name = name;
078: }
079:
080: String getLocation() {
081: return location;
082: }
083:
084: void setLocation(String location) {
085: this .location = location;
086: }
087:
088: public String toString() {
089: return name + " = " + location;
090: }
091:
092: public boolean equals(Object obj) {
093: if (this == obj)
094: return true;
095: if (!(obj instanceof Variable))
096: return false;
097: final Variable var = (Variable) obj;
098: if (name != null ? !name.equals(var.name)
099: : var.name != null)
100: return false;
101: if (location != null ? !location.equals(var.location)
102: : var.location != null)
103: return false;
104: return true;
105: }
106:
107: public int hashCode() {
108: int result = 17;
109: result = 37 * result + System.identityHashCode(name);
110: result = 37 * result + System.identityHashCode(location);
111: return result;
112: }
113: }
114:
115: private static final String RUNTIME_SETTINGS = ".metadata/.plugins/org.eclipse.core.runtime/.settings/";
116: static final String CORE_PREFERENCE = RUNTIME_SETTINGS
117: + "org.eclipse.jdt.core.prefs";
118: static final String LAUNCHING_PREFERENCES = RUNTIME_SETTINGS
119: + "org.eclipse.jdt.launching.prefs";
120:
121: static final String RESOURCE_PROJECTS_DIR = ".metadata/.plugins/org.eclipse.core.resources/.projects";
122:
123: static final String DEFAULT_JRE_CONTAINER = "org.eclipse.jdt.launching.JRE_CONTAINER";
124:
125: private File corePrefFile;
126: private File launchingPrefsFile;
127: private File resourceProjectsDir;
128: private File workspaceDir;
129:
130: private Set variables;
131: private Set projects = new HashSet();
132: private Map jreContainers;
133: private Map userLibraries;
134:
135: /**
136: * Returns <code>Workspace</code> instance representing Eclipse Workspace
137: * found in the given <code>workspaceDir</code>. If a workspace is not found
138: * in the specified directory, <code>null</code> is returned.
139: *
140: * @return either a <code>Workspace</code> instance or null if a given
141: * <code>workspaceDir</code> doesn't contain valid Eclipse workspace.
142: */
143: static Workspace createWorkspace(File workspaceDir) {
144: if (!EclipseUtils.isRegularWorkSpace(workspaceDir)) {
145: ErrorManager.getDefault().log(
146: ErrorManager.INFORMATIONAL,
147: "There is not a regular workspace in "
148: + workspaceDir);
149: return null;
150: }
151: Workspace workspace = new Workspace(workspaceDir);
152: return workspace;
153: }
154:
155: /** Sets up a workspace directory. */
156: private Workspace(File workspaceDir) {
157: this .workspaceDir = workspaceDir;
158: corePrefFile = new File(workspaceDir, CORE_PREFERENCE);
159: launchingPrefsFile = new File(workspaceDir,
160: LAUNCHING_PREFERENCES);
161: resourceProjectsDir = new File(workspaceDir,
162: RESOURCE_PROJECTS_DIR);
163: }
164:
165: File getDirectory() {
166: return workspaceDir;
167: }
168:
169: File getCorePreferenceFile() {
170: return corePrefFile;
171: }
172:
173: File getLaunchingPrefsFile() {
174: return launchingPrefsFile;
175: }
176:
177: File getResourceProjectsDir() {
178: return resourceProjectsDir;
179: }
180:
181: void addVariable(Variable var) {
182: if (variables == null) {
183: variables = new HashSet();
184: }
185: variables.add(var);
186: }
187:
188: Set getVariables() {
189: return variables;
190: }
191:
192: void setJREContainers(Map jreContainers) {
193: this .jreContainers = jreContainers;
194: }
195:
196: void addProject(EclipseProject project) {
197: projects.add(project);
198: }
199:
200: void addUserLibrary(String libName, Collection jars) {
201: if (userLibraries == null) {
202: userLibraries = new HashMap();
203: }
204: userLibraries.put(libName, jars);
205: }
206:
207: Collection getJarsForUserLibrary(String libRawPath) {
208: Collection retVal = Collections.EMPTY_LIST;
209: if (userLibraries != null) {
210: retVal = (Collection) userLibraries.get(libRawPath);
211: }
212: return retVal;
213: }
214:
215: /**
216: * Tries to find an <code>EclipseProject</code> in the workspace and either
217: * returns its instance or null in the case it's not found.
218: */
219: EclipseProject getProjectByRawPath(String rawPath) {
220: EclipseProject project = null;
221: for (Iterator it = projects.iterator(); it.hasNext();) {
222: EclipseProject prj = (EclipseProject) it.next();
223: // rawpath = /name
224: if (prj.getName().equals(rawPath.substring(1))) {
225: project = prj;
226: }
227: }
228: if (project == null) {
229: logger.info("Project with raw path \"" + rawPath
230: + "\" cannot" + // NOI18N
231: " be found in project list: " + projects); // NOI18N
232: }
233: return project;
234: }
235:
236: public Set getProjects() {
237: return projects;
238: }
239:
240: String getProjectAbsolutePath(String projectName) {
241: for (Iterator it = projects.iterator(); it.hasNext();) {
242: EclipseProject project = ((EclipseProject) it.next());
243: if (project.getName().equals(projectName)) {
244: return project.getDirectory().getAbsolutePath();
245: }
246: }
247: return null;
248: }
249:
250: /**
251: * Returns JDK used for compilation of project with the specified
252: * projectDirName.
253: */
254: String getJDKDirectory(String jreContainer) {
255: if (jreContainer != null) {
256: if (!DEFAULT_JRE_CONTAINER.equals(jreContainer)) {
257: // JRE name seems to be after the last slash
258: jreContainer = jreContainer.substring(jreContainer
259: .lastIndexOf('/') + 1);
260: }
261: for (Iterator it = jreContainers.entrySet().iterator(); it
262: .hasNext();) {
263: Map.Entry entry = (Map.Entry) it.next();
264: if (entry.getKey().equals(jreContainer)) {
265: return (String) entry.getValue();
266: }
267: }
268: }
269: return null;
270: }
271: }
|