Search in sources :

Example 1 with ClusterNode

use of com.openmeap.model.dto.ClusterNode in project OpenMEAP by OpenMEAP.

the class GlobalSettingsBacking method process.

@Override
public Collection<ProcessingEvent> process(ProcessingContext context, Map<Object, Object> templateVariables, Map<Object, Object> parameterMap) {
    List<ProcessingEvent> events = new ArrayList<ProcessingEvent>();
    GlobalSettings settings = modelManager.getGlobalSettings();
    // setup the variables required for form render
    templateVariables.put(PROCESS_TARGET_PARAM, ProcessingTargets.GLOBAL_SETTINGS);
    Boolean hasPerm = modelManager.getAuthorizer().may(Action.MODIFY, settings);
    templateVariables.put("mayModify", hasPerm);
    if (hasPerm == Boolean.FALSE) {
        events.add(new MessagesEvent("The user logged in does not have permissions to change the global settings."));
    }
    if (!empty(PROCESS_TARGET_PARAM, parameterMap)) {
        if (!empty(EXT_SVC_URL_PREFIX_PARAM, parameterMap)) {
            String svcUrl = firstValue(EXT_SVC_URL_PREFIX_PARAM, parameterMap);
            settings.setExternalServiceUrlPrefix(svcUrl);
        }
        if (!empty(MAX_FILE_UPLOAD_SIZE_PARAM, parameterMap)) {
            Integer maxFileUploadSize = Integer.valueOf(firstValue(MAX_FILE_UPLOAD_SIZE_PARAM, parameterMap));
            settings.setMaxFileUploadSize(maxFileUploadSize);
        }
        // process the storage path parameter
        if (!empty(STORAGE_PATH_PARAM, parameterMap)) {
            String path = firstValue(STORAGE_PATH_PARAM, parameterMap);
            settings.setTemporaryStoragePath(path);
        }
        // process auth salt
        if (!empty(AUTH_SALT_PARAM, parameterMap)) {
            if (empty(AUTH_SALT_VERIFY_PARAM, parameterMap) || !equalsEachOther(AUTH_SALT_PARAM, AUTH_SALT_VERIFY_PARAM, parameterMap)) {
                events.add(new MessagesEvent("Authentication salt and salt verify must match"));
            } else {
                settings.setServiceManagementAuthSalt(firstValue(AUTH_SALT_PARAM, parameterMap));
            }
        }
        List<ClusterNode> toDelete = new ArrayList<ClusterNode>();
        // process the ClusterNode objects
        if (parameterMap.get(CLUSTER_NODE_URLS_PARAM) != null) {
            String[] clusterNodeUrls = (String[]) parameterMap.get(CLUSTER_NODE_URLS_PARAM);
            String[] clusterNodePaths = (String[]) parameterMap.get(CLUSTER_NODE_PATHS_PARAM);
            int end = clusterNodeUrls.length;
            // make sure there is a map in cluster nodes
            List<ClusterNode> clusterNodes = settings.getClusterNodes();
            if (clusterNodes == null) {
                clusterNodes = new Vector<ClusterNode>();
                settings.setClusterNodes(clusterNodes);
            }
            // iterate over each node configuration, updating the clusterNodes as per input
            boolean warn = false;
            for (int i = 0; i < end; i++) {
                String thisNodeUrl = clusterNodeUrls[i].trim();
                String thisNodePath = clusterNodePaths[i].trim();
                if (thisNodeUrl.length() == 0 && warn == false) {
                    warn = true;
                    events.add(new MessagesEvent("A cluster node must be specified.  The service url must be internally accessible by the administrative service, and should point to the services context.  The rest of settings changes will be applied."));
                    continue;
                }
                // remove any nodes that no longer appear
                List<String> urls = Arrays.asList(clusterNodeUrls);
                List<String> urlsToRemove = new ArrayList<String>();
                for (ClusterNode node : clusterNodes) {
                    if (!urls.contains(node.getServiceWebUrlPrefix())) {
                        urlsToRemove.add(node.getServiceWebUrlPrefix());
                    }
                }
                for (String url : urlsToRemove) {
                    ClusterNode node = settings.getClusterNode(url);
                    clusterNodes.remove(node);
                    modelManager.delete(node, events);
                }
                ClusterNode node = null;
                if ((node = settings.getClusterNode(thisNodeUrl)) != null) {
                    node.setFileSystemStoragePathPrefix(thisNodePath);
                } else {
                    ClusterNode thisNode = new ClusterNode();
                    thisNode.setServiceWebUrlPrefix(thisNodeUrl);
                    thisNode.setFileSystemStoragePathPrefix(thisNodePath);
                    settings.addClusterNode(thisNode);
                }
            }
            // remove any nodes that no longer appear
            List<String> urls = Arrays.asList(clusterNodeUrls);
            for (ClusterNode node : settings.getClusterNodes()) {
                if (!urls.contains(node.getServiceWebUrlPrefix())) {
                    toDelete.add(node);
                }
            }
        }
        try {
            modelManager.begin();
            if (toDelete != null) {
                for (ClusterNode node : toDelete) {
                    settings.removeClusterNode(node);
                    modelManager.delete(node, events);
                }
            }
            modelManager.addModify(settings, events);
            modelManager.commit(events);
            modelManager.refresh(settings, events);
            events.add(new MessagesEvent("The settings were successfully modified."));
        } catch (InvalidPropertiesException e) {
            modelManager.rollback();
            logger.info("Invalid properties submitted for an application", e);
            events.add(new MessagesEvent(e.getMessage()));
        } catch (PersistenceException e) {
            modelManager.rollback();
            logger.error("An exception occurred commiting the transaction", e);
            events.add(new MessagesEvent(e.getMessage()));
        }
        try {
            healthChecker.refreshSettings();
            List<Exception> es = healthChecker.checkNowAndWait();
            if (es.size() > 0) {
                for (Exception e : es) {
                    events.add(new MessagesEvent(e.getMessage()));
                }
            }
        } catch (InterruptedException e) {
            logger.error("Exception occurred waiting on the health check thread after updating global settings", e);
            events.add(new MessagesEvent(e.getMessage()));
        }
    }
    if (settings.getExternalServiceUrlPrefix() != null) {
        templateVariables.put(EXT_SVC_URL_PREFIX_PARAM, settings.getExternalServiceUrlPrefix());
    }
    if (settings.getTemporaryStoragePath() != null) {
        templateVariables.put(STORAGE_PATH_PARAM, settings.getTemporaryStoragePath());
    }
    if (settings.getServiceManagementAuthSalt() != null) {
        templateVariables.put(AUTH_SALT_PARAM, settings.getServiceManagementAuthSalt());
        templateVariables.put(AUTH_SALT_VERIFY_PARAM, settings.getServiceManagementAuthSalt());
    }
    if (settings.getClusterNodes() != null && settings.getClusterNodes().size() > 0) {
        if (healthChecker != null) {
            for (ClusterNode node : settings.getClusterNodes()) {
                ClusterNode checkerNode = healthChecker.getSettings().getClusterNode(node.getServiceWebUrlPrefix());
                if (checkerNode != null) {
                    synchronized (checkerNode) {
                        node.setLastStatus(checkerNode.getLastStatus());
                        Date date = null;
                        node.setLastStatusCheck((Date) ((date = checkerNode.getLastStatusCheck()) != null ? date.clone() : null));
                        node.setLastStatusMessage(checkerNode.getLastStatusMessage());
                    }
                }
            }
        }
        templateVariables.put(CLUSTER_NODES_VAR, settings.getClusterNodes());
    }
    if (settings.getMaxFileUploadSize() != null) {
        templateVariables.put(MAX_FILE_UPLOAD_SIZE_PARAM, settings.getMaxFileUploadSize());
    }
    if (events.size() > 0)
        return events;
    return null;
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) ArrayList(java.util.ArrayList) GlobalSettings(com.openmeap.model.dto.GlobalSettings) InvalidPropertiesException(com.openmeap.model.InvalidPropertiesException) PersistenceException(javax.persistence.PersistenceException) Date(java.util.Date) InvalidPropertiesException(com.openmeap.model.InvalidPropertiesException) MessagesEvent(com.openmeap.event.MessagesEvent) PersistenceException(javax.persistence.PersistenceException) ProcessingEvent(com.openmeap.event.ProcessingEvent)

Example 2 with ClusterNode

use of com.openmeap.model.dto.ClusterNode in project OpenMEAP by OpenMEAP.

the class AbstractClusterServiceMgmtNotifier method notify.

public <E extends Event<T>> void notify(final E event, List<ProcessingEvent> events) throws ClusterNotificationException {
    final ThrowableList exceptions = new ThrowableList();
    onBeforeNotify(event);
    final Map<String, Boolean> urlRequestCompleteStatus = new HashMap<String, Boolean>();
    GlobalSettings globalSettings = modelManager.getGlobalSettings();
    List<ClusterNode> clusterNodes = globalSettings.getClusterNodes();
    for (ClusterNode thisNode : clusterNodes) {
        URL thisUrl = null;
        try {
            thisUrl = new URL(thisNode.getServiceWebUrlPrefix());
        } catch (MalformedURLException e) {
            logger.error("Could not create URL object from " + thisNode.getServiceWebUrlPrefix() + ": {}", e);
            continue;
        }
        if (executorService != null) {
            logger.debug("Making request to {} using the executor.", thisUrl);
            executorService.execute(new Runnable() {

                URL url;

                public void run() {
                    notifyMakeRequest(exceptions, url, urlRequestCompleteStatus, event);
                }

                Runnable setUrl(URL url) {
                    this.url = url;
                    return this;
                }
            }.setUrl(thisUrl));
        } else {
            logger.debug("Making request to {} serially.", thisUrl);
            notifyMakeRequest(exceptions, thisUrl, urlRequestCompleteStatus, event);
        }
    }
    // this set of if-else handles any exceptions in the list accumulated
    if (executorService != null && executorTimeout != null) {
        try {
            // terminate, but make sure nothing is still waiting when we terminate
            if (!executorService.awaitTermination(executorTimeout, TimeUnit.SECONDS)) {
                executorService.shutdownNow();
                List<String> waiting = new ArrayList<String>();
                for (Map.Entry<String, Boolean> completed : urlRequestCompleteStatus.entrySet()) {
                    if (completed.getValue().equals(Boolean.FALSE)) {
                        waiting.add(completed.getKey());
                    }
                }
                logger.error("Blocking timed-out still waiting to notify: {}", StringUtils.join(waiting, ", "));
                throw new ClusterNotificationException(String.format("Blocking timed-out still waiting to notify: %s", StringUtils.join(waiting, ", ")));
            }
        } catch (InterruptedException ie) {
            throw new ClusterNotificationException("The notification thread was interrupted", ie);
        }
    } else if (exceptions.size() > 0) {
        throw new ClusterNotificationException(String.format("The following exceptions were thrown: %s", exceptions.getMessages()));
    }
    onAfterNotify(event);
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) ThrowableList(com.openmeap.util.ThrowableList) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GlobalSettings(com.openmeap.model.dto.GlobalSettings) URL(java.net.URL) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ClusterNode

use of com.openmeap.model.dto.ClusterNode in project OpenMEAP by OpenMEAP.

the class AdminTestHelper method postGlobalSettings.

public HttpResponse postGlobalSettings(GlobalSettings settings) throws HttpRequestException, ParameterMapBuilderException {
    Hashtable<String, Object> getData = new Hashtable<String, Object>();
    getData.put(FormConstants.PAGE_BEAN, FormConstants.PAGE_BEAN_GLOBAL_SETTINGS);
    Hashtable<String, Object> postData = new Hashtable<String, Object>();
    postData.put(FormConstants.PROCESS_TARGET, ProcessingTargets.GLOBAL_SETTINGS);
    paramsBuilder.toParameters(postData, settings);
    if (settings.getClusterNodes() != null && !settings.getClusterNodes().isEmpty()) {
        List<String> nodeUrls = new ArrayList<String>();
        List<String> nodePaths = new ArrayList<String>();
        for (ClusterNode clusterNode : settings.getClusterNodes()) {
            nodeUrls.add(clusterNode.getServiceWebUrlPrefix());
            nodePaths.add(clusterNode.getFileSystemStoragePathPrefix());
        }
        postData.put("clusterNodeUrl", nodeUrls);
        postData.put("clusterNodePath", nodePaths);
    }
    return requestExecuter.postData(adminUrl, getData, postData);
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject)

Example 4 with ClusterNode

use of com.openmeap.model.dto.ClusterNode in project OpenMEAP by OpenMEAP.

the class ServiceManagementServlet method service.

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Result result = null;
    PrintWriter os = new PrintWriter(response.getOutputStream());
    logger.debug("Request uri: {}", request.getRequestURI());
    logger.debug("Request url: {}", request.getRequestURL());
    logger.debug("Query string: {}", request.getQueryString());
    if (logger.isDebugEnabled()) {
        logger.debug("Parameter map: {}", ParameterMapUtils.toString(request.getParameterMap()));
    }
    String action = request.getParameter(UrlParamConstants.ACTION);
    if (action == null) {
        action = "";
    }
    if (!authenticates(request)) {
        logger.error("Request failed to authenticate ", request);
        result = new Result(Result.Status.FAILURE, "Authentication failed");
        sendResult(response, os, result);
        return;
    }
    if (action.equals(ModelEntityEventAction.MODEL_REFRESH.getActionName())) {
        logger.trace("Processing refresh");
        result = refresh(request, response);
        sendResult(response, os, result);
        return;
    } else if (action.equals(ClusterNodeRequest.HEALTH_CHECK)) {
        logger.trace("Cluster node health check");
        result = healthCheck(request, response);
        sendResult(response, os, result);
        return;
    }
    GlobalSettings settings = modelManager.getGlobalSettings();
    ClusterNode clusterNode = modelManager.getClusterNode();
    if (clusterNode == null) {
        throw new RuntimeException("openmeap-services-web needs to be configured as a cluster node in the settings of the admin interface.");
    }
    Map<Method, String> validationErrors = clusterNode.validate();
    if (validationErrors != null) {
        throw new RuntimeException(new InvalidPropertiesException(clusterNode, validationErrors));
    }
    if (request.getParameter("clearPersistenceContext") != null && context instanceof AbstractApplicationContext) {
        logger.trace("Clearing persistence context");
        clearPersistenceContext();
    } else if (action.equals(ModelEntityEventAction.ARCHIVE_UPLOAD.getActionName())) {
        logger.trace("Processing archive upload - max file size: {}, storage path prefix: {}", settings.getMaxFileUploadSize(), clusterNode.getFileSystemStoragePathPrefix());
        archiveUploadHandler.setFileSystemStoragePathPrefix(clusterNode.getFileSystemStoragePathPrefix());
        Map<Object, Object> paramMap = ServletUtils.cloneParameterMap(settings.getMaxFileUploadSize(), clusterNode.getFileSystemStoragePathPrefix(), request);
        result = handleArchiveEvent(archiveUploadHandler, new MapPayloadEvent(paramMap), paramMap);
    } else if (action.equals(ModelEntityEventAction.ARCHIVE_DELETE.getActionName())) {
        logger.trace("Processing archive delete - max file size: {}, storage path prefix: {}", settings.getMaxFileUploadSize(), clusterNode.getFileSystemStoragePathPrefix());
        archiveDeleteHandler.setFileSystemStoragePathPrefix(clusterNode.getFileSystemStoragePathPrefix());
        Map<Object, Object> paramMap = ServletUtils.cloneParameterMap(settings.getMaxFileUploadSize(), clusterNode.getFileSystemStoragePathPrefix(), request);
        result = handleArchiveEvent(archiveDeleteHandler, new MapPayloadEvent(paramMap), paramMap);
    }
    sendResult(response, os, result);
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) GlobalSettings(com.openmeap.model.dto.GlobalSettings) MapPayloadEvent(com.openmeap.model.event.MapPayloadEvent) Method(java.lang.reflect.Method) Result(com.openmeap.services.dto.Result) InvalidPropertiesException(com.openmeap.model.InvalidPropertiesException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 5 with ClusterNode

use of com.openmeap.model.dto.ClusterNode in project OpenMEAP by OpenMEAP.

the class ModelManagerImplTest method testGlobalSettings.

@Test
public void testGlobalSettings() throws Exception {
    GlobalSettings settings = new GlobalSettings();
    Boolean ipeThrown = false;
    try {
        modelManager.begin().addModify(settings, null);
        modelManager.commit();
    } catch (InvalidPropertiesException ipe) {
        modelManager.rollback();
        ipeThrown = true;
    }
    Assert.assertTrue(ipeThrown);
    settings = modelManager.getGlobalSettings();
    Assert.assertTrue(settings.getId().equals(Long.valueOf(1)));
    ClusterNode node = new ClusterNode();
    node.setServiceWebUrlPrefix("http://test");
    node.setFileSystemStoragePathPrefix("/");
    settings.addClusterNode(node);
    try {
        settings = modelManager.begin().addModify(settings, null);
        modelManager.commit();
    } catch (Exception e) {
        modelManager.rollback();
        throw new Exception(e);
    }
    settings = modelManager.getGlobalSettings();
    Assert.assertTrue(settings.getClusterNodes().size() == 3);
    Assert.assertTrue(settings.getClusterNode("http://test") != null);
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) GlobalSettings(com.openmeap.model.dto.GlobalSettings) PersistenceException(javax.persistence.PersistenceException) EventNotificationException(com.openmeap.event.EventNotificationException) Test(org.junit.Test)

Aggregations

ClusterNode (com.openmeap.model.dto.ClusterNode)9 GlobalSettings (com.openmeap.model.dto.GlobalSettings)7 JSONObject (com.openmeap.thirdparty.org.json.me.JSONObject)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 JSONObjectBuilder (com.openmeap.json.JSONObjectBuilder)2 InvalidPropertiesException (com.openmeap.model.InvalidPropertiesException)2 Result (com.openmeap.services.dto.Result)2 GenericRuntimeException (com.openmeap.util.GenericRuntimeException)2 Date (java.util.Date)2 PersistenceException (javax.persistence.PersistenceException)2 Test (org.junit.Test)2 ClusterNodeRequest (com.openmeap.cluster.dto.ClusterNodeRequest)1 DigestException (com.openmeap.digest.DigestException)1 EventNotificationException (com.openmeap.event.EventNotificationException)1 MessagesEvent (com.openmeap.event.MessagesEvent)1 ProcessingEvent (com.openmeap.event.ProcessingEvent)1 HttpResponse (com.openmeap.http.HttpResponse)1 ModelManager (com.openmeap.model.ModelManager)1 Application (com.openmeap.model.dto.Application)1