Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework ContainerUtil to use ComponentModule instead of ApplicationEx (fixes #424) #441

Merged
merged 5 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions eclipse/bundles/org.openntf.xsp.cdi/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Import-Package: com.ibm.commons.util,
com.ibm.commons.xml.util,
com.ibm.designer.domino.napi,
com.ibm.designer.runtime.domino.adapter,
com.ibm.designer.runtime.domino.adapter.servlet,
com.ibm.domino.xsp.adapter.osgi,
com.ibm.domino.xsp.module.nsf,
com.ibm.xsp.application,
com.ibm.xsp.application.events,
Expand Down
7 changes: 2 additions & 5 deletions eclipse/bundles/org.openntf.xsp.cdi/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@
<service type="org.openntf.xsp.cdi.discovery.WeldBeanClassContributor" class="org.openntf.xsp.cdi.bean.BuiltinBeanClassContributor" />
</extension>
<extension point="com.ibm.commons.Extension">
<service type="org.openntf.xsp.cdi.discovery.WeldBeanClassContributor" class="org.openntf.xsp.cdi.discovery.NSFComponentModuleClassContributor" />
<service type="org.openntf.xsp.cdi.discovery.WeldBeanClassContributor" class="org.openntf.xsp.cdi.discovery.ComponentModuleClassContributor" />
</extension>

<!-- Default CDI container locators -->
<extension point="com.ibm.commons.Extension">
<service type="org.openntf.xsp.cdi.ext.CDIContainerLocator" class="org.openntf.xsp.cdi.provider.ApplicationExCDIContainerLocator" />
</extension>
<extension point="com.ibm.commons.Extension">
<service type="org.openntf.xsp.cdi.ext.CDIContainerLocator" class="org.openntf.xsp.cdi.provider.ComponentModuleDatabaseCDIContainerLocator" />
<service type="org.openntf.xsp.cdi.ext.CDIContainerLocator" class="org.openntf.xsp.cdi.provider.ComponentModuleCDIContainerLocator" />
</extension>
<extension point="com.ibm.commons.Extension">
<service type="org.openntf.xsp.cdi.ext.CDIContainerLocator" class="org.openntf.xsp.cdi.provider.ThreadContextDatabasePathCDIContainerLocator" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.openntf.xsp.cdi;

import org.openntf.xsp.cdi.provider.NSFCDIProvider;
import org.openntf.xsp.cdi.provider.DominoCDIProvider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

Expand All @@ -29,7 +29,7 @@ public class CDIActivator implements BundleActivator {

@Override
public void start(BundleContext context) throws Exception {
CDI.setCDIProvider(new NSFCDIProvider());
CDI.setCDIProvider(new DominoCDIProvider());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @author Jesse Gallagher
* @since 2.9.0
*/
public class NSFComponentModuleClassContributor implements WeldBeanClassContributor {
public class ComponentModuleClassContributor implements WeldBeanClassContributor {

@Override
public Collection<Class<?>> getBeanClasses() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import javax.faces.el.VariableResolver;

import org.openntf.xsp.cdi.CDILibrary;
import org.openntf.xsp.cdi.util.ContainerUtil;
import org.openntf.xsp.jakartaee.util.LibraryUtil;

import com.ibm.commons.util.StringUtil;
Expand Down Expand Up @@ -58,9 +57,6 @@ public Object resolveVariable(FacesContext facesContext, String name) throws Eva
ApplicationEx app = ApplicationEx.getInstance(facesContext);
if(LibraryUtil.usesLibrary(CDILibrary.LIBRARY_ID, app)) {
CDI<Object> container = CDI.current();
if(container == null) {
container = ContainerUtil.getContainer(app);
}
if(container != null) {
CDI<Object> cdi = container;
return AccessController.doPrivileged((PrivilegedAction<Object>)() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,22 @@
*/
package org.openntf.xsp.cdi.impl;

import java.io.IOException;

import org.openntf.xsp.cdi.ext.CDIContainerUtility;
import org.openntf.xsp.cdi.util.ContainerUtil;
import org.osgi.framework.Bundle;

import com.ibm.designer.domino.napi.NotesAPIException;
import com.ibm.designer.domino.napi.NotesDatabase;
import com.ibm.xsp.application.ApplicationEx;
import com.ibm.designer.runtime.domino.adapter.ComponentModule;

/**
*
* @author Jesse Gallagher
* @since 1.2.0
*/
public class ContainerUtilProvider implements CDIContainerUtility {

@Override
public Object getContainer(NotesDatabase database) throws NotesAPIException, IOException {
return ContainerUtil.getContainer(database);
}


@Override
public Object getContainer(ApplicationEx app) {
return ContainerUtil.getContainer(app);
public Object getContainer(ComponentModule module) {
return ContainerUtil.getContainer(module);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/
package org.openntf.xsp.cdi.impl;

import jakarta.enterprise.inject.spi.CDI;

import org.jboss.weld.environment.se.WeldContainer;
import org.openntf.xsp.cdi.CDILibrary;
import org.openntf.xsp.cdi.util.ContainerUtil;
import org.openntf.xsp.jakartaee.module.ComponentModuleLocator;
import org.openntf.xsp.jakartaee.util.LibraryUtil;

import com.ibm.commons.util.StringUtil;
import com.ibm.xsp.application.ApplicationEx;
import com.ibm.xsp.application.events.ApplicationListener2;

Expand All @@ -42,19 +40,17 @@ public void applicationCreated(ApplicationEx application) {
@Override
public void applicationDestroyed(ApplicationEx application) {
if(LibraryUtil.usesLibrary(CDILibrary.LIBRARY_ID, application)) {
String bundleId = ContainerUtil.getApplicationCDIBundle(application);
if(StringUtil.isNotEmpty(bundleId)) {
// Leave it alive
return;
}

CDI<Object> container = ContainerUtil.getContainerUnchecked(application);
if(container instanceof WeldContainer) {
WeldContainer c = (WeldContainer)container;
if(c.isRunning()) {
c.close();
}
}
ComponentModuleLocator.getDefault()
.map(ComponentModuleLocator::getActiveModule)
.map(ContainerUtil::getContainerUnchecked)
.ifPresent(container -> {
if(container instanceof WeldContainer) {
WeldContainer c = (WeldContainer)container;
if(c.isRunning()) {
c.close();
}
}
});
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
*/
package org.openntf.xsp.cdi.provider;

import java.io.IOException;

import org.openntf.xsp.cdi.ext.CDIContainerLocator;
import org.openntf.xsp.cdi.ext.CDIContainerUtility;
import org.openntf.xsp.jakartaee.module.ComponentModuleLocator;
import org.openntf.xsp.jakartaee.util.LibraryUtil;

import com.ibm.designer.domino.napi.NotesAPIException;
import com.ibm.designer.runtime.domino.adapter.ComponentModule;

import jakarta.annotation.Priority;
Expand All @@ -35,21 +32,15 @@
* @since 2.10.0
*/
@Priority(1)
public class ComponentModuleDatabaseCDIContainerLocator implements CDIContainerLocator {
public class ComponentModuleCDIContainerLocator implements CDIContainerLocator {

@Override
public Object getContainer() {
CDIContainerUtility util = LibraryUtil.findRequiredExtension(CDIContainerUtility.class);

return ComponentModuleLocator.getDefault()
.flatMap(ComponentModuleLocator::getNotesDatabase)
.map(t -> {
try {
return util.getContainer(t);
} catch (NotesAPIException | IOException e) {
return null;
}
})
.map(ComponentModuleLocator::getActiveModule)
.map(util::getContainer)
.orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.openntf.xsp.cdi.provider;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
Expand All @@ -24,30 +23,26 @@
import org.openntf.xsp.cdi.ext.CDIContainerLocator;
import org.openntf.xsp.cdi.ext.CDIContainerUtility;
import org.openntf.xsp.jakartaee.util.LibraryUtil;
import org.openntf.xsp.jakartaee.util.ModuleUtil;
import org.osgi.framework.Bundle;

import com.ibm.commons.util.StringUtil;
import com.ibm.designer.domino.napi.NotesAPIException;
import com.ibm.designer.domino.napi.NotesDatabase;
import com.ibm.designer.domino.napi.NotesSession;

import jakarta.enterprise.inject.spi.CDI;
import jakarta.enterprise.inject.spi.CDIProvider;

/**
* Provides access to the current application's Weld context.
* Provides access to the current application's CDI context.
*
* @author Jesse Gallagher
* @since 1.0.0
*/
public class NSFCDIProvider implements CDIProvider {
private static final Logger log = Logger.getLogger(NSFCDIProvider.class.getPackage().getName());
public class DominoCDIProvider implements CDIProvider {
private static final Logger log = Logger.getLogger(DominoCDIProvider.class.getPackage().getName());

@SuppressWarnings("unchecked")
@Override
public synchronized CDI<Object> getCDI() {
CDI<Object> result = null;

CDIContainerUtility util = LibraryUtil.findRequiredExtension(CDIContainerUtility.class);

// Check in any available locator extensions
Expand All @@ -61,28 +56,15 @@ public synchronized CDI<Object> getCDI() {

String nsfPath = locator.getNsfPath();
if(StringUtil.isNotEmpty(nsfPath)) {
NotesSession session = new NotesSession();
try {
NotesDatabase database = session.getDatabaseByPath(nsfPath);
try {
database.open();
result = (CDI<Object>)util.getContainer(database);
if(result != null) {
return result;
}
} finally {
if(database != null) {
database.recycle();
}
}
} catch (NotesAPIException | IOException e) {
// Log and move on
e.printStackTrace();
} finally {
session.recycle();
container = ModuleUtil.getNSFComponentModule(nsfPath)
.map(util::getContainer)
.orElse(null);
if(container != null) {
return (CDI<Object>)container;
}
}


String bundleId = locator.getBundleId();
if(StringUtil.isNotEmpty(bundleId)) {
Optional<Bundle> bundle = LibraryUtil.getBundle(bundleId);
Expand All @@ -91,8 +73,6 @@ public synchronized CDI<Object> getCDI() {
}
}
}
} catch (NotesAPIException e) {
// Ignore
} catch(IllegalStateException e) {
// Will almost definitely be "Invalid disposed application ClassLoader", which occurs
// during active development of an NSF - ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

/**
* This {@link CDIContainerLocator} looks for a thread-context database path,
* which may be specified as an overried by user applications.
* which may be specified as an override by user applications.
*
* @author Jesse Gallagher
* @since 2.8.0
*/
@Priority(3)
@Priority(100)
public class ThreadContextDatabasePathCDIContainerLocator implements CDIContainerLocator {

@Override
Expand Down
Loading