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