[jbpm-commits] JBoss JBPM SVN: r4218 - jbpm3/trunk/modules/core/src/main/java/org/jbpm.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Mar 10 12:09:53 EDT 2009


Author: thomas.diesler at jboss.com
Date: 2009-03-10 12:09:53 -0400 (Tue, 10 Mar 2009)
New Revision: 4218

Modified:
   jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmConfiguration.java
   jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmContext.java
Log:
[JBPM-2074] Removed invalid usage of finalize

Modified: jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmConfiguration.java
===================================================================
--- jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmConfiguration.java	2009-03-10 16:06:14 UTC (rev 4217)
+++ jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmConfiguration.java	2009-03-10 16:09:53 UTC (rev 4218)
@@ -248,7 +248,8 @@
  * </tr>
  * </table>
  */
-public class JbpmConfiguration implements Serializable {
+public class JbpmConfiguration implements Serializable
+{
 
   private static final long serialVersionUID = 1L;
 
@@ -261,45 +262,55 @@
   private JobExecutor jobExecutor;
   private boolean isClosed;
 
-  static class StackThreadLocal<E> extends ThreadLocal<List<E>> {
+  static class StackThreadLocal<E> extends ThreadLocal<List<E>>
+  {
     @Override
-    protected List<E> initialValue() {
+    protected List<E> initialValue()
+    {
       return new ArrayList<E>();
     }
   }
 
-  public JbpmConfiguration(ObjectFactory objectFactory) {
+  public JbpmConfiguration(ObjectFactory objectFactory)
+  {
     this.objectFactory = objectFactory;
   }
 
-  public static JbpmConfiguration getInstance() {
+  public static JbpmConfiguration getInstance()
+  {
     return getInstance(null);
   }
 
-  public static JbpmConfiguration getInstance(String resource) {
-    if (resource == null) {
+  public static JbpmConfiguration getInstance(String resource)
+  {
+    if (resource == null)
+    {
       resource = "jbpm.cfg.xml";
     }
 
     JbpmConfiguration instance;
-    synchronized (instances) {
+    synchronized (instances)
+    {
       // look for configuration in cache
       instance = instances.get(resource);
-      if (instance == null) {
+      if (instance == null)
+      {
         // configuration does not exist or was evicted, construct it
-        if (defaultObjectFactory != null) {
+        if (defaultObjectFactory != null)
+        {
           log.debug("creating configuration from default object factory: " + defaultObjectFactory);
           instance = new JbpmConfiguration(defaultObjectFactory);
         }
-        else {
+        else
+        {
           log.info("using configuration resource: " + resource);
           InputStream jbpmCfgXmlStream = ClassLoaderUtil.getJbpmConfigurationStream(resource);
           /*
-           * if a custom resource is specified, but not found in the classpath, log a warning;
-           * otherwise, users who want to load custom stuff will not receive any feedback when their
-           * resource cannot be found
+           * if a custom resource is specified, but not found in the classpath, log a warning; otherwise, users who want to load custom stuff will not receive any
+           * feedback when their resource cannot be found
            */
-          if (jbpmCfgXmlStream == null && !"jbpm.cfg.xml".equals(resource)) {
+          if (jbpmCfgXmlStream == null && !"jbpm.cfg.xml".equals(resource))
+          {
             log.warn("configuration resource '" + resource + "' could not be found");
           }
           ObjectFactory objectFactory = parseObjectFactory(jbpmCfgXmlStream);
@@ -312,18 +323,20 @@
     return instance;
   }
 
-  public static boolean hasInstance(String resource) {
+  public static boolean hasInstance(String resource)
+  {
     return instances.containsKey(resource != null ? resource : "jbpm.cfg.xml");
   }
 
-  protected static ObjectFactory parseObjectFactory(InputStream inputStream) {
+  protected static ObjectFactory parseObjectFactory(InputStream inputStream)
+  {
     log.debug("loading defaults in jbpm configuration");
     ObjectFactoryParser objectFactoryParser = new ObjectFactoryParser();
     ObjectFactoryImpl objectFactoryImpl = new ObjectFactoryImpl();
-    objectFactoryParser.parseElementsFromResource("org/jbpm/default.jbpm.cfg.xml",
-        objectFactoryImpl);
+    objectFactoryParser.parseElementsFromResource("org/jbpm/default.jbpm.cfg.xml", objectFactoryImpl);
 
-    if (inputStream != null) {
+    if (inputStream != null)
+    {
       log.debug("loading specific configuration...");
       objectFactoryParser.parseElementsStream(inputStream, objectFactoryImpl);
     }
@@ -331,26 +344,31 @@
     return objectFactoryImpl;
   }
 
-  public static JbpmConfiguration parseXmlString(String xml) {
+  public static JbpmConfiguration parseXmlString(String xml)
+  {
     log.debug("creating jbpm configuration from xml string");
     InputStream inputStream = null;
-    if (xml != null) {
+    if (xml != null)
+    {
       inputStream = new ByteArrayInputStream(xml.getBytes());
     }
     ObjectFactory objectFactory = parseObjectFactory(inputStream);
     return createJbpmConfiguration(objectFactory);
   }
 
-  protected static JbpmConfiguration createJbpmConfiguration(ObjectFactory objectFactory) {
+  protected static JbpmConfiguration createJbpmConfiguration(ObjectFactory objectFactory)
+  {
     JbpmConfiguration jbpmConfiguration = new JbpmConfiguration(objectFactory);
 
     // make the bean jbpm.configuration always available
-    if (objectFactory instanceof ObjectFactoryImpl) {
-      ObjectFactoryImpl objectFactoryImpl = (ObjectFactoryImpl) objectFactory;
+    if (objectFactory instanceof ObjectFactoryImpl)
+    {
+      ObjectFactoryImpl objectFactoryImpl = (ObjectFactoryImpl)objectFactory;
       ObjectInfo jbpmConfigurationInfo = new ValueInfo("jbpmConfiguration", jbpmConfiguration);
       objectFactoryImpl.addObjectInfo(jbpmConfigurationInfo);
 
-      if (getHideStaleObjectExceptions(objectFactory)) {
+      if (getHideStaleObjectExceptions(objectFactory))
+      {
         StaleObjectLogConfigurer.hideStaleObjectExceptions();
       }
     }
@@ -358,184 +376,226 @@
     return jbpmConfiguration;
   }
 
-  private static boolean getHideStaleObjectExceptions(ObjectFactory objectFactory) {
-    if (!objectFactory.hasObject("jbpm.hide.stale.object.exceptions")) return true;
+  private static boolean getHideStaleObjectExceptions(ObjectFactory objectFactory)
+  {
+    if (!objectFactory.hasObject("jbpm.hide.stale.object.exceptions"))
+      return true;
 
     Object object = objectFactory.createObject("jbpm.hide.stale.object.exceptions");
-    return object instanceof Boolean ? (Boolean) object : true;
+    return object instanceof Boolean ? (Boolean)object : true;
   }
 
-  public static JbpmConfiguration parseInputStream(InputStream inputStream) {
+  public static JbpmConfiguration parseInputStream(InputStream inputStream)
+  {
     log.debug("creating jbpm configuration from input stream");
     ObjectFactory objectFactory = parseObjectFactory(inputStream);
     return createJbpmConfiguration(objectFactory);
   }
 
-  public static JbpmConfiguration parseResource(String resource) {
+  public static JbpmConfiguration parseResource(String resource)
+  {
     log.debug("creating jbpm configuration from resource: " + resource);
     InputStream inputStream = null;
-    if (resource != null) {
+    if (resource != null)
+    {
       inputStream = ClassLoaderUtil.getJbpmConfigurationStream(resource);
     }
     ObjectFactory objectFactory = parseObjectFactory(inputStream);
     return createJbpmConfiguration(objectFactory);
   }
 
-  public JbpmContext createJbpmContext() {
+  public JbpmContext createJbpmContext()
+  {
     return createJbpmContext(JbpmContext.DEFAULT_JBPM_CONTEXT_NAME);
   }
 
-  public JbpmContext createJbpmContext(String name) {
+  public JbpmContext createJbpmContext(String name)
+  {
     ensureOpen();
 
-    JbpmContext jbpmContext = (JbpmContext) objectFactory.createObject(name);
+    JbpmContext jbpmContext = (JbpmContext)objectFactory.createObject(name);
     jbpmContext.jbpmConfiguration = this;
     jbpmContextCreated(jbpmContext);
     return jbpmContext;
   }
 
-  private void ensureOpen() {
-    if (isClosed) throw new JbpmException("configuration closed");
+  private void ensureOpen()
+  {
+    if (isClosed)
+      throw new JbpmException("configuration closed");
   }
 
-  public ServiceFactory getServiceFactory(String serviceName) {
+  public ServiceFactory getServiceFactory(String serviceName)
+  {
     return getServiceFactory(serviceName, JbpmContext.DEFAULT_JBPM_CONTEXT_NAME);
   }
 
-  public ServiceFactory getServiceFactory(String serviceName, String jbpmContextName) {
+  public ServiceFactory getServiceFactory(String serviceName, String jbpmContextName)
+  {
     ServiceFactory serviceFactory = null;
     JbpmContext jbpmContext = createJbpmContext(jbpmContextName);
-    try {
+    try
+    {
       serviceFactory = jbpmContext.getServices().getServiceFactory(serviceName);
     }
-    finally {
+    finally
+    {
       jbpmContext.close();
     }
     return serviceFactory;
   }
 
-  public static ClassLoader getProcessClassLoader(ProcessDefinition processDefinition) {
+  public static ClassLoader getProcessClassLoader(ProcessDefinition processDefinition)
+  {
     ProcessClassLoaderFactory factory = null;
-    if (Configs.hasObject("jbpm.processClassLoader")) {
-      factory = (ProcessClassLoaderFactory) Configs.getObject("jbpm.processClassLoader");
+    if (Configs.hasObject("jbpm.processClassLoader"))
+    {
+      factory = (ProcessClassLoaderFactory)Configs.getObject("jbpm.processClassLoader");
     }
-    else {
+    else
+    {
       factory = new DefaultProcessClassLoaderFactory();
     }
     return factory.getProcessClassLoader(processDefinition);
   }
 
-  /**
+  /*
    * gives the jbpm domain model access to configuration information via the current JbpmContext.
    */
-  public static class Configs {
+  public static class Configs
+  {
 
-    private Configs() {
+    private Configs()
+    {
       // hide default constructor to prevent instantiation
     }
 
-    public static ObjectFactory getObjectFactory() {
+    public static ObjectFactory getObjectFactory()
+    {
       ObjectFactory objectFactory = null;
       JbpmContext jbpmContext = JbpmContext.getCurrentJbpmContext();
-      if (jbpmContext != null) {
+      if (jbpmContext != null)
+      {
         objectFactory = jbpmContext.objectFactory;
       }
-      else {
+      else
+      {
         objectFactory = getInstance().objectFactory;
       }
       return objectFactory;
     }
 
-    public static void setDefaultObjectFactory(ObjectFactory objectFactory) {
+    public static void setDefaultObjectFactory(ObjectFactory objectFactory)
+    {
       defaultObjectFactory = objectFactory;
     }
 
-    public static boolean hasObject(String name) {
+    public static boolean hasObject(String name)
+    {
       ObjectFactory objectFactory = getObjectFactory();
       return objectFactory.hasObject(name);
     }
 
-    public static synchronized Object getObject(String name) {
+    public static synchronized Object getObject(String name)
+    {
       ObjectFactory objectFactory = getObjectFactory();
       return objectFactory.createObject(name);
     }
 
-    public static String getString(String name) {
-      return (String) getObject(name);
+    public static String getString(String name)
+    {
+      return (String)getObject(name);
     }
 
-    public static long getLong(String name) {
-      return ((Long) getObject(name)).longValue();
+    public static long getLong(String name)
+    {
+      return ((Long)getObject(name)).longValue();
     }
 
-    public static int getInt(String name) {
-      return ((Integer) getObject(name)).intValue();
+    public static int getInt(String name)
+    {
+      return ((Integer)getObject(name)).intValue();
     }
 
-    public static boolean getBoolean(String name) {
-      return ((Boolean) getObject(name)).booleanValue();
+    public static boolean getBoolean(String name)
+    {
+      return ((Boolean)getObject(name)).booleanValue();
     }
   }
 
-  public void cleanSchema() {
+  public void cleanSchema()
+  {
     cleanSchema(JbpmContext.DEFAULT_JBPM_CONTEXT_NAME);
   }
 
-  public void cleanSchema(String jbpmContextName) {
+  public void cleanSchema(String jbpmContextName)
+  {
     getPersistenceServiceFactory(jbpmContextName).cleanSchema();
   }
 
-  public void createSchema() {
+  public void createSchema()
+  {
     createSchema(JbpmContext.DEFAULT_JBPM_CONTEXT_NAME);
   }
 
-  public void createSchema(String jbpmContextName) {
+  public void createSchema(String jbpmContextName)
+  {
     getPersistenceServiceFactory(jbpmContextName).createSchema();
   }
 
-  public void dropSchema() {
+  public void dropSchema()
+  {
     dropSchema(JbpmContext.DEFAULT_JBPM_CONTEXT_NAME);
   }
 
-  public void dropSchema(String jbpmContextName) {
+  public void dropSchema(String jbpmContextName)
+  {
     getPersistenceServiceFactory(jbpmContextName).dropSchema();
   }
 
-  private DbPersistenceServiceFactory getPersistenceServiceFactory(String jbpmContextName) {
-    return (DbPersistenceServiceFactory) getServiceFactory(Services.SERVICENAME_PERSISTENCE,
-        jbpmContextName);
+  private DbPersistenceServiceFactory getPersistenceServiceFactory(String jbpmContextName)
+  {
+    return (DbPersistenceServiceFactory)getServiceFactory(Services.SERVICENAME_PERSISTENCE, jbpmContextName);
   }
 
-  public boolean isClosed() {
+  public boolean isClosed()
+  {
     return isClosed;
   }
 
-  public void close() {
+  public void close()
+  {
     close(JbpmContext.DEFAULT_JBPM_CONTEXT_NAME);
   }
 
-  public void close(String jbpmContextName) {
+  public void close(String jbpmContextName)
+  {
     // prevent configuration from being closed more than once
-    if (isClosed) return;
+    if (isClosed)
+      return;
 
     // stop job executor
-    if (jobExecutor != null) {
+    if (jobExecutor != null)
+    {
       jobExecutor.stop();
       jobExecutor = null;
     }
 
     // close service factories
     JbpmContext jbpmContext = createJbpmContext(jbpmContextName);
-    try {
-      Map<String, ServiceFactory> serviceFactories = jbpmContext.getServices()
-          .getServiceFactories();
-      if (serviceFactories != null) {
-        for (ServiceFactory serviceFactory : serviceFactories.values()) {
+    try
+    {
+      Map<String, ServiceFactory> serviceFactories = jbpmContext.getServices().getServiceFactories();
+      if (serviceFactories != null)
+      {
+        for (ServiceFactory serviceFactory : serviceFactories.values())
+        {
           serviceFactory.close();
         }
       }
     }
-    finally {
+    finally
+    {
       jbpmContext.close();
     }
 
@@ -546,9 +606,12 @@
     jbpmContextStacks.remove();
 
     // remove from configuration cache
-    synchronized (instances) {
-      for (java.util.Iterator<JbpmConfiguration> i = instances.values().iterator(); i.hasNext();) {
-        if (this == i.next()) {
+    synchronized (instances)
+    {
+      for (java.util.Iterator<JbpmConfiguration> i = instances.values().iterator(); i.hasNext();)
+      {
+        if (this == i.next())
+        {
           i.remove();
           break;
         }
@@ -556,90 +619,122 @@
     }
   }
 
-  @Override
-  protected void finalize() throws Throwable {
-    close();
-  }
-
-  static JbpmConfiguration getCurrentJbpmConfiguration() {
+  static JbpmConfiguration getCurrentJbpmConfiguration()
+  {
     JbpmConfiguration currentJbpmConfiguration = null;
     List<JbpmConfiguration> stack = getJbpmConfigurationStack();
-    if (!stack.isEmpty()) {
+    if (!stack.isEmpty())
+    {
       currentJbpmConfiguration = stack.get(stack.size() - 1);
     }
     return currentJbpmConfiguration;
   }
 
-  static List<JbpmConfiguration> getJbpmConfigurationStack() {
+  static List<JbpmConfiguration> getJbpmConfigurationStack()
+  {
     return jbpmConfigurationStacks.get();
   }
 
-  synchronized void pushJbpmConfiguration() {
+  static void clearJbpmConfigurationStack()
+  {
+    List<JbpmConfiguration> configStack = getJbpmConfigurationStack();
+    if (configStack != null)
+    {
+      for (JbpmConfiguration config : new ArrayList<JbpmConfiguration>(configStack))
+      {
+        List<JbpmContext> contextStack = config.getJbpmContextStack();
+        if (contextStack != null)
+        {
+          for (JbpmContext context : new ArrayList<JbpmContext>(contextStack))
+          {
+            context.close();
+          }
+        }
+        contextStack.clear();
+      }
+      configStack.clear();
+    }
+  }
+
+  synchronized void pushJbpmConfiguration()
+  {
     getJbpmConfigurationStack().add(this);
   }
 
-  synchronized void popJbpmConfiguration() {
+  synchronized void popJbpmConfiguration()
+  {
     getJbpmConfigurationStack().remove(this);
   }
 
-  public JbpmContext getCurrentJbpmContext() {
+  public JbpmContext getCurrentJbpmContext()
+  {
     ensureOpen();
 
     JbpmContext currentJbpmContext = null;
     List<JbpmContext> stack = getJbpmContextStack();
-    if (!stack.isEmpty()) {
+    if (!stack.isEmpty())
+    {
       currentJbpmContext = stack.get(stack.size() - 1);
     }
     return currentJbpmContext;
   }
 
-  List<JbpmContext> getJbpmContextStack() {
+  List<JbpmContext> getJbpmContextStack()
+  {
     return jbpmContextStacks.get();
   }
 
-  void pushJbpmContext(JbpmContext jbpmContext) {
+  void pushJbpmContext(JbpmContext jbpmContext)
+  {
     getJbpmContextStack().add(jbpmContext);
   }
 
-  void popJbpmContext(JbpmContext jbpmContext) {
+  void popJbpmContext(JbpmContext jbpmContext)
+  {
     List<JbpmContext> stack = getJbpmContextStack();
     int size = stack.size();
-    if (size == 0) {
-      log.warn("closed JbpmContext more than once... "
-          + "check your try-finally clauses around JbpmContext blocks");
+    if (size == 0)
+    {
+      log.warn("closed JbpmContext more than once... " + "check your try-finally clauses around JbpmContext blocks");
     }
-    else if (jbpmContext != stack.remove(size - 1)) {
+    else if (jbpmContext != stack.remove(size - 1))
+    {
       stack.remove(jbpmContext); // prevent context from remaining in the stack
-      log.warn("closed JbpmContext in some order that differs from creation... "
-          + "check your try-finally clauses around JbpmContext blocks");
+      log.warn("closed JbpmContext in some order that differs from creation... " + "check your try-finally clauses around JbpmContext blocks");
     }
   }
 
-  void jbpmContextCreated(JbpmContext jbpmContext) {
+  void jbpmContextCreated(JbpmContext jbpmContext)
+  {
     pushJbpmConfiguration();
     pushJbpmContext(jbpmContext);
   }
 
-  void jbpmContextClosed(JbpmContext jbpmContext) {
+  void jbpmContextClosed(JbpmContext jbpmContext)
+  {
     popJbpmContext(jbpmContext);
     popJbpmConfiguration();
   }
 
-  public void startJobExecutor() {
+  public void startJobExecutor()
+  {
     getJobExecutor().start();
   }
 
-  public synchronized JobExecutor getJobExecutor() {
+  public synchronized JobExecutor getJobExecutor()
+  {
     ensureOpen();
 
-    if (jobExecutor == null) {
+    if (jobExecutor == null)
+    {
       Object object = objectFactory.createObject("jbpm.job.executor");
-      if (object instanceof JobExecutor) {
-        jobExecutor = (JobExecutor) object;
+      if (object instanceof JobExecutor)
+      {
+        jobExecutor = (JobExecutor)object;
       }
-      else if (object != null) {
-        throw new JbpmException("configuration object named 'jbpm.job.executor' is not a "
-            + JobExecutor.class.getSimpleName());
+      else if (object != null)
+      {
+        throw new JbpmException("configuration object named 'jbpm.job.executor' is not a " + JobExecutor.class.getSimpleName());
       }
     }
     return jobExecutor;

Modified: jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmContext.java
===================================================================
--- jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmContext.java	2009-03-10 16:06:14 UTC (rev 4217)
+++ jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmContext.java	2009-03-10 16:09:53 UTC (rev 4218)
@@ -113,7 +113,7 @@
     this.services = services;
     this.objectFactory = objectFactory;
   }
-
+  
   /**
    * make sure you close your JbpmContext in a finally block.
    */




More information about the jbpm-commits mailing list