1 19 package org.lucane.applications.webadmin; 20 21 import java.sql.Connection ; 22 import java.sql.PreparedStatement ; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 26 import org.lucane.common.Message; 27 import org.lucane.common.net.ObjectConnection; 28 import org.lucane.server.Server; 29 import org.lucane.server.Service; 30 import org.lucane.server.ServiceManager; 31 import org.lucane.server.database.DatabaseAbstractionLayer; 32 33 34 public class WebAdminService extends Service 35 { 36 private DatabaseAbstractionLayer layer = null; 37 38 public WebAdminService() 39 { 40 } 41 42 public void init(Server parent) 43 { 44 layer = parent.getDBLayer(); 45 } 46 47 private WebConfig readConfig() 48 throws SQLException 49 { 50 WebConfig config = null; 51 52 Connection c = layer.getConnection(); 53 PreparedStatement select = c.prepareStatement( 54 "SELECT * FROM WebConnector"); 55 56 ResultSet r = select.executeQuery(); 57 if(r.next()) 58 { 59 String realmName = r.getString(1); 60 boolean httpEnabled = (r.getInt(2) == 1); 61 int httpPort = r.getInt(3); 62 boolean httpsEnabled = (r.getInt(4) == 1); 63 int httpsPort = r.getInt(5); 64 String httpsKeystorePassword = r.getString(6); 65 String httpsKeyPassword = r.getString(7); 66 config = new WebConfig(realmName, httpEnabled, httpPort, 67 httpsEnabled, httpsPort, httpsKeyPassword, httpsKeyPassword); 68 } 69 r.close(); 70 select.close(); 71 c.close(); 72 73 return config; 74 } 75 76 private void writeConfig(WebConfig config) 77 throws SQLException 78 { 79 Connection c = layer.getConnection(); 80 PreparedStatement update = c.prepareStatement( 81 "UPDATE WebConnector SET realmName=?, httpEnabled=?, httpPort=?, " + 82 "httpsEnabled=?, httpsPort=?, httpsKeystorePassword=?, httpsKeyPassword=?"); 83 84 update.setString(1, config.realmName); 85 update.setInt(2, config.httpEnabled ? 1 : 0); 86 update.setInt(3, config.httpPort); 87 update.setInt(4, config.httpsEnabled ? 1 : 0); 88 update.setInt(5, config.httpsPort); 89 update.setString(6, config.httpsKeystorePassword); 90 update.setString(7, config.httpsKeyPassword); 91 92 update.execute(); 93 update.close(); 94 c.close(); 95 } 96 97 public void process(ObjectConnection oc, Message message) 98 { 99 WebAdminAction action = (WebAdminAction)message.getData(); 100 101 try { 102 switch(action.getAction()) 103 { 104 case WebAdminAction.GET_CONFIG: 105 WebConfig config = readConfig(); 106 oc.write("OK"); 107 oc.write(config); 108 break; 109 110 case WebAdminAction.SET_CONFIG: 111 writeConfig(action.getConfig()); 112 oc.write("OK"); 113 reloadWebConnector(); 114 break; 115 } 116 } 117 catch(Exception e) { 118 try { 119 e.printStackTrace(); 120 oc.write("FAILED " + e); 121 } catch(Exception e2) {} 122 } 123 } 124 125 private void reloadWebConnector() 126 { 127 Service wc = ServiceManager.getInstance().getService("org.lucane.webconnector"); 128 wc.shutdown(); 129 wc.init(Server.getInstance()); 130 } 131 } 132 | Popular Tags |