From 23cd7769d4c63fdf5cf2db175c49166c5b246f76 Mon Sep 17 00:00:00 2001 From: Laramie Crocker Date: Wed, 22 Dec 2010 20:35:20 +0000 Subject: [PATCH] CSPACE-2496 Added handling of InitHandlers in ServiceMain, with changes to tenant-bindings.xml and some new classes. InitHandlers do not *do* anything yet. --- .../collectionspace-client.properties | 5 +- .../main/config/services/tenant-bindings.xml | 59 ++++++++++ .../services/common/InitHandler.java | 102 ++++++++++++++++++ .../services/common/ServiceMain.java | 100 +++++++++++++++-- .../common/document/IInitHandler.java | 15 +++ .../client/java/RepositoryJavaClientImpl.java | 6 +- .../common/src/main/resources/service.xsd | 64 +++++++++++ 7 files changed, 341 insertions(+), 10 deletions(-) create mode 100644 services/common/src/main/java/org/collectionspace/services/common/InitHandler.java create mode 100644 services/common/src/main/java/org/collectionspace/services/common/document/IInitHandler.java diff --git a/services/client/src/main/resources/collectionspace-client.properties b/services/client/src/main/resources/collectionspace-client.properties index e469b8f37..4deaf2bf6 100644 --- a/services/client/src/main/resources/collectionspace-client.properties +++ b/services/client/src/main/resources/collectionspace-client.properties @@ -1,6 +1,9 @@ #url of the collectionspace server -cspace.url=http://localhost:8180/cspace-services/ +#cspace.url=http://localhost:8180/cspace-services/ #cspace.url=http://localhost:8200/cspace-services/ +#for sockspy: +cspace.url=http://localhost:8280/cspace-services/ + cspace.ssl=false cspace.auth=true # default user diff --git a/services/common/src/main/config/services/tenant-bindings.xml b/services/common/src/main/config/services/tenant-bindings.xml index 1be456321..c6c662251 100644 --- a/services/common/src/main/config/services/tenant-bindings.xml +++ b/services/common/src/main/config/services/tenant-bindings.xml @@ -232,9 +232,50 @@ org.collectionspace.services.loanin.nuxeo.LoaninDocumentModelHandler + + org.collectionspace.services.loanin.nuxeo.LoaninDocumentModelHandler + + loansin + loansin + loanInNumber|lenderList|loanReturnDate|uri|csid + loansin + org.collectionspace.services.loanin.LoansinCommonList + org.collectionspace.services.loanin.LoansinCommonList$LoaninListItem + getLoaninListItem + + + setLoanInNumber + loanInNumber + + + + + + setLender + lenderList + lenderGroupList + lender + + + + setLoanReturnDate + loanReturnDate + + + + + + org.collectionspace.services.loanin.nuxeo.LoaninValidatorHandler + + org.collectionspace.services.common.InitHandler + + ID + Foo + + @@ -388,6 +429,14 @@ + + authRefcontributor + authRefcoverage + authRefcreator + authRefpublisher + authRefrightsHolder + authRefsubjects|subject + + + + authRefcontributor + authRefcoverage + authRefcreator + authRefpublisher + authRefrightsHolder + authRefsubjects|subject + + fields) throws Exception { + //todo: all post-init tasks for services, or delegate to services that override. + System.out.println("\r\n\r\n~~~~~~~~~~~~~ in onRepositoryInitialized with ServiceBindingType: "+sbt); + + //call something like this: services.common.storage.DBUtils.addIndex(String tablename, String fields[]); + //for every field that has an authRef, do ... + // --> Connection conn = getConnection(); + //see parameter that you need for adding indices to SQL. + + } + /* + private void doJDBC(){ + Connection conn = null; + PreparedStatement pstmt = null; + Statement stmt = null; + // First find or create the tenants + try { + String queryTenantSQL = "UPDATE TABLE ADD KEY `tablename`.`id`..."; + conn = getConnection(); + // First find or create the tenants + stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(queryTenantSQL); + ArrayList existingTenants = new ArrayList(); + while (rs.next()) { + String tId = rs.getString("id"); + String tName = rs.getString("name"); + if(tenantInfo.containsKey(tId)) { + existingTenants.add(tId); + if(!tenantInfo.get(tId).equalsIgnoreCase(tName)) { + logger.warn("Configured name for tenant: " + +tId+" in repository: "+tName + +" does not match config'd name: "+ tenantInfo.get(tId)); + } + } + } + rs.close(); + pstmt.close(); + stmt.close(); + } catch (RuntimeException rte) { + if (logger.isDebugEnabled()) { + logger.debug("Exception in createDefaultAccounts: "+ + rte.getLocalizedMessage()); + logger.debug(rte.getStackTrace().toString()); + } + throw rte; + } catch (SQLException sqle) { + // SQLExceptions can be chained. We have at least one exception, so + // set up a loop to make sure we let the user know about all of them + // if there happens to be more than one. + if (logger.isDebugEnabled()) { + SQLException tempException = sqle; + while (null != tempException) { + logger.debug("SQL Exception: " + sqle.getLocalizedMessage()); + tempException = tempException.getNextException(); + } + logger.debug(sqle.getStackTrace().toString()); + } + throw new RuntimeException("SQL problem in createDefaultAccounts: ", sqle); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Exception in createDefaultAccounts: "+ + e.getLocalizedMessage()); + } + } finally { + try { + if(conn!=null) + conn.close(); + if(pstmt!=null) + pstmt.close(); + if(stmt!=null) + stmt.close(); + } catch (SQLException sqle) { + if (logger.isDebugEnabled()) { + logger.debug("SQL Exception closing statement/connection: " + + sqle.getLocalizedMessage()); + } + } + } + } + */ + +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java index 43af76e74..8a28079ee 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java @@ -17,15 +17,16 @@ import javax.naming.InitialContext; import javax.naming.NamingException; import javax.security.auth.login.LoginException; import javax.sql.DataSource; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.Response; import org.collectionspace.services.common.config.ServicesConfigReaderImpl; import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl; +import org.collectionspace.services.common.document.IInitHandler; import org.collectionspace.services.common.security.SecurityUtils; +import org.collectionspace.services.common.service.*; import org.collectionspace.services.common.tenant.TenantBindingType; import org.collectionspace.services.common.types.PropertyItemType; import org.collectionspace.services.common.types.PropertyType; +import org.collectionspace.services.nuxeo.client.java.DocHandlerBase; import org.collectionspace.services.nuxeo.client.java.NuxeoConnector; import org.collectionspace.services.nuxeo.client.java.TenantRepository; import org.slf4j.Logger; @@ -89,8 +90,7 @@ public class ServiceMain { return instance; } - private void initialize() throws Exception { - setServerRootDir(); + private void initialize() throws Exception { setServerRootDir(); readConfig(); propagateConfiguredProperties(); try { @@ -98,11 +98,18 @@ public class ServiceMain { } catch(Exception e) { logger.error("Default Account setup failed on exception: "+e.getLocalizedMessage()); } + try { + //loadDocHandlers(); + firePostInitHandlers(); + } catch(Exception e) { + logger.error("ServiceMain.initialize firePostInitHandlers failed on exception: "+e.getLocalizedMessage()); + } + if (getClientType().equals(ClientType.JAVA)) { nuxeoConnector = NuxeoConnector.getInstance(); - nuxeoConnector.initialize( - getServicesConfigReader().getConfiguration().getRepositoryClient()); + nuxeoConnector.initialize(getServicesConfigReader().getConfiguration().getRepositoryClient()); } + } /** @@ -508,7 +515,86 @@ public class ServiceMain { private String getDefaultReaderUserID(String tenantName) { return TENANT_READER_ACCT_PREFIX+tenantName; } - + + private void firePostInitHandlers() throws Exception { + Hashtable tenantBindingTypeMap = tenantBindingConfigReader.getTenantBindings(); + //Loop through all tenants in tenant-bindings.xml + for (TenantBindingType tbt: tenantBindingTypeMap.values()){ + //String name = tbt.getName(); + //String id = tbt.getId(); + //Loop through all the services in this tenant + List sbtList = tbt.getServiceBindings(); + for (ServiceBindingType sbt: sbtList){ + //Get the list of InitHandler elements, extract the first one (only one supported right now) and fire it using reflection. + List list = sbt.getInitHandler(); + if (list!=null && list.size()>0){ + org.collectionspace.services.common.service.InitHandler handlerType = list.get(0); + String initHandlerClassname = handlerType.getClassname(); + org.collectionspace.services.common.service.InitHandler.Fields ft = handlerType.getFields(); + List fields = ft.getField(); + Object o = instantiate(initHandlerClassname, IInitHandler.class); + if (o != null && o instanceof IInitHandler){ + IInitHandler handler = (IInitHandler)o; + handler.onRepositoryInitialized(sbt, fields); + //The InitHandler may be the default one, + // or specialized classes which still implement this interface and are registered in tenant-bindings.xml. + } + } + } + } + } + + + public Object instantiate(String clazz, Class castTo) throws Exception { + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + clazz = clazz.trim(); + Class c = tccl.loadClass(clazz); + if (castTo.isAssignableFrom(c)) { + return c.newInstance(); + } + return null; + } + + private void loadDocHandlers() throws Exception { + Hashtable tenantBindingTypeMap = tenantBindingConfigReader.getTenantBindings(); + for (TenantBindingType tbt: tenantBindingTypeMap.values()){ + String name = tbt.getName(); + String id = tbt.getId(); + + List sbtList = tbt.getServiceBindings(); + for (ServiceBindingType sbt: sbtList){ + DocHandlerParams params = sbt.getDocHandlerParams(); + if (params!=null){ + params.getClassname(); + DocHandlerParams.Params p = params.getParams(); + DocHandlerBase.CommonListReflection clr = new DocHandlerBase.CommonListReflection(); + //List items = p.getListItemsArrays(); //todo: this thing only returns one row. xsd is wrong. tenant-bindings.xml has an example of multiple elements in loansin. + List items = /*DocHandlerParams.Params.ListItemsArrays items =*/ + p.getListItemsArrays().getListItemsArray(); //todo: this thing only returns one row. xsd is wrong. tenant-bindings.xml has an example of multiple elements in loansin. + int size = items.size(); + String[][] rows = new String[size][4]; + int r = 0; + for (ListItemsArray item: items){ + String[] row = rows[r]; + row[0] = item.getSetter(); + row[1] = item.getElement(); + row[2] = item.getContainer(); + row[3] = item.getSubelement(); + r++; + } + clr.ListItemsArray = rows; + clr.AbstractCommonListClassname = p.getAbstractCommonListClassname(); + clr.CommonListItemClassname = p.getCommonListItemClassname(); + clr.DublinCoreTitle = p.getDublinCoreTitle(); + clr.ListItemMethodName = p.getListItemMethodName(); + clr.NuxeoSchemaName = p.getNuxeoSchemaName(); + clr.SummaryFields = p.getSummaryFields(); + } + } + } + } + + private Connection getConnection() throws LoginException, SQLException { InitialContext ctx = null; Connection conn = null; diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/IInitHandler.java b/services/common/src/main/java/org/collectionspace/services/common/document/IInitHandler.java new file mode 100644 index 000000000..9a72c2468 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/document/IInitHandler.java @@ -0,0 +1,15 @@ +package org.collectionspace.services.common.document; + +import org.collectionspace.services.common.context.ServiceContext; +import org.collectionspace.services.common.service.ServiceBindingType; + +import java.util.List; + +/** + * User: laramie + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public interface IInitHandler { + public void onRepositoryInitialized(ServiceBindingType sbt, List fields) throws Exception; +} diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java index f29d184b4..93ddb4f5e 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java @@ -567,8 +567,10 @@ public class RepositoryJavaClientImpl implements RepositoryClient { throws DocumentNotFoundException, DocumentException { DocumentFilter filter = handler.getDocumentFilter(); - //filter.setOrderByClause("collectionspace_core:updatedAt DESC"); //per http://issues.collectionspace.org/browse/CSPACE-705 - + String oldOrderBy = filter.getOrderByClause(); + if (oldOrderBy!=null && oldOrderBy.isEmpty()){ + filter.setOrderByClause("collectionspace_core:updatedAt DESC"); //per http://issues.collectionspace.org/browse/CSPACE-705 + } QueryContext queryContext = new QueryContext(ctx, handler); RepositoryInstance repoSession = null; try { diff --git a/services/common/src/main/resources/service.xsd b/services/common/src/main/resources/service.xsd index 7ab3b93fc..9ef2ccf2f 100644 --- a/services/common/src/main/resources/service.xsd +++ b/services/common/src/main/resources/service.xsd @@ -38,9 +38,15 @@ + + + + + + @@ -175,4 +181,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- 2.47.3