/*
* 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.admin.clbadmin;
import com.sun.enterprise.admin.server.core.AdminService;
import com.sun.enterprise.config.ConfigChange;
import com.sun.enterprise.config.ConfigContext;
import com.sun.enterprise.config.ConfigContextEvent;
import com.sun.enterprise.config.ConfigContextEventListener;
import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.Configs;
import com.sun.enterprise.config.serverbeans.ConvergedLoadBalancer;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.admin.AdminContext;
import com.sun.enterprise.config.ConfigUpdate;
import com.sun.enterprise.config.ConfigAdd;
import com.sun.enterprise.config.ConfigDelete;
import com.sun.enterprise.config.ConfigChangeFactory;
import com.sun.enterprise.admin.event.ElementChangeHelper;
import com.sun.enterprise.admin.event.AdminEvent;
import com.sun.enterprise.admin.event.AdminEventMulticaster;
import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
import com.sun.enterprise.config.serverbeans.ServerRef;
import org.jvnet.glassfish.comms.util.LogUtil;
import com.sun.enterprise.util.i18n.StringManagerBase;
import java.util.ArrayList;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.reflect.Method;
import com.sun.enterprise.admin.meta.XPathHelper;
/**
* This class listens to the element change event
*
* @author Vijaya Gadhamsetty
*/
public class ClbAdminEventListener implements ConfigContextEventListener {
private static Logger _logger = LogUtil.SIP_LOGGER.getLogger();
private static final StringManagerBase _sMgr = StringManagerBase.getStringManager(_logger.getResourceBundleName());
/**
* Default constructor.
*/
public ClbAdminEventListener() {
}
/**
* before config add, delete, set, update or flush. type is in ev
*/
public void postAccessNotification(ConfigContextEvent ev) {
}
/**
* after config add, delete, set, update or flush. type is in ev
*/
public void preChangeNotification(ConfigContextEvent ev) {
}
/**
* before config add, delete, set, update or flush. type is in ev
*/
public void preAccessNotification(ConfigContextEvent ev) {
}
/**
* after config add, delete, set, update or flush. type is in ev
* applies the changes to the applicable load balancers
*/
public void postChangeNotification(ConfigContextEvent ev) {
String choice = ev.getChoice();
if (choice == null) {
return;
}
if (!choice.equals("ADD") && !choice.equals("DELETE") &&
!choice.equals("UPDATE") && !choice.equals("SET")) {
return;
}
try {
ConfigContext ctx = AdminService.getAdminService().getAdminContext()
.getAdminConfigContext();
Configs configs = ((Domain) ctx.getRootConfigBean()).getConfigs();
// Check if the Event is caused by create/delete instance command
// Also in the case of port conflicts during create instance if it is a system property
boolean createInstance = false;
if ( (ev.getChoice().equals("ADD") || ev.getChoice().equals("DELETE"))
&& (ev.getName().equals("ServerRef") || ev.getName().equals("SystemProperty")) )
createInstance = true;
if (configs == null) {
return;
}
Config[] cfgs = configs.getConfig();
if (cfgs == null) {
return;
}
for (com.sun.enterprise.config.serverbeans.Config cfg : cfgs) {
if ((cfg == null) || (cfg.getAvailabilityService() == null) ||
(cfg.getAvailabilityService().getConvergedLoadBalancer() == null)) {
continue;
} else {
ConvergedLoadBalancer clb = cfg.getAvailabilityService()
.getConvergedLoadBalancer();
if (!clb.isAutoCommit()) {
continue;
}
String configName = cfg.getName();
ClbConfigPublisher clbp = new ClbConfigPublisher(ctx,
clb.getConvergedLbConfigName(), clb.getName(), configName);
ClbAdminEventHelper lbAdminEventHelper = new ClbAdminEventHelper(ctx, clbp, clb, configName);
if (lbAdminEventHelper.isApplyChangesRequired()) {
_logger.log(Level.FINE, "sip.clbadmin.changes_detected");
String configFileNewValue = clbp.publish(clb.getConfigFile());
// set the value of config-file in the converged-load-balancer element
MBeanServer mbs = AdminService.getAdminService().getAdminContext().getMBeanServer();
String[] types = new String[] {(new String[]{}).getClass().getName()};
_logger.log(Level.FINE,
_sMgr.getString("sip.clbadmin.set_configfile",configFileNewValue));
if (!createInstance) {
Object[] returnValues = (Object[])mbs.invoke(
new ObjectName("com.sun.appserv:name=dotted-name-get-set,type=dotted-name-support"),
"dottedNameSet",
new Object[] {new String[] {cfg.getName() + ".availability-service.converged-load-balancer.config-file=" + configFileNewValue}},
types);
}
else {
String configFileOldValue = clb.getConfigFile();
Object[] returnValues = (Object[])mbs.invoke(
new ObjectName("com.sun.appserv:name=dotted-name-get-set,type=dotted-name-support"),
"dottedNameSet",
new Object[] {new String[] {cfg.getName() + ".availability-service.converged-load-balancer.config-file=" + configFileNewValue}},
types);
AdminContext _adminContext = AdminService.getAdminService().getAdminContext();
String instanceName = _adminContext.getServerName();
String xpathCLB = "/domain/configs/config[@name='" + cfg.getName() + "']/availability-service/converged-load-balancer";
ConfigUpdate cfgUpdate = ConfigChangeFactory.createConfigUpdate(xpathCLB,
"config-file", configFileOldValue, configFileNewValue);
ArrayList cfgList = new ArrayList<ConfigChange>();
cfgList.add(cfgUpdate);
ElementChangeHelper elementHelper = new ElementChangeHelper();
AdminEvent[] elementChangeEvents = elementHelper.generateElementChangeEventsFromChangeList(instanceName, cfgList, ctx);
for(AdminEvent elementChangeEvent : elementChangeEvents) {
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "sip.administration.clb.mbean.event_sent",
elementChangeEvent.getEventInfo());
} else {
_logger.log(Level.INFO, "sip.administration.clb.mbean.send_event", elementChangeEvent.toString());
}
AdminEventMulticaster.multicastEvent((AdminEvent) elementChangeEvent);
}
// generate an event for create/delete instance to be used by DataCentricUtil
if (ev.getChoice().equals("ADD") || ev.getChoice().equals("DELETE")) {
Object value = ev.getObject();
java.lang.reflect.Method[] methods = value.getClass().getMethods();
String xpath = null;
for (Method m:methods) {
if (m.getName().equals("getXPath")) {
xpath = (String) m.invoke(value, (Object [])null);
break;
}
}
String clusterXPath = ServerXPathHelper.getParentXPath(xpath);
// XPath will be /domain/clusters/cluster[@name='{1}']/server-ref[@ref='{2}']
int beginIndex = clusterXPath.indexOf("[@name='")+7;
int endIndex = clusterXPath.indexOf("']");
String clusterName = clusterXPath.substring(beginIndex, endIndex);
cfgList = new ArrayList<ConfigChange>();
if (ev.getChoice().equals("ADD")) {
ConfigAdd cfgAdd = ConfigChangeFactory.createConfigAdd(ctx, xpath);
cfgList.add(cfgAdd);
} else if (ev.getChoice().equals("DELETE")) {
ConfigDelete cfgDelete = ConfigChangeFactory.createConfigDelete(xpath);
cfgList.add(cfgDelete);
}
elementHelper = new ElementChangeHelper();
elementChangeEvents = elementHelper.generateElementChangeEventsFromChangeList(clusterName, cfgList, ctx);
for(AdminEvent elementChangeEvent : elementChangeEvents) {
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "sip.administration.clb.mbean.event_sent",
elementChangeEvent.getEventInfo());
} else {
_logger.log(Level.INFO, "sip.administration.clb.mbean.send_event", elementChangeEvent.toString());
}
AdminEventMulticaster.multicastEvent((AdminEvent) elementChangeEvent);
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}