Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
| 41 | import java.io.ByteArrayInputStream; |
| 42 | import java.io.File; |
| 43 | import java.io.FileInputStream; |
| 44 | import java.io.FileNotFoundException; |
| 45 | import java.io.IOException; |
| 46 | import java.io.InputStream; |
| 47 | import java.net.URI; |
HIGUCHI Yuta | 436f8d5 | 2015-12-07 21:17:48 -0800 | [diff] [blame] | 48 | import java.nio.charset.StandardCharsets; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 49 | import java.nio.file.NoSuchFileException; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 50 | import java.util.List; |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 51 | import java.util.Locale; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 52 | import java.util.Set; |
| 53 | import java.util.zip.ZipEntry; |
| 54 | import java.util.zip.ZipInputStream; |
| 55 | |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 56 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 57 | import static com.google.common.io.ByteStreams.toByteArray; |
| 58 | import static com.google.common.io.Files.createParentDirs; |
| 59 | import static com.google.common.io.Files.write; |
| 60 | |
| 61 | /** |
| 62 | * Facility for reading application archive stream and managing application |
| 63 | * directory structure. |
| 64 | */ |
| 65 | public class ApplicationArchive |
| 66 | extends AbstractStore<ApplicationEvent, ApplicationStoreDelegate> { |
| 67 | |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 68 | private static Logger log = LoggerFactory.getLogger(ApplicationArchive.class); |
| 69 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 70 | // Magic strings to search for at the beginning of the archive stream |
| 71 | private static final String XML_MAGIC = "<?xml "; |
Brian O'Connor | e4a4f99 | 2016-04-06 22:50:31 -0700 | [diff] [blame] | 72 | private static final String ZIP_MAGIC = "PK"; |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 73 | |
| 74 | // Magic strings to search for and how deep to search it into the archive stream |
| 75 | private static final String APP_MAGIC = "<app "; |
| 76 | private static final int APP_MAGIC_DEPTH = 1024; |
| 77 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 78 | private static final String NAME = "[@name]"; |
| 79 | private static final String ORIGIN = "[@origin]"; |
| 80 | private static final String VERSION = "[@version]"; |
| 81 | private static final String FEATURES_REPO = "[@featuresRepo]"; |
| 82 | private static final String FEATURES = "[@features]"; |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 83 | private static final String APPS = "[@apps]"; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 84 | private static final String DESCRIPTION = "description"; |
| 85 | |
Jian Li | 01b0f595 | 2016-01-20 11:02:07 -0800 | [diff] [blame] | 86 | private static final String UTILITY = "utility"; |
| 87 | |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 88 | private static final String CATEGORY = "[@category]"; |
| 89 | private static final String URL = "[@url]"; |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 90 | private static final String TITLE = "[@title]"; |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 91 | |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 92 | private static final String ROLE = "security.role"; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 93 | private static final String APP_PERMISSIONS = "security.permissions.app-perm"; |
| 94 | private static final String NET_PERMISSIONS = "security.permissions.net-perm"; |
| 95 | private static final String JAVA_PERMISSIONS = "security.permissions.java-perm"; |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 96 | |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 97 | private static final String JAR = ".jar"; |
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 | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 100 | private static final String APP_PNG = "app.png"; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 101 | private static final String M2_PREFIX = "m2"; |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 102 | private static final String FEATURES_XML = "features.xml"; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 103 | |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 104 | private static final String ROOT = "../"; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 105 | private static final String M2_ROOT = "system/"; |
Thomas Vachuska | d5d9bcb | 2015-03-18 17:46:20 -0700 | [diff] [blame] | 106 | private static final String APPS_ROOT = "apps/"; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 107 | |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 108 | private File root = new File(ROOT); |
| 109 | private File appsDir = new File(root, APPS_ROOT); |
| 110 | private File m2Dir = new File(M2_ROOT); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 111 | |
| 112 | /** |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 113 | * Sets the root directory where apps directory is contained. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 114 | * |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 115 | * @param root top-level directory path |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 116 | */ |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 117 | protected void setRootPath(String root) { |
| 118 | this.root = new File(root); |
| 119 | this.appsDir = new File(this.root, APPS_ROOT); |
| 120 | this.m2Dir = new File(M2_ROOT); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 124 | * Returns the root directory where apps directory is contained. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 125 | * |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 126 | * @return top-level directory path |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 127 | */ |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 128 | public String getRootPath() { |
Thomas Vachuska | 40a398b | 2015-04-03 22:26:30 -0700 | [diff] [blame] | 129 | return root.getPath(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns the set of installed application names. |
| 134 | * |
| 135 | * @return installed application names |
| 136 | */ |
| 137 | public Set<String> getApplicationNames() { |
| 138 | ImmutableSet.Builder<String> names = ImmutableSet.builder(); |
| 139 | File[] files = appsDir.listFiles(File::isDirectory); |
| 140 | if (files != null) { |
| 141 | for (File file : files) { |
| 142 | names.add(file.getName()); |
| 143 | } |
| 144 | } |
| 145 | return names.build(); |
| 146 | } |
| 147 | |
| 148 | /** |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 149 | * Returns the timestamp in millis since start of epoch, of when the |
| 150 | * specified application was last modified or changed state. |
| 151 | * |
| 152 | * @param appName application name |
| 153 | * @return number of millis since start of epoch |
| 154 | */ |
| 155 | public long getUpdateTime(String appName) { |
| 156 | return appFile(appName, APP_XML).lastModified(); |
| 157 | } |
| 158 | |
| 159 | /** |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 160 | * Loads the application descriptor from the specified application archive |
| 161 | * stream and saves the stream in the appropriate application archive |
| 162 | * directory. |
| 163 | * |
| 164 | * @param appName application name |
| 165 | * @return application descriptor |
| 166 | * @throws org.onosproject.app.ApplicationException if unable to read application description |
| 167 | */ |
| 168 | public ApplicationDescription getApplicationDescription(String appName) { |
| 169 | try { |
Thomas Vachuska | ad35c34 | 2015-06-11 17:25:36 -0700 | [diff] [blame] | 170 | XMLConfiguration cfg = new XMLConfiguration(); |
| 171 | cfg.setAttributeSplittingDisabled(true); |
| 172 | cfg.setDelimiterParsingDisabled(true); |
| 173 | cfg.load(appFile(appName, APP_XML)); |
| 174 | return loadAppDescription(cfg); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 175 | } catch (Exception e) { |
| 176 | throw new ApplicationException("Unable to get app description", e); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Loads the application descriptor from the specified application archive |
| 182 | * stream and saves the stream in the appropriate application archive |
| 183 | * directory. |
| 184 | * |
| 185 | * @param stream application archive stream |
| 186 | * @return application descriptor |
| 187 | * @throws org.onosproject.app.ApplicationException if unable to read the |
| 188 | * archive stream or store |
| 189 | * the application archive |
| 190 | */ |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 191 | public synchronized ApplicationDescription saveApplication(InputStream stream) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 192 | try (InputStream ais = stream) { |
| 193 | byte[] cache = toByteArray(ais); |
| 194 | InputStream bis = new ByteArrayInputStream(cache); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 195 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 196 | boolean plainXml = isPlainXml(cache); |
| 197 | ApplicationDescription desc = plainXml ? |
| 198 | parsePlainAppDescription(bis) : parseZippedAppDescription(bis); |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 199 | checkState(!appFile(desc.name(), APP_XML).exists(), |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 200 | "Application %s already installed", desc.name()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 201 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 202 | if (plainXml) { |
| 203 | expandPlainApplication(cache, desc); |
| 204 | } else { |
| 205 | bis.reset(); |
Thomas Vachuska | 08b4dec | 2017-08-31 15:20:17 -0700 | [diff] [blame] | 206 | boolean isSelfContainedJar = expandZippedApplication(bis, desc); |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 207 | |
| 208 | if (isSelfContainedJar) { |
| 209 | bis.reset(); |
| 210 | stageSelfContainedJar(bis, desc); |
| 211 | } |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 212 | |
| 213 | bis.reset(); |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 214 | saveApplication(bis, desc, isSelfContainedJar); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 217 | installArtifacts(desc); |
| 218 | return desc; |
| 219 | } catch (IOException e) { |
| 220 | throw new ApplicationException("Unable to save application", e); |
| 221 | } |
| 222 | } |
| 223 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 224 | // Indicates whether the stream encoded in the given bytes is plain XML. |
| 225 | private boolean isPlainXml(byte[] bytes) { |
Brian O'Connor | e4a4f99 | 2016-04-06 22:50:31 -0700 | [diff] [blame] | 226 | return !substring(bytes, ZIP_MAGIC.length()).equals(ZIP_MAGIC) && |
| 227 | (substring(bytes, XML_MAGIC.length()).equals(XML_MAGIC) || |
| 228 | substring(bytes, APP_MAGIC_DEPTH).contains(APP_MAGIC)); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Returns the substring of maximum possible length from the specified bytes. |
| 232 | private String substring(byte[] bytes, int length) { |
HIGUCHI Yuta | 436f8d5 | 2015-12-07 21:17:48 -0800 | [diff] [blame] | 233 | 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] | 234 | } |
| 235 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 236 | /** |
| 237 | * Purges the application archive directory. |
| 238 | * |
| 239 | * @param appName application name |
| 240 | */ |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 241 | public synchronized void purgeApplication(String appName) { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 242 | File appDir = new File(appsDir, appName); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 243 | try { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 244 | Tools.removeDirectory(appDir); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 245 | } catch (IOException e) { |
| 246 | throw new ApplicationException("Unable to purge application " + appName, e); |
| 247 | } |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 248 | if (appDir.exists()) { |
| 249 | throw new ApplicationException("Unable to purge application " + appName); |
| 250 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 254 | * Returns application archive stream for the specified application. This |
Thomas Vachuska | 08b4dec | 2017-08-31 15:20:17 -0700 | [diff] [blame] | 255 | * will be either the application OAR file, JAR file or the plain XML file. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 256 | * |
| 257 | * @param appName application name |
| 258 | * @return application archive stream |
| 259 | */ |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 260 | public synchronized InputStream getApplicationInputStream(String appName) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 261 | try { |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 262 | File appFile = appFile(appName, appName + OAR); |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 263 | if (!appFile.exists()) { |
| 264 | appFile = appFile(appName, appName + JAR); |
| 265 | } |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 266 | return new FileInputStream(appFile.exists() ? appFile : appFile(appName, APP_XML)); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 267 | } catch (FileNotFoundException e) { |
| 268 | throw new ApplicationException("Application " + appName + " not found"); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // Scans the specified ZIP stream for app.xml entry and parses it producing |
| 273 | // an application descriptor. |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 274 | private ApplicationDescription parseZippedAppDescription(InputStream stream) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 275 | throws IOException { |
| 276 | try (ZipInputStream zis = new ZipInputStream(stream)) { |
| 277 | ZipEntry entry; |
| 278 | while ((entry = zis.getNextEntry()) != null) { |
| 279 | if (entry.getName().equals(APP_XML)) { |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 280 | byte[] data = ByteStreams.toByteArray(zis); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 281 | return parsePlainAppDescription(new ByteArrayInputStream(data)); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 282 | } |
| 283 | zis.closeEntry(); |
| 284 | } |
| 285 | } |
| 286 | throw new IOException("Unable to locate " + APP_XML); |
| 287 | } |
| 288 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 289 | // Scans the specified XML stream and parses it producing an application descriptor. |
| 290 | private ApplicationDescription parsePlainAppDescription(InputStream stream) |
| 291 | throws IOException { |
| 292 | XMLConfiguration cfg = new XMLConfiguration(); |
Thomas Vachuska | ad35c34 | 2015-06-11 17:25:36 -0700 | [diff] [blame] | 293 | cfg.setAttributeSplittingDisabled(true); |
| 294 | cfg.setDelimiterParsingDisabled(true); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 295 | try { |
| 296 | cfg.load(stream); |
| 297 | return loadAppDescription(cfg); |
| 298 | } catch (ConfigurationException e) { |
| 299 | throw new IOException("Unable to parse " + APP_XML, e); |
| 300 | } |
| 301 | } |
| 302 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 303 | private ApplicationDescription loadAppDescription(XMLConfiguration cfg) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 304 | String name = cfg.getString(NAME); |
| 305 | Version version = Version.version(cfg.getString(VERSION)); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 306 | String origin = cfg.getString(ORIGIN); |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 307 | |
| 308 | String title = cfg.getString(TITLE); |
| 309 | // FIXME: title should be set as attribute to APP, but fallback for now... |
| 310 | title = title == null ? name : title; |
| 311 | |
Jian Li | 01b0f595 | 2016-01-20 11:02:07 -0800 | [diff] [blame] | 312 | String category = cfg.getString(CATEGORY, UTILITY); |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 313 | String url = cfg.getString(URL); |
| 314 | byte[] icon = getApplicationIcon(name); |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 315 | ApplicationRole role = getRole(cfg.getString(ROLE)); |
| 316 | Set<Permission> perms = getPermissions(cfg); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 317 | String featRepo = cfg.getString(FEATURES_REPO); |
| 318 | URI featuresRepo = featRepo != null ? URI.create(featRepo) : null; |
Thomas Vachuska | ad35c34 | 2015-06-11 17:25:36 -0700 | [diff] [blame] | 319 | List<String> features = ImmutableList.copyOf(cfg.getString(FEATURES).split(",")); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 320 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 321 | String apps = cfg.getString(APPS, ""); |
| 322 | List<String> requiredApps = apps.isEmpty() ? |
| 323 | ImmutableList.of() : ImmutableList.copyOf(apps.split(",")); |
| 324 | |
Jian Li | 8bcb4f2 | 2016-01-20 10:36:18 -0800 | [diff] [blame] | 325 | // put full description to readme field |
| 326 | String readme = cfg.getString(DESCRIPTION); |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 327 | |
Jian Li | 8bcb4f2 | 2016-01-20 10:36:18 -0800 | [diff] [blame] | 328 | // put short description to description field |
| 329 | String desc = compactDescription(readme); |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 330 | |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 331 | return DefaultApplicationDescription.builder() |
| 332 | .withName(name) |
| 333 | .withVersion(version) |
| 334 | .withTitle(title) |
| 335 | .withDescription(desc) |
| 336 | .withOrigin(origin) |
| 337 | .withCategory(category) |
| 338 | .withUrl(url) |
| 339 | .withReadme(readme) |
| 340 | .withIcon(icon) |
| 341 | .withRole(role) |
| 342 | .withPermissions(perms) |
| 343 | .withFeaturesRepo(featuresRepo) |
| 344 | .withFeatures(features) |
| 345 | .withRequiredApps(requiredApps) |
| 346 | .build(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // Expands the specified ZIP stream into app-specific directory. |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 350 | // Returns true of the application is a self-contained jar rather than an oar file. |
| 351 | private boolean expandZippedApplication(InputStream stream, ApplicationDescription desc) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 352 | throws IOException { |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 353 | boolean isSelfContained = false; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 354 | ZipInputStream zis = new ZipInputStream(stream); |
| 355 | ZipEntry entry; |
| 356 | File appDir = new File(appsDir, desc.name()); |
| 357 | while ((entry = zis.getNextEntry()) != null) { |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 358 | if (!entry.isDirectory()) { |
| 359 | byte[] data = ByteStreams.toByteArray(zis); |
| 360 | zis.closeEntry(); |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 361 | File file = new File(appDir, entry.getName()); |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 362 | if (isTopLevel(file)) { |
| 363 | createParentDirs(file); |
| 364 | write(data, file); |
| 365 | } else { |
| 366 | isSelfContained = true; |
| 367 | } |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 368 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 369 | } |
| 370 | zis.close(); |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 371 | return isSelfContained; |
| 372 | } |
| 373 | |
| 374 | // Returns true if the specified file is a top-level app file, i.e. app.xml, |
| 375 | // features.xml, .jar or a directory; false if anything else. |
| 376 | private boolean isTopLevel(File file) { |
| 377 | String name = file.getName(); |
| 378 | return name.equals(APP_XML) || name.endsWith(FEATURES_XML) || name.endsWith(JAR) || file.isDirectory(); |
| 379 | } |
| 380 | |
| 381 | // Expands the self-contained JAR stream into the app-specific directory, |
| 382 | // using the bundle coordinates retrieved from the features.xml file. |
| 383 | private void stageSelfContainedJar(InputStream stream, ApplicationDescription desc) |
| 384 | throws IOException { |
| 385 | // First extract the bundle coordinates |
| 386 | String coords = getSelfContainedBundleCoordinates(desc); |
| 387 | if (coords == null) { |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | // Split the coordinates into segments and build the file name. |
| 392 | String[] f = coords.substring(4).split("/"); |
| 393 | String base = "m2/" + f[0].replace('.', '/') + "/" + f[1] + "/" + f[2] + "/" + f[1] + "-" + f[2]; |
| 394 | String jarName = base + (f.length < 4 ? "" : "-" + f[3]) + ".jar"; |
| 395 | String featuresName = base + "-features.xml"; |
| 396 | |
| 397 | // Create the file directory structure and copy the file there. |
| 398 | File jar = appFile(desc.name(), jarName); |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 399 | boolean ok = jar.getParentFile().exists() || jar.getParentFile().mkdirs(); |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 400 | if (ok) { |
| 401 | Files.write(toByteArray(stream), jar); |
| 402 | Files.copy(appFile(desc.name(), FEATURES_XML), appFile(desc.name(), featuresName)); |
| 403 | if (!appFile(desc.name(), FEATURES_XML).delete()) { |
| 404 | log.warn("Unable to delete self-contained application {} features.xml", desc.name()); |
| 405 | } |
| 406 | } else { |
| 407 | throw new IOException("Unable to save self-contained application " + desc.name()); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Returns the bundle coordinates from the features.xml file. |
| 412 | private String getSelfContainedBundleCoordinates(ApplicationDescription desc) { |
| 413 | try { |
| 414 | XMLConfiguration cfg = new XMLConfiguration(); |
| 415 | cfg.setAttributeSplittingDisabled(true); |
| 416 | cfg.setDelimiterParsingDisabled(true); |
| 417 | cfg.load(appFile(desc.name(), FEATURES_XML)); |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 418 | return cfg.getString("feature.bundle") |
| 419 | .replaceFirst("wrap:", "") |
| 420 | .replaceFirst("\\$Bundle-.*$", ""); |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 421 | } catch (ConfigurationException e) { |
| 422 | log.warn("Self-contained application {} has no features.xml", desc.name()); |
| 423 | return null; |
| 424 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 427 | // Saves the specified XML stream into app-specific directory. |
| 428 | private void expandPlainApplication(byte[] stream, ApplicationDescription desc) |
| 429 | throws IOException { |
| 430 | File file = appFile(desc.name(), APP_XML); |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 431 | checkState(!file.getParentFile().exists(), "Application already installed"); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 432 | createParentDirs(file); |
| 433 | write(stream, file); |
| 434 | } |
| 435 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 436 | // Saves the specified ZIP stream into a file under app-specific directory. |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 437 | private void saveApplication(InputStream stream, ApplicationDescription desc, |
| 438 | boolean isSelfContainedJar) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 439 | throws IOException { |
Thomas Vachuska | 6cc7488 | 2017-01-10 16:01:02 -0800 | [diff] [blame] | 440 | String name = desc.name() + (isSelfContainedJar ? JAR : OAR); |
| 441 | Files.write(toByteArray(stream), appFile(desc.name(), name)); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // Installs application artifacts into M2 repository. |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 445 | private void installArtifacts(ApplicationDescription desc) throws IOException { |
| 446 | try { |
| 447 | Tools.copyDirectory(appFile(desc.name(), M2_PREFIX), m2Dir); |
| 448 | } catch (NoSuchFileException e) { |
| 449 | log.debug("Application {} has no M2 artifacts", desc.name()); |
| 450 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 453 | /** |
| 454 | * Marks the app as active by creating token file in the app directory. |
| 455 | * |
| 456 | * @param appName application name |
| 457 | * @return true if file was created |
| 458 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 459 | protected boolean setActive(String appName) { |
| 460 | try { |
Thomas Vachuska | e965b3d | 2016-03-03 11:42:48 -0800 | [diff] [blame] | 461 | File active = appFile(appName, "active"); |
| 462 | createParentDirs(active); |
| 463 | return active.createNewFile() && updateTime(appName); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 464 | } catch (IOException e) { |
Thomas Vachuska | e965b3d | 2016-03-03 11:42:48 -0800 | [diff] [blame] | 465 | log.warn("Unable to mark app {} as active", appName, e); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 466 | throw new ApplicationException("Unable to mark app as active", e); |
| 467 | } |
| 468 | } |
| 469 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 470 | /** |
| 471 | * Clears the app as active by deleting token file in the app directory. |
| 472 | * |
| 473 | * @param appName application name |
| 474 | * @return true if file was deleted |
| 475 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 476 | protected boolean clearActive(String appName) { |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 477 | return appFile(appName, "active").delete() && updateTime(appName); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Updates the time-stamp of the app descriptor file. |
| 482 | * |
| 483 | * @param appName application name |
| 484 | * @return true if the app descriptor was updated |
| 485 | */ |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 486 | protected boolean updateTime(String appName) { |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 487 | return appFile(appName, APP_XML).setLastModified(System.currentTimeMillis()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 490 | /** |
| 491 | * Indicates whether the app was marked as active by checking for token file. |
| 492 | * |
| 493 | * @param appName application name |
| 494 | * @return true if the app is marked as active |
| 495 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 496 | protected boolean isActive(String appName) { |
| 497 | return appFile(appName, "active").exists(); |
| 498 | } |
| 499 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 500 | // Returns the name of the file located under the specified app directory. |
| 501 | private File appFile(String appName, String fileName) { |
| 502 | return new File(new File(appsDir, appName), fileName); |
| 503 | } |
| 504 | |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 505 | // Returns the icon file located under the specified app directory. |
| 506 | private File iconFile(String appName, String fileName) { |
| 507 | return new File(new File(appsDir, appName), fileName); |
| 508 | } |
| 509 | |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 510 | // Returns the set of Permissions specified in the app.xml file |
| 511 | private ImmutableSet<Permission> getPermissions(XMLConfiguration cfg) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 512 | List<Permission> permissionList = Lists.newArrayList(); |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 513 | |
| 514 | for (Object o : cfg.getList(APP_PERMISSIONS)) { |
Changhoon Yoon | a7841ed | 2015-05-15 02:51:08 +0900 | [diff] [blame] | 515 | String name = (String) o; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 516 | permissionList.add(new Permission(AppPermission.class.getName(), name)); |
| 517 | } |
| 518 | for (Object o : cfg.getList(NET_PERMISSIONS)) { |
| 519 | //TODO: TO BE FLESHED OUT WHEN NETWORK PERMISSIONS ARE SUPPORTED |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | List<HierarchicalConfiguration> fields = |
| 524 | cfg.configurationsAt(JAVA_PERMISSIONS); |
| 525 | for (HierarchicalConfiguration sub : fields) { |
| 526 | String classname = sub.getString("classname"); |
| 527 | String name = sub.getString("name"); |
| 528 | String actions = sub.getString("actions"); |
| 529 | |
| 530 | if (classname != null && name != null) { |
| 531 | permissionList.add(new Permission(classname, name, actions)); |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 532 | } |
| 533 | } |
Changhoon Yoon | a7841ed | 2015-05-15 02:51:08 +0900 | [diff] [blame] | 534 | return ImmutableSet.copyOf(permissionList); |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 535 | } |
| 536 | |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 537 | // Returns the byte stream from icon.png file in oar application archive. |
| 538 | private byte[] getApplicationIcon(String appName) { |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 539 | File iconFile = iconFile(appName, APP_PNG); |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 540 | try { |
Brian O'Connor | a7903ae | 2016-04-29 15:02:32 -0700 | [diff] [blame] | 541 | final InputStream iconStream; |
| 542 | if (iconFile.exists()) { |
| 543 | iconStream = new FileInputStream(iconFile); |
| 544 | } else { |
| 545 | // assume that we can always fallback to default icon |
| 546 | iconStream = ApplicationArchive.class.getResourceAsStream("/" + APP_PNG); |
| 547 | } |
| 548 | return ByteStreams.toByteArray(iconStream); |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 549 | } catch (IOException e) { |
Brian O'Connor | a7903ae | 2016-04-29 15:02:32 -0700 | [diff] [blame] | 550 | log.warn("Unable to read app icon for app {}", appName, e); |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 551 | } |
Brian O'Connor | a7903ae | 2016-04-29 15:02:32 -0700 | [diff] [blame] | 552 | return new byte[0]; |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 555 | // Returns application role type |
| 556 | public ApplicationRole getRole(String value) { |
| 557 | if (value == null) { |
| 558 | return ApplicationRole.UNSPECIFIED; |
| 559 | } else { |
| 560 | try { |
| 561 | return ApplicationRole.valueOf(value.toUpperCase(Locale.ENGLISH)); |
| 562 | } catch (IllegalArgumentException e) { |
| 563 | log.debug("Unknown role value: %s", value); |
| 564 | return ApplicationRole.UNSPECIFIED; |
| 565 | } |
| 566 | } |
| 567 | } |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 568 | |
| 569 | // Returns the first sentence of the given sentence |
| 570 | private String compactDescription(String sentence) { |
| 571 | if (StringUtils.isNotEmpty(sentence)) { |
| 572 | if (StringUtils.contains(sentence, ".")) { |
| 573 | return StringUtils.substringBefore(sentence, ".") + "."; |
| 574 | } else { |
Jian Li | 8bcb4f2 | 2016-01-20 10:36:18 -0800 | [diff] [blame] | 575 | return sentence; |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | return sentence; |
| 579 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 580 | } |