onami-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject svn commit: r1451714 - in /incubator/onami/trunk: ./ scopes/ scopes/src/ scopes/src/main/ scopes/src/main/java/ scopes/src/main/java/org/ scopes/src/main/java/org/apache/ scopes/src/main/java/org/apache/onami/ scopes/src/main/java/org/apache/onami/scop...
Date Fri, 01 Mar 2013 20:53:02 GMT
Author: randgalt
Date: Fri Mar  1 20:53:01 2013
New Revision: 1451714

URL: http://svn.apache.org/r1451714
Log:
ONAMI-85
Initial checkin for Onami Scopes extension

Added:
    incubator/onami/trunk/scopes/
    incubator/onami/trunk/scopes/pom.xml   (with props)
    incubator/onami/trunk/scopes/src/
    incubator/onami/trunk/scopes/src/main/
    incubator/onami/trunk/scopes/src/main/java/
    incubator/onami/trunk/scopes/src/main/java/org/
    incubator/onami/trunk/scopes/src/main/java/org/apache/
    incubator/onami/trunk/scopes/src/main/java/org/apache/onami/
    incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/
    incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingleton.java   (with props)
    incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScope.java   (with props)
    incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScopeImpl.java   (with props)
    incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingleton.java   (with props)
    incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScope.java   (with props)
    incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScopeImpl.java   (with props)
    incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ScopesModule.java   (with props)
    incubator/onami/trunk/scopes/src/test/
    incubator/onami/trunk/scopes/src/test/java/
    incubator/onami/trunk/scopes/src/test/java/org/
    incubator/onami/trunk/scopes/src/test/java/org/apache/
    incubator/onami/trunk/scopes/src/test/java/org/apache/onami/
    incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/
    incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedConcurrentLazySingletonObject.java   (with props)
    incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedLazySingletonObject.java   (with props)
    incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedLazySingletonObjectAlt.java   (with props)
    incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/LazySingletonObject.java   (with props)
    incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/TestConcurrentLazySingleton.java   (with props)
    incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/TestLazySingleton.java   (with props)
Modified:
    incubator/onami/trunk/pom.xml

Modified: incubator/onami/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/pom.xml?rev=1451714&r1=1451713&r2=1451714&view=diff
==============================================================================
--- incubator/onami/trunk/pom.xml (original)
+++ incubator/onami/trunk/pom.xml Fri Mar  1 20:53:01 2013
@@ -1,5 +1,4 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
+<?xml version="1.0" encoding="UTF-8"?><!--
  | 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
@@ -17,7 +16,8 @@
  | specific language governing permissions and limitations
  | under the License.
 -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
 
   <groupId>org.apache.onami</groupId>
@@ -46,6 +46,7 @@
     <module>spi</module>
     <module>test</module>
     <module>validation</module>
+    <module>scopes</module>
   </modules>
 
   <build>

Added: incubator/onami/trunk/scopes/pom.xml
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/pom.xml?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/pom.xml (added)
+++ incubator/onami/trunk/scopes/pom.xml Fri Mar  1 20:53:01 2013
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <parent>
+    <groupId>org.apache.onami</groupId>
+    <artifactId>org.apache.onami.parent</artifactId>
+    <version>2-incubating</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>org.apache.onami.scopes</artifactId>
+  <version>1.0.0-incubating-SNAPSHOT</version>
+
+  <description>Apache Onami-Scopes adds useful Scopes to Google Guice</description>
+
+  <dependencies>
+    <dependency>
+      <groupId>com.google.inject</groupId>
+      <artifactId>guice</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.onami</groupId>
+      <artifactId>org.apache.onami.lifecycle</artifactId>
+      <version>0.2.0-incubating-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.onami</groupId>
+      <artifactId>org.apache.onami.test</artifactId>
+      <version>1.4.0-incubating</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>

Propchange: incubator/onami/trunk/scopes/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/onami/trunk/scopes/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: incubator/onami/trunk/scopes/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingleton.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingleton.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingleton.java (added)
+++ incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingleton.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,43 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 com.google.inject.ScopeAnnotation;
+import com.google.inject.Scopes;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Same as {@link LazySingleton} with the addition of allowing for more
+ * concurrency. The various {@link Scopes#SINGLETON} based scopes have
+ * a major concurrency restriction due to a blunt synchronization (see
+ * the comment inside of the Guice code). This version synchronizes
+ * on the object key and, thus, can construct multiple types of singletons
+ * concurrently.
+ */
+@Target({ ElementType.TYPE, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+@ScopeAnnotation
+public @interface ConcurrentLazySingleton
+{
+}

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingleton.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingleton.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingleton.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScope.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScope.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScope.java (added)
+++ incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScope.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,42 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 com.google.inject.Scope;
+
+/**
+ * A singleton factory that returns a Guice {@link Scope} that enables concurrent lazy singletons.
+ *
+ * @see ConcurrentLazySingleton
+ */
+public class ConcurrentLazySingletonScope
+{
+    private static final Scope instance = new ConcurrentLazySingletonScopeImpl();
+
+    /**
+     * Returns the scope
+     *
+     * @return scope
+     */
+    public static Scope get()
+    {
+        return instance;
+    }
+}

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScope.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScope.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScope.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScopeImpl.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScopeImpl.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScopeImpl.java (added)
+++ incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScopeImpl.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,143 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 com.google.inject.Key;
+import com.google.inject.Provider;
+import com.google.inject.ProvisionException;
+import com.google.inject.Scope;
+import com.google.inject.internal.CircularDependencyProxy;
+
+import java.util.HashMap;
+import java.util.Map;
+
+class ConcurrentLazySingletonScopeImpl
+    implements Scope
+{
+    private static final Object NULL = new Object();
+
+    private static final Map<Key<?>, LockRecord> locks = new HashMap<Key<?>, LockRecord>();
+
+    private static class LockRecord
+    {
+        private final Object lock = new Object();
+
+        private int useCount = 0;
+    }
+
+    public <T> Provider<T> scope( final Key<T> key, final Provider<T> creator )
+    {
+        return new Provider<T>()
+        {
+            /*
+             * The lazily initialized singleton instance. Once set, this will either have type T or will
+             * be equal to NULL.
+             */
+            private volatile Object instance;
+
+            // DCL on a volatile is safe as of Java 5, which we obviously require.
+            @SuppressWarnings("DoubleCheckedLocking")
+            public T get()
+            {
+                if ( instance == null )
+                {
+                    try
+                    {
+                        final Object lock = getLock( key );
+                        //noinspection SynchronizationOnLocalVariableOrMethodParameter
+                        synchronized ( lock )
+                        {
+                            if ( instance == null )
+                            {
+                                T provided = creator.get();
+
+                                // don't remember proxies; these exist only to serve circular dependencies
+                                if ( provided instanceof CircularDependencyProxy )
+                                {
+                                    return provided;
+                                }
+
+                                Object providedOrSentinel = ( provided == null ) ? NULL : provided;
+                                if ( ( instance != null ) && ( instance != providedOrSentinel ) )
+                                {
+                                    throw new ProvisionException( "Provider was reentrant while creating a singleton" );
+                                }
+
+                                instance = providedOrSentinel;
+                            }
+                        }
+                    }
+                    finally
+                    {
+                        releaseLock( key );
+                    }
+                }
+
+                Object localInstance = instance;
+                // This is safe because instance has type T or is equal to NULL
+                @SuppressWarnings({ "unchecked", "UnnecessaryLocalVariable" }) T returnedInstance =
+                    ( localInstance != NULL ) ? (T) localInstance : null;
+                return returnedInstance;
+            }
+
+            public String toString()
+            {
+                return String.format( "%s[%s]", creator, instance );
+            }
+        };
+    }
+
+    @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
+    private Object getLock( Key<?> key )
+    {
+        synchronized ( locks )
+        {
+            LockRecord lock = locks.get( key );
+            if ( lock == null )
+            {
+                lock = new LockRecord();
+                locks.put( key, lock );
+            }
+            ++lock.useCount;
+            return lock.lock;
+        }
+    }
+
+    private void releaseLock( Key<?> key )
+    {
+        synchronized ( locks )
+        {
+            LockRecord lock = locks.get( key );
+            if ( lock != null )
+            {
+                if ( --lock.useCount <= 0 )
+                {
+                    locks.remove( key );
+                }
+            }
+        }
+    }
+
+    @Override
+    public String toString()
+    {
+        return "ConcurrentLazySingletonScope.SCOPE";
+    }
+}

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScopeImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScopeImpl.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ConcurrentLazySingletonScopeImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingleton.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingleton.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingleton.java (added)
+++ incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingleton.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,38 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 com.google.inject.ScopeAnnotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Scope annotation that marks a class as singleton that should NOT be
+ * allocated eagerly
+ */
+@Target({ ElementType.TYPE, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+@ScopeAnnotation
+public @interface LazySingleton
+{
+}

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingleton.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingleton.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingleton.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScope.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScope.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScope.java (added)
+++ incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScope.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,44 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 com.google.inject.Scope;
+
+/**
+ * A singleton factory that returns a Guice {@link Scope} that enables lazy singletons
+ */
+public class LazySingletonScope
+{
+    /**
+     * Returns the scope
+     *
+     * @return scope
+     */
+    public static Scope get()
+    {
+        return instance;
+    }
+
+    private static final Scope instance = new LazySingletonScopeImpl();
+
+    private LazySingletonScope()
+    {
+    }
+}

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScope.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScope.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScope.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScopeImpl.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScopeImpl.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScopeImpl.java (added)
+++ incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScopeImpl.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,37 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 com.google.inject.Key;
+import com.google.inject.Provider;
+import com.google.inject.Scope;
+import com.google.inject.Scopes;
+
+/**
+ * A Guice {@link Scope} that enables lazy singletons
+ */
+final class LazySingletonScopeImpl
+    implements Scope
+{
+    public <T> Provider<T> scope( Key<T> key, Provider<T> unscoped )
+    {
+        return Scopes.SINGLETON.scope( key, unscoped );
+    }
+}

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScopeImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScopeImpl.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/LazySingletonScopeImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ScopesModule.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ScopesModule.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ScopesModule.java (added)
+++ incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ScopesModule.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,36 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 com.google.inject.AbstractModule;
+
+/**
+ * Guice module to register scopes.
+ */
+public class ScopesModule
+    extends AbstractModule
+{
+    @Override
+    protected void configure()
+    {
+        bindScope( LazySingleton.class, LazySingletonScope.get() );
+        bindScope( ConcurrentLazySingleton.class, ConcurrentLazySingletonScope.get() );
+    }
+}

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ScopesModule.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ScopesModule.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/onami/trunk/scopes/src/main/java/org/apache/onami/scopes/ScopesModule.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedConcurrentLazySingletonObject.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedConcurrentLazySingletonObject.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedConcurrentLazySingletonObject.java (added)
+++ incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedConcurrentLazySingletonObject.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,43 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 org.apache.onami.lifecycle.AfterInjection;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+@ConcurrentLazySingleton
+public class AnnotatedConcurrentLazySingletonObject
+{
+    public static final AtomicInteger constructorCount = new AtomicInteger( 0 );
+
+    public static final AtomicInteger postConstructCount = new AtomicInteger( 0 );
+
+    public AnnotatedConcurrentLazySingletonObject()
+    {
+        constructorCount.incrementAndGet();
+    }
+
+    @AfterInjection
+    public void postConstruct()
+    {
+        postConstructCount.incrementAndGet();
+    }
+}

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

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

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

Added: incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedLazySingletonObject.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedLazySingletonObject.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedLazySingletonObject.java (added)
+++ incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedLazySingletonObject.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,43 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 org.apache.onami.lifecycle.AfterInjection;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+@LazySingleton
+public class AnnotatedLazySingletonObject
+{
+    public static final AtomicInteger constructorCount = new AtomicInteger( 0 );
+
+    public static final AtomicInteger postConstructCount = new AtomicInteger( 0 );
+
+    public AnnotatedLazySingletonObject()
+    {
+        constructorCount.incrementAndGet();
+    }
+
+    @AfterInjection
+    public void postConstruct()
+    {
+        postConstructCount.incrementAndGet();
+    }
+}

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

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

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

Added: incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedLazySingletonObjectAlt.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedLazySingletonObjectAlt.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedLazySingletonObjectAlt.java (added)
+++ incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/AnnotatedLazySingletonObjectAlt.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,42 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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.annotation.PostConstruct;
+import java.util.concurrent.atomic.AtomicInteger;
+
+@LazySingleton
+public class AnnotatedLazySingletonObjectAlt
+{
+    public static final AtomicInteger constructorCount = new AtomicInteger( 0 );
+
+    public static final AtomicInteger postConstructCount = new AtomicInteger( 0 );
+
+    public AnnotatedLazySingletonObjectAlt()
+    {
+        constructorCount.incrementAndGet();
+    }
+
+    @PostConstruct
+    public void postConstruct()
+    {
+        postConstructCount.incrementAndGet();
+    }
+}

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

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

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

Added: incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/LazySingletonObject.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/LazySingletonObject.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/LazySingletonObject.java (added)
+++ incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/LazySingletonObject.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,42 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 org.apache.onami.lifecycle.AfterInjection;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class LazySingletonObject
+{
+    public static final AtomicInteger constructorCount = new AtomicInteger( 0 );
+
+    public static final AtomicInteger postConstructCount = new AtomicInteger( 0 );
+
+    public LazySingletonObject()
+    {
+        constructorCount.incrementAndGet();
+    }
+
+    @AfterInjection
+    public void postConstruct()
+    {
+        postConstructCount.incrementAndGet();
+    }
+}

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

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

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

Added: incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/TestConcurrentLazySingleton.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/TestConcurrentLazySingleton.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/TestConcurrentLazySingleton.java (added)
+++ incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/TestConcurrentLazySingleton.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,134 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 com.google.inject.Guice;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Provider;
+import org.apache.onami.lifecycle.AfterInjectionModule;
+import org.apache.onami.test.OnamiRunner;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+
+@RunWith(OnamiRunner.class)
+public class TestConcurrentLazySingleton
+{
+    public static class InjectedAnnotatedProvider
+    {
+        public final Provider<AnnotatedConcurrentLazySingletonObject> provider;
+
+        @Inject
+        public InjectedAnnotatedProvider( Provider<AnnotatedConcurrentLazySingletonObject> provider )
+        {
+            this.provider = provider;
+        }
+    }
+
+    @Before
+    public void setup()
+    {
+        AnnotatedConcurrentLazySingletonObject.constructorCount.set( 0 );
+        AnnotatedConcurrentLazySingletonObject.postConstructCount.set( 0 );
+        LazySingletonObject.constructorCount.set( 0 );
+        LazySingletonObject.postConstructCount.set( 0 );
+    }
+
+    @ConcurrentLazySingleton
+    public static class DeadLockTester
+    {
+        @Inject
+        public DeadLockTester( final Injector injector )
+            throws InterruptedException
+        {
+            final CountDownLatch latch = new CountDownLatch( 1 );
+            Executors.newSingleThreadExecutor().submit( new Callable<Object>()
+            {
+                public Object call()
+                    throws Exception
+                {
+                    injector.getInstance( AnnotatedConcurrentLazySingletonObject.class );
+                    latch.countDown();
+                    return null;
+                }
+            } );
+            latch.await();
+        }
+    }
+
+    @Test
+    public void testDeadLock()
+        throws InterruptedException
+    {
+        Injector injector = Guice.createInjector( new ScopesModule() );
+        injector.getInstance( DeadLockTester.class ); // if ConcurrentLazySingleton is not used, this line will deadlock
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.constructorCount.get(), 1 );
+    }
+
+    @Test
+    public void testUsingAnnotation()
+    {
+        Injector injector = Guice.createInjector( new AfterInjectionModule(), new ScopesModule() );
+
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.constructorCount.get(), 0 );
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.postConstructCount.get(), 0 );
+
+        AnnotatedConcurrentLazySingletonObject instance =
+            injector.getInstance( AnnotatedConcurrentLazySingletonObject.class );
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.postConstructCount.get(), 1 );
+
+        AnnotatedConcurrentLazySingletonObject instance2 =
+            injector.getInstance( AnnotatedConcurrentLazySingletonObject.class );
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.postConstructCount.get(), 1 );
+
+        Assert.assertSame( instance, instance2 );
+    }
+
+    @Test
+    public void testUsingInWithProviderAndAnnotation()
+    {
+        Injector injector = Guice.createInjector( new AfterInjectionModule(), new ScopesModule() );
+
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.constructorCount.get(), 0 );
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.postConstructCount.get(), 0 );
+
+        InjectedAnnotatedProvider injectedProvider = injector.getInstance( InjectedAnnotatedProvider.class );
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.constructorCount.get(), 0 );
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.postConstructCount.get(), 0 );
+
+        AnnotatedConcurrentLazySingletonObject instance = injectedProvider.provider.get();
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.postConstructCount.get(), 1 );
+
+        AnnotatedConcurrentLazySingletonObject instance2 = injectedProvider.provider.get();
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( AnnotatedConcurrentLazySingletonObject.postConstructCount.get(), 1 );
+
+        Assert.assertSame( instance, instance2 );
+    }
+}

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

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

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

Added: incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/TestLazySingleton.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/TestLazySingleton.java?rev=1451714&view=auto
==============================================================================
--- incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/TestLazySingleton.java (added)
+++ incubator/onami/trunk/scopes/src/test/java/org/apache/onami/scopes/TestLazySingleton.java Fri Mar  1 20:53:01 2013
@@ -0,0 +1,166 @@
+package org.apache.onami.scopes;
+
+/*
+ * 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 com.google.inject.Binder;
+import com.google.inject.Guice;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Module;
+import com.google.inject.Provider;
+import org.apache.onami.lifecycle.AfterInjectionModule;
+import org.apache.onami.test.OnamiRunner;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(OnamiRunner.class)
+public class TestLazySingleton
+{
+    public static class InjectedProvider
+    {
+        public final Provider<LazySingletonObject> provider;
+
+        @Inject
+        public InjectedProvider( Provider<LazySingletonObject> provider )
+        {
+            this.provider = provider;
+        }
+    }
+
+    public static class InjectedAnnotatedProvider
+    {
+        public final Provider<AnnotatedLazySingletonObject> provider;
+
+        @Inject
+        public InjectedAnnotatedProvider( Provider<AnnotatedLazySingletonObject> provider )
+        {
+            this.provider = provider;
+        }
+    }
+
+    @Before
+    public void setup()
+    {
+        AnnotatedLazySingletonObject.constructorCount.set( 0 );
+        AnnotatedLazySingletonObject.postConstructCount.set( 0 );
+        LazySingletonObject.constructorCount.set( 0 );
+        LazySingletonObject.postConstructCount.set( 0 );
+    }
+
+    @Test
+    public void testUsingAnnotation()
+    {
+        Injector injector = Guice.createInjector( new AfterInjectionModule(), new ScopesModule() );
+
+        Assert.assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 0 );
+        Assert.assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 0 );
+
+        AnnotatedLazySingletonObject instance = injector.getInstance( AnnotatedLazySingletonObject.class );
+        Assert.assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 1 );
+
+        AnnotatedLazySingletonObject instance2 = injector.getInstance( AnnotatedLazySingletonObject.class );
+        Assert.assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 1 );
+
+        Assert.assertSame( instance, instance2 );
+    }
+
+    @Test
+    public void testUsingInWithProviderAndAnnotation()
+    {
+        Injector injector = Guice.createInjector( new AfterInjectionModule(), new ScopesModule() );
+
+        Assert.assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 0 );
+        Assert.assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 0 );
+
+        InjectedAnnotatedProvider injectedProvider = injector.getInstance( InjectedAnnotatedProvider.class );
+        Assert.assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 0 );
+        Assert.assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 0 );
+
+        AnnotatedLazySingletonObject instance = injectedProvider.provider.get();
+        Assert.assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 1 );
+
+        AnnotatedLazySingletonObject instance2 = injectedProvider.provider.get();
+        Assert.assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 1 );
+
+        Assert.assertSame( instance, instance2 );
+    }
+
+    @Test
+    public void testUsingIn()
+    {
+        Module module = new Module()
+        {
+            public void configure( Binder binder )
+            {
+                binder.bind( LazySingletonObject.class ).in( LazySingletonScope.get() );
+            }
+        };
+        Injector injector = Guice.createInjector( new AfterInjectionModule(), new ScopesModule(), module );
+
+        Assert.assertEquals( LazySingletonObject.constructorCount.get(), 0 );
+        Assert.assertEquals( LazySingletonObject.postConstructCount.get(), 0 );
+
+        LazySingletonObject instance = injector.getInstance( LazySingletonObject.class );
+        Assert.assertEquals( LazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( LazySingletonObject.postConstructCount.get(), 1 );
+
+        LazySingletonObject instance2 = injector.getInstance( LazySingletonObject.class );
+        Assert.assertEquals( LazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( LazySingletonObject.postConstructCount.get(), 1 );
+
+        Assert.assertSame( instance, instance2 );
+    }
+
+    @Test
+    public void testUsingInWithProvider()
+    {
+        Module module = new Module()
+        {
+            public void configure( Binder binder )
+            {
+                binder.bind( LazySingletonObject.class ).in( LazySingletonScope.get() );
+            }
+        };
+        Injector injector = Guice.createInjector( new AfterInjectionModule(), new ScopesModule(), module );
+
+        Assert.assertEquals( LazySingletonObject.constructorCount.get(), 0 );
+        Assert.assertEquals( LazySingletonObject.postConstructCount.get(), 0 );
+
+        InjectedProvider injectedProvider = injector.getInstance( InjectedProvider.class );
+        Assert.assertEquals( LazySingletonObject.constructorCount.get(), 0 );
+        Assert.assertEquals( LazySingletonObject.postConstructCount.get(), 0 );
+
+        LazySingletonObject instance = injectedProvider.provider.get();
+        Assert.assertEquals( LazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( LazySingletonObject.postConstructCount.get(), 1 );
+
+        LazySingletonObject instance2 = injectedProvider.provider.get();
+        Assert.assertEquals( LazySingletonObject.constructorCount.get(), 1 );
+        Assert.assertEquals( LazySingletonObject.postConstructCount.get(), 1 );
+
+        Assert.assertSame( instance, instance2 );
+    }
+}

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

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

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



Mime
View raw message