Search in sources :

Example 6 with ApplicationServiceException

use of org.codice.ddf.admin.application.service.ApplicationServiceException in project ddf by codice.

the class ApplicationServiceImpl method traverseDependencies.

/**
     * Finds a parent and children dependencies for each app.  Needs to be run twice
     * in order to get full dependency correlations.
     *
     * @param appMap               Application Map containing all the application nodes.
     * @param filteredApplications Set containing all the application nodes minus those in the ignored list
     * @param reportDebug          Boolean that allows debug statements to be output or not.  Only reason
     *                             why this exists is because this function will be called twice and only
     *                             the second set of statements will be relevant
     */
private void traverseDependencies(Map<Application, ApplicationNodeImpl> appMap, Set<Application> filteredApplications, boolean reportDebug) {
    // find dependencies in each app and add them into correct node
    for (Entry<Application, ApplicationNodeImpl> curAppNode : appMap.entrySet()) {
        try {
            // main feature will contain dependencies
            Feature mainFeature = curAppNode.getKey().getMainFeature();
            if (null == mainFeature) {
                if (reportDebug) {
                    LOGGER.debug("Application \"{}\" does not contain a main feature", curAppNode.getKey().getName());
                }
                continue;
            }
            // eliminate duplications with a set
            Set<Dependency> dependencies = new HashSet<>(mainFeature.getDependencies());
            // remove any features that are local to the application
            dependencies.removeAll(curAppNode.getKey().getFeatures());
            // loop through all of the features that are left to determine
            // where they are from
            Set<Application> depAppSet = new HashSet<>();
            for (Dependency curDepFeature : dependencies) {
                Application dependencyApp = findFeature(featuresService.getFeature(curDepFeature.getName()), filteredApplications);
                if (dependencyApp != null) {
                    if (dependencyApp.equals(curAppNode.getKey())) {
                        if (reportDebug) {
                            LOGGER.debug("Self-dependency");
                        }
                        continue;
                    } else {
                        if (reportDebug) {
                            LOGGER.debug("Application {} depends on the feature {} which is located in application {}.", curAppNode.getKey().getName(), curDepFeature.getName(), dependencyApp.getName());
                        }
                        depAppSet.add(dependencyApp);
                    }
                }
            }
            if (!depAppSet.isEmpty()) {
                Application parentApp;
                if (depAppSet.size() > 1) {
                    parentApp = findCommonParent(depAppSet, appMap);
                    if (parentApp == null) {
                        if (reportDebug) {
                            LOGGER.debug("Found more than 1 application dependency for application {}. Could not determine which one is the correct parent. Application will be sent back as root application.", curAppNode.getKey().getName());
                        }
                        continue;
                    }
                } else {
                    parentApp = depAppSet.iterator().next();
                }
                // update the dependency app with a new child
                ApplicationNode parentAppNode = appMap.get(parentApp);
                parentAppNode.getChildren().add(curAppNode.getValue());
                curAppNode.getValue().setParent(parentAppNode);
            } else {
                if (reportDebug) {
                    LOGGER.debug("No dependency applications found for {}. This will be sent back as a root application.", curAppNode.getKey().getName());
                }
            }
        // ApplicationServiceException from DDF and Exception from Karaf
        // (FeaturesService)
        } catch (Exception e) {
            if (reportDebug) {
                LOGGER.warn("Encountered error while determining dependencies for \"{}\". This may cause an incomplete application hierarchy to be created.", curAppNode.getKey().getName(), e);
            }
        }
    }
}
Also used : ApplicationNode(org.codice.ddf.admin.application.service.ApplicationNode) Dependency(org.apache.karaf.features.Dependency) Application(org.codice.ddf.admin.application.service.Application) Feature(org.apache.karaf.features.Feature) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(ddf.security.service.SecurityServiceException) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) HashSet(java.util.HashSet)

Example 7 with ApplicationServiceException

use of org.codice.ddf.admin.application.service.ApplicationServiceException in project ddf by codice.

the class ApplicationServiceImpl method getApplications.

@Override
public Set<Application> getApplications() {
    LOGGER.trace("Getting all applications.");
    Repository[] repos = {};
    try {
        repos = featuresService.listRepositories();
        LOGGER.debug("Found {} applications from feature service.", repos.length);
        if (LOGGER.isDebugEnabled()) {
            for (int ii = 0; ii < repos.length; ++ii) {
                LOGGER.debug("Repo/App {}: {}", ii, repos[ii].getName());
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Unable to get list of Repositories.", e);
    }
    Set<Application> applications = new HashSet<Application>(repos.length);
    for (int i = 0; i < repos.length; i++) {
        Application newApp = new ApplicationImpl(repos[i]);
        try {
            if (!ignoredApplicationNames.contains(newApp.getName()) && newApp.getFeatures().size() > 0 && isPermittedToViewFeature(newApp.getName())) {
                applications.add(newApp);
            }
        } catch (ApplicationServiceException ase) {
            LOGGER.warn("Exception while trying to find information for application named {}. " + "It will be excluded from the application list.", newApp.getName(), ase);
        }
    }
    return new TreeSet<>(applications);
}
Also used : ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Repository(org.apache.karaf.features.Repository) TreeSet(java.util.TreeSet) Application(org.codice.ddf.admin.application.service.Application) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(ddf.security.service.SecurityServiceException) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) HashSet(java.util.HashSet)

Example 8 with ApplicationServiceException

use of org.codice.ddf.admin.application.service.ApplicationServiceException in project ddf by codice.

the class ApplicationServiceImpl method addApplication.

@Override
public void addApplication(URI applicationURL) throws ApplicationServiceException {
    try {
        if (applicationURL.toString().startsWith("file:")) {
            applicationURL = ApplicationFileInstaller.install(new File(applicationURL));
            LOGGER.info("Installing newly added feature repo: {}", applicationURL);
        }
        featuresService.addRepository(applicationURL, false);
    } catch (Exception e) {
        LOGGER.warn("Could not add new application due to error.", e);
        throw new ApplicationServiceException(e);
    }
}
Also used : ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) File(java.io.File) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(ddf.security.service.SecurityServiceException) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException)

Example 9 with ApplicationServiceException

use of org.codice.ddf.admin.application.service.ApplicationServiceException in project ddf by codice.

the class ApplicationUploadEndpoint method create.

@POST
@Path("/")
public Response create(MultipartBody multipartBody, @Context UriInfo requestUriInfo) {
    LOGGER.trace("ENTERING: create");
    Response response;
    List<Attachment> attachmentList = multipartBody.getAllAttachments();
    File newFile = null;
    for (Attachment attachment : attachmentList) {
        newFile = createFileFromAttachement(attachment);
    }
    try {
        if (newFile != null) {
            appService.addApplication(newFile.toURI());
        }
        Response.ResponseBuilder responseBuilder = Response.ok();
        response = responseBuilder.build();
    } catch (ApplicationServiceException e) {
        LOGGER.warn("Unable to add the application to the server: {}", newFile, e);
        Response.ResponseBuilder responseBuilder = Response.serverError();
        response = responseBuilder.build();
    }
    LOGGER.trace("EXITING: create");
    return response;
}
Also used : Response(javax.ws.rs.core.Response) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) File(java.io.File) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 10 with ApplicationServiceException

use of org.codice.ddf.admin.application.service.ApplicationServiceException in project ddf by codice.

the class ApplicationUploadEndpoint method update.

@POST
@Path("/update")
@Produces("application/json")
public Response update(MultipartBody multipartBody, @Context UriInfo requestUriInfo) {
    LOGGER.trace("ENTERING: update");
    Response response;
    List<Attachment> attachmentList = multipartBody.getAllAttachments();
    File newFile = null;
    for (Attachment attachment : attachmentList) {
        newFile = createFileFromAttachement(attachment);
    }
    try {
        if (newFile != null) {
            ZipFileApplicationDetails appDetails = ApplicationFileInstaller.getAppDetails(newFile);
            if (appDetails != null) {
                // lets get the existing app if it exists.
                Application existingApplication = appService.getApplication(appDetails.getName());
                // assume false until proved
                boolean wasExistingAppStarted = false;
                // otherwise.
                if (existingApplication != null) {
                    wasExistingAppStarted = appService.isApplicationStarted(existingApplication);
                    appService.removeApplication(existingApplication);
                }
                appService.addApplication(newFile.toURI());
                // if application was started before it was removed, lets try and start it.
                if (wasExistingAppStarted) {
                    appService.startApplication(appDetails.getName());
                }
            } else {
                throw new ApplicationServiceException("No Application details could be extracted from the provided file.");
            }
        } else {
            throw new ApplicationServiceException("No file attachment provided.");
        }
        // we need to output valid JSON to the client so fileupload can correctly call
        // done/fail callbacks correctly.
        Response.ResponseBuilder responseBuilder = Response.ok("{\"status\":\"success\"}").type("application/json");
        response = responseBuilder.build();
    } catch (ApplicationServiceException e) {
        LOGGER.warn("Unable to update an application on the server: {}", newFile, e);
        Response.ResponseBuilder responseBuilder = Response.serverError();
        response = responseBuilder.build();
    }
    LOGGER.trace("EXITING: update");
    return response;
}
Also used : Response(javax.ws.rs.core.Response) ZipFileApplicationDetails(org.codice.ddf.admin.application.service.impl.ZipFileApplicationDetails) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) File(java.io.File) Application(org.codice.ddf.admin.application.service.Application) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Aggregations

ApplicationServiceException (org.codice.ddf.admin.application.service.ApplicationServiceException)27 Test (org.junit.Test)14 Application (org.codice.ddf.admin.application.service.Application)9 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)7 Logger (org.slf4j.Logger)7 Appender (ch.qos.logback.core.Appender)6 SecurityServiceException (ddf.security.service.SecurityServiceException)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 URI (java.net.URI)6 ArgumentMatcher (org.mockito.ArgumentMatcher)6 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 File (java.io.File)5 HashSet (java.util.HashSet)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Response (javax.ws.rs.core.Response)3 Feature (org.apache.karaf.features.Feature)3 Repository (org.apache.karaf.features.Repository)3