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