Search in sources :

Example 1 with ApplicationException

use of org.onosproject.app.ApplicationException in project onos by opennetworkinglab.

the class ApplicationArchive method expandZippedApplication.

// Expands the specified ZIP stream into app-specific directory.
// Returns true of the application is a self-contained jar rather than an oar file.
private boolean expandZippedApplication(InputStream stream, ApplicationDescription desc) throws IOException {
    boolean isSelfContained = false;
    ZipInputStream zis = new ZipInputStream(stream);
    ZipEntry entry;
    File appDir = new File(appsDir, desc.name());
    if (!FilePathValidator.validateFile(appDir, appsDir)) {
        throw new ApplicationException("Application attempting to create files outside the apps directory");
    }
    while ((entry = zis.getNextEntry()) != null) {
        if (!entry.isDirectory()) {
            byte[] data = ByteStreams.toByteArray(zis);
            zis.closeEntry();
            if (FilePathValidator.validateZipEntry(entry, appDir)) {
                File file = new File(appDir, entry.getName());
                if (isTopLevel(file)) {
                    createParentDirs(file);
                    write(data, file);
                } else {
                    isSelfContained = true;
                }
            } else {
                throw new ApplicationException("Application Zip archive is attempting to leave application root");
            }
        }
    }
    zis.close();
    return isSelfContained;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ApplicationException(org.onosproject.app.ApplicationException) ZipEntry(java.util.zip.ZipEntry) File(java.io.File)

Example 2 with ApplicationException

use of org.onosproject.app.ApplicationException in project onos by opennetworkinglab.

the class DistributedApplicationStore method create.

@Override
public Application create(InputStream appDescStream) {
    ApplicationDescription appDesc = saveApplication(appDescStream);
    if (hasPrerequisites(appDesc)) {
        return create(appDesc, true);
    }
    // Purge bits off disk if we don't have prerequisites to allow app to be
    // reinstalled later
    purgeApplication(appDesc.name());
    throw new ApplicationException("Missing dependencies for app " + appDesc.name());
}
Also used : ApplicationException(org.onosproject.app.ApplicationException) ApplicationDescription(org.onosproject.app.ApplicationDescription)

Example 3 with ApplicationException

use of org.onosproject.app.ApplicationException in project onos by opennetworkinglab.

the class ApplicationArchive method setActive.

/**
 * Marks the app as active by creating token file in the app directory.
 *
 * @param appName application name
 * @return true if file was created
 */
protected boolean setActive(String appName) {
    try {
        File active = appFile(appName, "active");
        createParentDirs(active);
        return active.createNewFile() && updateTime(appName);
    } catch (IOException e) {
        log.warn("Unable to mark app {} as active", appName, e);
        throw new ApplicationException("Unable to mark app as active", e);
    }
}
Also used : ApplicationException(org.onosproject.app.ApplicationException) IOException(java.io.IOException) File(java.io.File)

Example 4 with ApplicationException

use of org.onosproject.app.ApplicationException in project onos by opennetworkinglab.

the class ApplicationArchive method saveApplication.

/**
 * Loads the application descriptor from the specified application archive
 * stream and saves the stream in the appropriate application archive
 * directory.
 *
 * @param stream application archive stream
 * @return application descriptor
 * @throws org.onosproject.app.ApplicationException if unable to read the
 *                                                  archive stream or store
 *                                                  the application archive
 */
public synchronized ApplicationDescription saveApplication(InputStream stream) {
    try (InputStream ais = stream) {
        byte[] cache = toByteArray(ais);
        InputStream bis = new ByteArrayInputStream(cache);
        boolean plainXml = isPlainXml(cache);
        ApplicationDescription desc = plainXml ? parsePlainAppDescription(bis) : parseZippedAppDescription(bis);
        checkState(!appFile(desc.name(), APP_XML).exists(), "Application %s already installed", desc.name());
        if (plainXml) {
            expandPlainApplication(cache, desc);
        } else {
            bis.reset();
            boolean isSelfContainedJar = expandZippedApplication(bis, desc);
            if (isSelfContainedJar) {
                bis.reset();
                stageSelfContainedJar(bis, desc);
            }
            /*
                 * Reset the ZIP file and reparse the app description now
                 * that the ZIP is expanded onto the filesystem. This way any
                 * file referenced as part of the description (i.e. app.png)
                 * can be loaded into the app description.
                 */
            bis.reset();
            desc = parseZippedAppDescription(bis);
            bis.reset();
            saveApplication(bis, desc, isSelfContainedJar);
        }
        installArtifacts(desc);
        return desc;
    } catch (IOException e) {
        throw new ApplicationException("Unable to save application", e);
    }
}
Also used : ApplicationException(org.onosproject.app.ApplicationException) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ApplicationDescription(org.onosproject.app.ApplicationDescription) DefaultApplicationDescription(org.onosproject.app.DefaultApplicationDescription)

Example 5 with ApplicationException

use of org.onosproject.app.ApplicationException in project onos by opennetworkinglab.

the class ApplicationArchive method getApplicationDescription.

/**
 * Loads the application descriptor from the specified application archive
 * stream and saves the stream in the appropriate application archive
 * directory.
 *
 * @param appName application name
 * @return application descriptor
 * @throws org.onosproject.app.ApplicationException if unable to read application description
 */
public ApplicationDescription getApplicationDescription(String appName) {
    try {
        XMLConfiguration cfg = new XMLConfiguration();
        cfg.setAttributeSplittingDisabled(true);
        cfg.setDelimiterParsingDisabled(true);
        cfg.load(appFile(appName, APP_XML));
        return loadAppDescription(cfg);
    } catch (Exception e) {
        throw new ApplicationException("Unable to get app description", e);
    }
}
Also used : XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) ApplicationException(org.onosproject.app.ApplicationException) NoSuchFileException(java.nio.file.NoSuchFileException) ApplicationException(org.onosproject.app.ApplicationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ConfigurationException(org.apache.commons.configuration.ConfigurationException)

Aggregations

ApplicationException (org.onosproject.app.ApplicationException)7 IOException (java.io.IOException)4 ApplicationDescription (org.onosproject.app.ApplicationDescription)3 File (java.io.File)2 ZipInputStream (java.util.zip.ZipInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 ZipEntry (java.util.zip.ZipEntry)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 XMLConfiguration (org.apache.commons.configuration.XMLConfiguration)1 DefaultApplicationDescription (org.onosproject.app.DefaultApplicationDescription)1 Application (org.onosproject.core.Application)1 ApplicationId (org.onosproject.core.ApplicationId)1 DefaultApplication (org.onosproject.core.DefaultApplication)1 StorageException (org.onosproject.store.service.StorageException)1 Activate (org.osgi.service.component.annotations.Activate)1