]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-2588: Add a simple profiling utility class
authorRichard Millet <richard.millet@berkeley.edu>
Fri, 6 Aug 2010 02:25:23 +0000 (02:25 +0000)
committerRichard Millet <richard.millet@berkeley.edu>
Fri, 6 Aug 2010 02:25:23 +0000 (02:25 +0000)
services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java
services/collectionobject/sample/sample/src/main/java/org/collectionspace/services/collectionobject/client/sample/Sample.java
services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java
services/common/src/main/java/org/collectionspace/services/common/profile/Profiler.java [new file with mode: 0644]
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java

index dcf50b38ed20d7c0ae43f519f48fad63e1e1bfcb..dd974c382d3f8c1627d11379f6d2f3500ea4c715 100644 (file)
@@ -49,6 +49,7 @@ import java.util.Set;
 import org.collectionspace.services.authorization.PermissionResource;
 import org.collectionspace.services.authorization.RoleResource;
 import org.collectionspace.services.common.security.SecurityInterceptor;
+import org.collectionspace.services.common.profile.Profiler;
 
 /**
  * CollectionSpaceJaxRsApplication, the root application
@@ -61,9 +62,16 @@ import org.collectionspace.services.common.security.SecurityInterceptor;
 public class CollectionSpaceJaxRsApplication extends Application {
 
     private Set<Object> singletons = new HashSet<Object>();
-    private Set<Class<?>> empty = new HashSet<Class<?>>();
+    private Set<Class<?>> empty = new HashSet<Class<?>>();    
 
     public CollectionSpaceJaxRsApplication() {
+       //
+       // Setup the profiler logger
+       //
+       Profiler.setup();
+       //
+       // Instantiate all our JaxRS resources
+       //
         singletons.add(new SecurityInterceptor());
         singletons.add(new AccountResource());
         singletons.add(new RoleResource());
index 799446a1ac0de4cca057632b0dacfc4c200076bb..0eedbaa11afb423d1641a0a670e6b473db4268b1 100644 (file)
@@ -211,9 +211,9 @@ public class Sample {
        depts.add("urn:org.collectionspace.services.department:Registrar");
        depts.add("urn:org.walkerart.department:Fine Art");
        collectionObject.setAge(""); // Test using an empty String.
-       collectionObject.setBriefDescription("Papier mache bird mask with horns, " +
-               "painted red with black and yellow spots. " +
-               "Puerto Rico. ca. 8&quot; high, 6&quot; wide, projects 10&quot; (with horns).");
+//       collectionObject.setBriefDescription("Papier mache bird mask with horns, " +
+//               "painted red with black and yellow spots. " +
+//               "Puerto Rico. ca. 8&quot; high, 6&quot; wide, projects 10&quot; (with horns).");
 
        MultipartOutput multipart = new MultipartOutput();
        OutputPart commonPart = multipart.addPart(collectionObject,
index f76dbb9d448500edc974f324fb222f151752ae75..0cfbcfeb4d356472969ed8760eaf571db0ca02fe 100644 (file)
-/**
- * Copyright 2009 University of California at Berkeley
- */
-package org.collectionspace.services.common;
-
-import java.util.Hashtable;
-import java.util.List;
-
-import org.collectionspace.services.common.config.ServicesConfigReaderImpl;
-import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
-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.NuxeoConnector;
-import org.collectionspace.services.nuxeo.client.java.TenantRepository;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Main class for Services layer. It reads configuration and performs service
- * level initialization. It is a singleton.
- * @author 
- */
-public class ServiceMain {
-
-    /**
-     * volatile is used here to assume about ordering (post JDK 1.5)
-     */
-    private static volatile ServiceMain instance = null;
-    final Logger logger = LoggerFactory.getLogger(ServiceMain.class);
-    private NuxeoConnector nuxeoConnector;
-    private String serverRootDir = null;
-    private ServicesConfigReaderImpl servicesConfigReader;
-    private TenantBindingConfigReaderImpl tenantBindingConfigReader;
-
-    private ServiceMain() {
-    }
-
-    /**
-     * getInstance returns the ServiceMain singleton instance after proper
-     * initialization in a thread-safe manner
-     * @return
-     */
-    public static ServiceMain getInstance() {
-        if (instance == null) {
-            synchronized (ServiceMain.class) {
-                if (instance == null) {
-                    ServiceMain temp = new ServiceMain();
-                    try {
-                        temp.initialize();
-                    } catch (Exception e) {
-                        instance = null;
-                        if (e instanceof RuntimeException) {
-                            throw (RuntimeException) e;
-                        } else {
-                            throw new RuntimeException(e);
-                        }
-                    }
-                    instance = temp;
-                }
-            }
-        }
-        return instance;
-    }
-
-    private void initialize() throws Exception {
-        setServerRootDir();
-        readConfig();
-        propagateConfiguredProperties();
-        if (getClientType().equals(ClientType.JAVA)) {
-            nuxeoConnector = NuxeoConnector.getInstance();
-            nuxeoConnector.initialize(
-                    getServicesConfigReader().getConfiguration().getRepositoryClient());
-        }
-    }
-
-    /**
-     * release releases all resources occupied by service layer infrastructure
-     * but not necessarily those occupied by individual services
-     */
-    public void release() {
-        try {
-            if (nuxeoConnector != null) {
-                nuxeoConnector.release();
-            }
-            instance = null;
-        } catch (Exception e) {
-            e.printStackTrace();
-            //gobble it
-        }
-    }
-
-    private void readConfig() throws Exception {
-        //read service config
-        servicesConfigReader = new ServicesConfigReaderImpl(getServerRootDir());
-        getServicesConfigReader().read();
-
-        tenantBindingConfigReader = new TenantBindingConfigReaderImpl(getServerRootDir());
-        getTenantBindingConfigReader().read();
-    }
-
-    private void propagateConfiguredProperties() {
-        List<PropertyType> repoPropListHolder =
-                servicesConfigReader.getConfiguration().getRepositoryClient().getProperties();
-        if (repoPropListHolder != null && !repoPropListHolder.isEmpty()) {
-            List<PropertyItemType> propList = repoPropListHolder.get(0).getItem();
-            if (propList != null && !propList.isEmpty()) {
-                tenantBindingConfigReader.setDefaultPropertiesOnTenants(propList, true);
-            }
-        }
-    }
-
-    void retrieveAllWorkspaceIds() throws Exception {
-        //all configs are read, connector is initialized, retrieve workspaceids
-        Hashtable<String, TenantBindingType> tenantBindings =
-                getTenantBindingConfigReader().getTenantBindings();
-        TenantRepository.get().setup(tenantBindings);
-    }
-
-    /**
-     * getWorkspaceId returns workspace id for given tenant and service name
-     * @param tenantId
-     * @param serviceName
-     * @return
-     */
-    public String getWorkspaceId(String tenantId, String serviceName) {
-        return TenantRepository.get().getWorkspaceId(tenantId, serviceName);
-    }
-
-    /**
-     * @return the nuxeoConnector
-     */
-    public NuxeoConnector getNuxeoConnector() {
-        return nuxeoConnector;
-    }
-    
-    /**
-     * @return the serverRootDir
-     */
-    public String getServerRootDir() {
-        return serverRootDir;
-    }
-
-    private void setServerRootDir() {
-        serverRootDir = System.getProperty("jboss.server.home.dir");
-        if (serverRootDir == null) {
-            serverRootDir = "."; //assume server is started from server root, e.g. server/cspace
-        }
-    }
-
-
-    /**
-     * @return the serviceConfig
-     */
-    public ServiceConfig getServiceConfig() {
-        return getServicesConfigReader().getConfiguration();
-    }
-
-    /**
-     * @return the clientType
-     */
-    public ClientType getClientType() {
-        return getServicesConfigReader().getClientType();
-    }
-
-    /**
-     * @return the servicesConfigReader
-     */
-    public ServicesConfigReaderImpl getServicesConfigReader() {
-        return servicesConfigReader;
-    }
-
-    /**
-     * @return the tenantBindingConfigReader
-     */
-    public TenantBindingConfigReaderImpl getTenantBindingConfigReader() {
-        return tenantBindingConfigReader;
-    }
-}
+/**\r
+ * Copyright 2009-2010 University of California at Berkeley\r
+ */\r
+package org.collectionspace.services.common;\r
+\r
+import java.util.Hashtable;\r
+import java.util.List;\r
+\r
+import org.collectionspace.services.common.config.ServicesConfigReaderImpl;\r
+import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;\r
+import org.collectionspace.services.common.tenant.TenantBindingType;\r
+import org.collectionspace.services.common.types.PropertyItemType;\r
+import org.collectionspace.services.common.types.PropertyType;\r
+import org.collectionspace.services.nuxeo.client.java.NuxeoConnector;\r
+import org.collectionspace.services.nuxeo.client.java.TenantRepository;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+/**\r
+ * Main class for Services layer. It reads configuration and performs service\r
+ * level initialization. It is a singleton.\r
+ * @author \r
+ */\r
+public class ServiceMain {\r
+\r
+    /**\r
+     * volatile is used here to assume about ordering (post JDK 1.5)\r
+     */\r
+    private static volatile ServiceMain instance = null;\r
+    final Logger logger = LoggerFactory.getLogger(ServiceMain.class);\r
+    private NuxeoConnector nuxeoConnector;\r
+    private String serverRootDir = null;\r
+    private ServicesConfigReaderImpl servicesConfigReader;\r
+    private TenantBindingConfigReaderImpl tenantBindingConfigReader;\r
+\r
+    private ServiceMain() {\r
+    }\r
+    \r
+    /**\r
+     * getInstance returns the ServiceMain singleton instance after proper\r
+     * initialization in a thread-safe manner\r
+     * @return\r
+     */\r
+    public static ServiceMain getInstance() {\r
+        if (instance == null) {\r
+            synchronized (ServiceMain.class) {\r
+                if (instance == null) {\r
+                    ServiceMain temp = new ServiceMain();\r
+                    try {\r
+                        temp.initialize();\r
+                    } catch (Exception e) {\r
+                        instance = null;\r
+                        if (e instanceof RuntimeException) {\r
+                            throw (RuntimeException) e;\r
+                        } else {\r
+                            throw new RuntimeException(e);\r
+                        }\r
+                    }\r
+                    instance = temp;\r
+                }\r
+            }\r
+        }\r
+        return instance;\r
+    }\r
+\r
+    private void initialize() throws Exception {\r
+        setServerRootDir();\r
+        readConfig();\r
+        propagateConfiguredProperties();\r
+        if (getClientType().equals(ClientType.JAVA)) {\r
+            nuxeoConnector = NuxeoConnector.getInstance();\r
+            nuxeoConnector.initialize(\r
+                    getServicesConfigReader().getConfiguration().getRepositoryClient());\r
+        }\r
+    }\r
+\r
+    /**\r
+     * release releases all resources occupied by service layer infrastructure\r
+     * but not necessarily those occupied by individual services\r
+     */\r
+    public void release() {\r
+        try {\r
+            if (nuxeoConnector != null) {\r
+                nuxeoConnector.release();\r
+            }\r
+            instance = null;\r
+        } catch (Exception e) {\r
+            e.printStackTrace();\r
+            //gobble it\r
+        }\r
+    }\r
+\r
+    private void readConfig() throws Exception {\r
+        //read service config\r
+        servicesConfigReader = new ServicesConfigReaderImpl(getServerRootDir());\r
+        getServicesConfigReader().read();\r
+\r
+        tenantBindingConfigReader = new TenantBindingConfigReaderImpl(getServerRootDir());\r
+        getTenantBindingConfigReader().read();\r
+    }\r
+\r
+    private void propagateConfiguredProperties() {\r
+        List<PropertyType> repoPropListHolder =\r
+                servicesConfigReader.getConfiguration().getRepositoryClient().getProperties();\r
+        if (repoPropListHolder != null && !repoPropListHolder.isEmpty()) {\r
+            List<PropertyItemType> propList = repoPropListHolder.get(0).getItem();\r
+            if (propList != null && !propList.isEmpty()) {\r
+                tenantBindingConfigReader.setDefaultPropertiesOnTenants(propList, true);\r
+            }\r
+        }\r
+    }\r
+\r
+    void retrieveAllWorkspaceIds() throws Exception {\r
+        //all configs are read, connector is initialized, retrieve workspaceids\r
+        Hashtable<String, TenantBindingType> tenantBindings =\r
+                getTenantBindingConfigReader().getTenantBindings();\r
+        TenantRepository.get().setup(tenantBindings);\r
+    }\r
+\r
+    /**\r
+     * getWorkspaceId returns workspace id for given tenant and service name\r
+     * @param tenantId\r
+     * @param serviceName\r
+     * @return\r
+     */\r
+    public String getWorkspaceId(String tenantId, String serviceName) {\r
+        return TenantRepository.get().getWorkspaceId(tenantId, serviceName);\r
+    }\r
+\r
+    /**\r
+     * @return the nuxeoConnector\r
+     */\r
+    public NuxeoConnector getNuxeoConnector() {\r
+        return nuxeoConnector;\r
+    }\r
+    \r
+    /**\r
+     * @return the serverRootDir\r
+     */\r
+    public String getServerRootDir() {\r
+        return serverRootDir;\r
+    }\r
+\r
+    private void setServerRootDir() {\r
+        serverRootDir = System.getProperty("jboss.server.home.dir");\r
+        if (serverRootDir == null) {\r
+            serverRootDir = "."; //assume server is started from server root, e.g. server/cspace\r
+        }\r
+    }\r
+\r
+\r
+    /**\r
+     * @return the serviceConfig\r
+     */\r
+    public ServiceConfig getServiceConfig() {\r
+        return getServicesConfigReader().getConfiguration();\r
+    }\r
+\r
+    /**\r
+     * @return the clientType\r
+     */\r
+    public ClientType getClientType() {\r
+        return getServicesConfigReader().getClientType();\r
+    }\r
+\r
+    /**\r
+     * @return the servicesConfigReader\r
+     */\r
+    public ServicesConfigReaderImpl getServicesConfigReader() {\r
+        return servicesConfigReader;\r
+    }\r
+\r
+    /**\r
+     * @return the tenantBindingConfigReader\r
+     */\r
+    public TenantBindingConfigReaderImpl getTenantBindingConfigReader() {\r
+        return tenantBindingConfigReader;\r
+    }\r
+}\r
diff --git a/services/common/src/main/java/org/collectionspace/services/common/profile/Profiler.java b/services/common/src/main/java/org/collectionspace/services/common/profile/Profiler.java
new file mode 100644 (file)
index 0000000..a628bf8
--- /dev/null
@@ -0,0 +1,156 @@
+/**    \r
+ * Profiler.java\r
+ *\r
+ * {Purpose of This Class}\r
+ *\r
+ * {Other Notes Relating to This Class (Optional)}\r
+ *\r
+ * $LastChangedBy: $\r
+ * $LastChangedRevision: $\r
+ * $LastChangedDate: $\r
+ *\r
+ * This document is a part of the source code and related artifacts\r
+ * for CollectionSpace, an open source collections management system\r
+ * for museums and related institutions:\r
+ *\r
+ * http://www.collectionspace.org\r
+ * http://wiki.collectionspace.org\r
+ *\r
+ * Copyright © 2009 {Contributing Institution}\r
+ *\r
+ * Licensed under the Educational Community License (ECL), Version 2.0.\r
+ * You may not use this file except in compliance with this License.\r
+ *\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
+ */\r
+package org.collectionspace.services.common.profile;\r
+\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+\r
+/**\r
+ * The Class Profiler.\r
+ */\r
+public class Profiler {\r
+       \r
+       /** The start time. */\r
+       private long startTime = 0;\r
+       \r
+       /** The stop time. */\r
+       private long stopTime = 0;\r
+       \r
+       /** The final time. */\r
+       private long finalTime = 0;\r
+       \r
+       /** The message prefix. */\r
+       private String messagePrefix = "Default profiler prefix:";\r
+\r
+    /** The profiler logger. */\r
+       private static String LOG4J_CATEGORY = "perf.collectionspace";\r
+    private static Logger profilerLogger = LoggerFactory.getLogger(LOG4J_CATEGORY);\r
+       \r
+    /**\r
+     * private default constructor.\r
+     */\r
+    private Profiler() {\r
+       //empty default constructor\r
+    }\r
+    \r
+    /**\r
+     * Sets the message prefix.\r
+     *\r
+     * @param theMessagePrefix the new message prefix\r
+     */\r
+    protected void setMessagePrefix(String theMessagePrefix) {\r
+       messagePrefix = theMessagePrefix + ":";\r
+    }\r
+    \r
+    protected StringBuffer getMessagePrefix() {\r
+       return new StringBuffer(messagePrefix);\r
+    }\r
+    \r
+    /**\r
+     * Instantiates a new profiler.\r
+     * @param theObject \r
+     *\r
+     * @param theClass the the class\r
+     */\r
+    public Profiler(Object theObject) {\r
+       if (theObject != null) {\r
+               this.setMessagePrefix(theObject.getClass().getSimpleName());\r
+       }\r
+    }\r
+       /**\r
+        * Instantiates a new profiler.\r
+        *\r
+        * @param theMessagePrefix the the message prefix\r
+        */\r
+       public Profiler(String theMessagePrefix) {\r
+               this.setMessagePrefix(theMessagePrefix);\r
+       }\r
+       \r
+       /*\r
+        * For some reason, we need to call this method "early" at startup time for our\r
+        * Logger instance to get created correctly.  FIXME: REM - Can we figure this out and fix it?\r
+        */\r
+       /**\r
+        * Setup.\r
+        */\r
+       public static void setup() {\r
+               //do nothing\r
+       }\r
+       \r
+       /**\r
+        * Gets the logger.\r
+        *\r
+        * @return the logger\r
+        */\r
+       protected Logger getLogger() {\r
+               return profilerLogger;\r
+       }\r
+       \r
+       /**\r
+        * Private log.\r
+        *\r
+        * @param inMessage the in message\r
+        */\r
+       public void log(String inMessage) {\r
+               if (getLogger().isDebugEnabled() == true) {\r
+                       StringBuffer finalMessage = getMessagePrefix();\r
+                       if (inMessage != null) {\r
+                               finalMessage.append(inMessage);\r
+                       }\r
+                       getLogger().debug(finalMessage.toString());\r
+               }\r
+       }\r
+       \r
+       /**\r
+        * Start\r
+        */\r
+       public void start() {\r
+               if (getLogger().isDebugEnabled() == true) {\r
+                       StringBuffer message = getMessagePrefix();\r
+                       message.append(">>>> Start >>>>");\r
+                       getLogger().debug(message.toString());\r
+               }\r
+               startTime = System.currentTimeMillis();\r
+       }\r
+       \r
+       /**\r
+        * Stop.\r
+        */\r
+       public void stop() {\r
+               stopTime = System.currentTimeMillis();\r
+               finalTime = finalTime + (stopTime - startTime);\r
+               if (getLogger().isDebugEnabled() == true) {\r
+                       StringBuffer message = getMessagePrefix();\r
+                       message.append("<<<< Stopped <<<< [");\r
+                       message.append(finalTime);\r
+                       message.append("ms]");\r
+                       message.append('\n');\r
+                       getLogger().debug(message.toString());\r
+               }               \r
+       }\r
+}\r
index 1ff686e611884145fcaa5edad7af8b6eba7dc737..8e98d20fc80b20e7b947dc6a36477c8a3f1ecee3 100644 (file)
@@ -35,6 +35,7 @@ import org.collectionspace.services.common.document.DocumentWrapperImpl;
 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
 import org.collectionspace.services.common.query.IQueryManager;
 import org.collectionspace.services.common.repository.RepositoryClient;
+import org.collectionspace.services.common.profile.Profiler;
 
 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
@@ -144,12 +145,14 @@ public class RepositoryJavaClientImpl implements RepositoryClient {
     }
     /** The logger. */
     private final Logger logger = LoggerFactory.getLogger(RepositoryJavaClientImpl.class);
-
+//    private final Logger profilerLogger = LoggerFactory.getLogger("remperf");
+//    private String foo = Profiler.createLogger();
     /**
      * Instantiates a new repository java client impl.
      */
     public RepositoryJavaClientImpl() {
         //Empty constructor
+       
     }
 
     /**
@@ -640,12 +643,16 @@ public class RepositoryJavaClientImpl implements RepositoryClient {
 
             // If we have limit and/or offset, then pass true to get totalSize
             // in returned DocumentModelList.
+               Profiler profiler = new Profiler(this);
+               profiler.log("Executing NXQL query: " + query.toString());
+               profiler.start();
             if ((queryContext.docFilter.getOffset() > 0) || (queryContext.docFilter.getPageSize() > 0)) {
                 docList = repoSession.query(query, null,
                         queryContext.docFilter.getPageSize(), queryContext.docFilter.getOffset(), true);
             } else {
                 docList = repoSession.query(query);
             }
+            profiler.stop();
 
             //set repoSession to handle the document
             ((DocumentModelHandler) handler).setRepositorySession(repoSession);
@@ -914,12 +921,12 @@ public class RepositoryJavaClientImpl implements RepositoryClient {
         // TODO This is a slow method for tenant-filter
         // We should make this a property that is indexed.
         //
-        query.append(" WHERE ecm:path STARTSWITH '/" + queryContext.domain + "'");
+//        query.append(" WHERE ecm:path STARTSWITH '/" + queryContext.domain + "'");
 
         //
         // Restrict search to the current tenant ID.  Is the domain path filter (above) still needed?
         //
-        query.append(IQueryManager.SEARCH_QUALIFIER_AND + DocumentModelHandler.COLLECTIONSPACE_CORE_SCHEMA + ":"
+        query.append(/*IQueryManager.SEARCH_QUALIFIER_AND +*/ " WHERE " + DocumentModelHandler.COLLECTIONSPACE_CORE_SCHEMA + ":"
                 + DocumentModelHandler.COLLECTIONSPACE_CORE_TENANTID
                 + " = " + queryContext.tenantId);
         //