tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject tomee git commit: TOMEE-1490 more test on LazyRealm and making it more tolerant regarding cdi integration
Date Fri, 09 Jan 2015 09:42:31 GMT
Repository: tomee
Updated Branches:
  refs/heads/develop 6338b12ce -> 02c4e0a2f


TOMEE-1490 more test on LazyRealm and making it more tolerant regarding cdi integration


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

Branch: refs/heads/develop
Commit: 02c4e0a2f1fb5a7c2e5bd2861d49c0beaf0c0f98
Parents: 6338b12
Author: Romain Manni-Bucau <rmannibucau@apache.org>
Authored: Fri Jan 9 10:41:56 2015 +0100
Committer: Romain Manni-Bucau <rmannibucau@apache.org>
Committed: Fri Jan 9 10:41:56 2015 +0100

----------------------------------------------------------------------
 .../tests/realm/CdiLazyRealmTOMEE1490Test.java  |  63 ++++++++++
 .../CdiLifecycleLazyRealmTOMEE1490Test.java     |  63 ++++++++++
 .../tests/realm/LowTypedRealmTOMEE1490Test.java |  63 ++++++++++
 .../arquillian/tests/realm/MyCdiLazyRealm.java  | 121 +++++++++++++++++++
 .../tests/realm/MyCdiRealmBaseLazyRealm.java    |  42 +++++++
 .../arquillian/tests/realm/SimpleEndpoint.java  |  33 +++++
 .../apache/tomee/catalina/realm/LazyRealm.java  | 100 ++++++++++-----
 7 files changed, 457 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/02c4e0a2/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/CdiLazyRealmTOMEE1490Test.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/CdiLazyRealmTOMEE1490Test.java
b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/CdiLazyRealmTOMEE1490Test.java
new file mode 100644
index 0000000..8a53e00
--- /dev/null
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/CdiLazyRealmTOMEE1490Test.java
@@ -0,0 +1,63 @@
+/**
+ * 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.
+ */
+package org.apache.openejb.arquillian.tests.realm;
+
+import org.apache.catalina.authenticator.BasicAuthenticator;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.IOException;
+import java.net.URL;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(Arquillian.class)
+public class CdiLazyRealmTOMEE1490Test {
+    @Deployment(testable = false)
+    public static WebArchive createDeployment() {
+        return ShrinkWrap.create(WebArchive.class, "example.war")
+                .addClasses(SimpleEndpoint.class, MyCdiLazyRealm.class)
+                .addAsManifestResource(new StringAsset("<Context preemptiveAuthentication=\"true\">\n"
+
+                        "  <Valve className=\"" + BasicAuthenticator.class.getName() +
"\" />\n" +
+                        "  <Realm cdi=\"true\"\n" +
+                        "         className=\"org.apache.tomee.catalina.realm.LazyRealm\"\n"
+
+                        "         realmClass=\"" + MyCdiLazyRealm.class.getName() + "\" />\n"
+
+                        "</Context>"), "context.xml")
+                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
+    }
+
+    @ArquillianResource
+    private URL webapp;
+
+    @Test
+    public void success() throws IOException {
+        assertEquals("ok", WebClient.create(webapp.toExternalForm(), "user", "pwd", null).get(String.class));
+    }
+
+    @Test(expected = Exception.class)
+    public void failure() throws IOException {
+        assertEquals("ok", WebClient.create(webapp.toExternalForm(), "user", "wrong", null).get(String.class));
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/02c4e0a2/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/CdiLifecycleLazyRealmTOMEE1490Test.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/CdiLifecycleLazyRealmTOMEE1490Test.java
b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/CdiLifecycleLazyRealmTOMEE1490Test.java
new file mode 100644
index 0000000..ccfa527
--- /dev/null
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/CdiLifecycleLazyRealmTOMEE1490Test.java
@@ -0,0 +1,63 @@
+/**
+ * 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.
+ */
+package org.apache.openejb.arquillian.tests.realm;
+
+import org.apache.catalina.authenticator.BasicAuthenticator;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.IOException;
+import java.net.URL;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(Arquillian.class)
+public class CdiLifecycleLazyRealmTOMEE1490Test {
+    @Deployment(testable = false)
+    public static WebArchive createDeployment() {
+        return ShrinkWrap.create(WebArchive.class, "example.war")
+                .addClasses(SimpleEndpoint.class, MyCdiRealmBaseLazyRealm.class)
+                .addAsManifestResource(new StringAsset("<Context preemptiveAuthentication=\"true\">\n"
+
+                        "  <Valve className=\"" + BasicAuthenticator.class.getName() +
"\" />\n" +
+                        "  <Realm cdi=\"true\"\n" +
+                        "         className=\"org.apache.tomee.catalina.realm.LazyRealm\"\n"
+
+                        "         realmClass=\"" + MyCdiRealmBaseLazyRealm.class.getName()
+ "\" />\n" +
+                        "</Context>"), "context.xml")
+                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
+    }
+
+    @ArquillianResource
+    private URL webapp;
+
+    @Test
+    public void success() throws IOException {
+        assertEquals("ok", WebClient.create(webapp.toExternalForm(), "user", "pwd", null).get(String.class));
+    }
+
+    @Test(expected = Exception.class)
+    public void failure() throws IOException {
+        assertEquals("ok", WebClient.create(webapp.toExternalForm(), "user", "wrong", null).get(String.class));
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/02c4e0a2/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/LowTypedRealmTOMEE1490Test.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/LowTypedRealmTOMEE1490Test.java
b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/LowTypedRealmTOMEE1490Test.java
new file mode 100644
index 0000000..16604ae
--- /dev/null
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/LowTypedRealmTOMEE1490Test.java
@@ -0,0 +1,63 @@
+/**
+ * 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.
+ */
+package org.apache.openejb.arquillian.tests.realm;
+
+import org.apache.catalina.authenticator.BasicAuthenticator;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.IOException;
+import java.net.URL;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(Arquillian.class)
+public class LowTypedRealmTOMEE1490Test {
+    @Deployment(testable = false)
+    public static WebArchive createDeployment() {
+        return ShrinkWrap.create(WebArchive.class, "example.war")
+                .addClasses(SimpleEndpoint.class, MyCdiLazyRealm.class)
+                .addAsManifestResource(new StringAsset("<Context preemptiveAuthentication=\"true\">\n"
+
+                        "  <Valve className=\"" + BasicAuthenticator.class.getName() +
"\" />\n" +
+                        "  <Realm cdi=\"true\"\n" +
+                        "         className=\"org.apache.tomee.catalina.realm.LazyRealm\"\n"
+
+                        "         realmClass=\"" + MyCdiLazyRealm.class.getName() + "\" />\n"
+
+                        "</Context>"), "context.xml")
+                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
+    }
+
+    @ArquillianResource
+    private URL webapp;
+
+    @Test
+    public void success() throws IOException {
+        assertEquals("ok", WebClient.create(webapp.toExternalForm(), "user", "pwd", null).get(String.class));
+    }
+
+    @Test(expected = Exception.class)
+    public void failure() throws IOException {
+        assertEquals("ok", WebClient.create(webapp.toExternalForm(), "user", "wrong", null).get(String.class));
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/02c4e0a2/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/MyCdiLazyRealm.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/MyCdiLazyRealm.java
b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/MyCdiLazyRealm.java
new file mode 100644
index 0000000..c52127b
--- /dev/null
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/MyCdiLazyRealm.java
@@ -0,0 +1,121 @@
+/**
+ * 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.
+ */
+package org.apache.openejb.arquillian.tests.realm;
+
+import org.apache.catalina.Container;
+import org.apache.catalina.Context;
+import org.apache.catalina.CredentialHandler;
+import org.apache.catalina.Realm;
+import org.apache.catalina.Wrapper;
+import org.apache.catalina.connector.Request;
+import org.apache.catalina.connector.Response;
+import org.apache.catalina.realm.GenericPrincipal;
+import org.apache.catalina.realm.RealmBase;
+import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
+import org.ietf.jgss.GSSContext;
+
+import javax.enterprise.context.ApplicationScoped;
+import java.beans.PropertyChangeListener;
+import java.io.IOException;
+import java.security.Principal;
+import java.security.cert.X509Certificate;
+
+import static java.util.Arrays.asList;
+
+@ApplicationScoped // can't be request scoped cause it is a realm impl
+public class MyCdiLazyRealm implements Realm {
+    private Container container;
+
+    @Override
+    public Container getContainer() {
+        return container;
+    }
+
+    @Override
+    public void setContainer(final Container container) {
+        this.container = container;
+    }
+
+    @Override
+    public CredentialHandler getCredentialHandler() {
+        return null;
+    }
+
+    @Override
+    public void setCredentialHandler(final CredentialHandler credentialHandler) {
+
+    }
+
+    @Override
+    public void addPropertyChangeListener(final PropertyChangeListener listener) {
+
+    }
+
+    @Override
+    public Principal authenticate(final String username, final String credentials) {
+        return "user".equalsIgnoreCase(username) && "pwd".equalsIgnoreCase(credentials)
? new GenericPrincipal(username, "pwd", asList("role")) : null;
+    }
+
+    @Override
+    public Principal authenticate(final String username, final String digest, final String
nonce,
+                                  final String nc, final String cnonce, final String qop,
+                                  final String realm, final String md5a2) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Principal authenticate(final GSSContext gssContext, final boolean storeCreds)
{
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Principal authenticate(final X509Certificate[] certs) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void backgroundProcess() {
+        // no-op
+    }
+
+    @Override
+    public SecurityConstraint[] findSecurityConstraints(final Request request, final Context
context) {
+        return null;
+    }
+
+    @Override
+    public boolean hasResourcePermission(final Request request, final Response response,
+                                         final SecurityConstraint[] constraint, final Context
context) throws IOException {
+        return false;
+    }
+
+    @Override
+    public boolean hasRole(final Wrapper wrapper, final Principal principal, final String
role) {
+        return false;
+    }
+
+    @Override
+    public boolean hasUserDataPermission(final Request request, final Response response,
+                                         final SecurityConstraint[] constraint) throws IOException
{
+        return false;
+    }
+
+    @Override
+    public void removePropertyChangeListener(final PropertyChangeListener listener) {
+        // no-op
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/02c4e0a2/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/MyCdiRealmBaseLazyRealm.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/MyCdiRealmBaseLazyRealm.java
b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/MyCdiRealmBaseLazyRealm.java
new file mode 100644
index 0000000..f1b5e30
--- /dev/null
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/MyCdiRealmBaseLazyRealm.java
@@ -0,0 +1,42 @@
+/**
+ * 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.
+ */
+package org.apache.openejb.arquillian.tests.realm;
+
+import org.apache.catalina.realm.GenericPrincipal;
+import org.apache.catalina.realm.RealmBase;
+
+import java.security.Principal;
+
+import static java.util.Arrays.asList;
+
+// no scope cause RealmBase is not proxyable, realm impl would put a scope + implement Realm,
Lifecycle
+public class MyCdiRealmBaseLazyRealm extends RealmBase {
+    @Override
+    protected String getName() {
+        return "user";
+    }
+
+    @Override
+    protected String getPassword(final String username) {
+        return "pwd";
+    }
+
+    @Override
+    protected Principal getPrincipal(final String username) {
+        return new GenericPrincipal(username, getPassword(username), asList("role"));
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/02c4e0a2/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/SimpleEndpoint.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/SimpleEndpoint.java
b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/SimpleEndpoint.java
new file mode 100644
index 0000000..1157436
--- /dev/null
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-webprofile-tests/src/test/java/org/apache/openejb/arquillian/tests/realm/SimpleEndpoint.java
@@ -0,0 +1,33 @@
+/**
+ * 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.
+ */
+package org.apache.openejb.arquillian.tests.realm;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+@WebServlet("/*")
+public class SimpleEndpoint extends HttpServlet {
+    @Override
+    protected void service(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
+        resp.getWriter().write("ok");
+        resp.setContentType("text/plain");
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/02c4e0a2/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/realm/LazyRealm.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/realm/LazyRealm.java
b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/realm/LazyRealm.java
index 6e29153..91bbfaf 100644
--- a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/realm/LazyRealm.java
+++ b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/realm/LazyRealm.java
@@ -41,6 +41,7 @@ import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import java.beans.PropertyChangeListener;
+import java.beans.PropertyChangeSupport;
 import java.io.IOException;
 import java.security.Principal;
 import java.security.cert.X509Certificate;
@@ -55,6 +56,9 @@ public class LazyRealm extends LifecycleBase implements Realm {
     private volatile Realm delegate;
     private Context container;
     private CredentialHandler credentialHandler;
+    private volatile boolean init;
+    private volatile boolean start;
+    private final PropertyChangeSupport support = new PropertyChangeSupport(this);
 
     private CreationalContext<Object> creationalContext;
 
@@ -105,10 +109,16 @@ public class LazyRealm extends LifecycleBase implements Realm {
                             throw new TomEERuntimeException(e);
                         }
                     } else {
-                        final WebBeansContext webBeansContext = WebBeansContext.currentInstance();
-                        if (webBeansContext == null) {
+                        final WebBeansContext webBeansContext;
+                        try {
+                            webBeansContext = WebBeansContext.currentInstance();
+                            if (webBeansContext == null) {
+                                return null;
+                            }
+                        } catch (final IllegalStateException ise) {
                             return null; // too early to have a cdi bean, skip these methods
- mainly init() but @PostConstruct works then
                         }
+
                         final BeanManager bm = webBeansContext.getBeanManagerImpl();
                         final Set<Bean<?>> beans = bm.getBeans(clazz);
                         final Bean<?> bean = bm.resolve(beans);
@@ -122,50 +132,84 @@ public class LazyRealm extends LifecycleBase implements Realm {
                     if (instance == null) {
                         throw new TomEERuntimeException("realm can't be retrieved from cdi");
                     }
+
                     if (instance instanceof Realm) {
                         delegate = (Realm) instance;
+                        delegate.setContainer(container);
+                        delegate.setCredentialHandler(credentialHandler);
+                        if (Lifecycle.class.isInstance(delegate)) {
+                            if (init) {
+                                try {
+                                    final Lifecycle lifecycle = Lifecycle.class.cast(delegate);
+                                    lifecycle.init();
+                                    if (start) {
+                                        lifecycle.start();
+                                    }
+                                } catch (final LifecycleException e) {
+                                    // no-op
+                                }
+                            }
+                        }
                     } else {
                         delegate = new LowTypedRealm(instance);
+                        delegate.setContainer(container);
+                        delegate.setCredentialHandler(credentialHandler);
+                    }
+                    for (final PropertyChangeListener listener : support.getPropertyChangeListeners())
{
+                        delegate.addPropertyChangeListener(listener);
                     }
-                    delegate.setContainer(container);
-                    delegate.setCredentialHandler(credentialHandler);
                 }
             }
         }
         return delegate;
     }
 
+    private Class<?> loadClass() {
+        if (container != null && container.getLoader() != null && container.getLoader().getClassLoader()
!= null) {
+            try {
+                return container.getLoader().getClassLoader().loadClass(realmClass);
+            } catch (final ClassNotFoundException e) {
+                // no-op
+            }
+        }
+        return null;
+    }
+
     @Override
     protected void initInternal() throws LifecycleException {
-        final Realm r = instance();
-        if (r != null && Lifecycle.class.isInstance(r)) {
-            Lifecycle.class.cast(r).init();
+        final Class<?> r = loadClass();
+        if (r != null && Lifecycle.class.isAssignableFrom(r) && instance()
!= null) {
+            Lifecycle.class.cast(delegate).init();
+        } else {
+            init = true;
         }
     }
 
     @Override
     protected void startInternal() throws LifecycleException {
-        final Realm r = instance();
-        if (r != null && Lifecycle.class.isInstance(r)) {
-            Lifecycle.class.cast(r).start();
+        final Class<?> r = loadClass();
+        if (r != null && Lifecycle.class.isAssignableFrom(r) && instance()
!= null) {
+            Lifecycle.class.cast(instance()).start();
+        } else {
+            start = true;
         }
         setState(LifecycleState.STARTING);
     }
 
     @Override
     protected void stopInternal() throws LifecycleException {
-        final Realm r = instance();
-        if (r != null && Lifecycle.class.isInstance(r)) {
-            Lifecycle.class.cast(r).stop();
+        final Class<?> r = loadClass();
+        if (r != null && Lifecycle.class.isAssignableFrom(r) && instance()
!= null) {
+            Lifecycle.class.cast(instance()).stop();
         }
         setState(LifecycleState.STOPPING);
     }
 
     @Override
     protected void destroyInternal() throws LifecycleException {
-        final Realm r = instance();
-        if (r != null && Lifecycle.class.isInstance(r)) {
-            Lifecycle.class.cast(r).destroy();
+        final Class<?> r = loadClass();
+        if (r != null && Lifecycle.class.isAssignableFrom(r) && instance()
!= null) {
+            Lifecycle.class.cast(instance()).destroy();
         }
     }
 
@@ -205,19 +249,18 @@ public class LazyRealm extends LifecycleBase implements Realm {
     @Override
     public void setCredentialHandler(final CredentialHandler credentialHandler) {
         this.credentialHandler = credentialHandler;
-        final Realm r = instance();
-        if (r != null) {
-            r.setCredentialHandler(credentialHandler);
+        final Class<?> r = loadClass();
+        if (r != null && instance() != null) {
+            delegate.setCredentialHandler(credentialHandler);
         }
     }
 
     @Override
     public void addPropertyChangeListener(final PropertyChangeListener listener) {
-        final Realm instance = instance();
-        if (instance == null) {
-            return;
+        if (delegate != null) {
+            delegate.addPropertyChangeListener(listener);
         }
-        instance.addPropertyChangeListener(listener);
+        support.addPropertyChangeListener(listener);
     }
 
     @Override
@@ -243,7 +286,9 @@ public class LazyRealm extends LifecycleBase implements Realm {
 
     @Override
     public void backgroundProcess() {
-        instance().backgroundProcess();
+        if (delegate != null) {
+            instance().backgroundProcess();
+        }
     }
 
     @Override
@@ -270,10 +315,9 @@ public class LazyRealm extends LifecycleBase implements Realm {
 
     @Override
     public void removePropertyChangeListener(final PropertyChangeListener listener) {
-        final Realm instance = instance();
-        if (instance == null) {
-            return;
+        if (delegate != null) {
+            delegate.removePropertyChangeListener(listener);
         }
-        instance.removePropertyChangeListener(listener);
+        support.removePropertyChangeListener(listener);
     }
 }


Mime
View raw message