-/**
- * 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
--- /dev/null
+/** \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