You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by an...@apache.org on 2015/11/18 19:53:41 UTC

[4/6] tomee git commit: Call close on finalize

Call close on finalize


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/7e07f10b
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/7e07f10b
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/7e07f10b

Branch: refs/heads/tomee-1.7.x
Commit: 7e07f10b47149ce4a01441d186a7c8316e2156bc
Parents: 05855aa
Author: AndyGee <an...@gmx.de>
Authored: Wed Nov 18 19:51:32 2015 +0100
Committer: AndyGee <an...@gmx.de>
Committed: Wed Nov 18 19:51:32 2015 +0100

----------------------------------------------------------------------
 .../org/apache/openejb/OpenEjbContainer.java    |  25 +-
 .../openejb/core/ivm/naming/ContextWrapper.java |  11 +
 .../junit/context/OpenEjbTestContext.java       |  11 +
 .../apache/tomee/RemoteTomEEEJBContainer.java   | 333 ++++++++++---------
 .../apache/tomee/catalina/OpenEJBContext.java   |  42 ++-
 .../tomee/embedded/EmbeddedTomEEContainer.java  |  11 +
 6 files changed, 265 insertions(+), 168 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/7e07f10b/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java b/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
index 5f42516..803b6cf 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
@@ -115,11 +115,11 @@ public final class OpenEjbContainer extends EJBContainer {
     private static Logger logger; // initialized lazily to get the logging config from properties
 
     private ServiceManagerProxy serviceManager;
-    private Options options;
-    private OpenEjbContainer.GlobalContext globalJndiContext;
-    private WebBeansContext webBeanContext;
-    private ServletContext servletContext;
-    private HttpSession session;
+    private final Options options;
+    private final OpenEjbContainer.GlobalContext globalJndiContext;
+    private final WebBeansContext webBeanContext;
+    private final ServletContext servletContext;
+    private final HttpSession session;
 
     private OpenEjbContainer(final Map<?, ?> map, final AppContext appContext) {
         webBeanContext = appContext.getWebBeansContext();
@@ -142,6 +142,17 @@ public final class OpenEjbContainer extends EJBContainer {
     }
 
     @Override
+    protected void finalize() throws Throwable {
+        try {
+            this.close();
+        } catch (final Exception e) {
+            //no-op
+        } finally {
+            super.finalize();
+        }
+    }
+
+    @Override
     public void close() {
         if (isSingleClose()) {
             return;
@@ -329,6 +340,10 @@ public final class OpenEjbContainer extends EJBContainer {
 
                 final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
 
+                if(null == assembler){
+                    throw new IllegalStateException("Assembler has not been initialized");
+                }
+
                 final AppContext appContext;
 
                 try {

http://git-wip-us.apache.org/repos/asf/tomee/blob/7e07f10b/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/ContextWrapper.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/ContextWrapper.java b/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/ContextWrapper.java
index 2ff0ac6..fc2f94c 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/ContextWrapper.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/ContextWrapper.java
@@ -181,4 +181,15 @@ public class ContextWrapper implements Context {
     public void unbind(final String name) throws NamingException {
         context.unbind(name);
     }
+
+    @Override
+    protected void finalize() throws Throwable {
+        try {
+            this.close();
+        } catch (final Exception e) {
+            //no-op
+        } finally {
+            super.finalize();
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/tomee/blob/7e07f10b/container/openejb-junit/src/main/java/org/apache/openejb/junit/context/OpenEjbTestContext.java
----------------------------------------------------------------------
diff --git a/container/openejb-junit/src/main/java/org/apache/openejb/junit/context/OpenEjbTestContext.java b/container/openejb-junit/src/main/java/org/apache/openejb/junit/context/OpenEjbTestContext.java
index 26c464b..df2bf02 100644
--- a/container/openejb-junit/src/main/java/org/apache/openejb/junit/context/OpenEjbTestContext.java
+++ b/container/openejb-junit/src/main/java/org/apache/openejb/junit/context/OpenEjbTestContext.java
@@ -134,6 +134,17 @@ public class OpenEjbTestContext implements TestContext {
     }
 
     @Override
+    protected void finalize() throws Throwable {
+        try {
+            this.close();
+        } catch (final Exception e) {
+            //no-op
+        } finally {
+            super.finalize();
+        }
+    }
+
+    @Override
     public void close() {
         try {
             initialContext.close();

http://git-wip-us.apache.org/repos/asf/tomee/blob/7e07f10b/tomee/apache-tomee/src/main/java/org/apache/tomee/RemoteTomEEEJBContainer.java
----------------------------------------------------------------------
diff --git a/tomee/apache-tomee/src/main/java/org/apache/tomee/RemoteTomEEEJBContainer.java b/tomee/apache-tomee/src/main/java/org/apache/tomee/RemoteTomEEEJBContainer.java
index 95b8a38..67f68b1 100644
--- a/tomee/apache-tomee/src/main/java/org/apache/tomee/RemoteTomEEEJBContainer.java
+++ b/tomee/apache-tomee/src/main/java/org/apache/tomee/RemoteTomEEEJBContainer.java
@@ -1,161 +1,172 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- * <p/>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p/>
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.tomee;
-
-import org.apache.geronimo.osgi.locator.ProviderLocator;
-import org.apache.openejb.OpenEJBException;
-import org.apache.openejb.assembler.Deployer;
-import org.apache.openejb.assembler.DeployerEjb;
-import org.apache.openejb.client.RemoteInitialContextFactory;
-import org.apache.openejb.config.RemoteServer;
-import org.apache.openejb.loader.IO;
-import org.apache.tomee.util.QuickServerXmlParser;
-
-import javax.ejb.EJBException;
-import javax.ejb.embeddable.EJBContainer;
-import javax.ejb.spi.EJBContainerProvider;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.validation.ValidationException;
-import java.io.File;
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
-public class RemoteTomEEEJBContainer extends EJBContainer {
-    private static RemoteTomEEEJBContainer instance;
-    private RemoteServer container;
-    private InitialContext context;
-
-    @Override
-    public void close() {
-        instance.container.destroy();
-        instance.container = null;
-    }
-
-    @Override
-    public Context getContext() {
-        return context;
-    }
-
-    public static class Provider implements EJBContainerProvider {
-        private static final List<String> CONTAINER_NAMES = Arrays.asList(RemoteTomEEEJBContainer.class.getName(), "tomee-remote", "remote-tomee");
-
-        @Override
-        public EJBContainer createEJBContainer(final Map<?, ?> properties) {
-            final Object provider = properties.get(EJBContainer.PROVIDER);
-            int ejbContainerProviders = 1;
-            try {
-                ejbContainerProviders = ProviderLocator.getServices(EJBContainerProvider.class.getName(), EJBContainer.class, Thread.currentThread().getContextClassLoader()).size();
-            } catch (final Exception e) {
-                // no-op
-            }
-
-            if ((provider == null && ejbContainerProviders > 1)
-                    || (!RemoteTomEEEJBContainer.class.equals(provider)
-                    && !CONTAINER_NAMES.contains(String.valueOf(provider)))) {
-                return null;
-            }
-
-            if (instance != null) {
-                return instance;
-            }
-
-            final Object modules = properties.get(EJBContainer.MODULES);
-
-            System.getProperties().putAll(properties);
-            final File home = new File(System.getProperty("openejb.home", "doesn't exist"));
-            if (!home.exists()) {
-                throw new IllegalArgumentException("You need to set openejb.home");
-            }
-
-            final QuickServerXmlParser parser = QuickServerXmlParser.parse(new File(home, "conf/server.xml"));
-            final String remoteEjb = System.getProperty(Context.PROVIDER_URL, "http://" + parser.host() + ":" + parser.http() + "/tomee/ejb");
-
-            try {
-                instance = new RemoteTomEEEJBContainer();
-                instance.container = new RemoteServer();
-                instance.container.setPortStartup(Integer.parseInt(parser.http()));
-
-                try {
-                    instance.container.start();
-                } catch (final Exception e) {
-                    instance.container.destroy();
-                    throw e;
-                }
-
-                instance.context = new InitialContext(new Properties() {{
-                    setProperty(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
-                    setProperty(Context.PROVIDER_URL, remoteEjb);
-                }});
-
-                final Deployer deployer = Deployer.class.cast(instance.context.lookup("openejb/DeployerBusinessRemote"));
-
-                if (modules instanceof File) {
-                    final File file = File.class.cast(modules);
-                    deployFile(deployer, file);
-                } else if (modules instanceof String) {
-                    final String path = String.class.cast(modules);
-                    final File file = new File(path);
-                    deployFile(deployer, file);
-                } else if (modules instanceof String[]) {
-                    for (final String path : (String[]) modules) {
-                        deployFile(deployer, new File(path));
-                    }
-                } else if (modules instanceof File[]) {
-                    for (final File file : (File[]) modules) {
-                        deployFile(deployer, file);
-                    }
-                } // else suppose already deployed
-
-                return instance;
-            } catch (final OpenEJBException e) {
-                throw new EJBException(e);
-            } catch (final MalformedURLException e) {
-                throw new EJBException(e);
-            } catch (final ValidationException ve) {
-                throw ve;
-            } catch (final Exception e) {
-                if (e instanceof EJBException) {
-                    throw (EJBException) e;
-                }
-                throw new TomEERemoteEJBContainerException("initialization exception", e);
-            }
-        }
-    }
-
-    private static void deployFile(final Deployer deployer, final File file) throws IOException, OpenEJBException {
-        if ("true".equalsIgnoreCase(System.getProperty(DeployerEjb.OPENEJB_USE_BINARIES, "false"))) {
-            final Properties props = new Properties();
-            final byte[] slurpBinaries = IO.slurp(file).getBytes();
-            props.put(DeployerEjb.OPENEJB_VALUE_BINARIES, slurpBinaries);
-            props.put(DeployerEjb.OPENEJB_PATH_BINARIES, file.getName());
-            deployer.deploy(file.getAbsolutePath(), props);
-        } else {
-            deployer.deploy(file.getAbsolutePath());
-        }
-    }
-
-    protected static class TomEERemoteEJBContainerException extends RuntimeException {
-        protected TomEERemoteEJBContainerException(final String s, final Exception e) {
-            super(s, e);
-        }
-    }
-}
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomee;
+
+import org.apache.geronimo.osgi.locator.ProviderLocator;
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.assembler.Deployer;
+import org.apache.openejb.assembler.DeployerEjb;
+import org.apache.openejb.client.RemoteInitialContextFactory;
+import org.apache.openejb.config.RemoteServer;
+import org.apache.openejb.loader.IO;
+import org.apache.tomee.util.QuickServerXmlParser;
+
+import javax.ejb.EJBException;
+import javax.ejb.embeddable.EJBContainer;
+import javax.ejb.spi.EJBContainerProvider;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.validation.ValidationException;
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+public class RemoteTomEEEJBContainer extends EJBContainer {
+    private static RemoteTomEEEJBContainer instance;
+    private RemoteServer container;
+    private InitialContext context;
+
+    @Override
+    protected void finalize() throws Throwable {
+        try {
+            this.close();
+        } catch (final Exception e) {
+            //no-op
+        } finally {
+            super.finalize();
+        }
+    }
+
+    @Override
+    public void close() {
+        instance.container.destroy();
+        instance.container = null;
+    }
+
+    @Override
+    public Context getContext() {
+        return context;
+    }
+
+    public static class Provider implements EJBContainerProvider {
+        private static final List<String> CONTAINER_NAMES = Arrays.asList(RemoteTomEEEJBContainer.class.getName(), "tomee-remote", "remote-tomee");
+
+        @Override
+        public EJBContainer createEJBContainer(final Map<?, ?> properties) {
+            final Object provider = properties.get(EJBContainer.PROVIDER);
+            int ejbContainerProviders = 1;
+            try {
+                ejbContainerProviders = ProviderLocator.getServices(EJBContainerProvider.class.getName(), EJBContainer.class, Thread.currentThread().getContextClassLoader()).size();
+            } catch (final Exception e) {
+                // no-op
+            }
+
+            if ((provider == null && ejbContainerProviders > 1)
+                    || (!RemoteTomEEEJBContainer.class.equals(provider)
+                    && !CONTAINER_NAMES.contains(String.valueOf(provider)))) {
+                return null;
+            }
+
+            if (instance != null) {
+                return instance;
+            }
+
+            final Object modules = properties.get(EJBContainer.MODULES);
+
+            System.getProperties().putAll(properties);
+            final File home = new File(System.getProperty("openejb.home", "doesn't exist"));
+            if (!home.exists()) {
+                throw new IllegalArgumentException("You need to set openejb.home");
+            }
+
+            final QuickServerXmlParser parser = QuickServerXmlParser.parse(new File(home, "conf/server.xml"));
+            final String remoteEjb = System.getProperty(Context.PROVIDER_URL, "http://" + parser.host() + ":" + parser.http() + "/tomee/ejb");
+
+            try {
+                instance = new RemoteTomEEEJBContainer();
+                instance.container = new RemoteServer();
+                instance.container.setPortStartup(Integer.parseInt(parser.http()));
+
+                try {
+                    instance.container.start();
+                } catch (final Exception e) {
+                    instance.container.destroy();
+                    throw e;
+                }
+
+                instance.context = new InitialContext(new Properties() {{
+                    setProperty(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
+                    setProperty(Context.PROVIDER_URL, remoteEjb);
+                }});
+
+                final Deployer deployer = Deployer.class.cast(instance.context.lookup("openejb/DeployerBusinessRemote"));
+
+                if (modules instanceof File) {
+                    final File file = File.class.cast(modules);
+                    deployFile(deployer, file);
+                } else if (modules instanceof String) {
+                    final String path = String.class.cast(modules);
+                    final File file = new File(path);
+                    deployFile(deployer, file);
+                } else if (modules instanceof String[]) {
+                    for (final String path : (String[]) modules) {
+                        deployFile(deployer, new File(path));
+                    }
+                } else if (modules instanceof File[]) {
+                    for (final File file : (File[]) modules) {
+                        deployFile(deployer, file);
+                    }
+                } // else suppose already deployed
+
+                return instance;
+            } catch (final OpenEJBException e) {
+                throw new EJBException(e);
+            } catch (final MalformedURLException e) {
+                throw new EJBException(e);
+            } catch (final ValidationException ve) {
+                throw ve;
+            } catch (final Exception e) {
+                if (e instanceof EJBException) {
+                    throw (EJBException) e;
+                }
+                throw new TomEERemoteEJBContainerException("initialization exception", e);
+            }
+        }
+    }
+
+    private static void deployFile(final Deployer deployer, final File file) throws IOException, OpenEJBException {
+        if ("true".equalsIgnoreCase(System.getProperty(DeployerEjb.OPENEJB_USE_BINARIES, "false"))) {
+            final Properties props = new Properties();
+            final byte[] slurpBinaries = IO.slurp(file).getBytes();
+            props.put(DeployerEjb.OPENEJB_VALUE_BINARIES, slurpBinaries);
+            props.put(DeployerEjb.OPENEJB_PATH_BINARIES, file.getName());
+            deployer.deploy(file.getAbsolutePath(), props);
+        } else {
+            deployer.deploy(file.getAbsolutePath());
+        }
+    }
+
+    protected static class TomEERemoteEJBContainerException extends RuntimeException {
+        protected TomEERemoteEJBContainerException(final String s, final Exception e) {
+            super(s, e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/7e07f10b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/OpenEJBContext.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/OpenEJBContext.java b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/OpenEJBContext.java
index 4e7703f..8458672 100644
--- a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/OpenEJBContext.java
+++ b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/OpenEJBContext.java
@@ -40,6 +40,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Object lookup(final Name name) throws NamingException {
         return getThreadContext().lookup(name);
     }
@@ -47,6 +48,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Object lookup(final String name) throws NamingException {
         return getThreadContext().lookup(name);
     }
@@ -54,6 +56,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public void bind(final Name name, final Object obj) throws NamingException {
         getThreadContext().bind(name, obj);
     }
@@ -61,6 +64,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public void bind(final String name, final Object obj) throws NamingException {
         getThreadContext().bind(name, obj);
     }
@@ -68,6 +72,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public void rebind(final Name name, final Object obj) throws NamingException {
         getThreadContext().rebind(name, obj);
     }
@@ -75,6 +80,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public void rebind(final String name, final Object obj) throws NamingException {
         getThreadContext().rebind(name, obj);
     }
@@ -82,6 +88,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public void unbind(final Name name) throws NamingException {
         getThreadContext().unbind(name);
     }
@@ -89,6 +96,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public void unbind(final String name) throws NamingException {
         getThreadContext().unbind(name);
     }
@@ -96,6 +104,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public void rename(final Name oldName, final Name newName) throws NamingException {
         getThreadContext().rename(oldName, newName);
     }
@@ -103,6 +112,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public void rename(final String oldName, final String newName) throws NamingException {
         getThreadContext().rename(oldName, newName);
     }
@@ -110,6 +120,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public NamingEnumeration<NameClassPair> list(final Name name) throws NamingException {
         return getThreadContext().list(name);
     }
@@ -117,6 +128,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public NamingEnumeration<NameClassPair> list(final String name) throws NamingException {
         return getThreadContext().list(name);
     }
@@ -124,6 +136,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public NamingEnumeration<Binding> listBindings(final Name name) throws NamingException {
         return getThreadContext().listBindings(name);
     }
@@ -131,6 +144,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public NamingEnumeration<Binding> listBindings(final String name) throws NamingException {
         return getThreadContext().listBindings(name);
     }
@@ -138,6 +152,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public void destroySubcontext(final Name name) throws NamingException {
         getThreadContext().destroySubcontext(name);
     }
@@ -145,6 +160,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public void destroySubcontext(final String name) throws NamingException {
         getThreadContext().destroySubcontext(name);
     }
@@ -152,6 +168,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Context createSubcontext(final Name name) throws NamingException {
         return getThreadContext().createSubcontext(name);
     }
@@ -159,6 +176,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Context createSubcontext(final String name) throws NamingException {
         return getThreadContext().createSubcontext(name);
     }
@@ -166,6 +184,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Object lookupLink(final Name name) throws NamingException {
         return getThreadContext().lookupLink(name);
     }
@@ -173,6 +192,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Object lookupLink(final String name) throws NamingException {
         return getThreadContext().lookupLink(name);
     }
@@ -180,6 +200,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public NameParser getNameParser(final Name name) throws NamingException {
         return getThreadContext().getNameParser(name);
     }
@@ -187,6 +208,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public NameParser getNameParser(final String name) throws NamingException {
         return getThreadContext().getNameParser(name);
     }
@@ -194,6 +216,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Name composeName(final Name name, final Name prefix) throws NamingException {
         return getThreadContext().composeName(name, prefix);
     }
@@ -201,6 +224,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public String composeName(final String name, final String prefix) throws NamingException {
         return getThreadContext().composeName(name, prefix);
     }
@@ -208,6 +232,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Object addToEnvironment(final String propName, final Object propVal) throws NamingException {
         return getThreadContext().addToEnvironment(propName, propVal);
     }
@@ -215,6 +240,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Object removeFromEnvironment(final String propName) throws NamingException {
         return getThreadContext().removeFromEnvironment(propName);
     }
@@ -222,6 +248,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public Hashtable<?, ?> getEnvironment() throws NamingException {
         return getThreadContext().getEnvironment();
     }
@@ -229,6 +256,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public void close() throws NamingException {
         getThreadContext().close();
     }
@@ -236,6 +264,7 @@ public class OpenEJBContext implements Context {
     /**
      * {@inheritDoc}
      */
+    @Override
     public String getNameInNamespace() throws NamingException {
         return "";
     }
@@ -251,8 +280,7 @@ public class OpenEJBContext implements Context {
         if (skipEjbContext(threadContext)) {
             return ContextBindings.getClassLoader();
         }
-        final Context context = threadContext.getBeanContext().getJndiEnc();
-        return context;
+        return threadContext.getBeanContext().getJndiEnc();
     }
 
     private boolean skipEjbContext(final ThreadContext threadContext) {
@@ -262,5 +290,15 @@ public class OpenEJBContext implements Context {
         return threadContext == null || DeployerEjb.class.equals(threadContext.getBeanContext().getBeanClass());
     }
 
+    @Override
+    protected void finalize() throws Throwable {
+        try {
+            this.close();
+        } catch (final Exception e) {
+            //no-op
+        } finally {
+            super.finalize();
+        }
+    }
 }
 

http://git-wip-us.apache.org/repos/asf/tomee/blob/7e07f10b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/EmbeddedTomEEContainer.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/EmbeddedTomEEContainer.java b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/EmbeddedTomEEContainer.java
index 835bb6b..964dd5d 100644
--- a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/EmbeddedTomEEContainer.java
+++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/EmbeddedTomEEContainer.java
@@ -57,6 +57,17 @@ public final class EmbeddedTomEEContainer extends EJBContainer {
     }
 
     @Override
+    protected void finalize() throws Throwable {
+        try {
+            this.close();
+        } catch (final Exception e) {
+            //no-op
+        } finally {
+            super.finalize();
+        }
+    }
+
+    @Override
     public void close() {
         final Collection<Exception> errors = new ArrayList<Exception>();
         for (final String id : deployedIds) {