Permalink
Browse files

NSFDocumentJDBCProvider: initial commit not yet tested (just refactor…

…ed a bit after contributing)

Stlil need to have a think about being able to turn on / off the global provider because this is the part that uses the session trickery
  • Loading branch information...
camac committed Nov 26, 2017
1 parent dc9e605 commit 77d9f9dcdc46c71ece6b1a573316955d13bfd9d4
@@ -6,6 +6,12 @@ Bundle-Version: 9.0.1.v00_16_qualifier
Bundle-Vendor: IBM
Bundle-ActivationPolicy: lazy
Require-Bundle: org.eclipse.core.runtime,
com.ibm.xsp.extlib
com.ibm.xsp.extlib,
com.ibm.xsp.extlib.relational,
com.ibm.xsp.core,
com.ibm.commons,
com.ibm.domino.napi,
com.ibm.designer.runtime,
com.ibm.xsp.domino
Bundle-Activator: com.ibm.xsp.extlibx.plugin.ExtLibXCoreActivator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
@@ -0,0 +1,99 @@
package com.ibm.xsp.extlibx.relational.jdbc;
import com.ibm.commons.log.LogMgr;
import com.ibm.commons.util.StringUtil;
import com.ibm.domino.napi.NException;
import com.ibm.domino.napi.c.NotesUtil;
import com.ibm.domino.napi.c.Os;
import com.ibm.domino.napi.c.xsp.XSPNative;
import com.ibm.xsp.application.ApplicationEx;
import com.ibm.xsp.application.events.ApplicationListener;
import com.ibm.xsp.extlib.log.ExtlibCoreLogger;
import lotus.domino.NotesException;
import lotus.domino.Session;
public class ApplicationInitializer implements ApplicationListener {
private static final LogMgr logger = ExtlibCoreLogger.RELATIONAL;
public static final String XSPPROP_USERNAME = "horizon.jdbc.nsfdocumentprovider.username";
@Override
public void applicationCreated(ApplicationEx application) {
String userName = application.getApplicationProperty(XSPPROP_USERNAME, "");
if (StringUtil.isEmpty(userName)) {
logger.info("JDBC Connection Details Application Initializer Not Running - No Username specified");
return;
} else {
logger.info("JDBC Connection Details Application Initializer Running with credentials for {}", userName);
}
Session mySession = null;
long userHandle = 0L;
try {
userHandle = NotesUtil.createUserNameList(userName);
mySession = XSPNative.createXPageSessionExt(userName, userHandle, false, true, true);
} catch (Exception e) {
logger.error("Error creating a Notes Session in ApplicationInitializer", e);
}
if (mySession != null) {
//DominoSession.registerSession(mySession);
}
try {
JdbcDataSourceProvider.resetGlobalProvider();
} catch (Throwable ex) {
logger.error("Error Initialising Global NSF JDBC Provider", ex);
}
try {
JdbcDataSourceProvider.resetLocalProvider();
} catch (Throwable ex) {
logger.error("Error Initialising Local NSF JDBC Provider", ex);
}
if (mySession != null) {
try {
mySession.recycle();
} catch (NotesException e) {
logger.warn("Error During cleanup of ApplicationInitialiser Session", e);
}
}
if (userHandle != 0L) {
try {
Os.OSUnlock(userHandle);
} catch (NException e) {
logger.warn("Error During cleanup of ApplicationInitialiser Session", e);
}
try {
Os.OSMemFree(userHandle);
} catch (NException e) {
logger.warn("Error During cleanup of ApplicationInitialiser Session", e);
}
}
}
public void applicationRefreshed(ApplicationEx application) {
try {
JdbcDataSourceProvider.resetLocalProvider();
} catch (Throwable ex) {
logger.error("Error refreshing Local Provider", ex);
}
}
@Override
public void applicationDestroyed(ApplicationEx application) {
try {
JdbcDataSourceProvider.unregisterLocalProvider();
} catch (Throwable ex) {
logger.error("Error unregistering local provider", ex);
}
}
}
@@ -0,0 +1,106 @@
package com.ibm.xsp.extlibx.relational.jdbc;
import com.ibm.commons.log.LogMgr;
import com.ibm.commons.util.StringUtil;
import com.ibm.designer.runtime.resources.IResourceFactory;
import com.ibm.designer.runtime.resources.ResourceFactoriesException;
import com.ibm.designer.runtime.resources.ResourceFactoriesPool;
import com.ibm.designer.runtime.resources.ResourceFactoryProvider;
import com.ibm.xsp.extlib.log.ExtlibCoreLogger;
import com.ibm.xsp.extlib.relational.resources.IJdbcResourceFactory;
import com.ibm.xsp.extlib.relational.resources.provider.IJdbcResourceFactoryProvider;
import com.ibm.xsp.extlibx.relational.jdbc.jndi.ExtLibXJndiRegistry;
public class JdbcDataSourceProvider implements ResourceFactoryProvider {
private static final LogMgr logger = ExtlibCoreLogger.RELATIONAL;
private static final String GLOBAL_CONFIG_DB = "Horizon/Globals.nsf";
public static final String RESOURCETYPE = "HorizonJDBC";
private static Boolean globalRegistered = false;
private static IJdbcResourceFactoryProvider globalProvider = new NSFDocumentJdbcProvider(GLOBAL_CONFIG_DB);
private static IJdbcResourceFactoryProvider localProvider = new NSFDocumentJdbcProvider();
public static void resetLocalProvider() throws ResourceFactoriesException {
ExtLibXJndiRegistry.registerConnections(localProvider);
}
public static void resetGlobalProvider() throws ResourceFactoriesException {
ExtLibXJndiRegistry.registerConnections(globalProvider);
}
public static void initGlobalProvider() throws ResourceFactoriesException {
synchronized (globalRegistered) {
if (!globalRegistered) {
ExtLibXJndiRegistry.registerConnections(globalProvider);
globalRegistered = true;
}
}
}
public static void unregisterLocalProvider() throws ResourceFactoriesException {
ExtLibXJndiRegistry.unregisterConnections(localProvider);
}
public static void unregisterGlobalProvider() throws ResourceFactoriesException {
ExtLibXJndiRegistry.unregisterConnections(globalProvider);
}
public JdbcDataSourceProvider() {
logger.traceDebug("Created a JdbcDataSourceProvider");
}
public IJdbcResourceFactoryProvider getLocalProvider() {
return localProvider;
}
public IJdbcResourceFactoryProvider getGlobalProvider() {
return globalProvider;
}
@Override
public IResourceFactory loadResource(ResourceFactoriesPool pool, String type, String name, int scope)
throws ResourceFactoriesException {
if (StringUtil.equals(type, RESOURCETYPE)) {
logger.traceDebug("Being asked for Jdbc Resource '{}'", name);
switch (scope) {
case IResourceFactory.SCOPE_APPLICATION: {
// Look at the local providers
IJdbcResourceFactory f = getLocalProvider().loadResourceFactory(name);
if (f != null) {
logger.traceDebug("Found " + name + " in Local");
return f;
}
f = getGlobalProvider().loadResourceFactory(name);
if (f != null) {
logger.traceDebug("Found JDBC Resource " + name + " in Global");
return f;
}
logger.traceDebug("Could not find Jdbc Resource '{}'", name);
return null;
}
case IResourceFactory.SCOPE_GLOBAL: {
IJdbcResourceFactory f = getGlobalProvider().loadResourceFactory(name);
if (f != null) {
return f;
}
return null;
}
}
}
return null;
}
}
Oops, something went wrong.

0 comments on commit 77d9f9d

Please sign in to comment.