1 package com.atlassian.plugin.loaders;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 import com.atlassian.plugin.PluginException;
13 import com.atlassian.plugin.loaders.classloading.DeploymentUnit;
14 import com.atlassian.plugin.loaders.classloading.Scanner;
15
16 import org.apache.commons.io.FileUtils;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import static com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22
23
24
25
26
27
28 public class RosterFileScanner implements Scanner
29 {
30 private static final Logger log = LoggerFactory.getLogger(RosterFileScanner.class);
31
32 final private File rosterFile;
33 private Map<String, DeploymentUnit> deploymentUnits;
34 private long lastModified;
35
36
37
38
39
40
41
42
43
44
45
46
47 public RosterFileScanner(final File rosterFile)
48 {
49 checkNotNull(rosterFile);
50 checkArgument(isKnownRosterFileFormat(rosterFile), "Roster file '%s' does not end with '%s'", rosterFile, getListSuffix());
51 this.rosterFile = rosterFile;
52 this.deploymentUnits = Collections.emptyMap();
53 }
54
55 @Override
56 public Collection<DeploymentUnit> scan()
57 {
58 try
59 {
60 final List<DeploymentUnit> scanned = new ArrayList<DeploymentUnit>();
61 final long updatedLastModified = rosterFile.lastModified();
62 if ((updatedLastModified != 0) && (updatedLastModified != lastModified))
63 {
64
65
66
67
68 final List<String> filePaths = (List<String>) FileUtils.readLines(rosterFile);
69 final Map<String, DeploymentUnit> updatedDeploymentUnits = new HashMap<String, DeploymentUnit>(filePaths.size());
70 for(final String filePath : filePaths)
71 {
72 final DeploymentUnit priorUnit = deploymentUnits.get(filePath);
73 if (null == priorUnit)
74 {
75
76 final File file = new File(filePath);
77 final File absoluteFile = file.isAbsolute() ? file : new File(rosterFile.getParentFile(), filePath);
78 final DeploymentUnit deploymentUnit = new DeploymentUnit(absoluteFile);
79 updatedDeploymentUnits.put(filePath, deploymentUnit);
80 scanned.add(deploymentUnit);
81 }
82 else
83 {
84
85 updatedDeploymentUnits.put(filePath, priorUnit);
86 }
87 }
88 deploymentUnits = updatedDeploymentUnits;
89 lastModified = updatedLastModified;
90 return scanned;
91 }
92 }
93 catch (final IOException eio)
94 {
95 log.warn("Cannot read roster file '{}': {}", rosterFile.getAbsolutePath(), eio.getMessage());
96 }
97
98 return Collections.emptyList();
99 }
100
101 @Override
102 public Collection<DeploymentUnit> getDeploymentUnits()
103 {
104 return Collections.unmodifiableCollection(deploymentUnits.values());
105 }
106
107 @Override
108 public void reset()
109 {
110 deploymentUnits = Collections.emptyMap();
111 lastModified = 0;
112 }
113
114 @Override
115 public void remove(final DeploymentUnit deploymentUnit) throws PluginException
116 {
117
118
119
120 }
121
122
123
124
125
126
127
128
129
130
131 public static String getListSuffix()
132 {
133 return ".list";
134 }
135
136
137
138
139
140
141
142
143 public static boolean isKnownRosterFileFormat(final File rosterFile)
144 {
145 return rosterFile.getName().endsWith(getListSuffix());
146 }
147 }