| 1 | |
package org.jtheque.core.managers.update.versions; |
| 2 | |
|
| 3 | |
import org.jtheque.core.managers.error.IErrorManager; |
| 4 | |
import org.jtheque.core.managers.module.beans.ModuleContainer; |
| 5 | |
import org.jtheque.core.managers.update.Updatable; |
| 6 | |
import org.jtheque.utils.bean.Version; |
| 7 | |
import org.jtheque.utils.collections.CollectionUtils; |
| 8 | |
|
| 9 | |
import javax.annotation.Resource; |
| 10 | |
import java.util.Collection; |
| 11 | |
import java.util.HashMap; |
| 12 | |
import java.util.Map; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public final class VersionsLoader implements IVersionsLoader { |
| 36 | |
private final Map<Object, VersionsFile> cache; |
| 37 | |
|
| 38 | |
private static final int CACHE_INITIAL_SIZE = 20; |
| 39 | |
|
| 40 | |
@Resource |
| 41 | |
private IErrorManager errorManager; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public VersionsLoader() { |
| 47 | 0 | super(); |
| 48 | |
|
| 49 | 0 | cache = new HashMap<Object, VersionsFile>(CACHE_INITIAL_SIZE); |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
@Override |
| 53 | |
public Version getVersion(Object object) { |
| 54 | 0 | if (isModule(object)) { |
| 55 | 0 | return new Version(((ModuleContainer) object).getInfos().version()); |
| 56 | 0 | } else if (isUpdatable(object)) { |
| 57 | 0 | return ((Updatable) object).getVersion(); |
| 58 | |
} |
| 59 | |
|
| 60 | 0 | return null; |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
private static boolean isModule(Object object) { |
| 70 | 0 | return object instanceof ModuleContainer; |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
private static boolean isUpdatable(Object object) { |
| 80 | 0 | return object instanceof Updatable; |
| 81 | |
} |
| 82 | |
|
| 83 | |
@Override |
| 84 | |
public Collection<Version> getVersions(Object object) { |
| 85 | 0 | VersionsFile file = getVersionsFile(object); |
| 86 | |
|
| 87 | 0 | return file == null ? CollectionUtils.<Version>emptyList() : CollectionUtils.expand(file.getVersions(), new VersionExpander()); |
| 88 | |
} |
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public Collection<OnlineVersion> getOnlineVersions(Object object) { |
| 92 | 0 | return getVersionsFile(object).getVersions(); |
| 93 | |
} |
| 94 | |
|
| 95 | |
@Override |
| 96 | |
public OnlineVersion getOnlineVersion(Version version, Object object) { |
| 97 | 0 | for (OnlineVersion v : getOnlineVersions(object)) { |
| 98 | 0 | if (v.getVersion().equals(version)) { |
| 99 | 0 | return v; |
| 100 | |
} |
| 101 | |
} |
| 102 | |
|
| 103 | 0 | return null; |
| 104 | |
} |
| 105 | |
|
| 106 | |
@Override |
| 107 | |
public InstallVersion getInstallVersion(String versionFileURL) { |
| 108 | 0 | return new VersionsFileReader().read(versionFileURL).getInstallVersion(); |
| 109 | |
} |
| 110 | |
|
| 111 | |
@Override |
| 112 | |
public Version getMostRecentVersion(Object object) { |
| 113 | 0 | return getVersionsFile(object).getMostRecentVersion().getVersion(); |
| 114 | |
} |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
private VersionsFile getVersionsFile(Object object) { |
| 123 | 0 | if (!cache.containsKey(object)) { |
| 124 | 0 | cache.put(object, new VersionsFileReader().read(object)); |
| 125 | |
|
| 126 | 0 | if (!cache.containsKey(object)) { |
| 127 | 0 | errorManager.addInternationalizedError("error.update.internet"); |
| 128 | |
} |
| 129 | |
} |
| 130 | |
|
| 131 | 0 | return cache.get(object); |
| 132 | |
} |
| 133 | |
} |