Skip to content

Commit

Permalink
Merge pull request #3 from dprietob/dev
Browse files Browse the repository at this point in the history
Improvements for version 0.2.0
  • Loading branch information
dprietob authored Jul 18, 2021
2 parents 41c07aa + 46e2bde commit d8780ae
Show file tree
Hide file tree
Showing 37 changed files with 790 additions and 306 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ some Apache Server basics statistics.

## Installation

For Windows, Hoster uses `httpd -k restart` to restart Apache Server.
Obviously, in order to use Hoster, it's necessary have installed Apache Server
and PHP in your computer.

In Windows systems, Hoster uses `httpd -k restart` to restart Apache Server.
If this command fails, Hoster runs `httpd -k install` which register Apache
as a Service in the system. This can be avoided if `httpd.exe` is added to
`PATH` being accessible through console globally.

For Linux systems, Hoster uses `service apache2 status` and `service apache2 restart`
commands to manage Apache Server, so they need to be accessible through terminal.

To compile, it's necessary add `lib/flatlaf-1.3.jar` as library in your IDE or
code editor and configure `Java 1.8` as minimum JDK.

## Usage

A GIF is worth a thousand words:
Expand Down Expand Up @@ -47,6 +56,11 @@ $ git clone git@github.com:dprietob/hoster.git
$ cd hoster
```

## Dependencies

Hoster use [FlatLaf](https://github.com/JFormDesigner/FlatLaf) to GUI customization,
also some [FarmFresh icons](https://www.fatcow.com/free-icons).

## License

This project is licensed under the terms of the MIT license.
36 changes: 31 additions & 5 deletions src/com/hoster/Hoster.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@

import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatLightLaf;
import com.hoster.data.HList;
import com.hoster.data.Host;
import com.hoster.data.Properties;
import com.hoster.files.HostsFile;
import com.hoster.files.PropertiesFile;
import com.hoster.gui.HostFrame;
import com.hoster.files.VHostsFile;
import com.hoster.gui.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class Hoster
{
public static void main(String[] args)
{
EventQueue.invokeLater(() -> {
Properties properties = new Properties(PropertiesFile.load());
List<Host> hostsList = HostsFile.load(properties.getString("hosts_file"));
PropertiesFile propertiesFile = new PropertiesFile();
HostsFile hostsFile = new HostsFile();
VHostsFile vHostsFile = new VHostsFile();

Properties properties = new Properties(propertiesFile, hostsFile, vHostsFile);
HList hostsList = mergeHostLists(
hostsFile.load(properties.getString("hosts_file")),
vHostsFile.load(properties.getString("vhosts_file"))
);

setTheme(properties.getString("theme"));
HostFrame frame = new HostFrame(hostsList, properties);
MainFrame frame = new MainFrame(properties, hostsList);
frame.build();
});
}
Expand All @@ -38,4 +46,22 @@ private static void setTheme(String themeType)
JOptionPane.showMessageDialog(null, e.getMessage());
}
}

private static HList mergeHostLists(HList hosts, HList vhosts)
{
Host h;
for (Host vh : vhosts) {
h = hosts.getEquals(vh);
if (h != null) {
vh.setIp(h.getIp());
vh.setActive(h.isActive());
}
}
/*
* Adds the rest of the hosts defined in the hosts file that are
* not defined in the vhosts file.
*/
vhosts.addAll(hosts.diff(vhosts));
return vhosts;
}
}
11 changes: 11 additions & 0 deletions src/com/hoster/data/HList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hoster.data;

import java.util.Collection;
import java.util.List;

public interface HList extends List<Host>
{
Host getEquals(Host h);

Collection<Host> diff(Collection<Host> hl);
}
57 changes: 40 additions & 17 deletions src/com/hoster/data/Host.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class Host

private boolean active;
private String ip;
private String domain;
private String serverAdmin;
private String serverName;
private String documentRoot;
Expand All @@ -34,24 +33,26 @@ public void setActive(boolean active)
this.active = active;
}

public String getIp()
public HostStatus getStatus()
{
return ip;
}
if (!hasDomainData()) {
return HostStatus.NO_DOMAIN;

public void setIp(String ip)
{
this.ip = ip;
} else if (!hasVirtualHostData()) {
return HostStatus.NO_VHOST;
}

return HostStatus.ALL_OK;
}

public String getDomain()
public String getIp()
{
return domain;
return ip;
}

public void setDomain(String domain)
public void setIp(String ip)
{
this.domain = domain;
this.ip = ip;
}

public String getServerAdmin()
Expand Down Expand Up @@ -97,7 +98,7 @@ public void setServerAlias(String serverAlias)

public String getPort()
{
return port;
return (port != null) ? port : DEFAULT_PORT;
}

public void setPort(String port)
Expand Down Expand Up @@ -135,18 +136,40 @@ public void setDirectory(Directory directory)
this.directory = directory;
}

public boolean isValid()
private boolean hasDomainData()
{
return getIp() != null
&& !getIp().isEmpty()
&& getServerName() != null
&& !getServerName().isEmpty();
}

private boolean hasVirtualHostData()
{
return getDocumentRoot() != null
&& !getDocumentRoot().isEmpty()
&& getServerName() != null
&& !getServerName().isEmpty();
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
return false;
}

Host host = (Host) obj;
return serverName.equals(host.serverName);
}

public String parseToXML()
{
if (isValid()) {
String port = getPort() != null ? getPort() : DEFAULT_PORT;
if (hasVirtualHostData()) {
StringBuilder out = new StringBuilder();
Map<String, String> data = new LinkedHashMap<>();
Directory directory = getDirectory();
Expand All @@ -158,10 +181,10 @@ public String parseToXML()
data.put("ErrorLog", getErrorLog());
data.put("CustomLog", getCustomLog());

out.append("<VirtualHost *:").append(port).append(">").append("\n");
out.append("<VirtualHost *:").append(getPort()).append(">").append("\n");
insertTagsToXML(out, data);
out.append(directory.parseToXML(true));
out.append("</VirtualHost>").append("\n");
out.append("</VirtualHost>");

return out.toString();
}
Expand Down
30 changes: 30 additions & 0 deletions src/com/hoster/data/HostList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.hoster.data;

import java.util.ArrayList;
import java.util.Collection;

public class HostList extends ArrayList<Host> implements HList
{
@Override
public Host getEquals(Host h)
{
for (Host host : this) {
if (host.equals(h)) {
return host;
}
}
return null;
}

@Override
public Collection<Host> diff(Collection<Host> hl)
{
Collection<Host> c = new ArrayList<>();
for (Host h : this) {
if (!hl.contains(h)) {
c.add(h);
}
}
return c;
}
}
6 changes: 6 additions & 0 deletions src/com/hoster/data/HostStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.hoster.data;

public enum HostStatus
{
NO_DOMAIN, NO_VHOST, ALL_OK
}
32 changes: 29 additions & 3 deletions src/com/hoster/data/Properties.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
package com.hoster.data;

import com.hoster.files.HostsFile;
import com.hoster.files.PropertiesFile;
import com.hoster.files.VHostsFile;

import java.util.HashMap;
import java.util.Map;

public class Properties
{
private PropertiesFile propertiesFile;
private HostsFile hostsFile;
private VHostsFile vHostsFile;
private Map<String, Object> propertiesMap;
private Map<String, Object> properties;

public Properties(Map<String, Object> map)
public Properties(PropertiesFile pf, HostsFile hf, VHostsFile vf)
{
propertiesMap = map;
propertiesFile = pf;
hostsFile = hf;
vHostsFile = vf;
propertiesMap = pf.load();
properties = new HashMap<>();
parseToProperties();
}

public PropertiesFile getPropertiesFile()
{
return propertiesFile;
}

public HostsFile getHostsFile()
{
return hostsFile;
}

public VHostsFile getVHostsFile()
{
return vHostsFile;
}

public Map<String, Object> getPropertiesMap()
{
return propertiesMap;
Expand All @@ -33,7 +58,7 @@ public String getString(String key)

public boolean getBoolean(String key)
{
return (boolean)properties.get(key);
return (boolean) properties.get(key);
}

public Directory getMainDirectory()
Expand All @@ -52,6 +77,7 @@ private void parseToProperties()
properties.put("theme", propertiesMap.get("theme"));
properties.put("hosts_file", propertiesMap.get("hosts_file"));
properties.put("vhosts_file", propertiesMap.get("vhosts_file"));
properties.put("console_log", propertiesMap.get("console_log").equals("1"));
properties.put("apache_path", propertiesMap.get("apache_path"));
properties.put("restart_server", propertiesMap.get("restart_server").equals("1"));
properties.put("main_directory", mainDirectory);
Expand Down
Loading

0 comments on commit d8780ae

Please sign in to comment.