Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.common.app; |
| 17 | |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.io.ByteStreams; |
| 21 | import com.google.common.io.Files; |
| 22 | import org.apache.commons.configuration.ConfigurationException; |
| 23 | import org.apache.commons.configuration.XMLConfiguration; |
| 24 | import org.onlab.util.Tools; |
| 25 | import org.onosproject.app.ApplicationDescription; |
| 26 | import org.onosproject.app.ApplicationEvent; |
| 27 | import org.onosproject.app.ApplicationException; |
| 28 | import org.onosproject.app.ApplicationStoreDelegate; |
| 29 | import org.onosproject.app.DefaultApplicationDescription; |
| 30 | import org.onosproject.core.Permission; |
| 31 | import org.onosproject.core.Version; |
| 32 | import org.onosproject.store.AbstractStore; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.io.ByteArrayInputStream; |
| 37 | import java.io.File; |
| 38 | import java.io.FileInputStream; |
| 39 | import java.io.FileNotFoundException; |
| 40 | import java.io.IOException; |
| 41 | import java.io.InputStream; |
| 42 | import java.net.URI; |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 43 | import java.nio.charset.Charset; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 44 | import java.nio.file.NoSuchFileException; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 45 | import java.util.List; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 46 | import java.util.Set; |
| 47 | import java.util.zip.ZipEntry; |
| 48 | import java.util.zip.ZipInputStream; |
| 49 | |
| 50 | import static com.google.common.io.ByteStreams.toByteArray; |
| 51 | import static com.google.common.io.Files.createParentDirs; |
| 52 | import static com.google.common.io.Files.write; |
| 53 | |
| 54 | /** |
| 55 | * Facility for reading application archive stream and managing application |
| 56 | * directory structure. |
| 57 | */ |
| 58 | public class ApplicationArchive |
| 59 | extends AbstractStore<ApplicationEvent, ApplicationStoreDelegate> { |
| 60 | |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame^] | 61 | private static Logger log = LoggerFactory.getLogger(ApplicationArchive.class); |
| 62 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 63 | // Magic strings to search for at the beginning of the archive stream |
| 64 | private static final String XML_MAGIC = "<?xml "; |
| 65 | |
| 66 | // Magic strings to search for and how deep to search it into the archive stream |
| 67 | private static final String APP_MAGIC = "<app "; |
| 68 | private static final int APP_MAGIC_DEPTH = 1024; |
| 69 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 70 | private static final String NAME = "[@name]"; |
| 71 | private static final String ORIGIN = "[@origin]"; |
| 72 | private static final String VERSION = "[@version]"; |
| 73 | private static final String FEATURES_REPO = "[@featuresRepo]"; |
| 74 | private static final String FEATURES = "[@features]"; |
| 75 | private static final String DESCRIPTION = "description"; |
| 76 | |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame^] | 77 | private static final String OAR = ".oar"; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 78 | private static final String APP_XML = "app.xml"; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 79 | private static final String M2_PREFIX = "m2"; |
| 80 | |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 81 | private static final String ROOT = "../"; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 82 | private static final String M2_ROOT = "system/"; |
Thomas Vachuska | d5d9bcb | 2015-03-18 17:46:20 -0700 | [diff] [blame] | 83 | private static final String APPS_ROOT = "apps/"; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 84 | |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 85 | private File root = new File(ROOT); |
| 86 | private File appsDir = new File(root, APPS_ROOT); |
| 87 | private File m2Dir = new File(M2_ROOT); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 90 | * Sets the root directory where apps directory is contained. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 91 | * |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 92 | * @param root top-level directory path |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 93 | */ |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 94 | protected void setRootPath(String root) { |
| 95 | this.root = new File(root); |
| 96 | this.appsDir = new File(this.root, APPS_ROOT); |
| 97 | this.m2Dir = new File(M2_ROOT); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 101 | * Returns the root directory where apps directory is contained. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 102 | * |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 103 | * @return top-level directory path |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 104 | */ |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 105 | protected String getRootPath() { |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 106 | return root.getPath(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the set of installed application names. |
| 111 | * |
| 112 | * @return installed application names |
| 113 | */ |
| 114 | public Set<String> getApplicationNames() { |
| 115 | ImmutableSet.Builder<String> names = ImmutableSet.builder(); |
| 116 | File[] files = appsDir.listFiles(File::isDirectory); |
| 117 | if (files != null) { |
| 118 | for (File file : files) { |
| 119 | names.add(file.getName()); |
| 120 | } |
| 121 | } |
| 122 | return names.build(); |
| 123 | } |
| 124 | |
| 125 | /** |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 126 | * Returns the timestamp in millis since start of epoch, of when the |
| 127 | * specified application was last modified or changed state. |
| 128 | * |
| 129 | * @param appName application name |
| 130 | * @return number of millis since start of epoch |
| 131 | */ |
| 132 | public long getUpdateTime(String appName) { |
| 133 | return appFile(appName, APP_XML).lastModified(); |
| 134 | } |
| 135 | |
| 136 | /** |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 137 | * Loads the application descriptor from the specified application archive |
| 138 | * stream and saves the stream in the appropriate application archive |
| 139 | * directory. |
| 140 | * |
| 141 | * @param appName application name |
| 142 | * @return application descriptor |
| 143 | * @throws org.onosproject.app.ApplicationException if unable to read application description |
| 144 | */ |
| 145 | public ApplicationDescription getApplicationDescription(String appName) { |
| 146 | try { |
| 147 | return loadAppDescription(new XMLConfiguration(appFile(appName, APP_XML))); |
| 148 | } catch (Exception e) { |
| 149 | throw new ApplicationException("Unable to get app description", e); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Loads the application descriptor from the specified application archive |
| 155 | * stream and saves the stream in the appropriate application archive |
| 156 | * directory. |
| 157 | * |
| 158 | * @param stream application archive stream |
| 159 | * @return application descriptor |
| 160 | * @throws org.onosproject.app.ApplicationException if unable to read the |
| 161 | * archive stream or store |
| 162 | * the application archive |
| 163 | */ |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 164 | public synchronized ApplicationDescription saveApplication(InputStream stream) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 165 | try (InputStream ais = stream) { |
| 166 | byte[] cache = toByteArray(ais); |
| 167 | InputStream bis = new ByteArrayInputStream(cache); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 168 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 169 | boolean plainXml = isPlainXml(cache); |
| 170 | ApplicationDescription desc = plainXml ? |
| 171 | parsePlainAppDescription(bis) : parseZippedAppDescription(bis); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 172 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 173 | if (plainXml) { |
| 174 | expandPlainApplication(cache, desc); |
| 175 | } else { |
| 176 | bis.reset(); |
| 177 | expandZippedApplication(bis, desc); |
| 178 | |
| 179 | bis.reset(); |
| 180 | saveApplication(bis, desc); |
| 181 | } |
| 182 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 183 | installArtifacts(desc); |
| 184 | return desc; |
| 185 | } catch (IOException e) { |
| 186 | throw new ApplicationException("Unable to save application", e); |
| 187 | } |
| 188 | } |
| 189 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 190 | // Indicates whether the stream encoded in the given bytes is plain XML. |
| 191 | private boolean isPlainXml(byte[] bytes) { |
| 192 | return substring(bytes, XML_MAGIC.length()).equals(XML_MAGIC) || |
| 193 | substring(bytes, APP_MAGIC_DEPTH).contains(APP_MAGIC); |
| 194 | } |
| 195 | |
| 196 | // Returns the substring of maximum possible length from the specified bytes. |
| 197 | private String substring(byte[] bytes, int length) { |
| 198 | return new String(bytes, 0, Math.min(bytes.length, length), Charset.forName("UTF-8")); |
| 199 | } |
| 200 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 201 | /** |
| 202 | * Purges the application archive directory. |
| 203 | * |
| 204 | * @param appName application name |
| 205 | */ |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 206 | public synchronized void purgeApplication(String appName) { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 207 | File appDir = new File(appsDir, appName); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 208 | try { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 209 | Tools.removeDirectory(appDir); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 210 | } catch (IOException e) { |
| 211 | throw new ApplicationException("Unable to purge application " + appName, e); |
| 212 | } |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 213 | if (appDir.exists()) { |
| 214 | throw new ApplicationException("Unable to purge application " + appName); |
| 215 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 219 | * Returns application archive stream for the specified application. This |
| 220 | * will be either the application ZIP file or the application XML file. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 221 | * |
| 222 | * @param appName application name |
| 223 | * @return application archive stream |
| 224 | */ |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 225 | public synchronized InputStream getApplicationInputStream(String appName) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 226 | try { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame^] | 227 | File appFile = appFile(appName, appName + OAR); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 228 | return new FileInputStream(appFile.exists() ? appFile : appFile(appName, APP_XML)); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 229 | } catch (FileNotFoundException e) { |
| 230 | throw new ApplicationException("Application " + appName + " not found"); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Scans the specified ZIP stream for app.xml entry and parses it producing |
| 235 | // an application descriptor. |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 236 | private ApplicationDescription parseZippedAppDescription(InputStream stream) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 237 | throws IOException { |
| 238 | try (ZipInputStream zis = new ZipInputStream(stream)) { |
| 239 | ZipEntry entry; |
| 240 | while ((entry = zis.getNextEntry()) != null) { |
| 241 | if (entry.getName().equals(APP_XML)) { |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 242 | byte[] data = ByteStreams.toByteArray(zis); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 243 | return parsePlainAppDescription(new ByteArrayInputStream(data)); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 244 | } |
| 245 | zis.closeEntry(); |
| 246 | } |
| 247 | } |
| 248 | throw new IOException("Unable to locate " + APP_XML); |
| 249 | } |
| 250 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 251 | // Scans the specified XML stream and parses it producing an application descriptor. |
| 252 | private ApplicationDescription parsePlainAppDescription(InputStream stream) |
| 253 | throws IOException { |
| 254 | XMLConfiguration cfg = new XMLConfiguration(); |
| 255 | try { |
| 256 | cfg.load(stream); |
| 257 | return loadAppDescription(cfg); |
| 258 | } catch (ConfigurationException e) { |
| 259 | throw new IOException("Unable to parse " + APP_XML, e); |
| 260 | } |
| 261 | } |
| 262 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 263 | private ApplicationDescription loadAppDescription(XMLConfiguration cfg) { |
| 264 | cfg.setAttributeSplittingDisabled(true); |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 265 | cfg.setDelimiterParsingDisabled(true); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 266 | String name = cfg.getString(NAME); |
| 267 | Version version = Version.version(cfg.getString(VERSION)); |
| 268 | String desc = cfg.getString(DESCRIPTION); |
| 269 | String origin = cfg.getString(ORIGIN); |
| 270 | Set<Permission> perms = ImmutableSet.of(); |
| 271 | String featRepo = cfg.getString(FEATURES_REPO); |
| 272 | URI featuresRepo = featRepo != null ? URI.create(featRepo) : null; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 273 | List<String> features = ImmutableList.copyOf(cfg.getStringArray(FEATURES)); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 274 | |
| 275 | return new DefaultApplicationDescription(name, version, desc, origin, |
| 276 | perms, featuresRepo, features); |
| 277 | } |
| 278 | |
| 279 | // Expands the specified ZIP stream into app-specific directory. |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 280 | private void expandZippedApplication(InputStream stream, ApplicationDescription desc) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 281 | throws IOException { |
| 282 | ZipInputStream zis = new ZipInputStream(stream); |
| 283 | ZipEntry entry; |
| 284 | File appDir = new File(appsDir, desc.name()); |
| 285 | while ((entry = zis.getNextEntry()) != null) { |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 286 | if (!entry.isDirectory()) { |
| 287 | byte[] data = ByteStreams.toByteArray(zis); |
| 288 | zis.closeEntry(); |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 289 | File file = new File(appDir, entry.getName()); |
| 290 | createParentDirs(file); |
| 291 | write(data, file); |
| 292 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 293 | } |
| 294 | zis.close(); |
| 295 | } |
| 296 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 297 | // Saves the specified XML stream into app-specific directory. |
| 298 | private void expandPlainApplication(byte[] stream, ApplicationDescription desc) |
| 299 | throws IOException { |
| 300 | File file = appFile(desc.name(), APP_XML); |
| 301 | createParentDirs(file); |
| 302 | write(stream, file); |
| 303 | } |
| 304 | |
| 305 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 306 | // Saves the specified ZIP stream into a file under app-specific directory. |
| 307 | private void saveApplication(InputStream stream, ApplicationDescription desc) |
| 308 | throws IOException { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame^] | 309 | Files.write(toByteArray(stream), appFile(desc.name(), desc.name() + OAR)); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // Installs application artifacts into M2 repository. |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 313 | private void installArtifacts(ApplicationDescription desc) throws IOException { |
| 314 | try { |
| 315 | Tools.copyDirectory(appFile(desc.name(), M2_PREFIX), m2Dir); |
| 316 | } catch (NoSuchFileException e) { |
| 317 | log.debug("Application {} has no M2 artifacts", desc.name()); |
| 318 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 321 | /** |
| 322 | * Marks the app as active by creating token file in the app directory. |
| 323 | * |
| 324 | * @param appName application name |
| 325 | * @return true if file was created |
| 326 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 327 | protected boolean setActive(String appName) { |
| 328 | try { |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 329 | return appFile(appName, "active").createNewFile() && updateTime(appName); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 330 | } catch (IOException e) { |
| 331 | throw new ApplicationException("Unable to mark app as active", e); |
| 332 | } |
| 333 | } |
| 334 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 335 | /** |
| 336 | * Clears the app as active by deleting token file in the app directory. |
| 337 | * |
| 338 | * @param appName application name |
| 339 | * @return true if file was deleted |
| 340 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 341 | protected boolean clearActive(String appName) { |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 342 | return appFile(appName, "active").delete() && updateTime(appName); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Updates the time-stamp of the app descriptor file. |
| 347 | * |
| 348 | * @param appName application name |
| 349 | * @return true if the app descriptor was updated |
| 350 | */ |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 351 | protected boolean updateTime(String appName) { |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 352 | return appFile(appName, APP_XML).setLastModified(System.currentTimeMillis()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 355 | /** |
| 356 | * Indicates whether the app was marked as active by checking for token file. |
| 357 | * |
| 358 | * @param appName application name |
| 359 | * @return true if the app is marked as active |
| 360 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 361 | protected boolean isActive(String appName) { |
| 362 | return appFile(appName, "active").exists(); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | // Returns the name of the file located under the specified app directory. |
| 367 | private File appFile(String appName, String fileName) { |
| 368 | return new File(new File(appsDir, appName), fileName); |
| 369 | } |
| 370 | |
| 371 | } |