公司用到了rackspace的cloudserver 做为压力测试的服务器但是一旦启动就开始收费一个小时刀因此要求用的时候打开不用的时候关掉
但是遇到一个问题就是这样的话在上边部署应用程序和软件就比较麻烦了不能每次都重新部署吧那样效率太低了因此想到写一些脚本进行自动的部署其他的都好办但是有些需要修改xml文件和properties文件的用shell实现起来就费力了
既然是java的应用就用java来实现吧用了一下午写好了下边我把修改properties和xml的代码贴出来希望对大家有所帮助
修改properties文件
package utilxml;
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioIOException;
import javautilList;
import javautilProperties;
import javautilDate;
import javatextSimpleDateFormat;
import javautilArrayList;
import javautilList;
public class ModifyPropertiesFile{
public String propertyFilePath=remoting_clientproperties;
public String stagingIp=;
public String messageIp=;
public void modifyPropertiesFile()
{
try {
Properties p = new Properties();
File f = new File(propertyFilePath);
List<String> list = new ArrayList<String>();
listadd(stagingIp);
listadd(messageIp);
if(fexists() && fisFile())
{
FileInputStream fis;
fis = new FileInputStream(f);
pload(fis);
psetProperty(serviceonlineurlsocket://+listget()+:);
psetProperty(ssageurlsocket://+listget()+:);
psetProperty(serviceaccounturlsocket://+listget()+:);
psetProperty(serviceorderurlsocket://+listget()+:);
psetProperty(servicepartyurlsocket://+listget()+:);
psetProperty(serviceproducturlsocket://+listget()+:);
psetProperty(servicewebimgatewayurlsocket://+listget()+:);
psetProperty(servicedirtywordurlsocket://+listget()+:);
FileOutputStream oFile = new FileOutputStream(f);
pstore(oFile);
fisclose();
oFileclose();
}
}
catch (Exception e)
{
eprintStackTrace();
egetMessage();
Systemoutprintln(Cant modify this properties fileplease check your program again!!!!);
}
}
public static void main(String args[])
{
ModifyPropertiesFile t=new ModifyPropertiesFile();
tmodifyPropertiesFile();
}
}
修改xml文件
package utilxml;
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioIOException;
import javautilList;
import javautilProperties;
import javautilDate;
import javatextSimpleDateFormat;
import orgjdomDocument;
import orgjdomElement;
import orgjdomJDOMException;
import orgjdominputSAXBuilder;
import orgjdomoutputXMLOutputter;
import orgjdomoutputFormat;
import orgjdomxpathXPath;
import orgapachelogjLogger;
import orgxmlsaxEntityResolver;
import orgxmlsaxInputSource;
import orgxmlsaxSAXException;
import javautilHashMap;
import javautilIterator;
public class ModifyHibernateXml{
private String dataBaseInfo;
private String userName;
private String userPass;
private String deploymentTitle;
private String cpmax_size;
private String fileName;
//defind propertis file name
public String propertyFilePath=ModifyXmlproperties;
public void modifyXml()
{
try
{
Properties p = new Properties();
HashMap<StringString> map=new HashMap<StringString>();
mapput(xmlFileNamepropertyFilePath);
File f = new File(mapget(xmlFileName));
// load properties value from properties file
if(fexists() && fisFile())
{
FileInputStream fis;
fis = new FileInputStream(f);
pload(fis);
dataBaseInfo =pgetProperty(nnectionurl);
userName =pgetProperty(nnectionusername);
userPass =pgetProperty(nnectionpassword);
deploymentTitle=pgetProperty(deploymenttitle);
cpmax_size =pgetProperty(hibernatecpmax_size);
fileName =pgetProperty(nfigrtionpath);
fisclose();
}
SAXBuilder builder = new SAXBuilder();
Document doc = builderbuild(fileName);
Element root = docgetRootElement();
XMLOutputter xmlOutputer = new XMLOutputter();
buildersetValidation(false);
buildersetEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId
String systemId) throws SAXException IOException {
return new InputSource(/hibernateconfigurationdt
d);
}
});
// use XPath search your need node
List list = XPathselectNodes(root /hibernateconfiguration/sessionfactory/property);
for(int i = ;i<listsize();i++)
{
Element disk_element = (Element) listget(i);
String name= disk_elementgetAttributue(name);
if (nameequals(connectionurl))
{
Systemoutprintln(database connection info+ +name);
disk_elementsetText(dataBaseInfo);
}
else if (nameequals(connectionusername))
{
Systemoutprintln(database connection username+ +name);
disk_elementsetText(userName);
}
else if (nameequals(connectionpassword))
{
Systemoutprintln(database connection password+ +name);
disk_elementsetText(userPass);
}
else if (nameequals(cpmax_size))
{
Systemoutprintln(database max connnection is +cpmax_size);
disk_elementsetText(cpmax_size);
}
}
// If you want to take out blank line you can uncomment format commect
// Format format = FormatgetPrettyFormat();
// formatsetEncoding(UTF);
// xmlOutputersetFormat(format);
xmlOutputeroutput(doc new FileOutputStream(fileName));
}catch (Exception e)
{
eprintStackTrace();
egetMessage();
Systemoutprintln(Cant modify xml fileplease check your program again!!!!);
}
}
public static void main(String[] args)
{
ModifyHibernateXml hibernate =new ModifyHibernateXml();
hibernatemodifyXml();
}
}
最后可以写一个总的类来调用以上两个类实现模块话厉害吧就几个文件还来了一个模块化
package utilxml;
public class CloudServer
{
public static void main(String args[])
{
ModifyPropertiesFile t=new ModifyPropertiesFile();
tmodifyPropertiesFile();
ModifyHibernateXml hibernate =new ModifyHibernateXml();
hibernatemodifyXml();
}
}