/*
* 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.servermgmt.pe.PEFileLayout;
import com.sun.enterprise.config.ConfigContext;
import com.sun.enterprise.config.ConfigException;
import com.sun.enterprise.config.serverbeans.ConvergedLbConfig;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.util.SystemPropertyConstants;
import org.jvnet.glassfish.comms.admin.clbadmin.reader.api.LoadbalancerReader;
import org.jvnet.glassfish.comms.admin.clbadmin.reader.impl.LoadbalancerReaderImpl;
import org.jvnet.glassfish.comms.admin.clbadmin.transform.LoadbalancerVisitor;
import org.jvnet.glassfish.comms.clb.admin.Loadbalancer;
import org.jvnet.glassfish.comms.clb.admin.ObjectFactory;
import org.jvnet.glassfish.comms.util.LogUtil;
import org.jvnet.glassfish.comms.admin.util.FileListFilter;
import java.io.File;
import java.io.FilenameFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sun.enterprise.util.i18n.StringManagerBase;
import java.util.Date;
/**
* Class to publish the converged-loadbalancer.xml to config directory.
*
* @author Vijaya Gadhamsetty
*/
public class ClbConfigPublisher {
private Loadbalancer _lb = null;
private String _name = null;
private String _lbName = null;
private ConfigContext _ctx = null;
private String _configName = null;
private static Logger _logger = LogUtil.SIP_LOGGER.getLogger();
private static final StringManagerBase _sMgr = StringManagerBase.getStringManager(_logger.getResourceBundleName());
private static final int DEFAULT_RETENTION_TIME = 60000;
private static int timeDiffProp;
/** Creates a new instance of ClbConfigPublisher */
public ClbConfigPublisher(ConfigContext ctx, String clbConfigName,
String lbName, String configName) throws IOException {
_name = clbConfigName;
_lbName = lbName;
_ctx = ctx;
_configName = configName;
String tmp = System.getProperty("clbreconfig.file_retention_time");
if (tmp == null)
timeDiffProp = DEFAULT_RETENTION_TIME;
else {
try {
timeDiffProp = Integer.parseInt(tmp) * 1000;
} catch (Exception e) {
timeDiffProp = DEFAULT_RETENTION_TIME;
}
}
}
public LoadbalancerReader getLbReader(ConfigContext ctx,
String clbConfigName) throws ConfigException {
// reads the load balancer related data
ConvergedLbConfig clbConfig = ((Domain) ctx.getRootConfigBean()).getConvergedLbConfigs()
.getConvergedLbConfigByName(clbConfigName);
return new LoadbalancerReaderImpl(ctx, clbConfig);
}
/**
* publishes the converged-loadbalancer.xml to config directory.
* @throws java.io.IOException
* @throws com.sun.enterprise.config.ConfigException
*/
public synchronized String publish(String fileName) throws IOException, ConfigException {
String iRoot = System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY);
String filePath = null;
String newConfigFileName = ClbAdminEventHelper.getConfigFileNewValue(fileName);
_logger.log(Level.FINE, "The New config-file name is " + newConfigFileName);
File f = new File(newConfigFileName);
if (!f.isAbsolute()) {
filePath = iRoot + File.separator + PEFileLayout.CONFIG_DIR + File.separator +
_configName + File.separator + newConfigFileName;
} else {
filePath = newConfigFileName;
}
// check if the lb exists
LoadbalancerReader lbr = getLbReader(_ctx, _name);
FileOutputStream out = null;
try {
out = new FileOutputStream(filePath);
// tranform the data using visitor pattern
Loadbalancer _lb = new Loadbalancer();
LoadbalancerVisitor lbVstr = new LoadbalancerVisitor(_lb);
lbr.accept(lbVstr);
ObjectFactory clbFactory = new ObjectFactory();
clbFactory.store(_lb, out);
out.flush();
if (!ClbAdminEventHelper.isClbDebug())
deleteClbCfgFiles(fileName);
} catch (Exception e) {
throw new IOException(e.getMessage());
} finally {
if (out != null) {
out.close();
out = null;
}
}
return newConfigFileName;
}
private void deleteClbCfgFiles(String fileName) {
String iRoot = System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY);
String filePath = iRoot + File.separator + PEFileLayout.CONFIG_DIR + File.separator + _configName + File.separator;
File myDir = new File(filePath);
long timeNow = (new Date()).getTime();
// Define a filter for clb xml files
if (fileName.indexOf(".v") == -1)
return;
fileName = fileName.substring(0,fileName.indexOf(".v"));
FilenameFilter select = new FileListFilter(fileName);
File[] contents = myDir.listFiles(select);
for (int i=0; i < contents.length; i++) {
long lastModified = contents[i].lastModified();
long timeDiff = timeNow - lastModified;
if (timeDiff > timeDiffProp)
contents[i].delete();
}
}
}