package jfconfig;
/**
* Created : Mar 9, 2012
*
* @author pquiring
*/
import java.io.*;
import java.util.*;
import javax.swing.*;
import javaforce.*;
import javaforce.linux.*;
public class TomcatPanel extends javax.swing.JPanel {
/**
* Creates new form TomcatPanel
*/
public TomcatPanel() {
initComponents();
loadConfig();
ConfigApp.This.setTitle("Config : Tomcat");
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jToolBar1 = new javax.swing.JToolBar();
back = new javax.swing.JButton();
addservice = new javax.swing.JButton();
delservice = new javax.swing.JButton();
save = new javax.swing.JButton();
apply = new javax.swing.JButton();
restart = new javax.swing.JButton();
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel2 = new javax.swing.JPanel();
jScrollPane3 = new javax.swing.JScrollPane();
hosts = new javax.swing.JList();
jToolBar2 = new javax.swing.JToolBar();
addHost = new javax.swing.JButton();
delHost = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
domains = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
appPath = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
port = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
name = new javax.swing.JTextField();
enabled = new javax.swing.JCheckBox();
jLabel7 = new javax.swing.JLabel();
sslPort = new javax.swing.JTextField();
sslKeys = new javax.swing.JButton();
sslEnabled = new javax.swing.JCheckBox();
jScrollPane2 = new javax.swing.JScrollPane();
services = new javax.swing.JList();
jLabel2 = new javax.swing.JLabel();
jToolBar1.setFloatable(false);
jToolBar1.setRollover(true);
back.setText("<Back");
back.setFocusable(false);
back.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
back.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
back.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
backActionPerformed(evt);
}
});
jToolBar1.add(back);
addservice.setText("Add Service");
addservice.setFocusable(false);
addservice.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
addservice.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
addservice.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addserviceActionPerformed(evt);
}
});
jToolBar1.add(addservice);
delservice.setText("Delete Service");
delservice.setFocusable(false);
delservice.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
delservice.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
delservice.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delserviceActionPerformed(evt);
}
});
jToolBar1.add(delservice);
save.setText("Save");
save.setFocusable(false);
save.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
save.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveActionPerformed(evt);
}
});
jToolBar1.add(save);
apply.setText("Apply");
apply.setFocusable(false);
apply.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
apply.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
apply.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
applyActionPerformed(evt);
}
});
jToolBar1.add(apply);
restart.setText("Restart Server");
restart.setFocusable(false);
restart.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
restart.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
restart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
restartActionPerformed(evt);
}
});
jToolBar1.add(restart);
hosts.setModel(hostsModel);
hosts.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
hosts.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
hostsValueChanged(evt);
}
});
jScrollPane3.setViewportView(hosts);
jToolBar2.setFloatable(false);
jToolBar2.setRollover(true);
addHost.setText("Add Host");
addHost.setFocusable(false);
addHost.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
addHost.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
addHost.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addHostActionPerformed(evt);
}
});
jToolBar2.add(addHost);
delHost.setText("Delete Host");
delHost.setFocusable(false);
delHost.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
delHost.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
delHost.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delHostActionPerformed(evt);
}
});
jToolBar2.add(delHost);
jLabel3.setText("Domain");
jLabel4.setText("AppsPath");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(domains, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
.addComponent(appPath)))
.addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 667, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(domains, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(appPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
jTabbedPane1.addTab("Hosts", jPanel2);
port.setText("80");
jLabel1.setText("Port");
jLabel5.setText("Name");
name.setEditable(false);
name.setText("default");
enabled.setText("Enabled");
jLabel7.setText("SSL Port");
sslPort.setText("8443");
sslKeys.setText("Launch SSL Keys Manager...");
sslKeys.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sslKeysActionPerformed(evt);
}
});
sslEnabled.setText("Enable SSL");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(port))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(name, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sslPort, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sslEnabled)
.addComponent(enabled)
.addComponent(sslKeys))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(enabled)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sslEnabled)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(sslPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sslKeys)
.addContainerGap(568, Short.MAX_VALUE))
);
jTabbedPane1.addTab("General", jPanel1);
services.setModel(servicesModel);
services.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
services.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
servicesValueChanged(evt);
}
});
jScrollPane2.setViewportView(services);
jLabel2.setText("Services:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTabbedPane1))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2))))
);
}// </editor-fold>//GEN-END:initComponents
private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
ConfigApp.This.setPanel(new ServersPanel());
}//GEN-LAST:event_backActionPerformed
private void addserviceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addserviceActionPerformed
String name = JF.getString("Enter Service Name", "");
if (name == null) return;
addService(name);
}//GEN-LAST:event_addserviceActionPerformed
private void delserviceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delserviceActionPerformed
int idx = services.getSelectedIndex();
if (idx == -1) return;
delService(idx);
}//GEN-LAST:event_delserviceActionPerformed
private void applyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyActionPerformed
apply();
}//GEN-LAST:event_applyActionPerformed
private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
saveConfig();
}//GEN-LAST:event_saveActionPerformed
private void restartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restartActionPerformed
restart();
}//GEN-LAST:event_restartActionPerformed
private void servicesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_servicesValueChanged
int sidx = services.getSelectedIndex();
if (sidx == -1) return;
loadService(sidx);
}//GEN-LAST:event_servicesValueChanged
private void hostsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_hostsValueChanged
int sidx = services.getSelectedIndex();
if (sidx == -1) return;
int hidx = hosts.getSelectedIndex();
if (hidx == -1) return;
loadHost(hidx);
}//GEN-LAST:event_hostsValueChanged
private void addHostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addHostActionPerformed
String name = JF.getString("Enter Host Name", "");
if (name == null) return;
addHost(name);
}//GEN-LAST:event_addHostActionPerformed
private void delHostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delHostActionPerformed
int idx = hosts.getSelectedIndex();
if (idx == -1) return;
delHost(idx);
}//GEN-LAST:event_delHostActionPerformed
private void sslKeysActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sslKeysActionPerformed
int idx = services.getSelectedIndex();
if (idx == -1) return;
Service service = config.service[idx];
String folder = "/etc/jconfig.d/tomcat/" + service.name;
OpenSSLApp dialog = new OpenSSLApp(new String[] {folder, "tomcat"});
dialog.setVisible(true);
}//GEN-LAST:event_sslKeysActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton addHost;
private javax.swing.JButton addservice;
private javax.swing.JTextField appPath;
private javax.swing.JButton apply;
private javax.swing.JButton back;
private javax.swing.JButton delHost;
private javax.swing.JButton delservice;
private javax.swing.JTextField domains;
private javax.swing.JCheckBox enabled;
private javax.swing.JList hosts;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JToolBar jToolBar2;
private javax.swing.JTextField name;
private javax.swing.JTextField port;
private javax.swing.JButton restart;
private javax.swing.JButton save;
private javax.swing.JList services;
private javax.swing.JCheckBox sslEnabled;
private javax.swing.JButton sslKeys;
private javax.swing.JTextField sslPort;
// End of variables declaration//GEN-END:variables
private DefaultListModel servicesModel = new DefaultListModel();
private DefaultListModel hostsModel = new DefaultListModel();
private String configFolder = "/etc/jconfig.d/";
private String configFile = "tomcat.xml";
public static class Host {
public String name, domain, appPath;
}
public static class Service {
public String name;
public boolean enabled, sslEnabled;
public int port, sslPort;
public Host host[];
}
public static class Config {
public Service service[];
}
private Config config = new Config();
private int currentServiceIdx = -1;
private int currentHostIdx = -1;
private void loadConfig() {
try {
XML xml = new XML();
FileInputStream fis = new FileInputStream(configFolder + configFile);
xml.read(fis);
xml.writeClass(config);
} catch (FileNotFoundException e1) {
defaultConfig();
} catch (Exception e2) {
JFLog.log(e2);
defaultConfig();
}
updateConfig();
}
private void defaultConfig() {
//creates the init default config
Service service1 = new Service();
service1.name = "Catalina";
service1.enabled = true;
service1.port = 8080;
service1.sslPort = 8443;
service1.sslEnabled = false;
Host host1 = new Host();
host1.name = "default";
host1.domain = "localhost";
host1.appPath = "webapps";
config.service = new Service[1];
config.service[0] = service1;
service1.host = new Host[1];
service1.host[0] = host1;
}
private void updateConfig() {
servicesModel.clear();
int cnt = config.service.length;
for(int a=0;a<cnt;a++) {
servicesModel.addElement(config.service[a].name);
}
hostsModel.clear();
resetService();
}
private void resetService() {
enabled.setSelected(false);
name.setText("");
port.setText("");
sslEnabled.setSelected(false);
sslPort.setText("");
domains.setText("");
appPath.setText("");
}
private void loadService(int idx) {
saveService(currentServiceIdx);
Service service = config.service[idx];
enabled.setSelected(service.enabled);
name.setText(service.name);
port.setText("" + service.port);
sslEnabled.setSelected(service.sslEnabled);
sslPort.setText("" + service.sslPort);
currentServiceIdx = idx;
currentHostIdx = -1;
loadHosts();
}
private void loadHosts() {
hostsModel.clear();
Service service = config.service[currentServiceIdx];
for(int a=0;a<service.host.length;a++) {
hostsModel.addElement(service.host[a].name);
}
}
private void saveService(int idx) {
if (idx == -1) return;
saveHost(currentHostIdx);
Service service = config.service[idx];
service.name = name.getText();
service.enabled = enabled.isSelected();
service.port = JF.atoi(port.getText());
service.sslEnabled = sslEnabled.isSelected();
service.sslPort = JF.atoi(sslPort.getText());
}
private void addService(String name) {
name = name.trim().replaceAll("\"", "");
name = name.replaceAll(" ", "-");
if (name.length() == 0) return;
for(int a=0;a<config.service.length;a++) {
if (config.service[a].name.equalsIgnoreCase(name)) {
JF.showError("Error", "That name already exists");
return;
}
}
//copy default values
Service newService = new Service();
Service def = config.service[0];
newService.name = name;
newService.port = def.port;
newService.sslPort = def.sslPort;
config.service = Arrays.copyOf(config.service, config.service.length + 1);
config.service[config.service.length-1] = newService;
updateConfig();
}
private void delService(int idx) {
if (idx == 0) {JF.showError("Error", "Can not delete 'Default' service"); return;}
Service service = config.service[idx];
if (!JF.showConfirm("Confirm", "Are you sure you want to delete '" + service.name + "'?")) return;
int len = config.service.length;
Service newList[] = new Service[len-1];
for(int a=0;a<idx;a++) {
newList[a] = config.service[a];
}
for(int a=idx+1;a<len;a++) {
newList[a-1] = config.service[a];
}
config.service = newList;
updateConfig();
}
private void saveConfig() {
//save config to configFile
saveService(currentServiceIdx);
try {
XML xml = new XML();
File tmpFile = File.createTempFile("tomcat", ".xml");
FileOutputStream fos = new FileOutputStream(tmpFile);
xml.readClass("tomcat", config);
xml.write(fos);
fos.close();
Linux.mkdir(configFolder);
if (!Linux.copyFile(tmpFile.getAbsolutePath(), configFolder + configFile)) {
tmpFile.delete();
throw new Exception("file io error");
}
tmpFile.delete();
} catch (Exception e) {
JFLog.log(e);
}
}
private void loadHost(int idx) {
saveHost(currentHostIdx);
Host host = config.service[currentServiceIdx].host[idx];
domains.setText(host.domain);
appPath.setText(host.appPath);
currentHostIdx = idx;
}
private void saveHost(int idx) {
if (idx == -1) return;
Host host = config.service[currentServiceIdx].host[idx];
host.domain = domains.getText();
host.appPath = appPath.getText();
}
private void addHost(String name) {
name = name.trim().replaceAll("\"", "");
name = name.replaceAll(" ", "-");
if (name.length() == 0) return;
Service service = config.service[currentServiceIdx];
for(int a=0;a<service.host.length;a++) {
if (service.host[a].name.equalsIgnoreCase(name)) {
JF.showError("Error", "That name already exists");
return;
}
}
//set default values
Host newHost = new Host();
newHost.name = name;
newHost.appPath = "webapps";
newHost.domain = "localhost";
service.host = Arrays.copyOf(service.host, service.host.length + 1);
service.host[service.host.length-1] = newHost;
updateConfig();
}
private void delHost(int idx) {
if (idx == 0) {JF.showError("Error", "Can not delete 'Default' host"); return;}
Service service = config.service[currentServiceIdx];
Host host = service.host[idx];
if (!JF.showConfirm("Confirm", "Are you sure you want to delete '" + host.name + "'?")) return;
int len = service.host.length;
Host newList[] = new Host[len-1];
System.arraycopy(service.host, 0, newList, 0, idx);
System.arraycopy(service.host, idx+1, newList, idx, len - idx);
service.host = newList;
updateConfig();
}
// os.write("\n".getBytes());
private void openServer(OutputStream os) throws Exception {
os.write("<?xml version='1.0' encoding='utf-8'?>\n".getBytes());
os.write("<Server port='8005' shutdown='SHUTDOWN'>\n".getBytes());
// if (config.SecurityListener)
// os.write(" <Listener className='org.apache.catalina.security.SecurityListener' />".getBytes());
// if (config.AprLifecycleListener)
// os.write(" <Listener className='org.apache.catalina.core.AprLifecycleListener' SSLEngine='on' />\n".getBytes());
// if (config.JasperListener) //default == on
// os.write(" <Listener className='org.apache.catalina.core.JasperListener' />\n".getBytes());
os.write(" <Listener className='org.apache.catalina.core.JreMemoryLeakPreventionListener' />\n".getBytes());
os.write(" <Listener className='org.apache.catalina.mbeans.GlobalResourcesLifecycleListener' />\n".getBytes());
os.write(" <Listener className='org.apache.catalina.core.ThreadLocalLeakPreventionListener' />\n".getBytes());
os.write(" <GlobalNamingResources>\n".getBytes());
os.write(" <Resource name='UserDatabase' auth='Container' type='org.apache.catalina.UserDatabase' description='User database that can be updated and saved' factory='org.apache.catalina.users.MemoryUserDatabaseFactory' pathname='conf/tomcat-users.xml' />\n".getBytes());
os.write(" </GlobalNamingResources>\n".getBytes());
}
private void closeServer(OutputStream os) throws Exception {
os.write("</Server>\n".getBytes());
}
private void openService(Service service, OutputStream os) throws Exception {
os.write((" <Service name='" + service.name + "'>\n").getBytes());
// if (service.enableThreadPool) {
// os.write(" <Executor name='tomcatThreadPool' namePrefix='catalina-exec-' maxThreads='150' minSpareThreads='4'/>\n".getBytes());
// os.write((" <Connector executor='tomcatThreadPool' port='" + service.port + "' protocol='HTTP/1.1' connectionTimeout='20000' URIEncoding='UTF-8' redirectPort='" + service.sslPort + "' />\n").getBytes());
// if (service.sslEnabled) {
// os.write((" <Connector executor='tomcatThreadPool' port='" + service.sslPort + "' protocol='HTTP/1.1' SSLEnabled='true' maxThreads='150' scheme='https' secure='true' clientAuth='false' sslProtocol='TLS' keystoreFile='/etc/jconfig.d/tomcat/" + service.name + "/.keystore'/>\n").getBytes());
// }
// } else {
os.write((" <Connector port='" + service.port + "' protocol='HTTP/1.1' connectionTimeout='20000' URIEncoding='UTF-8' redirectPort='" + service.sslPort + "' />\n").getBytes());
if (service.sslEnabled) {
os.write((" <Connector port='" + service.sslPort + "' protocol='HTTP/1.1' SSLEnabled='true' maxThreads='150' scheme='https' secure='true' clientAuth='false' sslProtocol='TLS' keystoreFile='/etc/jconfig.d/tomcat/" + service.name + "/.keystore'/>\n").getBytes());
}
// }
// if (service.enableAJP) {
// os.write((" <Connector port='8009' protocol='AJP/1.3' redirectPort='" + service.sslPort + "' />\n").getBytes());
// }
os.write(" <Engine name='Catalina' defaultHost='localhost'>\n".getBytes());
os.write(" <Realm className='org.apache.catalina.realm.LockOutRealm'>\n".getBytes());
os.write(" <Realm className='org.apache.catalina.realm.UserDatabaseRealm' resourceName='UserDatabase'/>\n".getBytes());
os.write(" </Realm>\n".getBytes());
}
private void closeService(Service service, OutputStream os) throws Exception {
os.write(" </Engine>\n".getBytes());
os.write(" </Service>\n".getBytes());
}
private void applyHost(Host host, OutputStream os) throws Exception {
os.write((" <Host name='" + host.domain + "' appBase='" + host.appPath + "' unpackWARs='true' autoDeploy='true'>\n").getBytes());
os.write(" <Valve className='org.apache.catalina.valves.AccessLogValve' directory='logs' prefix='localhost_access_log.' suffix='.txt' pattern='%h %l %u %t "%r" %s %b' />\n".getBytes());
os.write(" </Host>\n".getBytes());
}
private void apply() {
//save each service to /etc/tomcat7/server.xml
try {
File tmpFile = File.createTempFile("tomcat", ".conf");
FileOutputStream fos = new FileOutputStream(tmpFile);
openServer(fos);
for(int a=0;a<config.service.length;a++) {
Service service = config.service[a];
if (!service.enabled) continue;
openService(service, fos);
for(int b=0;b<config.service.length;b++) {
Host host = service.host[b];
applyHost(host, fos);
}
closeService(service, fos);
}
closeServer(fos);
fos.close();
if (!Linux.copyFile(tmpFile.getAbsolutePath(), "/etc/tomcat7/server.xml")) {
tmpFile.delete();
throw new Exception("file io error");
}
tmpFile.delete();
JF.showMessage("Notice", "Please restart server to make changes effective.");
} catch (Exception e) {
JFLog.log(e);
}
}
private void restart() {
if (Linux.restartService("tomcat7"))
JF.showMessage("Notice", "Tomcat Service Restarted");
else
JF.showError("Error", "Failed to Restart Tomcat Service");
}
}