blob: cba57023b8af3be4a3aa4ba1574d6fe109310036 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2018, 2019 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.rhelp.server;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.PreDestroy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.eclipse.statet.jcommons.collections.ImCollections;
import org.eclipse.statet.jcommons.collections.ImList;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.lang.ObjectUtils.ToStringBuilder;
import org.eclipse.statet.jcommons.runtime.BasicAppEnvironment;
import org.eclipse.statet.jcommons.runtime.CommonsRuntime;
import org.eclipse.statet.jcommons.status.InfoStatus;
import org.eclipse.statet.jcommons.status.Status;
import org.eclipse.statet.jcommons.status.StatusPrinter;
import org.eclipse.statet.jcommons.status.Statuses;
import org.eclipse.statet.internal.rhelp.server.RHelpServerInternals;
import org.eclipse.statet.internal.rhelp.server.update.REnvIndexController;
@SpringBootApplication
@ComponentScan (basePackageClasses= { RHelpServerInternals.class })
@ServletComponentScan (basePackageClasses= { RHelpServerInternals.class })
@NonNullByDefault
public class Application extends BasicAppEnvironment {
public static final String BUNDLE_ID= "org.eclipse.statet.rhelp.server"; //$NON-NLS-1$
public static void main(final String[] args) {
final SpringApplication app= new SpringApplication(Application.class);
if (args != null) {
final ImList<String> argList= ImCollections.newList(args);
if (argList.contains("-index-and-exit")) { //$NON-NLS-1$
app.setWebApplicationType(WebApplicationType.NONE);
app.setAdditionalProfiles(REnvIndexController.INDEX_AND_EXIT_PROFILE);
}
}
app.run(args);
}
private final ConcurrentHashMap<String, Log> logs= new ConcurrentHashMap<>();
private final StatusPrinter logStatusPrinter= new StatusPrinter();
@Autowired
private ApplicationContext appContext;
public Application() {
CommonsRuntime.init(this);
log(new InfoStatus(BUNDLE_ID, "Application started."));
}
@PreDestroy
protected void dispose() {
onAppStopping();
log(new InfoStatus(BUNDLE_ID, "Application stopped."));
}
@Override
public String getBundleId() {
return "org.eclipse.statet.rhelp.server"; //$NON-NLS-1$
}
@Override
public void log(final Status status) {
final Log log= this.logs.computeIfAbsent(status.getBundleId(),
(final String s) -> LogFactory.getLog(s) );
final ToStringBuilder sb= new ToStringBuilder();
sb.append(Statuses.getSeverityString(status.getSeverity()));
sb.append(" ["); //$NON-NLS-1$
sb.append(status.getCode());
sb.append(']');
if (status.getMessage().length() <= 80 && status.getMessage().indexOf('\n') == -1) {
sb.append(' ');
sb.append(status.getMessage());
}
else {
sb.addProp("message", status.getMessage()); //$NON-NLS-1$
}
if (status.isMultiStatus()) {
final ImList<Status> children= status.getChildren();
if (children != null && !children.isEmpty()) {
final StringBuilder sb0= new StringBuilder();
sb0.append("Status:\n");
this.logStatusPrinter.print(children, sb0);
sb.addProp("children", sb0.toString()); //$NON-NLS-1$
}
else {
sb.addProp("children", "<none>"); //$NON-NLS-1$
}
}
switch (status.getSeverity()) {
case Status.ERROR:
log.error(sb.toString(), status.getException());
break;
case Status.WARNING:
log.warn(sb.toString(), status.getException());
break;
default:
log.info(sb.toString(), status.getException());
break;
}
}
}