onami-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1433094 - in /incubator/onami/trunk/lifecycle/src: main/java/org/apache/onami/lifecycle/ test/java/org/apache/onami/lifecycle/
Date Mon, 14 Jan 2013 20:15:48 GMT
Author: simonetripodi
Date: Mon Jan 14 20:15:47 2013
New Revision: 1433094

URL: http://svn.apache.org/viewvc?rev=1433094&view=rev
Log:
[ONAMI-45] Injector creation can fail and in that case all created resources must be disposed
anyway - patch submitted by Mikhail Mazursky

Added:
    incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposableObject.java
  (with props)
    incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposeModuleTestCase.java
  (with props)
    incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/ThrowingExceptionConstructor.java
  (with props)
Modified:
    incubator/onami/trunk/lifecycle/src/main/java/org/apache/onami/lifecycle/DisposeModule.java

Modified: incubator/onami/trunk/lifecycle/src/main/java/org/apache/onami/lifecycle/DisposeModule.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/lifecycle/src/main/java/org/apache/onami/lifecycle/DisposeModule.java?rev=1433094&r1=1433093&r2=1433094&view=diff
==============================================================================
--- incubator/onami/trunk/lifecycle/src/main/java/org/apache/onami/lifecycle/DisposeModule.java
(original)
+++ incubator/onami/trunk/lifecycle/src/main/java/org/apache/onami/lifecycle/DisposeModule.java
Mon Jan 14 20:15:47 2013
@@ -19,9 +19,12 @@ package org.apache.onami.lifecycle;
  * under the License.
  */
 
+import static com.google.inject.matcher.Matchers.any;
+
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 
+import com.google.inject.Injector;
 import com.google.inject.TypeLiteral;
 import com.google.inject.matcher.Matcher;
 import com.google.inject.spi.InjectionListener;
@@ -29,17 +32,21 @@ import com.google.inject.spi.TypeEncount
 
 /**
  * Guice module to register methods to be invoked when {@link Disposer#dispose()} is invoked.
+ * <p>
+ * Module instance have state so it must not be used to construct more than one {@link Injector}.

  */
 public final class DisposeModule
     extends AbstractLifeCycleModule
 {
 
+    private final Disposer disposer;
+
     /**
      * Creates a new module which register methods annotated with {@link Dispose} on methods
in any type.
      */
     public DisposeModule()
     {
-        super( Dispose.class );
+        this( Dispose.class, any() );
     }
 
     /**
@@ -53,6 +60,19 @@ public final class DisposeModule
                                                  Matcher<? super TypeLiteral<?>>
typeMatcher )
     {
         super( disposeAnnotationType, typeMatcher );
+        disposer = new Disposer();
+    }
+
+    /**
+     * Creates a new module from the supplied {@link Builder}.
+     *
+     * @param builder settings container.
+     * @since 0.2.0
+     */
+    DisposeModule( Builder builder )
+    {
+        super( builder.disposeAnnotationType, builder.typeMatcher );
+        this.disposer = builder.disposer;
     }
 
     /**
@@ -61,8 +81,6 @@ public final class DisposeModule
     @Override
     protected void configure()
     {
-        final Disposer disposer = new Disposer();
-
         bind( Disposer.class ).toInstance( disposer );
 
         bindListener( getTypeMatcher(), new AbstractMethodTypeListener( getAnnotationType()
)
@@ -85,4 +103,98 @@ public final class DisposeModule
         } );
     }
 
+    /**
+     * Allows to create {@link DisposeModule} with builder pattern.
+     *
+     * @return builder for {@link DisposeModule}.
+     * @since 0.2.0
+     */
+    public static Builder builder()
+    {
+        return new Builder();
+    }
+
+    /**
+     * Builder pattern helper.
+     *
+     * @since 0.2.0
+     */
+    public static final class Builder
+    {
+
+        Class<? extends Annotation> disposeAnnotationType = Dispose.class;
+
+        Matcher<? super TypeLiteral<?>> typeMatcher = any();
+
+        Disposer disposer = new Disposer();
+
+        /**
+         * Hidden constructor.
+         */
+        Builder()
+        {
+        }
+
+        /**
+         * Builds {@link DisposeModule} with given settings.
+         *
+         * @return {@link DisposeModule} with given settings.
+         * @since 0.2.0
+         */
+        public DisposeModule build()
+        {
+            return new DisposeModule( this );
+        }
+
+        /**
+         * Sets <i>Dispose</i> annotation to be searched.
+         *
+         * @param disposeAnnotationType <i>Dispose</i> annotation to be searched.
+         * @return self
+         * @since 0.2.0
+         */
+        public Builder withDisposeAnnotationType( Class<? extends Annotation> disposeAnnotationType
)
+        {
+            this.disposeAnnotationType = checkNotNull( disposeAnnotationType,
+                                                       "Argument 'disposeAnnotationType'
must be not null." );
+            return this;
+        }
+
+        /**
+         * Sets the filter for injectee types.
+         *
+         * @param typeMatcher the filter for injectee types.
+         * @return self
+         * @since 0.2.0
+         */
+        public Builder withTypeMatcher( Matcher<? super TypeLiteral<?>> typeMatcher
)
+        {
+            this.typeMatcher = checkNotNull( typeMatcher, "Argument 'typeMatcher' must be
not null." );
+            return this;
+        }
+
+        /**
+         * Sets the container to register disposable objects.
+         *
+         * @param disposer container to register disposable objects.
+         * @return self
+         * @since 0.2.0
+         */
+        public Builder withDisposer( Disposer disposer )
+        {
+            this.disposer = checkNotNull( disposer, "Argument 'disposer' must be not null."
);
+            return this;
+        }
+
+        private static <T> T checkNotNull( T object, String message )
+        {
+            if ( object == null )
+            {
+                throw new IllegalArgumentException( message );
+            }
+            return object;
+        }
+
+    }
+
 }

Added: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposableObject.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposableObject.java?rev=1433094&view=auto
==============================================================================
--- incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposableObject.java
(added)
+++ incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposableObject.java
Mon Jan 14 20:15:47 2013
@@ -0,0 +1,33 @@
+package org.apache.onami.lifecycle;
+
+/*
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+public final class DisposableObject
+{
+
+    boolean disposed = false;
+
+    @Dispose
+    public void dispose()
+    {
+        disposed  = true;
+    }
+
+}

Propchange: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposableObject.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposableObject.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposableObject.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposeModuleTestCase.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposeModuleTestCase.java?rev=1433094&view=auto
==============================================================================
--- incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposeModuleTestCase.java
(added)
+++ incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposeModuleTestCase.java
Mon Jan 14 20:15:47 2013
@@ -0,0 +1,79 @@
+package org.apache.onami.lifecycle;
+
+/*
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+import static com.google.inject.Guice.createInjector;
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.fail;
+
+import org.junit.Test;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.CreationException;
+
+public final class DisposeModuleTestCase
+{
+
+    @Test
+    public void disposeUsingModuleOnInjectorFailure()
+    {
+        Disposer disposer = new Disposer();
+        try
+        {
+            createInjector( DisposeModule.builder().withDisposer( disposer ).build(), new
AbstractModule()
+            {
+
+                @Override
+                protected void configure()
+                {
+                    bind( ThrowingExceptionConstructor.class ).asEagerSingleton();
+                }
+
+            } );
+            fail( "Expected exception was not thrown" );
+        }
+        catch( CreationException e )
+        {
+            Throwable cause = e.getCause();
+            assertTrue( cause instanceof IllegalArgumentException );
+            assertEquals( "Expected exception", cause.getMessage() );
+        }
+        finally
+        {
+            disposer.dispose( new DisposeHandler()
+            {
+
+                public <I> void onSuccess( I injectee )
+                {
+                    assertTrue( injectee instanceof DisposableObject );
+                    assertTrue( ((DisposableObject) injectee).disposed );
+                }
+
+                public <I, E extends Throwable> void onError( I injectee, E error )
+                {
+                    fail( error.toString() );
+                }
+
+            });
+        }
+    }
+
+}

Propchange: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposeModuleTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposeModuleTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/DisposeModuleTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/ThrowingExceptionConstructor.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/ThrowingExceptionConstructor.java?rev=1433094&view=auto
==============================================================================
--- incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/ThrowingExceptionConstructor.java
(added)
+++ incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/ThrowingExceptionConstructor.java
Mon Jan 14 20:15:47 2013
@@ -0,0 +1,33 @@
+package org.apache.onami.lifecycle;
+
+/*
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+import javax.inject.Inject;
+
+public final class ThrowingExceptionConstructor
+{
+
+    @Inject
+    public ThrowingExceptionConstructor( DisposableObject disposableObject )
+    {
+        throw new IllegalArgumentException( "Expected exception" );
+    }
+
+}

Propchange: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/ThrowingExceptionConstructor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/ThrowingExceptionConstructor.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/onami/trunk/lifecycle/src/test/java/org/apache/onami/lifecycle/ThrowingExceptionConstructor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message