use of org.openntf.xsp.cdi.ext.CDIContainerLocator in project org.openntf.xsp.jakartaee by OpenNTF.
the class NSFCDIProvider method getCDI.
@SuppressWarnings("unchecked")
@Override
public synchronized CDI<Object> getCDI() {
CDI<Object> result = null;
CDIContainerUtility util = LibraryUtil.findRequiredExtension(CDIContainerUtility.class);
String databasePath = util.getThreadContextDatabasePath();
if (StringUtil.isNotEmpty(databasePath)) {
try {
NotesSession session = new NotesSession();
try {
NotesDatabase database = session.getDatabase(databasePath);
if (database != null) {
database.open();
try {
result = (CDI<Object>) util.getContainer(database);
} finally {
database.recycle();
}
}
} finally {
session.recycle();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
if (result != null) {
return result;
}
ApplicationEx application = ApplicationEx.getInstance();
if (application != null) {
result = (CDI<Object>) util.getContainer(application);
}
if (result != null) {
return result;
}
try {
NotesDatabase database = ContextInfo.getServerDatabase();
if (database != null) {
result = (CDI<Object>) util.getContainer(database);
}
} catch (Throwable t) {
t.printStackTrace();
}
if (result != null) {
return result;
}
// Check in any available locator extensions
List<CDIContainerLocator> locators = LibraryUtil.findExtensions(CDIContainerLocator.class);
NotesSession session = new NotesSession();
try {
for (CDIContainerLocator locator : locators) {
String nsfPath = locator.getNsfPath();
if (StringUtil.isNotEmpty(nsfPath)) {
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();
}
}
String bundleId = locator.getBundleId();
if (StringUtil.isNotEmpty(bundleId)) {
Optional<Bundle> bundle = LibraryUtil.getBundle(bundleId);
if (bundle.isPresent()) {
return (CDI<Object>) util.getContainer(bundle.get());
}
}
}
} finally {
try {
session.recycle();
} catch (NotesAPIException e) {
// Ignore
}
}
return null;
}
Aggregations