/*
 * Copyright 2015 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.cloud.cluster.lock.support;

import java.util.ArrayList;

import org.springframework.cloud.cluster.lock.LockService;
import org.springframework.cloud.cluster.lock.LockServiceLocator;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.Assert;
import org.springframework.util.PathMatcher;

/**
 * Default implementation of a {@link LockServiceLocator} which
 * uses a set of {@link LockService}s where matching happens using
 * a simple {@link PathMatcher}.
 * 
 * @author Janne Valkealahti
 *
 */
public class DefaultLockServiceLocator implements LockServiceLocator {
	
	private final ArrayList<PathMapping> mappings = new ArrayList<PathMapping>();
	
	private final PathMatcher matcher = new AntPathMatcher();
	
	private final LockService fallback;
	
	/**
	 * Instantiates a new default lock service locator.
	 *
	 * @param fallback the primary lock service
	 */
	public DefaultLockServiceLocator(LockService fallback) {
		Assert.notNull(fallback, "Fallback lock service must be set");
		this.fallback = fallback;
	}

	@Override
	public LockService locate(String lockKey) {
		LockService match = match(lockKey);
		return match != null ? match : fallback;
	}
	
	/**
	 * Adds a mapping path for lock service.
	 *
	 * @param path the path
	 * @param lockService the lock service
	 */
	public void addMapping(String path, LockService lockService) {
		Assert.notNull(lockService, "Lock service must not be null");
		mappings.add(new PathMapping(path, lockService));
	}
	
	private LockService match(String path) {
		for (PathMapping m : mappings) {
			if (matcher.match(m.getPath(), path)) {
				return m.getLockService();
			}
		}
		return null;
	}
	
	private static class PathMapping {
		
		private String path;
		
		private LockService lockService;
		
		public PathMapping(String path, LockService lockService) {
			this.path = path;
			this.lockService = lockService;
		}
		
		public String getPath() {
			return path;
		}
		
		public LockService getLockService() {
			return lockService;
		}
		
	}

}

@@ -678,16 +678,61 @@
 va.util.
+List;%0Aimport java.util.concurrent.CopyOnWrite
 ArrayLis
@@ -734,16 +734,16 @@
 ayList;%0A
-
 %0Aimport 
@@ -1061,19 +1061,16 @@
 r%7D which
-%0A *
  uses a 
@@ -1075,16 +1075,19 @@
 a set of
+%0A *
  %7B@link 
@@ -1128,19 +1128,16 @@
 ns using
-%0A *
  a simpl
@@ -1270,17 +1270,16 @@
 cator %7B%0A
-%09
 %0A%09privat
@@ -1286,21 +1286,16 @@
 e final 
-Array
 List%3CPat
@@ -1319,16 +1319,27 @@
 s = new 
+CopyOnWrite
 ArrayLis
@@ -1352,25 +1352,24 @@
 Mapping%3E();%0A
-%09
 %0A%09private fi
@@ -1416,18 +1416,17 @@
 cher();%0A
-%09
 %0A
+
 %09private
@@ -1454,18 +1454,17 @@
 llback;%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -1862,18 +1862,17 @@
 ack;%0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -2163,26 +2163,25 @@
 rvice));%0A%09%7D%0A
-%09
 %0A
+
 %09private Loc
@@ -2346,18 +2346,17 @@
 ull;%0A%09%7D%0A
-%09
 %0A
+
 %09private
@@ -2383,19 +2383,17 @@
 pping %7B%0A
-%09%09
 %0A
+
 %09%09privat
@@ -2407,19 +2407,17 @@
 g path;%0A
-%09%09
 %0A
+
 %09%09privat
@@ -2443,19 +2443,17 @@
 ervice;%0A
-%09%09
 %0A
+
 %09%09public
@@ -2561,27 +2561,25 @@
 ervice;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09public Str
@@ -2614,19 +2614,17 @@
 th;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09public
@@ -2682,17 +2682,15 @@
 ce;%0A%09%09%7D%0A
-%09%09
 %0A%09%7D%0A%0A%7D%0A

Use concurrent safe List for path mappings