/*
 * Copyright 2012-2013 the original author or authors.
 *
 * Licensed 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.springframework.boot.context.embedded;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Registration;

import org.springframework.core.Conventions;
import org.springframework.util.Assert;

/**
 * Base class for Servlet 3.0+ based registration beans.
 * 
 * @author Phillip Webb
 * @see ServletRegistrationBean
 * @see FilterRegistrationBean
 * @see ServletListenerRegistrationBean
 */
public abstract class RegistrationBean implements ServletContextInitializer {

	private String name;

	private boolean asyncSupported = true;

	private Map<String, String> initParameters = new LinkedHashMap<String, String>();

	/**
	 * Set the name of this registration. If not specified the bean name will be used.
	 */
	public void setName(String name) {
		Assert.hasLength(name, "Name must not be empty");
		this.name = name;
	}

	/**
	 * Sets if asynchronous operations are support for this registration. If not specified
	 * defaults to {@code true}.
	 */
	public void setAsyncSupported(boolean asyncSupported) {
		this.asyncSupported = asyncSupported;
	}

	/**
	 * Returns if asynchronous operations are support for this registration.
	 */
	public boolean isAsyncSupported() {
		return this.asyncSupported;
	}

	/**
	 * Set init-parameters for this registration. Calling this method will replace any
	 * existing init-parameters.
	 * @see #getInitParameters
	 * @see #addInitParameter
	 */
	public void setInitParameters(Map<String, String> initParameters) {
		Assert.notNull(initParameters, "InitParameters must not be null");
		this.initParameters = new LinkedHashMap<String, String>(initParameters);
	}

	/**
	 * Returns a mutable Map of the registration init-parameters.
	 */
	public Map<String, String> getInitParameters() {
		return this.initParameters;
	}

	/**
	 * Add a single init-parameter, replacing any existing parameter with the same name.
	 * @param name the init-parameter name
	 * @param value the init-parameter value
	 */
	public void addInitParameter(String name, String value) {
		Assert.notNull(name, "Name must not be null");
		this.initParameters.put(name, value);
	}

	/**
	 * Deduces the name for this registration. Will return user specified name or fallback
	 * to convention based naming.
	 * @param value the object used for convention based names
	 */
	protected final String getOrDeduceName(Object value) {
		return (this.name != null ? this.name : Conventions.getVariableName(value));
	}

	/**
	 * Configure registration base settings.
	 */
	protected void configure(Registration.Dynamic registration) {
		Assert.state(registration != null,
				"Registration is null. Was something already registered for name=["
						+ this.name + "]?");
		registration.setAsyncSupported(this.asyncSupported);
		if (this.initParameters.size() > 0) {
			registration.setInitParameters(this.initParameters);
		}
	}

}

@@ -798,16 +798,57 @@
 ntions;%0A
+import org.springframework.core.Ordered;%0A
 import o
@@ -1151,16 +1151,25 @@
 tializer
+, Ordered
  %7B%0A%0A%09pri
@@ -1183,24 +1183,73 @@
 ring name;%0A%0A
+%09private int order = Ordered.LOWEST_PRECEDENCE;%0A%0A
 %09private boo
@@ -3573,10 +3573,208 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09/**%0A%09 * @param order the order to set%0A%09 */%0A%09public void setOrder(int order) %7B%0A%09%09this.order = order;%0A%09%7D%0A%0A%09/**%0A%09 * @return the order%0A%09 */%0A%09@Override%0A%09public int getOrder() %7B%0A%09%09return this.order;%0A%09%7D%0A%0A
 %7D%0A

Add Ordered to RegistrationBean