blob: b60749243f57eb24b44ca5c0332c3b447eddef48 [file] [log] [blame]
Stuart McCullochd00f9712009-07-13 10:06:47 +00001package aQute.bnd.build;
2
3import java.io.*;
4import java.lang.ref.*;
5import java.util.*;
6
7import aQute.bnd.service.action.*;
8import aQute.lib.osgi.*;
9
10public class Workspace extends Processor {
11 public final static int STRATEGY_HIGHEST = 1;
12 public final static int STRATEGY_EXACT = 0;
13 public final static int STRATEGY_LOWEST = -1;
14 public static final String BUILDFILE = "build.bnd";
15 public static final String CNFDIR = "cnf";
16
17 static Map<File, WeakReference<Workspace>> cache = newHashMap();
18 final Map<String, Project> models = newHashMap();
19 final Map<String, Action> commands = newMap();
20
21 /**
22 * This static method finds the workspace and creates a project (or returns
23 * an existing project)
24 *
25 * @param projectDir
26 * @return
27 */
28 public static Project getProject(File projectDir) throws Exception {
29 projectDir = projectDir.getAbsoluteFile();
30 assert projectDir.isDirectory();
31
32 Workspace ws = getWorkspace(projectDir.getParentFile());
33 return ws.getProject(projectDir.getName());
34 }
35
36 public static Workspace getWorkspace(File workspaceDir) throws Exception {
37 workspaceDir = workspaceDir.getAbsoluteFile();
38 synchronized (cache) {
39 WeakReference<Workspace> wsr = cache.get(workspaceDir);
40 Workspace ws;
41 if (wsr == null || (ws = wsr.get()) == null) {
42 ws = new Workspace(workspaceDir);
43 cache.put(workspaceDir, new WeakReference<Workspace>(ws));
44 }
45 return ws;
46 }
47 }
48
49 public Workspace(File dir) throws Exception {
50 dir = dir.getAbsoluteFile();
51 dir.mkdirs();
52 assert dir.isDirectory();
53
54 File buildDir = new File(dir, CNFDIR).getAbsoluteFile();
55 File buildFile = new File(buildDir, BUILDFILE).getAbsoluteFile();
56 if (!buildFile.isFile())
57 warning("No Build File in " + dir);
58 setProperties(buildFile, dir);
59 }
60
61 public Project getProject(String bsn) throws Exception {
62 synchronized (models) {
63 Project project = models.get(bsn);
64 if (project != null)
65 return project;
66
67 File projectDir = getFile(bsn);
68 project = new Project(this, projectDir);
69 models.put(bsn, project);
70 return project;
71 }
72 }
73
74 public boolean isPresent(String name) {
75 return models.containsKey(name);
76 }
77
78 public Collection<Project> getCurrentProjects() {
79 return models.values();
80 }
81
82 public boolean refresh() {
83 if (super.refresh()) {
84 for (Project project : getCurrentProjects()) {
85 project.propertiesChanged();
86 }
87 return true;
88 }
89 return false;
90 }
91
92 public String _workspace(String args[]) {
93 return getBase().getAbsolutePath();
94 }
95
96 public void addCommand(String menu, Action action) {
97 commands.put(menu, action);
98 }
99
100 public void removeCommand(String menu) {
101 commands.remove(menu);
102 }
103
104 public void fillActions(Map<String, Action> all) {
105 all.putAll(commands);
106 }
107
108
109}