blob: 3699b5d054a9810fadb86ca62f414458e0aface3 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Jian Lic35415d2016-01-14 17:22:31 -08002 * Copyright 2015-2016 Open Networking Laboratory
Thomas Vachuska02aeb032015-01-06 22:36:30 -08003 *
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 */
16package org.onosproject.common.app;
17
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080018import com.google.common.collect.ImmutableList;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080019import com.google.common.collect.ImmutableSet;
Thomas Vachuska761f0042015-11-11 19:10:17 -080020import com.google.common.collect.Lists;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080021import com.google.common.io.ByteStreams;
22import com.google.common.io.Files;
23import org.apache.commons.configuration.ConfigurationException;
Changhoon Yoonb856b812015-08-10 03:47:19 +090024import org.apache.commons.configuration.HierarchicalConfiguration;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080025import org.apache.commons.configuration.XMLConfiguration;
Jian Lic35415d2016-01-14 17:22:31 -080026import org.apache.commons.lang.StringUtils;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080027import org.onlab.util.Tools;
28import org.onosproject.app.ApplicationDescription;
29import org.onosproject.app.ApplicationEvent;
30import org.onosproject.app.ApplicationException;
31import org.onosproject.app.ApplicationStoreDelegate;
32import org.onosproject.app.DefaultApplicationDescription;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090033import org.onosproject.core.ApplicationRole;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080034import org.onosproject.core.Version;
Changhoon Yoonb856b812015-08-10 03:47:19 +090035import org.onosproject.security.AppPermission;
36import org.onosproject.security.Permission;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080037import org.onosproject.store.AbstractStore;
38import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
Jian Lic35415d2016-01-14 17:22:31 -080041import javax.imageio.ImageIO;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080042import java.io.ByteArrayInputStream;
Jian Li97d6b2d2016-01-20 10:13:43 -080043import java.io.ByteArrayOutputStream;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080044import java.io.File;
45import java.io.FileInputStream;
46import java.io.FileNotFoundException;
47import java.io.IOException;
48import java.io.InputStream;
49import java.net.URI;
HIGUCHI Yuta436f8d52015-12-07 21:17:48 -080050import java.nio.charset.StandardCharsets;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080051import java.nio.file.NoSuchFileException;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080052import java.util.List;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090053import java.util.Locale;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080054import java.util.Set;
55import java.util.zip.ZipEntry;
56import java.util.zip.ZipInputStream;
57
Thomas Vachuskae18a3302015-06-23 12:48:28 -070058import static com.google.common.base.Preconditions.checkState;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080059import static com.google.common.io.ByteStreams.toByteArray;
60import static com.google.common.io.Files.createParentDirs;
61import static com.google.common.io.Files.write;
62
63/**
64 * Facility for reading application archive stream and managing application
65 * directory structure.
66 */
67public class ApplicationArchive
68 extends AbstractStore<ApplicationEvent, ApplicationStoreDelegate> {
69
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070070 private static Logger log = LoggerFactory.getLogger(ApplicationArchive.class);
71
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080072 // Magic strings to search for at the beginning of the archive stream
73 private static final String XML_MAGIC = "<?xml ";
74
75 // Magic strings to search for and how deep to search it into the archive stream
76 private static final String APP_MAGIC = "<app ";
77 private static final int APP_MAGIC_DEPTH = 1024;
78
Thomas Vachuska02aeb032015-01-06 22:36:30 -080079 private static final String NAME = "[@name]";
80 private static final String ORIGIN = "[@origin]";
81 private static final String VERSION = "[@version]";
82 private static final String FEATURES_REPO = "[@featuresRepo]";
83 private static final String FEATURES = "[@features]";
Thomas Vachuska761f0042015-11-11 19:10:17 -080084 private static final String APPS = "[@apps]";
Thomas Vachuska02aeb032015-01-06 22:36:30 -080085 private static final String DESCRIPTION = "description";
86
Jian Li01b0f5952016-01-20 11:02:07 -080087 private static final String UTILITY = "utility";
88
Jian Lic35415d2016-01-14 17:22:31 -080089 private static final String CATEGORY = "[@category]";
90 private static final String URL = "[@url]";
Simon Huntafae2f72016-03-04 21:18:23 -080091 private static final String TITLE = "[@title]";
Jian Lic35415d2016-01-14 17:22:31 -080092
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090093 private static final String ROLE = "security.role";
Changhoon Yoonb856b812015-08-10 03:47:19 +090094 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 Yoonbdeb88a2015-05-12 20:35:31 +090097
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070098 private static final String OAR = ".oar";
Jian Li97d6b2d2016-01-20 10:13:43 -080099 private static final String PNG = "png";
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800100 private static final String APP_XML = "app.xml";
Jian Li97d6b2d2016-01-20 10:13:43 -0800101 private static final String APP_PNG = "app.png";
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800102 private static final String M2_PREFIX = "m2";
103
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700104 private static final String ROOT = "../";
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800105 private static final String M2_ROOT = "system/";
Thomas Vachuskad5d9bcb2015-03-18 17:46:20 -0700106 private static final String APPS_ROOT = "apps/";
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800107
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700108 private File root = new File(ROOT);
109 private File appsDir = new File(root, APPS_ROOT);
110 private File m2Dir = new File(M2_ROOT);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800111
112 /**
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700113 * Sets the root directory where apps directory is contained.
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800114 *
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700115 * @param root top-level directory path
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800116 */
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700117 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 Vachuska02aeb032015-01-06 22:36:30 -0800121 }
122
123 /**
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700124 * Returns the root directory where apps directory is contained.
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800125 *
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700126 * @return top-level directory path
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800127 */
Thomas Vachuskae18a3302015-06-23 12:48:28 -0700128 public String getRootPath() {
Thomas Vachuska40a398b2015-04-03 22:26:30 -0700129 return root.getPath();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800130 }
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 Vachuskacf960112015-03-06 22:36:51 -0800149 * 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 Vachuska02aeb032015-01-06 22:36:30 -0800160 * 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 Vachuskaad35c342015-06-11 17:25:36 -0700170 XMLConfiguration cfg = new XMLConfiguration();
171 cfg.setAttributeSplittingDisabled(true);
172 cfg.setDelimiterParsingDisabled(true);
173 cfg.load(appFile(appName, APP_XML));
174 return loadAppDescription(cfg);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800175 } 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 Vachuska0249b532015-02-20 16:46:18 -0800191 public synchronized ApplicationDescription saveApplication(InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800192 try (InputStream ais = stream) {
193 byte[] cache = toByteArray(ais);
194 InputStream bis = new ByteArrayInputStream(cache);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800195
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800196 boolean plainXml = isPlainXml(cache);
197 ApplicationDescription desc = plainXml ?
198 parsePlainAppDescription(bis) : parseZippedAppDescription(bis);
Thomas Vachuskae18a3302015-06-23 12:48:28 -0700199 checkState(!appFile(desc.name(), APP_XML).exists(),
Jian Li97d6b2d2016-01-20 10:13:43 -0800200 "Application %s already installed", desc.name());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800201
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800202 if (plainXml) {
203 expandPlainApplication(cache, desc);
204 } else {
205 bis.reset();
206 expandZippedApplication(bis, desc);
207
208 bis.reset();
209 saveApplication(bis, desc);
210 }
211
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800212 installArtifacts(desc);
213 return desc;
214 } catch (IOException e) {
215 throw new ApplicationException("Unable to save application", e);
216 }
217 }
218
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800219 // Indicates whether the stream encoded in the given bytes is plain XML.
220 private boolean isPlainXml(byte[] bytes) {
221 return substring(bytes, XML_MAGIC.length()).equals(XML_MAGIC) ||
222 substring(bytes, APP_MAGIC_DEPTH).contains(APP_MAGIC);
223 }
224
225 // Returns the substring of maximum possible length from the specified bytes.
226 private String substring(byte[] bytes, int length) {
HIGUCHI Yuta436f8d52015-12-07 21:17:48 -0800227 return new String(bytes, 0, Math.min(bytes.length, length), StandardCharsets.UTF_8);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800228 }
229
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800230 /**
231 * Purges the application archive directory.
232 *
233 * @param appName application name
234 */
Thomas Vachuska0249b532015-02-20 16:46:18 -0800235 public synchronized void purgeApplication(String appName) {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800236 File appDir = new File(appsDir, appName);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800237 try {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800238 Tools.removeDirectory(appDir);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800239 } catch (IOException e) {
240 throw new ApplicationException("Unable to purge application " + appName, e);
241 }
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800242 if (appDir.exists()) {
243 throw new ApplicationException("Unable to purge application " + appName);
244 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800245 }
246
247 /**
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800248 * Returns application archive stream for the specified application. This
249 * will be either the application ZIP file or the application XML file.
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800250 *
251 * @param appName application name
252 * @return application archive stream
253 */
Thomas Vachuska0249b532015-02-20 16:46:18 -0800254 public synchronized InputStream getApplicationInputStream(String appName) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800255 try {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700256 File appFile = appFile(appName, appName + OAR);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800257 return new FileInputStream(appFile.exists() ? appFile : appFile(appName, APP_XML));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800258 } catch (FileNotFoundException e) {
259 throw new ApplicationException("Application " + appName + " not found");
260 }
261 }
262
263 // Scans the specified ZIP stream for app.xml entry and parses it producing
264 // an application descriptor.
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800265 private ApplicationDescription parseZippedAppDescription(InputStream stream)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800266 throws IOException {
267 try (ZipInputStream zis = new ZipInputStream(stream)) {
268 ZipEntry entry;
269 while ((entry = zis.getNextEntry()) != null) {
270 if (entry.getName().equals(APP_XML)) {
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800271 byte[] data = ByteStreams.toByteArray(zis);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800272 return parsePlainAppDescription(new ByteArrayInputStream(data));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800273 }
274 zis.closeEntry();
275 }
276 }
277 throw new IOException("Unable to locate " + APP_XML);
278 }
279
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800280 // Scans the specified XML stream and parses it producing an application descriptor.
281 private ApplicationDescription parsePlainAppDescription(InputStream stream)
282 throws IOException {
283 XMLConfiguration cfg = new XMLConfiguration();
Thomas Vachuskaad35c342015-06-11 17:25:36 -0700284 cfg.setAttributeSplittingDisabled(true);
285 cfg.setDelimiterParsingDisabled(true);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800286 try {
287 cfg.load(stream);
288 return loadAppDescription(cfg);
289 } catch (ConfigurationException e) {
290 throw new IOException("Unable to parse " + APP_XML, e);
291 }
292 }
293
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800294 private ApplicationDescription loadAppDescription(XMLConfiguration cfg) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800295 String name = cfg.getString(NAME);
296 Version version = Version.version(cfg.getString(VERSION));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800297 String origin = cfg.getString(ORIGIN);
Simon Huntafae2f72016-03-04 21:18:23 -0800298
299 String title = cfg.getString(TITLE);
300 // FIXME: title should be set as attribute to APP, but fallback for now...
301 title = title == null ? name : title;
302
Jian Li01b0f5952016-01-20 11:02:07 -0800303 String category = cfg.getString(CATEGORY, UTILITY);
Jian Lic35415d2016-01-14 17:22:31 -0800304 String url = cfg.getString(URL);
305 byte[] icon = getApplicationIcon(name);
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900306 ApplicationRole role = getRole(cfg.getString(ROLE));
307 Set<Permission> perms = getPermissions(cfg);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800308 String featRepo = cfg.getString(FEATURES_REPO);
309 URI featuresRepo = featRepo != null ? URI.create(featRepo) : null;
Thomas Vachuskaad35c342015-06-11 17:25:36 -0700310 List<String> features = ImmutableList.copyOf(cfg.getString(FEATURES).split(","));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800311
Thomas Vachuska761f0042015-11-11 19:10:17 -0800312 String apps = cfg.getString(APPS, "");
313 List<String> requiredApps = apps.isEmpty() ?
314 ImmutableList.of() : ImmutableList.copyOf(apps.split(","));
315
Jian Li8bcb4f22016-01-20 10:36:18 -0800316 // put full description to readme field
317 String readme = cfg.getString(DESCRIPTION);
Jian Lic35415d2016-01-14 17:22:31 -0800318
Jian Li8bcb4f22016-01-20 10:36:18 -0800319 // put short description to description field
320 String desc = compactDescription(readme);
Jian Lic35415d2016-01-14 17:22:31 -0800321
Simon Huntafae2f72016-03-04 21:18:23 -0800322 return new DefaultApplicationDescription(name, version, title, desc, origin,
Jian Li8bcb4f22016-01-20 10:36:18 -0800323 category, url, readme, icon,
324 role, perms, featuresRepo,
325 features, requiredApps);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800326 }
327
328 // Expands the specified ZIP stream into app-specific directory.
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800329 private void expandZippedApplication(InputStream stream, ApplicationDescription desc)
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800330 throws IOException {
331 ZipInputStream zis = new ZipInputStream(stream);
332 ZipEntry entry;
333 File appDir = new File(appsDir, desc.name());
334 while ((entry = zis.getNextEntry()) != null) {
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800335 if (!entry.isDirectory()) {
336 byte[] data = ByteStreams.toByteArray(zis);
337 zis.closeEntry();
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800338 File file = new File(appDir, entry.getName());
339 createParentDirs(file);
340 write(data, file);
341 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800342 }
343 zis.close();
344 }
345
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800346 // Saves the specified XML stream into app-specific directory.
347 private void expandPlainApplication(byte[] stream, ApplicationDescription desc)
348 throws IOException {
349 File file = appFile(desc.name(), APP_XML);
Thomas Vachuskae18a3302015-06-23 12:48:28 -0700350 checkState(!file.getParentFile().exists(), "Application already installed");
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800351 createParentDirs(file);
352 write(stream, file);
353 }
354
355
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800356 // Saves the specified ZIP stream into a file under app-specific directory.
357 private void saveApplication(InputStream stream, ApplicationDescription desc)
358 throws IOException {
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700359 Files.write(toByteArray(stream), appFile(desc.name(), desc.name() + OAR));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800360 }
361
362 // Installs application artifacts into M2 repository.
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800363 private void installArtifacts(ApplicationDescription desc) throws IOException {
364 try {
365 Tools.copyDirectory(appFile(desc.name(), M2_PREFIX), m2Dir);
366 } catch (NoSuchFileException e) {
367 log.debug("Application {} has no M2 artifacts", desc.name());
368 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800369 }
370
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800371 /**
372 * Marks the app as active by creating token file in the app directory.
373 *
374 * @param appName application name
375 * @return true if file was created
376 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800377 protected boolean setActive(String appName) {
378 try {
Thomas Vachuskae965b3d2016-03-03 11:42:48 -0800379 File active = appFile(appName, "active");
380 createParentDirs(active);
381 return active.createNewFile() && updateTime(appName);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800382 } catch (IOException e) {
Thomas Vachuskae965b3d2016-03-03 11:42:48 -0800383 log.warn("Unable to mark app {} as active", appName, e);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800384 throw new ApplicationException("Unable to mark app as active", e);
385 }
386 }
387
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800388 /**
389 * Clears the app as active by deleting token file in the app directory.
390 *
391 * @param appName application name
392 * @return true if file was deleted
393 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800394 protected boolean clearActive(String appName) {
Thomas Vachuskacf960112015-03-06 22:36:51 -0800395 return appFile(appName, "active").delete() && updateTime(appName);
396 }
397
398 /**
399 * Updates the time-stamp of the app descriptor file.
400 *
401 * @param appName application name
402 * @return true if the app descriptor was updated
403 */
Thomas Vachuska161baf52015-03-27 16:15:39 -0700404 protected boolean updateTime(String appName) {
Thomas Vachuskacf960112015-03-06 22:36:51 -0800405 return appFile(appName, APP_XML).setLastModified(System.currentTimeMillis());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800406 }
407
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800408 /**
409 * Indicates whether the app was marked as active by checking for token file.
410 *
411 * @param appName application name
412 * @return true if the app is marked as active
413 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800414 protected boolean isActive(String appName) {
415 return appFile(appName, "active").exists();
416 }
417
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800418 // Returns the name of the file located under the specified app directory.
419 private File appFile(String appName, String fileName) {
420 return new File(new File(appsDir, appName), fileName);
421 }
422
Jian Li97d6b2d2016-01-20 10:13:43 -0800423 // Returns the icon file located under the specified app directory.
424 private File iconFile(String appName, String fileName) {
425 return new File(new File(appsDir, appName), fileName);
426 }
427
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900428 // Returns the set of Permissions specified in the app.xml file
429 private ImmutableSet<Permission> getPermissions(XMLConfiguration cfg) {
Thomas Vachuska761f0042015-11-11 19:10:17 -0800430 List<Permission> permissionList = Lists.newArrayList();
Changhoon Yoonb856b812015-08-10 03:47:19 +0900431
432 for (Object o : cfg.getList(APP_PERMISSIONS)) {
Changhoon Yoona7841ed2015-05-15 02:51:08 +0900433 String name = (String) o;
Changhoon Yoonb856b812015-08-10 03:47:19 +0900434 permissionList.add(new Permission(AppPermission.class.getName(), name));
435 }
436 for (Object o : cfg.getList(NET_PERMISSIONS)) {
437 //TODO: TO BE FLESHED OUT WHEN NETWORK PERMISSIONS ARE SUPPORTED
438 break;
439 }
440
441 List<HierarchicalConfiguration> fields =
442 cfg.configurationsAt(JAVA_PERMISSIONS);
443 for (HierarchicalConfiguration sub : fields) {
444 String classname = sub.getString("classname");
445 String name = sub.getString("name");
446 String actions = sub.getString("actions");
447
448 if (classname != null && name != null) {
449 permissionList.add(new Permission(classname, name, actions));
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900450 }
451 }
Changhoon Yoona7841ed2015-05-15 02:51:08 +0900452 return ImmutableSet.copyOf(permissionList);
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900453 }
454
Jian Lic35415d2016-01-14 17:22:31 -0800455 // Returns the byte stream from icon.png file in oar application archive.
456 private byte[] getApplicationIcon(String appName) {
Jian Li97d6b2d2016-01-20 10:13:43 -0800457
458 byte[] icon = new byte[0];
459 File iconFile = iconFile(appName, APP_PNG);
Jian Lic35415d2016-01-14 17:22:31 -0800460
461 if (!iconFile.exists()) {
Jian Li97d6b2d2016-01-20 10:13:43 -0800462 // assume that we can always fallback to default icon
463 iconFile = new File(appsDir, APP_PNG);
Jian Lic35415d2016-01-14 17:22:31 -0800464 }
465
Jian Lic35415d2016-01-14 17:22:31 -0800466 try {
Jian Li97d6b2d2016-01-20 10:13:43 -0800467 ByteArrayOutputStream bos = new ByteArrayOutputStream();
468 ImageIO.write(ImageIO.read(iconFile), PNG, bos);
469 icon = bos.toByteArray();
470 bos.close();
Jian Lic35415d2016-01-14 17:22:31 -0800471 } catch (IOException e) {
472 e.printStackTrace();
473 }
474
Jian Li97d6b2d2016-01-20 10:13:43 -0800475 return icon;
Jian Lic35415d2016-01-14 17:22:31 -0800476 }
477
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900478 // Returns application role type
479 public ApplicationRole getRole(String value) {
480 if (value == null) {
481 return ApplicationRole.UNSPECIFIED;
482 } else {
483 try {
484 return ApplicationRole.valueOf(value.toUpperCase(Locale.ENGLISH));
485 } catch (IllegalArgumentException e) {
486 log.debug("Unknown role value: %s", value);
487 return ApplicationRole.UNSPECIFIED;
488 }
489 }
490 }
Jian Lic35415d2016-01-14 17:22:31 -0800491
492 // Returns the first sentence of the given sentence
493 private String compactDescription(String sentence) {
494 if (StringUtils.isNotEmpty(sentence)) {
495 if (StringUtils.contains(sentence, ".")) {
496 return StringUtils.substringBefore(sentence, ".") + ".";
497 } else {
Jian Li8bcb4f22016-01-20 10:36:18 -0800498 return sentence;
Jian Lic35415d2016-01-14 17:22:31 -0800499 }
500 }
501 return sentence;
502 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800503}