KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > webadmin > WebAdminService


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.webadmin;
20
21 import java.sql.Connection JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
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 JavaDoc
49     {
50         WebConfig config = null;
51         
52         Connection JavaDoc c = layer.getConnection();
53         PreparedStatement JavaDoc select = c.prepareStatement(
54             "SELECT * FROM WebConnector");
55                 
56         ResultSet JavaDoc r = select.executeQuery();
57         if(r.next())
58         {
59             String JavaDoc 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 JavaDoc httpsKeystorePassword = r.getString(6);
65             String JavaDoc 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 JavaDoc
78     {
79         Connection JavaDoc c = layer.getConnection();
80         PreparedStatement JavaDoc 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 JavaDoc e) {
118             try {
119                 e.printStackTrace();
120                 oc.write("FAILED " + e);
121             } catch(Exception JavaDoc 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