Search in sources :

Example 1 with CDIContainerUtility

use of org.openntf.xsp.cdi.ext.CDIContainerUtility 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;
}
Also used : CDIContainerUtility(org.openntf.xsp.cdi.ext.CDIContainerUtility) CDIContainerLocator(org.openntf.xsp.cdi.ext.CDIContainerLocator) Bundle(org.osgi.framework.Bundle) CDI(jakarta.enterprise.inject.spi.CDI) IOException(java.io.IOException) NotesDatabase(com.ibm.designer.domino.napi.NotesDatabase) NotesSession(com.ibm.designer.domino.napi.NotesSession) ApplicationEx(com.ibm.xsp.application.ApplicationEx) NotesAPIException(com.ibm.designer.domino.napi.NotesAPIException)

Aggregations

NotesAPIException (com.ibm.designer.domino.napi.NotesAPIException)1 NotesDatabase (com.ibm.designer.domino.napi.NotesDatabase)1 NotesSession (com.ibm.designer.domino.napi.NotesSession)1 ApplicationEx (com.ibm.xsp.application.ApplicationEx)1 CDI (jakarta.enterprise.inject.spi.CDI)1 IOException (java.io.IOException)1 CDIContainerLocator (org.openntf.xsp.cdi.ext.CDIContainerLocator)1 CDIContainerUtility (org.openntf.xsp.cdi.ext.CDIContainerUtility)1 Bundle (org.osgi.framework.Bundle)1