Search in sources :

Example 1 with Service

use of org.structr.api.service.Service in project structr by structr.

the class Services method startService.

public void startService(final Class serviceClass) {
    int retryCount = 10;
    int retryDelay = 30;
    boolean waitAndRetry = true;
    boolean isVital = false;
    logger.info("Creating {}..", serviceClass.getSimpleName());
    try {
        final Service service = (Service) serviceClass.newInstance();
        if (licenseManager != null && !licenseManager.isValid(service)) {
            logger.error("Configured service {} is not part of the currently licensed Structr Edition.", serviceClass.getSimpleName());
            return;
        }
        isVital = service.isVital();
        retryCount = service.getRetryCount();
        retryDelay = service.getRetryDelay();
        while (waitAndRetry && retryCount-- > 0) {
            waitAndRetry = service.waitAndRetry();
            try {
                if (service.initialize(this)) {
                    if (service instanceof RunnableService) {
                        RunnableService runnableService = (RunnableService) service;
                        if (runnableService.runOnStartup()) {
                            // start RunnableService and cache it
                            runnableService.startService();
                        }
                    }
                    if (service.isRunning()) {
                        // cache service instance
                        serviceCache.put(serviceClass, service);
                    }
                    // initialization callback
                    service.initialized();
                    // abort wait and retry loop
                    waitAndRetry = false;
                } else if (isVital && !waitAndRetry) {
                    checkVitalService(serviceClass, null);
                }
            } catch (Throwable t) {
                logger.warn("Service {} failed to start: {}", serviceClass.getSimpleName(), t.getMessage());
                if (isVital && !waitAndRetry) {
                    checkVitalService(serviceClass, t);
                }
            }
            if (waitAndRetry) {
                if (retryCount > 0) {
                    logger.warn("Retrying in {} seconds..", (retryDelay * 1000));
                    Thread.sleep(retryDelay * 1000);
                } else {
                    if (isVital) {
                        checkVitalService(serviceClass, null);
                    }
                }
            }
        }
    } catch (Throwable t) {
        if (isVital) {
            checkVitalService(serviceClass, t);
        }
    }
}
Also used : RunnableService(org.structr.api.service.RunnableService) DatabaseService(org.structr.api.DatabaseService) NodeService(org.structr.core.graph.NodeService) ExecutorService(java.util.concurrent.ExecutorService) Service(org.structr.api.service.Service) RunnableService(org.structr.api.service.RunnableService) SchemaService(org.structr.schema.SchemaService)

Example 2 with Service

use of org.structr.api.service.Service in project structr by structr.

the class Services method command.

/**
 * Creates and returns a command of the given <code>type</code>. If a command is
 * found, the corresponding service will be discovered and activated.
 *
 * @param <T>
 * @param securityContext
 * @param commandType the runtime type of the desired command
 * @return the command
 */
public <T extends Command> T command(final SecurityContext securityContext, final Class<T> commandType) {
    try {
        final T command = commandType.newInstance();
        final Class serviceClass = command.getServiceClass();
        // inject security context first
        command.setArgument("securityContext", securityContext);
        if ((serviceClass != null) && configuredServiceClasses.contains(serviceClass.getSimpleName())) {
            // search for already running service..
            Service service = serviceCache.get(serviceClass);
            if (service == null) {
                // start service
                startService(serviceClass);
                // reload service
                service = serviceCache.get(serviceClass);
                if (serviceClass.equals(NodeService.class)) {
                    logger.debug("(Re-)Started NodeService, (re-)compiling dynamic schema");
                    SchemaService.reloadSchema(new ErrorBuffer(), null);
                }
            }
            if (service != null) {
                logger.debug("Initializing command ", commandType.getName());
                service.injectArguments(command);
            }
        }
        command.initialized();
        return command;
    } catch (Throwable t) {
        logger.error("Exception while creating command {}", commandType.getName());
    }
    return null;
}
Also used : ErrorBuffer(org.structr.common.error.ErrorBuffer) DatabaseService(org.structr.api.DatabaseService) NodeService(org.structr.core.graph.NodeService) ExecutorService(java.util.concurrent.ExecutorService) Service(org.structr.api.service.Service) RunnableService(org.structr.api.service.RunnableService) SchemaService(org.structr.schema.SchemaService)

Example 3 with Service

use of org.structr.api.service.Service in project structr by structr.

the class ConfigServlet method createConfigDocument.

// ----- private methods -----
private Document createConfigDocument(final HttpServletRequest request, final PrintWriter writer) {
    final Document doc = new Document(writer);
    final Tag body = setupDocument(request, doc);
    final Tag form = body.block("form").css("config-form");
    final Tag main = form.block("div").id("main");
    final Tag tabs = main.block("div").id("configTabs");
    final Tag menu = tabs.block("ul").id("configTabsMenu");
    // configure form
    form.attr(new Attr("action", ConfigUrl), new Attr("method", "post"));
    for (final SettingsGroup group : Settings.getGroups()) {
        final String key = group.getKey();
        final String name = group.getName();
        menu.block("li").block("a").id(key + "Menu").attr(new Attr("href", "#" + key)).block("span").text(name);
        final Tag container = tabs.block("div").css("tab-content").id(key);
        // let settings group render itself
        group.render(container);
        // stop floating
        container.block("div").attr(new Style("clear: both;"));
    }
    // add services tab
    menu.block("li").block("a").id("servicesMenu").attr(new Attr("href", "#services")).block("span").text("Services");
    final Services services = Services.getInstance();
    final Tag container = tabs.block("div").css("tab-content").id("services");
    final Tag table = container.block("table").id("services-table");
    final Tag header = table.block("tr");
    header.block("th").text("Service Name");
    header.block("th").attr(new Attr("colspan", "2"));
    for (final String serviceClassName : services.getServices()) {
        final Class<Service> serviceClass = services.getServiceClassForName(serviceClassName);
        final boolean running = serviceClass != null ? services.isReady(serviceClass) : false;
        final Tag row = table.block("tr");
        row.block("td").text(serviceClassName);
        if (running) {
            row.block("td").block("button").attr(new Type("button"), new OnClick("window.location.href='" + ConfigUrl + "?restart=" + serviceClassName + "';")).text("Restart");
            if ("HttpService".equals(serviceClassName)) {
                row.block("td");
            } else {
                row.block("td").block("button").attr(new Type("button"), new OnClick("window.location.href='" + ConfigUrl + "?stop=" + serviceClassName + "';")).text("Stop");
            }
            row.block("td");
        } else {
            row.block("td");
            row.block("td");
            row.block("td").block("button").attr(new Type("button"), new OnClick("window.location.href='" + ConfigUrl + "?start=" + serviceClassName + "';")).text("Start");
        }
    }
    // update active section so we can restore it when redirecting
    container.empty("input").attr(new Type("hidden"), new Name("active_section")).id("active_section");
    // stop floating
    container.block("div").attr(new Style("clear: both;"));
    // buttons
    final Tag buttons = form.block("div").css("buttons");
    buttons.block("button").attr(new Type("button")).id("new-entry-button").text("Add entry");
    buttons.block("button").attr(new Type("button"), new OnClick("window.location.href='" + ConfigUrl + "?reload';")).text("Reload configuration file");
    buttons.empty("input").attr(new Type("submit"), new Value("Save to structr.conf"));
    body.block("script").text("$('#new-entry-button').on('click', createNewEntry);");
    return doc;
}
Also used : Service(org.structr.api.service.Service) Document(org.structr.api.util.html.Document) Attr(org.structr.api.util.html.Attr) Services(org.structr.core.Services) SettingsGroup(org.structr.api.config.SettingsGroup) Tag(org.structr.api.util.html.Tag)

Aggregations

Service (org.structr.api.service.Service)3 ExecutorService (java.util.concurrent.ExecutorService)2 DatabaseService (org.structr.api.DatabaseService)2 RunnableService (org.structr.api.service.RunnableService)2 NodeService (org.structr.core.graph.NodeService)2 SchemaService (org.structr.schema.SchemaService)2 SettingsGroup (org.structr.api.config.SettingsGroup)1 Attr (org.structr.api.util.html.Attr)1 Document (org.structr.api.util.html.Document)1 Tag (org.structr.api.util.html.Tag)1 ErrorBuffer (org.structr.common.error.ErrorBuffer)1 Services (org.structr.core.Services)1