| 1 | |
package org.jtheque.core.managers.update.versions; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import org.jdom.Element; |
| 20 | |
import org.jtheque.core.managers.Managers; |
| 21 | |
import org.jtheque.core.managers.core.ICore; |
| 22 | |
import org.jtheque.core.managers.log.ILoggingManager; |
| 23 | |
import org.jtheque.core.managers.module.beans.ModuleContainer; |
| 24 | |
import org.jtheque.core.managers.patch.OnlinePatch; |
| 25 | |
import org.jtheque.core.managers.update.Updatable; |
| 26 | |
import org.jtheque.core.managers.update.actions.AbstractUpdateAction; |
| 27 | |
import org.jtheque.core.managers.update.actions.DeleteAction; |
| 28 | |
import org.jtheque.core.managers.update.actions.DownloadAction; |
| 29 | |
import org.jtheque.core.managers.update.actions.MoveAction; |
| 30 | |
import org.jtheque.core.managers.update.actions.UpdateAction; |
| 31 | |
import org.jtheque.core.utils.file.XMLException; |
| 32 | |
import org.jtheque.core.utils.file.XMLReader; |
| 33 | |
import org.jtheque.utils.StringUtils; |
| 34 | |
import org.jtheque.utils.bean.Version; |
| 35 | |
import org.jtheque.utils.io.FileUtils; |
| 36 | |
|
| 37 | |
import java.net.MalformedURLException; |
| 38 | |
import java.net.URL; |
| 39 | |
import java.util.ArrayList; |
| 40 | |
import java.util.Collection; |
| 41 | |
import java.util.Collections; |
| 42 | |
import java.util.List; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 0 | public final class VersionsFileReader { |
| 50 | 0 | private final XMLReader reader = new XMLReader(); |
| 51 | |
private VersionsFile versionsFile; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public VersionsFile read(Object object) { |
| 60 | 0 | if (isModule(object)) { |
| 61 | 0 | ModuleContainer module = (ModuleContainer) object; |
| 62 | |
|
| 63 | 0 | if (StringUtils.isNotEmpty(module.getInfos().updateURL())) { |
| 64 | 0 | return read(module.getInfos().updateURL()); |
| 65 | |
} |
| 66 | 0 | } else if (isUpdatable(object)) { |
| 67 | 0 | Updatable updatable = (Updatable) object; |
| 68 | |
|
| 69 | 0 | return read(updatable.getVersionsFileURL()); |
| 70 | 0 | } else if (isCore(object)) { |
| 71 | 0 | return read(Managers.getCore().getFiles().getVersionsFileURL()); |
| 72 | |
} |
| 73 | |
|
| 74 | 0 | return null; |
| 75 | |
} |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
private static boolean isModule(Object object) { |
| 84 | 0 | return object instanceof ModuleContainer; |
| 85 | |
} |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
private static boolean isUpdatable(Object object) { |
| 94 | 0 | return object instanceof Updatable; |
| 95 | |
} |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
private static boolean isCore(Object object) { |
| 104 | 0 | return object instanceof ICore; |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
private VersionsFile read(String strUrl) { |
| 114 | |
try { |
| 115 | 0 | return readVersionsFile(new URL(strUrl)); |
| 116 | 0 | } catch (MalformedURLException e) { |
| 117 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e, "Unable to read versions file"); |
| 118 | |
} |
| 119 | |
|
| 120 | 0 | return null; |
| 121 | |
} |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public VersionsFile readURL(String versionsFileURL) { |
| 130 | |
try { |
| 131 | 0 | return readVersionsFile(new URL(versionsFileURL)); |
| 132 | 0 | } catch (MalformedURLException e) { |
| 133 | 0 | return null; |
| 134 | |
} |
| 135 | |
} |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
private VersionsFile readVersionsFile(URL url) { |
| 144 | 0 | versionsFile = new VersionsFile(); |
| 145 | 0 | List<OnlineVersion> onlineVersions = new ArrayList<OnlineVersion>(10); |
| 146 | |
|
| 147 | |
try { |
| 148 | 0 | reader.openURL(url); |
| 149 | |
|
| 150 | 0 | readInstallVersion(); |
| 151 | |
|
| 152 | 0 | for (Object currentNode : reader.getNodes("jt-version", reader.getRootElement())) { |
| 153 | 0 | readOnlineVersion(currentNode, onlineVersions); |
| 154 | |
} |
| 155 | |
|
| 156 | 0 | Collections.sort(onlineVersions); |
| 157 | |
|
| 158 | 0 | versionsFile.setVersions(onlineVersions); |
| 159 | 0 | } catch (XMLException e) { |
| 160 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e, "Unable to read versions file"); |
| 161 | |
} finally { |
| 162 | 0 | FileUtils.close(reader); |
| 163 | 0 | } |
| 164 | |
|
| 165 | 0 | return versionsFile; |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
private void readInstallVersion() throws XMLException { |
| 174 | 0 | InstallVersion installVersion = new InstallVersion(); |
| 175 | |
|
| 176 | 0 | Object installVersionNode = reader.getNode("install", reader.getRootElement()); |
| 177 | |
|
| 178 | 0 | readOnlineVersion(installVersionNode, installVersion); |
| 179 | |
|
| 180 | 0 | installVersion.setJarFile(reader.readString("jar-file", installVersionNode)); |
| 181 | 0 | installVersion.setTitle(reader.readString("title", installVersionNode)); |
| 182 | |
|
| 183 | 0 | versionsFile.setInstallVersion(installVersion); |
| 184 | 0 | } |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
private void readPatches(Object currentNode, OnlineVersion version) throws XMLException { |
| 194 | 0 | Collection<Element> nodes = reader.getNodes("patchs/patch", currentNode); |
| 195 | |
|
| 196 | 0 | Collection<OnlinePatch> patches = new ArrayList<OnlinePatch>(nodes.size()); |
| 197 | |
|
| 198 | 0 | for (Object node : nodes) { |
| 199 | 0 | OnlinePatch patch = new OnlinePatch(); |
| 200 | 0 | patch.setClassName(reader.readString("@class", node)); |
| 201 | |
|
| 202 | 0 | patches.add(patch); |
| 203 | 0 | } |
| 204 | |
|
| 205 | 0 | version.setPatches(patches); |
| 206 | 0 | } |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
private void readOnlineVersion(Object currentNode, Collection<OnlineVersion> onlineVersions) throws XMLException { |
| 216 | 0 | OnlineVersion onlineVersion = new OnlineVersion(); |
| 217 | |
|
| 218 | 0 | readOnlineVersion(currentNode, onlineVersion); |
| 219 | |
|
| 220 | 0 | onlineVersions.add(onlineVersion); |
| 221 | 0 | } |
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
private void readOnlineVersion(Object currentNode, OnlineVersion onlineVersion) throws XMLException { |
| 231 | 0 | readPatches(currentNode, onlineVersion); |
| 232 | 0 | readVersionNumber(currentNode, onlineVersion); |
| 233 | 0 | readCoreVersion(currentNode, onlineVersion); |
| 234 | 0 | readActions(currentNode, onlineVersion); |
| 235 | 0 | } |
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
private void readVersionNumber(Object currentNode, OnlineVersion onlineVersion) throws XMLException { |
| 245 | 0 | onlineVersion.setVersion(new Version(reader.readString("nom", currentNode))); |
| 246 | 0 | } |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
private void readActions(Object currentNode, OnlineVersion onlineVersion) throws XMLException { |
| 256 | 0 | Collection<Element> nodes = reader.getNodes("actions/*", currentNode); |
| 257 | |
|
| 258 | 0 | onlineVersion.setActions(new ArrayList<UpdateAction>(nodes.size())); |
| 259 | |
|
| 260 | 0 | for (Element node : nodes) { |
| 261 | 0 | String name = node.getName(); |
| 262 | |
|
| 263 | 0 | if ("add".equals(name)) { |
| 264 | 0 | AbstractUpdateAction action = readDownloadAction(node); |
| 265 | |
|
| 266 | 0 | onlineVersion.getActions().add(action); |
| 267 | 0 | } else if ("delete".equals(name)) { |
| 268 | 0 | AbstractUpdateAction action = readDeleteAction(node); |
| 269 | |
|
| 270 | 0 | onlineVersion.getActions().add(action); |
| 271 | 0 | } else if ("move".equals(name)) { |
| 272 | 0 | AbstractUpdateAction action = readMoveAction(node); |
| 273 | |
|
| 274 | 0 | onlineVersion.getActions().add(action); |
| 275 | |
} |
| 276 | 0 | } |
| 277 | 0 | } |
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
private AbstractUpdateAction readDownloadAction(Object node) throws XMLException { |
| 287 | 0 | DownloadAction action = new DownloadAction(); |
| 288 | |
|
| 289 | 0 | action.setOs(reader.readString("@os", node)); |
| 290 | 0 | action.setFolder(reader.readString("folder", node)); |
| 291 | 0 | action.setFile(reader.readString("file", node)); |
| 292 | 0 | action.setUrl(reader.readString("url", node)); |
| 293 | |
|
| 294 | 0 | return action; |
| 295 | |
} |
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
private AbstractUpdateAction readDeleteAction(Object node) throws XMLException { |
| 305 | 0 | AbstractUpdateAction action = new DeleteAction(); |
| 306 | |
|
| 307 | 0 | action.setFolder(reader.readString("folder", node)); |
| 308 | 0 | action.setFile(reader.readString("file", node)); |
| 309 | |
|
| 310 | 0 | return action; |
| 311 | |
} |
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
private AbstractUpdateAction readMoveAction(Object node) throws XMLException { |
| 321 | 0 | MoveAction action = new MoveAction(); |
| 322 | |
|
| 323 | 0 | action.setSourceFile(reader.readString("src-file", node)); |
| 324 | 0 | action.setSourceFolder(reader.readString("src-folder", node)); |
| 325 | 0 | action.setFile(reader.readString("dest-file", node)); |
| 326 | 0 | action.setFolder(reader.readString("dest-folder", node)); |
| 327 | |
|
| 328 | 0 | return action; |
| 329 | |
} |
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
private void readCoreVersion(Object currentNode, OnlineVersion onlineVersion) throws XMLException { |
| 339 | 0 | if (StringUtils.isEmpty(reader.readString("core", currentNode))) { |
| 340 | 0 | onlineVersion.setCoreVersion(Managers.getCore().getCoreCurrentVersion()); |
| 341 | |
} else { |
| 342 | 0 | onlineVersion.setCoreVersion(new Version(reader.readString("core", currentNode))); |
| 343 | |
} |
| 344 | 0 | } |
| 345 | |
} |