Skip to content

Commit

Permalink
port for server in propeteries file.
Browse files Browse the repository at this point in the history
  • Loading branch information
kostyansasha committed Feb 27, 2016
1 parent 2a87cf2 commit f97b310
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
9 changes: 9 additions & 0 deletions src/main/java/server/model/ConfigParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
*/
class ConfigParameters {
private boolean isGUI;
private int port;

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public boolean isGUI() {
return isGUI;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/server/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Model implements ModelActions {
private static UserIO USERIO;
private static boolean statusWork;
private static boolean isGui;
private static int port;
private HashMap<Long, User> list;

public Model() {
Expand Down Expand Up @@ -285,6 +286,7 @@ public boolean gracefulReload() {
try {
ConfigParameters conf = XmlMessageServer.loadProperties();
isGui = conf.isGUI();
port = conf.getPort();
return true;
} catch (SAXException e) {
LOG.error("read properties", e);
Expand All @@ -301,4 +303,12 @@ public boolean gracefulReload() {
public boolean isGUI() {
return isGui;
}

/**
* Method that return number of port.
* @return number of port.
*/
public static int getPort() {
return port;
}
}
48 changes: 37 additions & 11 deletions src/main/java/server/model/XmlMessageServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
* @version %I%, %G%
*/
public class XmlMessageServer extends XmlMessage {
private static Logger LOG = Logger.getLogger(XmlMessageServer.class);
private static final int DEFAULTPORT = 1506;
private static final String NAMEOFFILE = "MessengerConf.xml";
private static final String ROOTNAME = "Preference";
private static final String INCLUDLOG = "logger";
private static final String LEVELLOG = "levelLogger";
private static final String SERVERGUI = "serverGUI";
private static final String PORT = "Port";
private static Logger LOG = Logger.getLogger(XmlMessageServer.class);

/**
* Method for read properties.
Expand All @@ -37,11 +44,12 @@ protected synchronized static ConfigParameters loadProperties() throws SAXExcep

Document document = null;
try {
document = builder.parse(new File("MessengerConf.xml"));
document = builder.parse(new File(NAMEOFFILE));
} catch (IOException e) {
try {
ConfigParameters conf = new ConfigParameters();
conf.setGUI(false);
conf.setPort(DEFAULTPORT);

writeProperties(conf);
return conf;
Expand All @@ -51,15 +59,15 @@ protected synchronized static ConfigParameters loadProperties() throws SAXExcep
}
document.getDocumentElement().normalize();

NodeList nList = document.getElementsByTagName("logger");
NodeList nList = document.getElementsByTagName(INCLUDLOG);
Node node = nList.item(0);
String log = node.getTextContent();

if (log.equalsIgnoreCase("true")) {
nList = document.getElementsByTagName("levelLogger");
nList = document.getElementsByTagName(LEVELLOG);
node = nList.item(0);

String level = node.getTextContent();

if (!setLevelLog(level)) {
LOG.error("MessengerConf.xml in 'levelLogger' has mistake.");
}
Expand All @@ -73,7 +81,8 @@ protected synchronized static ConfigParameters loadProperties() throws SAXExcep

ConfigParameters conf = new ConfigParameters();

nList = document.getElementsByTagName("serverGUI");
// parameter of server GUI
nList = document.getElementsByTagName(SERVERGUI);
node = nList.item(0);
String parameter = node.getTextContent();

Expand All @@ -82,6 +91,18 @@ protected synchronized static ConfigParameters loadProperties() throws SAXExcep
} else {
LOG.error("MessengerConf.xml in 'serverGUI' has mistake, it must be 'true' or 'false'");
}

// read port.
nList = document.getElementsByTagName(PORT);
node = nList.item(0);
parameter = node.getTextContent();
try {
conf.setPort(Integer.parseInt(parameter));
} catch (Exception e) {
LOG.error("MessengerConf.xml in 'Port' has mistake, it must be number");
conf.setPort(DEFAULTPORT);
}

return conf;
}

Expand Down Expand Up @@ -120,28 +141,33 @@ private static boolean setLevelLog(String level) {
protected synchronized static void writeProperties(ConfigParameters conf) throws TransformerException, FileNotFoundException {
paramLangXML();
Document doc = builder.newDocument();
Element RootElement = doc.createElement("Preference");
Element RootElement = doc.createElement(ROOTNAME);

Element NameElementTitle = doc.createElement("logger");
Element NameElementTitle = doc.createElement(INCLUDLOG);
NameElementTitle.appendChild(doc.createTextNode("TRUE"));
RootElement.appendChild(NameElementTitle);

NameElementTitle = doc.createElement("levelLogger");
NameElementTitle = doc.createElement(LEVELLOG);
NameElementTitle.appendChild(doc.createTextNode(String.valueOf(LogManager.getRootLogger().getLevel())));
RootElement.appendChild(NameElementTitle);

//server's GUI
NameElementTitle = doc.createElement("serverGUI");
NameElementTitle = doc.createElement(SERVERGUI);
NameElementTitle.appendChild(doc.createTextNode(String.valueOf(conf.isGUI())));
RootElement.appendChild(NameElementTitle);

//server's port
NameElementTitle = doc.createElement(PORT);
NameElementTitle.appendChild(doc.createTextNode(String.valueOf(conf.getPort())));
RootElement.appendChild(NameElementTitle);

// add in XML
doc.appendChild(RootElement);
Transformer t= TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.METHOD, "xml");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream("MessengerConf.xml")));
t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(NAMEOFFILE)));
}

}

0 comments on commit f97b310

Please sign in to comment.