KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > server > AdminXmlRpcHandler


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.server;
26
27 import org.mortbay.jetty.servlet.WebApplicationContext;
28 import org.snipsnap.config.Globals;
29 import org.snipsnap.config.ServerConfiguration;
30 import org.snipsnap.xmlrpc.AuthXmlRpcHandler;
31
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.net.InetAddress JavaDoc;
37 import java.net.UnknownHostException JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Properties JavaDoc;
41 import java.util.jar.JarEntry JavaDoc;
42 import java.util.jar.JarOutputStream JavaDoc;
43 import java.util.jar.Manifest JavaDoc;
44 import java.util.prefs.Preferences JavaDoc;
45
46 public class AdminXmlRpcHandler extends AuthXmlRpcHandler {
47   public AdminXmlRpcHandler() {
48     super();
49   }
50
51   protected boolean authenticate(String JavaDoc user, String JavaDoc password) {
52     Preferences JavaDoc serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class);
53     String JavaDoc adminPassword = (String JavaDoc) serverPrefs.get(ServerConfiguration.ADMIN_PASS, null);
54     return null != adminPassword && adminPassword.equals(password);
55   }
56
57   public Hashtable JavaDoc getApplications() {
58     Hashtable JavaDoc appList = new Hashtable JavaDoc();
59     Iterator JavaDoc appIt = ApplicationLoader.applications.keySet().iterator();
60     while (appIt.hasNext()) {
61       String JavaDoc appName = (String JavaDoc) appIt.next();
62       WebApplicationContext context = (WebApplicationContext) ApplicationLoader.applications.get(appName);
63       String JavaDoc[] hosts = context.getHosts();
64       if (hosts == null) {
65         hosts = new String JavaDoc[1];
66         try {
67           hosts[0] = InetAddress.getLocalHost().getHostName();
68         } catch (UnknownHostException JavaDoc e) {
69           hosts[0] = "localhost";
70         }
71       }
72       int port = context.getHttpServer().getListeners()[0].getPort();
73       String JavaDoc url = "http://" + hosts[0] + (port != 80 ? ":" + port : "") + context.getContextPath();
74       appList.put(appName, url);
75     }
76     return appList;
77   }
78
79   public String JavaDoc shutdown() {
80     System.out.println("INFO: received remote shutdown request (waiting 1s) ...");
81     new Thread JavaDoc() {
82       public synchronized void run() {
83         System.err.println("AdminXmlRpcHandler: shutdown waiting for 1s ...");
84         try {
85           Thread.sleep(1000);
86         } catch (InterruptedException JavaDoc e) {
87           System.err.println("AdminXmlRpcHandler: shutdown delay cancelled");
88         }
89         System.exit(0);
90       }
91     }.start();
92     return "SnipSnap Server is shutting down ...";
93   }
94
95   public String JavaDoc install(String JavaDoc name, String JavaDoc host, String JavaDoc port, String JavaDoc path) throws Exception JavaDoc {
96     //System.err.println("AdminXmlRpcHandler: install("+name+","+port+","+path+")");
97
Preferences JavaDoc serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class);
98     File JavaDoc root = new File JavaDoc(serverPrefs.get(ServerConfiguration.WEBAPP_ROOT, System.getProperty("user.home")));
99     File JavaDoc webAppDir = new File JavaDoc(root, name + "/webapp");
100     File JavaDoc webInf = new File JavaDoc(webAppDir, "WEB-INF");
101     webInf.mkdirs();
102
103     File JavaDoc applicationConf = new File JavaDoc(webInf, "application.conf");
104     if (!applicationConf.exists()) {
105       Properties JavaDoc installConfig = new Properties JavaDoc();
106       installConfig.setProperty(Globals.APP_HOST, host);
107       installConfig.setProperty(Globals.APP_PORT, port);
108       installConfig.setProperty(Globals.APP_PATH, path);
109       try {
110         installConfig.store(new FileOutputStream JavaDoc(applicationConf), " Bootstrap Configuration");
111         ApplicationLoader.loadApplication(root.getPath(), name);
112         installConfig.load(new FileInputStream JavaDoc(applicationConf));
113         return ApplicationLoader.getUrl(installConfig) + "?key=" + installConfig.getProperty(Globals.APP_INSTALL_KEY);
114       } catch (Exception JavaDoc e) {
115         applicationConf.delete();
116         e.printStackTrace();
117         throw e;
118       }
119     } else {
120       throw new Exception JavaDoc("'" + applicationConf.getPath() + "' exists, delete application first");
121     }
122   }
123
124   /**
125    * Remove a web application from the server. Handle with care, because this deletes
126    * all the data you might have stored in that web application including snips and
127    * attachments.
128    *
129    * @param name
130    * @return
131    */

132   public Boolean JavaDoc delete(String JavaDoc name, Boolean JavaDoc backup) throws Exception JavaDoc {
133     //System.err.println("AdminXmlRpcHandler: delete(" + name+")");
134
Preferences JavaDoc serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class);
135     File JavaDoc root = new File JavaDoc(serverPrefs.get(ServerConfiguration.WEBAPP_ROOT, System.getProperty("user.home")));
136     File JavaDoc app = new File JavaDoc(root, name);
137     if (app.exists()) {
138       try {
139         ApplicationLoader.unloadApplication(root.getPath(), name);
140       } catch (Exception JavaDoc e) {
141         System.err.println("AdminXmlRpcHandler: unload failed: " + e);
142       }
143       if (backup.booleanValue()) {
144         createBackupJar(name + ".backup.jar", app);
145       }
146       return new Boolean JavaDoc(app.delete());
147     }
148     return Boolean.TRUE;
149   }
150
151   private void createBackupJar(String JavaDoc jarName, File JavaDoc file) throws IOException JavaDoc {
152     JarOutputStream JavaDoc jar = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(jarName),
153                                               new Manifest JavaDoc());
154     System.err.println("Jar: created '" + jarName + "'");
155     try {
156       addToJarFile(jar, file);
157     } finally {
158       jar.close();
159     }
160   }
161
162   private void addToJarFile(JarOutputStream JavaDoc jar, File JavaDoc file) throws IOException JavaDoc {
163     JarEntry JavaDoc entry = new JarEntry JavaDoc(file.getPath());
164     jar.putNextEntry(entry);
165     if (file.isDirectory()) {
166       File JavaDoc[] fileList = file.listFiles();
167       for (int fileNo = 0; fileNo < fileList.length; fileNo++) {
168         addToJarFile(jar, fileList[fileNo]);
169       }
170     } else {
171       FileInputStream JavaDoc fileStream = new FileInputStream JavaDoc(file);
172       try {
173         byte buffer[] = new byte[4096];
174         int bytesRead;
175         while ((bytesRead = fileStream.read(buffer)) != -1) {
176           jar.write(buffer, 0, bytesRead);
177         }
178         System.err.println("Jar: added '" + file.getPath() + "'");
179       } catch (IOException JavaDoc e) {
180         System.err.println("Jar: error adding '" + file.getPath() + "': " + e);
181       } finally {
182         fileStream.close();
183       }
184     }
185   }
186 }
187
Popular Tags