use of org.glassfish.api.container.Sniffer in project Payara by payara.
the class ArchivistFactory method getExtensionsArchivists.
/**
* Gets all the classes for processing parts of a specified archive
* @param sniffers
* @param moduleType
* @return
* @see org.glassfish.webservices.archivist.WebServicesArchivist
* @see org.glassfish.ejb.deployment.archivist.EjbInWarArchivist
* @see com.sun.enterprise.deployment.archivist.PersistenceArchivist
*/
@SuppressWarnings("unchecked")
public List<ExtensionsArchivist> getExtensionsArchivists(Collection<Sniffer> sniffers, ArchiveType moduleType) {
Set<String> containerTypes = new HashSet<String>();
for (Sniffer sniffer : sniffers) {
containerTypes.add(sniffer.getModuleType());
}
List<ExtensionsArchivist> archivists = new ArrayList<ExtensionsArchivist>();
for (String containerType : containerTypes) {
List<ActiveDescriptor<?>> descriptors = habitat.getDescriptors(new ArchivistFilter(containerType, EXTENSION_ARCHIVE_TYPE, ExtensionsArchivist.class));
for (ActiveDescriptor<?> item : descriptors) {
ActiveDescriptor<ExtensionsArchivist> descriptor = (ActiveDescriptor<ExtensionsArchivist>) item;
ServiceHandle<ExtensionsArchivist> handle = habitat.getServiceHandle(descriptor);
ExtensionsArchivist ea = handle.getService();
if (ea.supportsModuleType(moduleType)) {
archivists.add(ea);
}
}
}
return archivists;
}
use of org.glassfish.api.container.Sniffer in project Payara by payara.
the class ApplicationLifecycle method setupContainerInfos.
/**
* set up containers and prepare the sorted ModuleInfos
* @param handler
* @param sniffers
* @param context
* @return
* @throws java.lang.Exception
*/
@Override
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);
Map<Deployer, EngineInfo> containerInfosByDeployers = new LinkedHashMap<Deployer, EngineInfo>();
for (Sniffer sniffer : sniffers) {
if (sniffer.getContainersNames() == null || sniffer.getContainersNames().length == 0) {
report.failure(logger, "no container associated with application of type : " + sniffer.getModuleType(), null);
return null;
}
final String containerName = sniffer.getContainersNames()[0];
if (tracing != null) {
tracing.addContainerMark(DeploymentTracing.ContainerMark.SNIFFER_DONE, containerName);
}
// start all the containers associated with sniffers.
EngineInfo engineInfo = containerRegistry.getContainer(containerName);
if (engineInfo == null) {
// need to synchronize on the registry to not end up starting the same container from
// different threads.
Collection<EngineInfo> containersInfo = null;
synchronized (containerRegistry) {
if (containerRegistry.getContainer(containerName) == null) {
if (tracing != null) {
tracing.addContainerMark(DeploymentTracing.ContainerMark.BEFORE_CONTAINER_SETUP, containerName);
}
containersInfo = setupContainer(sniffer, logger, context);
if (tracing != null) {
tracing.addContainerMark(DeploymentTracing.ContainerMark.AFTER_CONTAINER_SETUP, containerName);
}
if (containersInfo == null || containersInfo.size() == 0) {
String msg = "Cannot start container(s) associated to application of type : " + sniffer.getModuleType();
report.failure(logger, msg, null);
throw new Exception(msg);
}
}
}
// now start all containers, by now, they should be all setup...
if (containersInfo != null && !startContainers(containersInfo, logger, context)) {
final String msg = "Aborting, Failed to start container " + containerName;
report.failure(logger, msg, null);
throw new Exception(msg);
}
}
engineInfo = containerRegistry.getContainer(sniffer.getContainersNames()[0]);
if (tracing != null) {
tracing.addContainerMark(DeploymentTracing.ContainerMark.GOT_CONTAINER, containerName);
}
if (engineInfo == null) {
final String msg = "Aborting, Failed to start container " + containerName;
report.failure(logger, msg, null);
throw new Exception(msg);
}
Deployer deployer = getDeployer(engineInfo);
if (deployer == null) {
if (!startContainers(Collections.singleton(engineInfo), logger, context)) {
final String msg = "Aborting, Failed to start container " + containerName;
report.failure(logger, msg, null);
throw new Exception(msg);
}
deployer = getDeployer(engineInfo);
if (deployer == null) {
report.failure(logger, "Got a null deployer out of the " + engineInfo.getContainer().getClass() + " container, is it annotated with @Service ?");
return null;
}
}
if (tracing != null) {
tracing.addContainerMark(DeploymentTracing.ContainerMark.GOT_DEPLOYER, containerName);
}
containerInfosByDeployers.put(deployer, engineInfo);
}
// all containers that have recognized parts of the application being deployed
// have now been successfully started. Start the deployment process.
List<ApplicationMetaDataProvider> providers = new LinkedList<ApplicationMetaDataProvider>();
providers.addAll(habitat.<ApplicationMetaDataProvider>getAllServices(ApplicationMetaDataProvider.class));
List<EngineInfo> sortedEngineInfos = new ArrayList<EngineInfo>();
Map<Class, ApplicationMetaDataProvider> typeByProvider = new HashMap<Class, ApplicationMetaDataProvider>();
for (ApplicationMetaDataProvider provider : habitat.<ApplicationMetaDataProvider>getAllServices(ApplicationMetaDataProvider.class)) {
if (provider.getMetaData() != null) {
for (Class provided : provider.getMetaData().provides()) {
typeByProvider.put(provided, provider);
}
}
}
// check if everything is provided.
for (ApplicationMetaDataProvider provider : habitat.<ApplicationMetaDataProvider>getAllServices(ApplicationMetaDataProvider.class)) {
if (provider.getMetaData() != null) {
for (Class dependency : provider.getMetaData().requires()) {
if (!typeByProvider.containsKey(dependency)) {
// at this point, I only log problems, because it maybe that what I am deploying now
// will not require this application metadata.
logger.log(Level.WARNING, KernelLoggerInfo.applicationMetaDataProvider, new Object[] { provider, dependency });
}
}
}
}
Map<Class, Deployer> typeByDeployer = new HashMap<Class, Deployer>();
for (Deployer deployer : containerInfosByDeployers.keySet()) {
if (deployer.getMetaData() != null) {
for (Class provided : deployer.getMetaData().provides()) {
typeByDeployer.put(provided, deployer);
}
}
}
for (Deployer deployer : containerInfosByDeployers.keySet()) {
if (deployer.getMetaData() != null) {
for (Class dependency : deployer.getMetaData().requires()) {
if (!typeByDeployer.containsKey(dependency) && !typeByProvider.containsKey(dependency)) {
Service s = deployer.getClass().getAnnotation(Service.class);
String serviceName;
if (s != null && s.name() != null && s.name().length() > 0) {
serviceName = s.name();
} else {
serviceName = deployer.getClass().getSimpleName();
}
report.failure(logger, serviceName + " deployer requires " + dependency + " but no other deployer provides it", null);
return null;
}
}
}
}
// ok everything is satisfied, just a matter of running things in order
List<Deployer> orderedDeployers = new ArrayList<Deployer>();
for (Deployer deployer : containerInfosByDeployers.keySet()) {
if (logger.isLoggable(Level.FINE)) {
logger.fine("Keyed Deployer " + deployer.getClass());
}
loadDeployer(orderedDeployers, deployer, typeByDeployer, typeByProvider, context);
}
// now load metadata from deployers.
for (Deployer deployer : orderedDeployers) {
if (logger.isLoggable(Level.FINE)) {
logger.fine("Ordered Deployer " + deployer.getClass());
}
final MetaData metadata = deployer.getMetaData();
try {
if (metadata != null) {
if (metadata.provides() == null || metadata.provides().length == 0) {
deployer.loadMetaData(null, context);
} else {
for (Class<?> provide : metadata.provides()) {
if (context.getModuleMetaData(provide) == null) {
context.addModuleMetaData(deployer.loadMetaData(provide, context));
} else {
deployer.loadMetaData(null, context);
}
}
}
} else {
deployer.loadMetaData(null, context);
}
} catch (Exception e) {
report.failure(logger, "Exception while invoking " + deployer.getClass() + " prepare method", e);
throw e;
}
sortedEngineInfos.add(containerInfosByDeployers.get(deployer));
}
return sortedEngineInfos;
}
use of org.glassfish.api.container.Sniffer in project Payara by payara.
the class ApplicationLifecycle method prepare.
@Override
public ApplicationDeployment prepare(Collection<? extends Sniffer> sniffers, final ExtendedDeploymentContext context) {
events.send(new Event<>(Deployment.DEPLOYMENT_START, context), false);
currentDeploymentContext.get().push(context);
final ActionReport report = context.getActionReport();
final DeployCommandParameters commandParams = context.getCommandParameters(DeployCommandParameters.class);
final String appName = commandParams.name();
ApplicationInfo appInfo;
final ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
ProgressTracker tracker = new ProgressTracker() {
@Override
public void actOn(Logger logger) {
// loaded but may not be started. Issue 18263
for (EngineRef module : get("loaded", EngineRef.class)) {
try {
module.stop(context);
} catch (Exception e) {
// ignore
}
}
try {
PreDestroy.class.cast(context).preDestroy();
} catch (Exception e) {
// ignore
}
for (EngineRef module : get("loaded", EngineRef.class)) {
try {
module.unload(context);
} catch (Exception e) {
// ignore
}
}
try {
ApplicationInfo appInfo = appRegistry.get(appName);
if (appInfo != null) {
// send the event to close necessary resources
events.send(new Event<ApplicationInfo>(Deployment.APPLICATION_DISABLED, appInfo));
}
} catch (Exception e) {
// ignore
}
for (EngineRef module : get("prepared", EngineRef.class)) {
try {
module.clean(context);
} catch (Exception e) {
// ignore
}
}
if (!commandParams.keepfailedstubs) {
try {
context.clean();
} catch (Exception e) {
// ignore
}
}
appRegistry.remove(appName);
}
};
try {
if (commandParams.origin == OpsParams.Origin.deploy && appRegistry.get(appName) != null) {
report.setMessage(localStrings.getLocalString("appnamenotunique", "Application name {0} is already in use. Please pick a different name.", appName));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return null;
}
// defined virtual servers minus __asadmin on that target
if (commandParams.virtualservers == null) {
commandParams.virtualservers = DeploymentUtils.getVirtualServers(commandParams.target, env, domain);
}
if (commandParams.enabled == null) {
commandParams.enabled = Boolean.TRUE;
}
if (commandParams.altdd != null) {
context.getSource().addArchiveMetaData(DeploymentProperties.ALT_DD, commandParams.altdd);
}
if (commandParams.runtimealtdd != null) {
context.getSource().addArchiveMetaData(DeploymentProperties.RUNTIME_ALT_DD, commandParams.runtimealtdd);
}
context.addTransientAppMetaData(ExtendedDeploymentContext.TRACKER, tracker);
context.setPhase(DeploymentContextImpl.Phase.PREPARE);
ArchiveHandler handler = context.getArchiveHandler();
if (handler == null) {
handler = getArchiveHandler(context.getSource(), commandParams.type);
context.setArchiveHandler(handler);
}
if (handler == null) {
report.setMessage(localStrings.getLocalString("unknownarchivetype", "Archive type of {0} was not recognized", context.getSourceDir()));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return null;
}
DeploymentTracing tracing = context.getModuleMetaData(DeploymentTracing.class);
if (tracing != null) {
tracing.addMark(DeploymentTracing.Mark.ARCHIVE_HANDLER_OBTAINED);
}
if (handler.requiresAnnotationScanning(context.getSource())) {
getDeployableTypes(context);
}
if (tracing != null) {
tracing.addMark(DeploymentTracing.Mark.PARSING_DONE);
}
// is that some container do not support to be restarted.
if (sniffers != null && logger.isLoggable(Level.FINE)) {
for (Sniffer sniffer : sniffers) {
logger.fine("Before Sorting" + sniffer.getModuleType());
}
}
sniffers = getSniffers(handler, sniffers, context);
ClassLoaderHierarchy clh = habitat.getService(ClassLoaderHierarchy.class);
if (tracing != null) {
tracing.addMark(DeploymentTracing.Mark.CLASS_LOADER_HIERARCHY);
}
context.createDeploymentClassLoader(clh, handler);
events.send(new Event<DeploymentContext>(Deployment.AFTER_DEPLOYMENT_CLASSLOADER_CREATION, context), false);
if (tracing != null) {
tracing.addMark(DeploymentTracing.Mark.CLASS_LOADER_CREATED);
}
Thread.currentThread().setContextClassLoader(context.getClassLoader());
List<EngineInfo> sortedEngineInfos = setupContainerInfos(handler, sniffers, context);
if (tracing != null) {
tracing.addMark(DeploymentTracing.Mark.CONTAINERS_SETUP_DONE);
}
if (logger.isLoggable(Level.FINE)) {
for (EngineInfo info : sortedEngineInfos) {
logger.fine("After Sorting " + info.getSniffer().getModuleType());
}
}
if (sortedEngineInfos == null || sortedEngineInfos.isEmpty()) {
report.failure(logger, localStrings.getLocalString("unknowncontainertype", "There is no installed container capable of handling this application {0}", context.getSource().getName()));
tracker.actOn(logger);
return null;
}
// create a temporary application info to hold metadata
// so the metadata could be accessed at classloader
// construction time through ApplicationInfo
ApplicationInfo tempAppInfo = new ApplicationInfo(events, context.getSource(), appName);
for (Object m : context.getModuleMetadata()) {
tempAppInfo.addMetaData(m);
}
tempAppInfo.setIsJavaEEApp(sortedEngineInfos);
// set the flag on the archive to indicate whether it's
// a JavaEE archive or not
context.getSource().setExtraData(Boolean.class, tempAppInfo.isJavaEEApp());
appRegistry.add(appName, tempAppInfo);
try {
notifyLifecycleInterceptorsBefore(ExtendedDeploymentContext.Phase.PREPARE, context);
} catch (Throwable interceptorException) {
report.failure(logger, "Exception while invoking the lifecycle interceptor", null);
report.setFailureCause(interceptorException);
logger.log(Level.SEVERE, KernelLoggerInfo.lifecycleException, interceptorException);
tracker.actOn(logger);
return null;
}
events.send(new Event<DeploymentContext>(Deployment.DEPLOYMENT_BEFORE_CLASSLOADER_CREATION, context), false);
context.createApplicationClassLoader(clh, handler);
events.send(new Event<DeploymentContext>(Deployment.AFTER_APPLICATION_CLASSLOADER_CREATION, context), false);
if (tracing != null) {
tracing.addMark(DeploymentTracing.Mark.CLASS_LOADER_CREATED);
}
// this is a first time deployment as opposed as load following an unload event,
// we need to create the application info
// todo : we should come up with a general Composite API solution
ModuleInfo moduleInfo = null;
try {
moduleInfo = prepareModule(sortedEngineInfos, appName, context, tracker);
// Now that the prepare phase is done, any artifacts
// should be available. Go ahead and create the
// downloadable client JAR. We want to do this now, or
// at least before the load and start phases, because
// (for example) the app client deployer start phase
// needs to find all generated files when it runs.
final ClientJarWriter cjw = new ClientJarWriter(context);
cjw.run();
} catch (Throwable prepareException) {
report.failure(logger, "Exception while preparing the app", null);
report.setFailureCause(prepareException);
logger.log(Level.SEVERE, KernelLoggerInfo.lifecycleException, prepareException);
tracker.actOn(logger);
return null;
}
// the deployer did not take care of populating the application info, this
// is not a composite module.
appInfo = context.getModuleMetaData(ApplicationInfo.class);
if (appInfo == null) {
appInfo = new ApplicationInfo(events, context.getSource(), appName);
appInfo.addModule(moduleInfo);
for (Object m : context.getModuleMetadata()) {
moduleInfo.addMetaData(m);
appInfo.addMetaData(m);
}
} else {
for (EngineRef ref : moduleInfo.getEngineRefs()) {
appInfo.add(ref);
}
}
// remove the temp application info from the registry
// first, then register the real one
appRegistry.remove(appName);
appInfo.setIsJavaEEApp(sortedEngineInfos);
appRegistry.add(appName, appInfo);
notifyLifecycleInterceptorsAfter(ExtendedDeploymentContext.Phase.PREPARE, context);
if (tracing != null) {
tracing.addMark(DeploymentTracing.Mark.PREPARED);
}
// send the APPLICATION_PREPARED event
// set the phase and thread context classloader properly
// before sending the event
context.setPhase(DeploymentContextImpl.Phase.PREPARED);
Thread.currentThread().setContextClassLoader(context.getClassLoader());
appInfo.setAppClassLoader(context.getClassLoader());
events.send(new Event<DeploymentContext>(Deployment.APPLICATION_PREPARED, context), false);
if (loadOnCurrentInstance(context)) {
appInfo.setLibraries(commandParams.libraries());
try {
notifyLifecycleInterceptorsBefore(ExtendedDeploymentContext.Phase.LOAD, context);
appInfo.load(context, tracker);
notifyLifecycleInterceptorsAfter(ExtendedDeploymentContext.Phase.LOAD, context);
} catch (Throwable loadException) {
logger.log(Level.SEVERE, KernelLoggerInfo.lifecycleException, loadException);
report.failure(logger, "Exception while loading the app", null);
report.setFailureCause(loadException);
tracker.actOn(logger);
return null;
}
}
} catch (Exception e) {
report.failure(logger, localStrings.getLocalString("error.deploying.app", "Exception while deploying the app [{0}]", appName), null);
report.setFailureCause(e);
logger.log(Level.SEVERE, KernelLoggerInfo.lifecycleException, e);
tracker.actOn(logger);
return null;
} finally {
Thread.currentThread().setContextClassLoader(currentCL);
if (report.getActionExitCode() != ActionReport.ExitCode.SUCCESS) {
context.postDeployClean(false);
events.send(new Event<>(Deployment.DEPLOYMENT_FAILURE, context));
}
}
ApplicationDeployment depl = new ApplicationDeployment(appInfo, context);
appRegistry.addTransient(depl);
return depl;
}
use of org.glassfish.api.container.Sniffer in project Payara by payara.
the class EJBContainerProviderImpl method createContainer.
private Locations createContainer(Map<?, ?> properties, Locations l) throws EJBException {
synchronized (lock) {
// if (container == null || !container.isOpen()) {
try {
if (runtime != null) {
// dispose of the old one
runtime.shutdown();
}
BootstrapProperties bootstrapProperties = new BootstrapProperties();
// Propagate non EJB embeddable container properties into GlassFishProperties
Properties newProps = new Properties();
if (properties != null) {
copyUserProperties(properties, newProps);
}
// Disable weaving if it is not spesified
if (newProps.getProperty(WEAVING) == null) {
newProps.setProperty(WEAVING, "false");
}
GlassFishProperties glassFishProperties = new GlassFishProperties(newProps);
if (Boolean.getBoolean(KEEP_TEMPORARY_FILES)) {
// set autodelete to false.
glassFishProperties.setProperty("org.glassfish.embeddable.autoDelete", "false");
// make sure the domain.xml is written back.
glassFishProperties.setConfigFileReadOnly(false);
}
if (l.installed_root != null && l.instance_root != null) {
// Real install
_logger.info("[EJBContainerProviderImpl] Using installation location " + l.installed_root.getCanonicalPath());
bootstrapProperties.setInstallRoot(l.installed_root.getCanonicalPath());
}
if (l.instance_root != null && l.reuse_instance_location) {
if (_logger.isLoggable(Level.FINE)) {
_logger.fine("[EJBContainerProviderImpl] Reusing instance location at: " + l.instance_root);
}
_logger.info("[EJBContainerProviderImpl] Using instance location: " + l.instance_root.getCanonicalPath());
glassFishProperties.setInstanceRoot(l.instance_root.getCanonicalPath());
} else if (l.domain_file != null) {
_logger.info("[EJBContainerProviderImpl] Using config file location: " + l.domain_file.toURI().toString());
glassFishProperties.setConfigFileURI(l.domain_file.toURI().toString());
}
addWebContainerIfRequested(properties, glassFishProperties);
runtime = GlassFishRuntime.bootstrap(bootstrapProperties);
_logger.info("[EJBContainerProviderImpl] Using runtime class: " + runtime.getClass());
GlassFish server = runtime.newGlassFish(glassFishProperties);
if (l.instance_root != null && !l.reuse_instance_location) {
// XXX Start the server to get the services
server.start();
EmbeddedSecurity es = server.getService(EmbeddedSecurity.class);
ServiceLocator habitat = server.getService(ServiceLocator.class);
server.stop();
// If we are running from an existing install, copy over security files to the temp instance
if (es != null) {
es.copyConfigFiles(habitat, l.instance_root, l.domain_file);
}
}
// server is started in EJBContainerImpl constructor
container = new EJBContainerImpl(server);
validateInstanceDirectory();
archiveFactory = server.getService(ArchiveFactory.class);
Sniffer sniffer = server.getService(Sniffer.class, "Ejb");
ejbAnnotations = sniffer.getAnnotationTypes();
} catch (Exception e) {
try {
if (container != null) {
container.stop();
}
} catch (Exception e0) {
_logger.log(Level.SEVERE, e0.getMessage(), e0);
}
container = null;
throw new EJBException(e);
}
// }
}
return l;
}
use of org.glassfish.api.container.Sniffer in project Payara by payara.
the class ExistingDomainTest method Test.
@Test
public void Test() {
ServiceLocator habitat = server.getHabitat();
System.out.println("Process type is " + habitat.<ProcessEnvironment>getService(ProcessEnvironment.class).getProcessType());
for (Sniffer s : habitat.<Sniffer>getAllServices(Sniffer.class)) {
System.out.println("Got sniffer " + s.getModuleType());
}
}
Aggregations