熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

用shell和java實現自動部署

2022-06-13   來源: Java核心技術 

  公司用到了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();

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26690.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.