/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.glassfish.comms.clb.admin;
import com.sun.enterprise.admin.event.AdminEventListenerException;
import java.util.List;
import org.jvnet.glassfish.comms.admin.event.extensions.clb.ClusterChangeEvent;
import org.jvnet.glassfish.comms.admin.event.extensions.clb.ClusterChangeEventListener;
import com.sun.enterprise.config.ConfigAdd;
import com.sun.enterprise.config.ConfigDelete;
import com.sun.enterprise.config.ConfigUpdate;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jvnet.glassfish.comms.gms.EventListener;
import org.jvnet.glassfish.comms.util.LogUtil;
/**
* This is implementation for ClusterChangeEventListener in
* sailfin/administration module
*
* @author kshitiz
*/
public class ClusterChangeEventListenerImpl implements
ClusterChangeEventListener{
private static Logger logger = LogUtil.CLB_LOGGER.getLogger();
public static final String LB_ENABLED_ATTRIBUTE="lb-enabled";
private EventListener handler;
private String clusterName;
private String pattern;
public ClusterChangeEventListenerImpl(String clusterName,
EventListener handler){
this.clusterName = clusterName;
this.handler = handler;
//Adding cluster name to pattern to validate event is meant
//for this cluster itself
pattern = "/domain/clusters/cluster[@name='" + clusterName + "']/server-ref[@ref='";
}
/**
* Handles instance create event
* @param clusterChangeEvent event containing the instance add details
* @throws com.sun.enterprise.admin.event.AdminEventListenerException
*/
public void handleCreate(ClusterChangeEvent clusterChangeEvent)
throws AdminEventListenerException {
List changeList = clusterChangeEvent.getConfigChangeList();
ConfigAdd configAdd = (ConfigAdd) changeList.get(0);
String xpath = configAdd.getXPath();
String instanceName = getInstanceNameFromXPath(xpath);
//TBD - if instance creation provides option to set lb-enabled
// then lb-enabled flag need to parsed and used instead
// till then using default value of false
handler.onAdd(clusterName, instanceName, false);
}
/**
* Handles instance delete event
* @param clusterChangeEvent event containing the instance delete details
* @throws com.sun.enterprise.admin.event.AdminEventListenerException
*/
public void handleDelete(ClusterChangeEvent clusterChangeEvent)
throws AdminEventListenerException {
List changeList = clusterChangeEvent.getConfigChangeList();
ConfigDelete configDelete = (ConfigDelete) changeList.get(0);
String xpath = configDelete.getXPath();
String instanceName = getInstanceNameFromXPath(xpath);
handler.onDelete(clusterName, instanceName);
}
/**
* Handles instance update event. This will only handle changes to
* lb-enabled attribute
* @param clusterChangeEvent event containing the lb-enabled change details
* @throws com.sun.enterprise.admin.event.AdminEventListenerException
*/
public void handleUpdate(ClusterChangeEvent clusterChangeEvent)
throws AdminEventListenerException {
Iterator<ConfigUpdate> changeListIter = clusterChangeEvent.
getConfigChangeList().iterator();
while (changeListIter.hasNext()) {
ConfigUpdate change = changeListIter.next();
String xpath= change.getXPath();
String instanceName = getInstanceNameFromXPath(xpath);
String newValue = change.getNewValue(LB_ENABLED_ATTRIBUTE);
String oldValue = change.getOldValue(LB_ENABLED_ATTRIBUTE);
if(newValue == null || oldValue == null){
logger.log(Level.WARNING, "clb.cluster_change_update_event_not_for_lb_enabled_attribute");
continue;
}
boolean lbEnabledNewValue = Boolean.parseBoolean(newValue);
boolean lbEnabledOldValue = Boolean.parseBoolean(oldValue);
if(lbEnabledNewValue && !lbEnabledOldValue)
handler.onEnable(clusterName, instanceName);
else if(!lbEnabledNewValue && lbEnabledOldValue)
handler.onDisable(clusterName, instanceName);
}
}
/**
* Parses the xpath provided to extract the instance name
* @param xpath xpath of the instance
* @return instance name
* @throws com.sun.enterprise.admin.event.AdminEventListenerException
*/
private String getInstanceNameFromXPath(String xpath)
throws AdminEventListenerException {
int index = xpath.indexOf(pattern);
if (index == -1){
throw new AdminEventListenerException("Malformed xpath \"" +
xpath + "\", either it does not contain instance name or "
+ "not meant for this cluster");
}
index += pattern.length();
int lastIndex=xpath.lastIndexOf("'");
if (lastIndex == -1 || lastIndex <= index){
throw new AdminEventListenerException("Malformed xpath \"" +
xpath + "\", does not contain instance name");
}
return xpath.substring(index, lastIndex);
}
}