Skip to content

Commit

Permalink
Small cleanup in ApplicationLifecycle
Browse files Browse the repository at this point in the history
- private fields
- bit optimized logging + stacktrace instead of toString
- removed unused configSupport

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Sep 18, 2022
1 parent b02a650 commit d8ab2bd
Showing 1 changed file with 70 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

Expand Down Expand Up @@ -158,48 +160,46 @@ public class ApplicationLifecycle implements Deployment, PostConstruct {
private static final Logger LOG = KernelLoggerInfo.getLogger();

@Inject
protected SnifferManagerImpl snifferManager;
private SnifferManagerImpl snifferManager;

@Inject
ServiceLocator serviceLocator;
private ServiceLocator serviceLocator;

@Inject
ArchiveFactory archiveFactory;
private ArchiveFactory archiveFactory;

@Inject
ContainerRegistry containerRegistry;
private ContainerRegistry containerRegistry;

@Inject
public ApplicationRegistry appRegistry;
private ApplicationRegistry appRegistry;

@Inject
protected Applications applications;
private Applications applications;

@Inject
@Named(DEFAULT_INSTANCE_NAME)
Server server;
private Server server;

@Inject
protected Domain domain;
private Domain domain;

@Inject
ServerEnvironmentImpl env;
private ServerEnvironmentImpl env;

@Inject
@Optional
VirtualizationEnv virtEnv;
private VirtualizationEnv virtEnv;

@Inject
Events events;
private Events events;

@Inject
ConfigSupport configSupport;

protected DeploymentLifecycleProbeProvider deploymentLifecycleProbeProvider;
private DeploymentLifecycleProbeProvider deploymentLifecycleProbeProvider;
private ExecutorService executorService;
private Collection<ApplicationLifecycleInterceptor> alcInterceptors = emptyList();

protected Deployer getDeployer(EngineInfo engineInfo) {
protected Deployer<?, ?> getDeployer(EngineInfo engineInfo) {
return engineInfo.getDeployer();
}

Expand Down Expand Up @@ -387,9 +387,8 @@ public void actOn(Logger logger) {
// the deployment fail, the main reason
// is that some container do not support to be restarted.
if (sniffers != null && LOG.isLoggable(FINE)) {
for (Sniffer sniffer : sniffers) {
LOG.fine("Before Sorting" + sniffer.getModuleType());
}
LOG.log(Level.FINE, "Before Sorting: "
+ sniffers.stream().map(Sniffer::getModuleType).collect(Collectors.joining(", ")));
}

sniffers = getSniffers(handler, sniffers, context);
Expand Down Expand Up @@ -417,14 +416,15 @@ public void actOn(Logger logger) {
}

if (LOG.isLoggable(FINE)) {
for (EngineInfo info : sortedEngineInfos) {
LOG.fine("After Sorting " + info.getSniffer().getModuleType());
}
LOG.log(Level.FINE, "After Sorting: " + sortedEngineInfos.stream()
.map(i -> i.getSniffer().getModuleType()).collect(Collectors.joining(", ")));
}

if (sortedEngineInfos == null || sortedEngineInfos.isEmpty()) {
report.failure(LOG, localStrings.getLocalString("unknowncontainertype",
"There is no installed container capable of handling this application {0}", context.getSource().getName()));
report.failure(LOG,
localStrings.getLocalString("unknowncontainertype",
"There is no installed container capable of handling this application {0}",
context.getSource().getName()));
tracker.actOn(LOG);
return null;
}
Expand Down Expand Up @@ -593,7 +593,7 @@ public Types getDeployableTypes(DeploymentContext context) throws IOException {
List<ReadableArchive> externalLibraries = getExternalLibraries(context);

for (ReadableArchive externalLibrary : externalLibraries) {
parser.parse(new ReadableArchiveScannerAdapter(parser, externalLibrary), null);
parser.parse(new ReadableArchiveScannerAdapter(parser, externalLibrary), null);
}

parser.awaitTermination();
Expand Down Expand Up @@ -731,11 +731,11 @@ public Collection<? extends Sniffer> getSniffers(final ArchiveHandler handler, C
return sniffers;
}


// set up containers and prepare the sorted ModuleInfos
@Override
public List<EngineInfo<?, ?>> setupContainerInfos(final ArchiveHandler handler, Collection<? extends Sniffer> sniffers,
DeploymentContext context) throws Exception {

public List<EngineInfo<?, ?>> setupContainerInfos(final ArchiveHandler handler,
Collection<? extends Sniffer> sniffers, DeploymentContext context) throws Exception {
final ActionReport report = context.getActionReport();

DeploymentTracing tracing = context.getModuleMetaData(DeploymentTracing.class);
Expand Down Expand Up @@ -796,7 +796,7 @@ public Collection<? extends Sniffer> getSniffers(final ArchiveHandler handler, C
throw new Exception(msg);
}

Deployer deployer = getDeployer(engineInfo);
Deployer<?, ?> deployer = getDeployer(engineInfo);
if (deployer == null) {
if (!startContainers(Collections.singleton(engineInfo), LOG, context)) {
final String msg = "Aborting, Failed to start container " + containerName;
Expand All @@ -823,8 +823,8 @@ public Collection<? extends Sniffer> getSniffers(final ArchiveHandler handler, C

List<EngineInfo<?, ?>> sortedEngineInfos = new ArrayList<>();

Map<Class, ApplicationMetaDataProvider> typeByProvider = new HashMap<>();
for (ApplicationMetaDataProvider provider : serviceLocator.getAllServices(ApplicationMetaDataProvider.class)) {
Map<Class<?>, ApplicationMetaDataProvider<?>> typeByProvider = new HashMap<>();
for (ApplicationMetaDataProvider<?> provider : serviceLocator.getAllServices(ApplicationMetaDataProvider.class)) {
if (provider.getMetaData() != null) {
for (Class<?> provided : provider.getMetaData().provides()) {
typeByProvider.put(provided, provider);
Expand All @@ -833,7 +833,7 @@ public Collection<? extends Sniffer> getSniffers(final ArchiveHandler handler, C
}

// Check if everything is provided.
for (ApplicationMetaDataProvider provider : serviceLocator.getAllServices(ApplicationMetaDataProvider.class)) {
for (ApplicationMetaDataProvider<?> provider : serviceLocator.getAllServices(ApplicationMetaDataProvider.class)) {
if (provider.getMetaData() != null) {
for (Class<?> dependency : provider.getMetaData().requires()) {
if (!typeByProvider.containsKey(dependency)) {
Expand Down Expand Up @@ -877,16 +877,14 @@ public Collection<? extends Sniffer> getSniffers(final ArchiveHandler handler, C
}

// ok everything is satisfied, just a matter of running things in order
List<Deployer> orderedDeployers = new ArrayList<>();
for (Deployer deployer : containerInfosByDeployers.keySet()) {
if (LOG.isLoggable(FINE)) {
LOG.fine("Keyed Deployer " + deployer.getClass());
}
loadDeployer(orderedDeployers, deployer, typeByDeployer, (Map) typeByProvider, context);
List<Deployer<?, ?>> orderedDeployers = new ArrayList<>();
for (Deployer<?, ?> deployer : containerInfosByDeployers.keySet()) {
LOG.log(Level.FINE, "Keyed Deployer {0}", deployer.getClass());
loadDeployer(orderedDeployers, deployer, typeByDeployer, typeByProvider, context);
}

// Now load metadata from deployers.
for (Deployer deployer : orderedDeployers) {
for (Deployer<?, ?> deployer : orderedDeployers) {
LOG.log(Level.FINE, "Ordered Deployer {0}", deployer);

final MetaData metadata = deployer.getMetaData();
Expand Down Expand Up @@ -919,9 +917,10 @@ public Collection<? extends Sniffer> getSniffers(final ArchiveHandler handler, C
return sortedEngineInfos;
}

private void loadDeployer(List<Deployer> results, Deployer<?, ?> deployer, Map<Class<?>, Deployer<?, ?>> typeByDeployer,
Map<Class<?>, ApplicationMetaDataProvider> typeByProvider, DeploymentContext dc) throws IOException {

private void loadDeployer(List<Deployer<?, ?>> results, Deployer<?, ?> deployer,
Map<Class<?>, Deployer<?, ?>> typeByDeployer, Map<Class<?>, ApplicationMetaDataProvider<?>> typeByProvider,
DeploymentContext dc) throws IOException {
if (results.contains(deployer)) {
return;
}
Expand All @@ -934,14 +933,14 @@ private void loadDeployer(List<Deployer> results, Deployer<?, ?> deployer, Map<C
if (typeByDeployer.containsKey(required)) {
loadDeployer(results, typeByDeployer.get(required), typeByDeployer, typeByProvider, dc);
} else {
ApplicationMetaDataProvider provider = typeByProvider.get(required);
ApplicationMetaDataProvider<?> provider = typeByProvider.get(required);
if (provider == null) {
LOG.log(SEVERE, inconsistentLifecycleState, required);
} else {
LinkedList<ApplicationMetaDataProvider> providers = new LinkedList<>();
LinkedList<ApplicationMetaDataProvider<?>> providers = new LinkedList<>();

addRecursively(providers, typeByProvider, provider);
for (ApplicationMetaDataProvider p : providers) {
for (ApplicationMetaDataProvider<?> p : providers) {
dc.addModuleMetaData(p.load(dc));
}
}
Expand All @@ -950,7 +949,9 @@ private void loadDeployer(List<Deployer> results, Deployer<?, ?> deployer, Map<C
}
}

private void addRecursively(LinkedList<ApplicationMetaDataProvider> results, Map<Class<?>, ApplicationMetaDataProvider> providers, ApplicationMetaDataProvider provider) {

private void addRecursively(LinkedList<ApplicationMetaDataProvider<?>> results,
Map<Class<?>, ApplicationMetaDataProvider<?>> providers, ApplicationMetaDataProvider<?> provider) {
results.addFirst(provider);

for (Class<?> type : provider.getMetaData().requires()) {
Expand All @@ -960,8 +961,10 @@ private void addRecursively(LinkedList<ApplicationMetaDataProvider> results, Map
}
}


@Override
public ModuleInfo prepareModule(List<EngineInfo<?, ?>> sortedEngineInfos, String moduleName, DeploymentContext context, ProgressTracker tracker) throws Exception {
public ModuleInfo prepareModule(List<EngineInfo<?, ?>> sortedEngineInfos, String moduleName,
DeploymentContext context, ProgressTracker tracker) throws Exception {
ActionReport report = context.getActionReport();
List<EngineRef> addedEngines = new ArrayList<>();

Expand Down Expand Up @@ -1053,33 +1056,33 @@ public ModuleInfo prepareModule(List<EngineInfo<?, ?>> sortedEngineInfos, String
return containersInfo;
}

@SuppressWarnings("unchecked")
protected boolean startContainers(Collection<EngineInfo<?, ?>> containersInfo, Logger logger, DeploymentContext context) {

ActionReport report = context.getActionReport();
for (EngineInfo<?, ?> engineInfo : containersInfo) {
Container container;
try {
container = engineInfo.getContainer();
} catch (Exception e) {
logger.log(SEVERE, KernelLoggerInfo.cantStartContainer, new Object[] { engineInfo.getSniffer().getModuleType(), e });
LogRecord log = new LogRecord(SEVERE, KernelLoggerInfo.cantStartContainer);
log.setParameters(new Object[] {engineInfo.getSniffer().getModuleType()});
log.setThrown(e);
LOG.log(log);
return false;
}

Class<? extends Deployer<?, ?>> deployerClass = (Class<? extends Deployer<?, ?>>) container.getDeployer();
@SuppressWarnings("rawtypes")
Deployer deployer;
Class<?> deployerClass = container.getDeployer();
Deployer<?, ?> deployer;
try {
deployer = serviceLocator.getService(deployerClass);
engineInfo.setDeployer(deployer);
deployer = (Deployer<?, ?>) serviceLocator.getService(deployerClass);
engineInfo.setDeployer((Deployer) deployer);
} catch (MultiException e) {
report.failure(logger, "Cannot instantiate or inject " + deployerClass, e);
engineInfo.stop(logger);
return false;
} catch (ClassCastException e) {
engineInfo.stop(logger);
report.failure(logger,
deployerClass + " does not implement " + " the org.jvnet.glassfish.api.deployment.Deployer interface", e);
report.failure(logger, deployerClass + " does not implement "
+ " the org.jvnet.glassfish.api.deployment.Deployer interface", e);
return false;
}
}
Expand Down Expand Up @@ -1139,12 +1142,12 @@ public void undeploy(String appName, ExtendedDeploymentContext context) {
ApplicationInfo info = appRegistry.get(appName);
if (info == null) {
report.failure(context.getLogger(), "Application " + appName + " not registered", null);
events.send(new Event(Deployment.UNDEPLOYMENT_FAILURE, context));
events.send(new Event<>(Deployment.UNDEPLOYMENT_FAILURE, context));
return;

}

events.send(new Event(Deployment.UNDEPLOYMENT_START, info));
events.send(new Event<>(Deployment.UNDEPLOYMENT_START, info));

// for DAS target, the undeploy should unload the application
// as well
Expand All @@ -1153,10 +1156,10 @@ public void undeploy(String appName, ExtendedDeploymentContext context) {
}

if (report.getActionExitCode().equals(ActionReport.ExitCode.SUCCESS)) {
events.send(new Event(Deployment.UNDEPLOYMENT_SUCCESS, context));
events.send(new Event<>(Deployment.UNDEPLOYMENT_SUCCESS, context));
deploymentLifecycleProbeProvider.applicationUndeployedEvent(appName, getApplicationType(info));
} else {
events.send(new Event(Deployment.UNDEPLOYMENT_FAILURE, context));
events.send(new Event<>(Deployment.UNDEPLOYMENT_FAILURE, context));
}

appRegistry.remove(appName);
Expand Down Expand Up @@ -1218,7 +1221,8 @@ public void registerAppInDomainXML(final ApplicationInfo applicationInfo, final
if (!DeploymentUtils.isDomainTarget(deployParams.target)) {
targets.add(deployParams.target);
} else {
List<String> previousTargets = context.getTransientAppMetaData(DeploymentProperties.PREVIOUS_TARGETS, List.class);
List<String> previousTargets = context
.getTransientAppMetaData(DeploymentProperties.PREVIOUS_TARGETS, List.class);
if (previousTargets == null) {
previousTargets = domain.getAllReferencedTargetsForApplication(deployParams.name);
}
Expand All @@ -1227,10 +1231,10 @@ public void registerAppInDomainXML(final ApplicationInfo applicationInfo, final

String origVS = deployParams.virtualservers;
Boolean origEnabled = deployParams.enabled;
Properties previousVirtualServers = context.getTransientAppMetaData(DeploymentProperties.PREVIOUS_VIRTUAL_SERVERS,
Properties.class);
Properties previousEnabledAttributes = context.getTransientAppMetaData(DeploymentProperties.PREVIOUS_ENABLED_ATTRIBUTES,
Properties.class);
Properties previousVirtualServers = context
.getTransientAppMetaData(DeploymentProperties.PREVIOUS_VIRTUAL_SERVERS, Properties.class);
Properties previousEnabledAttributes = context
.getTransientAppMetaData(DeploymentProperties.PREVIOUS_ENABLED_ATTRIBUTES, Properties.class);
for (String target : targets) {
// first reset the virtualservers, enabled attribute
deployParams.virtualservers = origVS;
Expand Down Expand Up @@ -1291,8 +1295,8 @@ public void registerAppInDomainXML(final ApplicationInfo applicationInfo, final
try {
t.commit();
} catch (RetryableException e) {
System.out.println("Retryable...");
// TODO : do something meaninful here
LOG.log(Level.INFO, "Rollbacking the transaction. Retryable...");
LOG.log(Level.FINEST, "Rollbacking the transaction.", e);
t.rollback();
} catch (TransactionFailure e) {
t.rollback();
Expand Down

0 comments on commit d8ab2bd

Please sign in to comment.