package org.apache.velocity.tools.generic;

/*
 * 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.
 */

import java.util.Map;

/**
 * Implements common logic and constants for tools which by default
 * lock down the public configure(Map) method, to keep the tool
 * thread-safe in templates and most other common uses.
 *
 * @author Nathan Bubna
 */
public abstract class AbstractLockConfig
{
    /**
     * The key used for specifying whether or not to prevent templates
     * from reconfiguring this tool.  The default is true.
     */
    public static final String LOCK_CONFIG_KEY = "lock-config";

    private boolean configLocked = false;

    public boolean isConfigLocked()
    {
        return this.configLocked;
    }

    /**
     * Looks for configuration values in the given params.
     */
    public void configure(Map params)
    {
        if (!isConfigLocked())
        {
            ValueParser values = new ValueParser(params);
            configure(values);

            // by default, lock down this method after use
            // to prevent templates from re-configuring this instance
            configLocked = values.getBoolean(LOCK_CONFIG_KEY, true);
        }
    }

    /**
     * Does the actual configuration. This is protected, so
     * subclasses may share the same ValueParser and call configure
     * at any time, while preventing templates from doing so when 
     * configure(Map) is locked.
     */
    protected abstract void configure(ValueParser values);

}

@@ -1339,60 +1339,296 @@
 lock
--config%22;%0A%0A    private boolean configLocked = false;
+Config%22;%0A    @Deprecated%0A    public static final String OLD_LOCK_CONFIG_KEY = %22lock-config%22;%0A%0A    private boolean configLocked = false;%0A%0A    /**%0A     * Only allow subclass access to this.%0A     */%0A    protected void setLockConfig(boolean lock)%0A    %7B%0A        this.configLocked = lock;%0A    %7D
 %0A%0A  
@@ -1958,16 +1958,223 @@
 lues);%0A%0A
+            // first check under the new key%0A            Boolean lock = values.getBoolean(LOCK_CONFIG_KEY);%0A            if (lock == null)%0A            %7B%0A                // now check the old key (for now)%0A    
         
@@ -2236,16 +2236,20 @@
         
+  
+  
 // to pr
@@ -2310,28 +2310,24 @@
         
-configL
+    l
 ock
-ed
  = value
@@ -2331,32 +2331,36 @@
 lues.getBoolean(
+OLD_
 LOCK_CONFIG_KEY,
@@ -2360,20 +2360,90 @@
 IG_KEY, 
-tr
+Boolean.TRUE);%0A            %7D%0A            configLocked = lock.booleanVal
 ue
+(
 );%0A     

update the lock config key and deprecate the old one, also allow subclasses to lock or unlock the config directly