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.settings;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.wicket.Application;
023import org.apache.wicket.Component;
024import org.apache.wicket.IComponentAwareEventSink;
025import org.apache.wicket.IDetachListener;
026import org.apache.wicket.IEventDispatcher;
027import org.apache.wicket.event.IEvent;
028import org.apache.wicket.event.IEventSink;
029import org.apache.wicket.serialize.ISerializer;
030import org.apache.wicket.serialize.java.JavaSerializer;
031import org.apache.wicket.util.lang.Args;
032import org.apache.wicket.util.string.Strings;
033
034/**
035 * Framework settings for retrieving and configuring framework settings.
036 *
037 * @author Jonathan Locke
038 * @author Chris Turner
039 * @author Eelco Hillenius
040 * @author Juergen Donnerstag
041 * @author Johan Compagner
042 * @author Igor Vaynberg (ivaynberg)
043 * @author Martijn Dashorst
044 * @author James Carman
045 */
046public class FrameworkSettings implements IEventDispatcher
047{
048        private IDetachListener detachListener;
049
050        private List<IEventDispatcher> eventDispatchers = null;
051
052        /**
053         * The {@link ISerializer} that will be used to convert the pages to/from byte arrays
054         */
055        private ISerializer serializer;
056
057        /**
058         * Construct.
059         * 
060         * @param application
061         */
062        public FrameworkSettings(final Application application)
063        {
064                serializer = new JavaSerializer(application.getApplicationKey());
065        }
066
067        /**
068         * Gets the Wicket version. The Wicket version is in the same format as the version element in
069         * the pom.xml file (project descriptor). The version is generated by maven in the build/release
070         * cycle and put in the /META-INF/MANIFEST.MF file located in the root folder of the Wicket jar.
071         *
072         * The version usually follows one of the following formats:
073         * <ul>
074         * <li>major.minor[.bug] for stable versions. 1.1, 1.2, 1.2.1 are examples</li>
075         * <li>major.minor-state for development versions. 1.2-beta2, 1.3-SNAPSHOT are examples</li>
076         * </ul>
077         *
078         * @return the Wicket version
079         */
080        public String getVersion()
081        {
082                String implVersion = null;
083                Package pkg = getClass().getPackage();
084                if (pkg != null)
085                {
086                        implVersion = pkg.getImplementationVersion();
087                }
088                return Strings.isEmpty(implVersion) ? "n/a" : implVersion;
089        }
090
091        /**
092         * @return detach listener or <code>null</code> if none
093         */
094        public IDetachListener getDetachListener()
095        {
096                return detachListener;
097        }
098
099        /**
100         * Sets detach listener
101         *
102         * @param detachListener
103         *            listener or <code>null</code> to remove
104         * @return {@code this} object for chaining
105         */
106        public FrameworkSettings setDetachListener(IDetachListener detachListener)
107        {
108                this.detachListener = detachListener;
109                return this;
110        }
111
112        /**
113         * Registers a new event dispatcher
114         *
115         * @param dispatcher
116         * @return {@code this} object for chaining
117         */
118        public FrameworkSettings add(IEventDispatcher dispatcher)
119        {
120                Args.notNull(dispatcher, "dispatcher");
121                if (eventDispatchers == null)
122                {
123                        eventDispatchers = new ArrayList<>();
124                }
125                if (!eventDispatchers.contains(dispatcher))
126                {
127                        eventDispatchers.add(dispatcher);
128                }
129                return this;
130        }
131
132        /**
133         * Dispatches event to registered dispatchers
134         * 
135         * @see IEventDispatcher#dispatchEvent(Object, IEvent, Component)
136         * 
137         * @param sink
138         * @param event
139         * @param component
140         */
141        @Override
142        public void dispatchEvent(Object sink, IEvent<?> event, Component component)
143        {
144                // direct delivery
145                if (component != null && sink instanceof IComponentAwareEventSink)
146                {
147                        ((IComponentAwareEventSink)sink).onEvent(component, event);
148                }
149                else if (sink instanceof IEventSink)
150                {
151                        ((IEventSink)sink).onEvent(event);
152                }
153
154                // additional dispatchers delivery
155                if (eventDispatchers == null)
156                {
157                        return;
158                }
159                for (IEventDispatcher dispatcher : eventDispatchers)
160                {
161                        dispatcher.dispatchEvent(sink, event, component);
162                }
163        }
164
165        /**
166         * Sets the {@link ISerializer} that will be used to convert objects to/from byte arrays
167         *
168         * @param serializer
169         *            the {@link ISerializer} to use
170         * @return {@code this} object for chaining
171         */
172        public FrameworkSettings setSerializer(ISerializer serializer)
173        {
174                this.serializer = Args.notNull(serializer, "serializer");
175                return this;
176        }
177
178        /**
179         * @return the {@link ISerializer} that will be used to convert objects to/from byte arrays
180         */
181        public ISerializer getSerializer()
182        {
183                return serializer;
184        }
185}