/**
 * Copyright 1999-2014 dangdang.com.
 *  
 * 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 com.dangdang.config.service.web.mb;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.dangdang.config.service.IAuthService;
import com.dangdang.config.service.IRootNodeRecorder;
import com.dangdang.config.service.observer.AbstractSubject;
import com.google.common.collect.Lists;

/**
 * 节点授权
 * 
 * @author <a href="mailto:wangyuxuan@dangdang.com">Yuxuan Wang</a>
 *
 */
@ManagedBean(name = "nodeAuthMB")
@SessionScoped
public class NodeAuthManagedBean extends AbstractSubject implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String nodeName;

	private String password;

	private String authedNode;

	@ManagedProperty(value = "#{authService}")
	private IAuthService authService;

	@ManagedProperty(value = "#{rootNodeRecorder}")
	private IRootNodeRecorder rootNodeRecorder;

	public void checkAuth() {
		boolean login = authService.checkAuth(nodeName, password);
		FacesContext context = FacesContext.getCurrentInstance();
		if (login) {
			context.addMessage(null, new FacesMessage("Login suc."));
			authedNode = nodeName;
			notify(authedNode, null);
			rootNodeRecorder.saveNode(authedNode);
		} else {
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login fail.", "Authentication not passed."));
		}
	}

	public List<String> complete(String prefix) {
		List<String> tips = Lists.newArrayList();
		List<String> nodes = rootNodeRecorder.listNode();
		if (nodes != null) {
			for (String node : nodes) {
				if (node.startsWith(prefix)) {
					tips.add(node);
				}
			}
		}
		return tips;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setAuthService(IAuthService authService) {
		this.authService = authService;
	}

	public final void setRootNodeRecorder(IRootNodeRecorder rootNodeRecorder) {
		this.rootNodeRecorder = rootNodeRecorder;
	}

	public String getNodeName() {
		return nodeName;
	}

	public String getPassword() {
		return password;
	}

	public String getAuthedNode() {
		return authedNode;
	}

	public void setAuthedNode(String authedNode) {
		this.authedNode = authedNode;
	}

}

@@ -1052,31 +1052,124 @@
 bserver.
-AbstractSubject
+IObserver;%0Aimport com.dangdang.config.service.observer.ISubject;%0Aimport com.google.common.base.Preconditions
 ;%0Aimport
@@ -1380,32 +1380,8 @@
 ean 
-extends AbstractSubject 
 impl
@@ -1399,16 +1399,26 @@
 alizable
+, ISubject
  %7B%0A%0A%09/**
@@ -3106,10 +3106,369 @@
 de;%0A%09%7D%0A%0A
+%09/**%0A%09 * %E8%A7%82%E5%AF%9F%E8%80%85%E5%88%97%E8%A1%A8%0A%09 */%0A%09private final List%3CIObserver%3E watchers = Lists.newArrayList();%0A%0A%09@Override%0A%09public void register(final IObserver watcher) %7B%0A%09%09watchers.add(Preconditions.checkNotNull(watcher));%0A%09%7D%0A%0A%09@Override%0A%09public void notify(final String key, final String value) %7B%0A%09%09for (final IObserver watcher : watchers) %7B%0A%09%09%09watcher.notified(key, value);%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

fix issue