You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by de...@apache.org on 2007/10/04 04:16:37 UTC
svn commit: r581760 - in /myfaces/core/branches/1_2_1/impl: ./
src/main/java/org/apache/myfaces/guice/
src/test/java/org/apache/myfaces/guice/
Author: dennisbyrne
Date: Wed Oct 3 19:16:36 2007
New Revision: 581760
URL: http://svn.apache.org/viewvc?rev=581760&view=rev
Log:
adding Guice DI support, w/ tests
Added:
myfaces/core/branches/1_2_1/impl/src/main/java/org/apache/myfaces/guice/
myfaces/core/branches/1_2_1/impl/src/main/java/org/apache/myfaces/guice/GuiceResolver.java
myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/
myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/BulkOrder.java
myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/GuiceResolverTestCase.java
myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/Order.java
myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/ShoppingCart.java
myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/ShoppingModule.java
Modified:
myfaces/core/branches/1_2_1/impl/pom.xml
Modified: myfaces/core/branches/1_2_1/impl/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/impl/pom.xml?rev=581760&r1=581759&r2=581760&view=diff
==============================================================================
--- myfaces/core/branches/1_2_1/impl/pom.xml (original)
+++ myfaces/core/branches/1_2_1/impl/pom.xml Wed Oct 3 19:16:36 2007
@@ -337,6 +337,12 @@
<artifactId>easymockclassextension</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>com.google.code.guice</groupId>
+ <artifactId>guice</artifactId>
+ <version>1.0</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
<reporting>
<plugins>
Added: myfaces/core/branches/1_2_1/impl/src/main/java/org/apache/myfaces/guice/GuiceResolver.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/impl/src/main/java/org/apache/myfaces/guice/GuiceResolver.java?rev=581760&view=auto
==============================================================================
--- myfaces/core/branches/1_2_1/impl/src/main/java/org/apache/myfaces/guice/GuiceResolver.java (added)
+++ myfaces/core/branches/1_2_1/impl/src/main/java/org/apache/myfaces/guice/GuiceResolver.java Wed Oct 3 19:16:36 2007
@@ -0,0 +1,113 @@
+/*
+ * 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.myfaces.guice;
+
+import javax.el.ELContext;
+import javax.el.ELException;
+import javax.el.PropertyNotFoundException;
+import javax.faces.FacesException;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+
+import org.apache.myfaces.config.element.ManagedBean;
+import org.apache.myfaces.el.unified.resolver.ManagedBeanResolver;
+
+import com.google.inject.Injector;
+
+/**
+ * <p>Register this ELResolver in faces-config.xml.</p>
+ *
+ * <application>
+ * <el-resolver>org.apache.guice.GuiceResolver</el-resolver>
+ * </application>
+ *
+ * <p>Implement and configure a ServletContextListener in web.xml .</p>
+ *
+ * <listener>
+ * <listener-class>com.your_company.GuiceServletContextListener</listener-class>
+ * </listener>
+ *
+ * <p>Configure Guice in your ServletContextListener implementation, and place the
+ * Injector in application scope.</p>
+ *
+ * public class GuiceServletContextListener implements ServletContextListener {
+ *
+ * public void contextInitialized(ServletContextEvent event) {
+ * ServletContext ctx = event.getServletContext();
+ * Injector injector = Guice.createInjector(new YourModule());
+ * ctx.setAttribute(GuiceResolver.KEY, injector);
+ * }
+ *
+ * public void contextDestroyed(ServletContextEvent event) {
+ * ServletContext ctx = event.getServletContext();
+ * ctx.removeAttribute(GuiceResolver.KEY);
+ * }
+ *
+ *}
+ *
+ * @author Dennis Byrne
+ */
+
+public class GuiceResolver extends ManagedBeanResolver {
+
+ public static final String KEY = "oam." + Injector.class.getName();
+
+ @Override
+ public Object getValue(ELContext ctx, Object base, Object property)
+ throws NullPointerException, PropertyNotFoundException, ELException {
+
+ if (base != null || !(property instanceof String))
+ return null;
+
+ if (property == null)
+ throw new PropertyNotFoundException();
+
+ FacesContext fctx = (FacesContext) ctx.getContext(FacesContext.class);
+
+ if(fctx == null)
+ return null;
+
+ ExternalContext ectx = fctx.getExternalContext();
+
+ if (ectx == null ||
+ ectx.getRequestMap().containsKey(property) ||
+ ectx.getSessionMap().containsKey(property) ||
+ ectx.getApplicationMap().containsKey(property) )
+ return null;
+
+ ManagedBean managedBean = runtimeConfig(ctx).getManagedBean((String)property);
+
+ return managedBean == null ? null : getValue(ctx, ectx, managedBean.getManagedBeanClass());
+ }
+
+ private Object getValue(ELContext ctx, ExternalContext ectx, Class managedBeanClass) {
+
+ Injector injector = (Injector) ectx.getApplicationMap().get(KEY);
+
+ if(injector == null)
+ throw new FacesException("Could not find an instance of " + Injector.class.getName()
+ + " in application scope using key '" + KEY + "'");
+
+ Object value = injector.getInstance(managedBeanClass);
+ ctx.setPropertyResolved(true);
+ return value;
+ }
+
+}
\ No newline at end of file
Added: myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/BulkOrder.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/BulkOrder.java?rev=581760&view=auto
==============================================================================
--- myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/BulkOrder.java (added)
+++ myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/BulkOrder.java Wed Oct 3 19:16:36 2007
@@ -0,0 +1,28 @@
+/*
+ * 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.myfaces.guice;
+
+public class BulkOrder implements Order {
+
+ public String toString() {
+ return "I am a Bulk Order";
+ }
+
+}
\ No newline at end of file
Added: myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/GuiceResolverTestCase.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/GuiceResolverTestCase.java?rev=581760&view=auto
==============================================================================
--- myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/GuiceResolverTestCase.java (added)
+++ myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/GuiceResolverTestCase.java Wed Oct 3 19:16:36 2007
@@ -0,0 +1,71 @@
+/*
+ * 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.myfaces.guice;
+
+import javax.el.ELResolver;
+import javax.faces.context.FacesContext;
+
+import org.apache.myfaces.config.RuntimeConfig;
+import org.apache.myfaces.config.impl.digester.elements.ManagedBean;
+import org.apache.shale.test.base.AbstractJsfTestCase;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+
+public class GuiceResolverTestCase extends AbstractJsfTestCase {
+
+ public GuiceResolverTestCase(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+
+ super.setUp();
+
+ // simulate a ServletContextListener
+ Injector injector = Guice.createInjector(new ShoppingModule());
+ servletContext.setAttribute(GuiceResolver.KEY, injector);
+
+ // simulate Myfaces starting up
+ RuntimeConfig rc = RuntimeConfig.getCurrentInstance(externalContext);
+ ManagedBean bean = new ManagedBean();
+ bean.setBeanClass(ShoppingCart.class.getName());
+ bean.setScope("request");
+ rc.addManagedBean("shoppingCart", bean);
+
+ }
+
+ public void testResolve() {
+
+ ELResolver resolver = new GuiceResolver();
+
+ ShoppingCart cart = (ShoppingCart) resolver.getValue(facesContext.getELContext(), ((Object)null), ((Object)"shoppingCart"));
+
+ assertNotNull(cart);
+
+ assertEquals(new BulkOrder().toString(), cart.getOrder().toString());
+
+ cart = (ShoppingCart) resolver.getValue(facesContext.getELContext(), ((Object)null), ((Object)"XXXshoppingCart"));
+
+ assertNull(cart);
+ }
+
+}
Added: myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/Order.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/Order.java?rev=581760&view=auto
==============================================================================
--- myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/Order.java (added)
+++ myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/Order.java Wed Oct 3 19:16:36 2007
@@ -0,0 +1,24 @@
+/*
+ * 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.myfaces.guice;
+
+public interface Order {
+
+}
Added: myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/ShoppingCart.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/ShoppingCart.java?rev=581760&view=auto
==============================================================================
--- myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/ShoppingCart.java (added)
+++ myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/ShoppingCart.java Wed Oct 3 19:16:36 2007
@@ -0,0 +1,37 @@
+/*
+ * 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.myfaces.guice;
+
+import com.google.inject.Inject;
+
+public class ShoppingCart {
+
+ private Order order;
+
+ @Inject
+ public ShoppingCart(Order order) {
+ this.order = order;
+ }
+
+ public Order getOrder() {
+ return order;
+ }
+
+}
Added: myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/ShoppingModule.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/ShoppingModule.java?rev=581760&view=auto
==============================================================================
--- myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/ShoppingModule.java (added)
+++ myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/guice/ShoppingModule.java Wed Oct 3 19:16:36 2007
@@ -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.myfaces.guice;
+
+import com.google.inject.Binder;
+import com.google.inject.Module;
+
+public class ShoppingModule implements Module {
+
+ public void configure(Binder binder) {
+
+ binder.bind(Order.class).to(BulkOrder.class);
+
+ }
+
+}
\ No newline at end of file