001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.cdi;
018
019import javax.enterprise.inject.spi.BeanManager;
020
021import org.apache.wicket.Application;
022import org.apache.wicket.MetaDataKey;
023import org.apache.wicket.request.cycle.RequestCycleListenerCollection;
024
025/**
026 * Configures CDI integration
027 * 
028 * @author igor
029 * 
030 */
031public class CdiConfiguration
032{
033        private static final MetaDataKey<CdiConfiguration> CDI_CONFIGURATION_KEY = new MetaDataKey<>()
034        {
035                private static final long serialVersionUID = 1L;
036        };
037
038        private IConversationPropagation propagation = ConversationPropagation.NONBOOKMARKABLE;
039
040        private BeanManager fallbackBeanManager;
041
042        /**
043         * Constructor
044         */
045        public CdiConfiguration()
046        {
047        }
048
049        public IConversationPropagation getPropagation()
050        {
051                return propagation;
052        }
053
054        public CdiConfiguration setPropagation(IConversationPropagation propagation)
055        {
056                this.propagation = propagation;
057                return this;
058        }
059
060        public BeanManager getFallbackBeanManager()
061        {
062                return fallbackBeanManager;
063        }
064
065        /**
066         * Sets a BeanManager that should be used if all strategies to lookup a
067         * BeanManager fail. This can be used in scenarios where you do not have
068         * JNDI available and do not want to bootstrap the CDI provider. It should
069         * be noted that the fallback BeanManager can only be used within the
070         * context of a Wicket application (ie. Application.get() should return the
071         * application that was configured with this CdiConfiguration).
072         * 
073         * @param fallbackBeanManager
074         * @return this instance
075         */
076        public CdiConfiguration setFallbackBeanManager(BeanManager fallbackBeanManager)
077        {
078                this.fallbackBeanManager = fallbackBeanManager;
079                return this;
080        }
081
082        /**
083         * Configures the specified application
084         * 
085         * @param application
086         */
087        public void configure(Application application)
088        {
089                if (application.getMetaData(CDI_CONFIGURATION_KEY) != null)
090                {
091                        throw new IllegalStateException("Cdi already configured for this application");
092                }
093                application.setMetaData(CDI_CONFIGURATION_KEY, this);
094
095                RequestCycleListenerCollection listeners = new RequestCycleListenerCollection();
096                application.getRequestCycleListeners().add(listeners);
097
098                // enable conversation propagation
099                if (getPropagation() != ConversationPropagation.NONE)
100                {
101                        listeners.add(new ConversationPropagator(application, getPropagation()));
102                        application.getComponentPreOnBeforeRenderListeners().add(
103                                        new AutoConversationManager(getPropagation()));
104                        application.getComponentPreOnBeforeRenderListeners().add(
105                                        new ConversationExpiryChecker());
106                }
107
108                // enable detach event
109                listeners.add(new DetachEventEmitter());
110
111                NonContextual.of(application).postConstruct(application);
112
113                // enable injection of various framework components
114                application.getSessionListeners().add(new SessionInjector());
115                application.getComponentInstantiationListeners().add(new ComponentInjector());
116                application.getBehaviorInstantiationListeners().add(new BehaviorInjector());
117
118                // enable cleanup
119                application.getApplicationListeners().add(new CdiShutdownCleaner());
120        }
121
122        public static CdiConfiguration get(Application application)
123        {
124                return application.getMetaData(CDI_CONFIGURATION_KEY);
125        }
126}