1 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 ; 33 import java.io.FileInputStream ; 34 import java.io.FileOutputStream ; 35 import java.io.IOException ; 36 import java.net.InetAddress ; 37 import java.net.UnknownHostException ; 38 import java.util.Hashtable ; 39 import java.util.Iterator ; 40 import java.util.Properties ; 41 import java.util.jar.JarEntry ; 42 import java.util.jar.JarOutputStream ; 43 import java.util.jar.Manifest ; 44 import java.util.prefs.Preferences ; 45 46 public class AdminXmlRpcHandler extends AuthXmlRpcHandler { 47 public AdminXmlRpcHandler() { 48 super(); 49 } 50 51 protected boolean authenticate(String user, String password) { 52 Preferences serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class); 53 String adminPassword = (String ) serverPrefs.get(ServerConfiguration.ADMIN_PASS, null); 54 return null != adminPassword && adminPassword.equals(password); 55 } 56 57 public Hashtable getApplications() { 58 Hashtable appList = new Hashtable (); 59 Iterator appIt = ApplicationLoader.applications.keySet().iterator(); 60 while (appIt.hasNext()) { 61 String appName = (String ) appIt.next(); 62 WebApplicationContext context = (WebApplicationContext) ApplicationLoader.applications.get(appName); 63 String [] hosts = context.getHosts(); 64 if (hosts == null) { 65 hosts = new String [1]; 66 try { 67 hosts[0] = InetAddress.getLocalHost().getHostName(); 68 } catch (UnknownHostException e) { 69 hosts[0] = "localhost"; 70 } 71 } 72 int port = context.getHttpServer().getListeners()[0].getPort(); 73 String url = "http://" + hosts[0] + (port != 80 ? ":" + port : "") + context.getContextPath(); 74 appList.put(appName, url); 75 } 76 return appList; 77 } 78 79 public String shutdown() { 80 System.out.println("INFO: received remote shutdown request (waiting 1s) ..."); 81 new Thread () { 82 public synchronized void run() { 83 System.err.println("AdminXmlRpcHandler: shutdown waiting for 1s ..."); 84 try { 85 Thread.sleep(1000); 86 } catch (InterruptedException 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 install(String name, String host, String port, String path) throws Exception { 96 Preferences serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class); 98 File root = new File (serverPrefs.get(ServerConfiguration.WEBAPP_ROOT, System.getProperty("user.home"))); 99 File webAppDir = new File (root, name + "/webapp"); 100 File webInf = new File (webAppDir, "WEB-INF"); 101 webInf.mkdirs(); 102 103 File applicationConf = new File (webInf, "application.conf"); 104 if (!applicationConf.exists()) { 105 Properties installConfig = new Properties (); 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 (applicationConf), " Bootstrap Configuration"); 111 ApplicationLoader.loadApplication(root.getPath(), name); 112 installConfig.load(new FileInputStream (applicationConf)); 113 return ApplicationLoader.getUrl(installConfig) + "?key=" + installConfig.getProperty(Globals.APP_INSTALL_KEY); 114 } catch (Exception e) { 115 applicationConf.delete(); 116 e.printStackTrace(); 117 throw e; 118 } 119 } else { 120 throw new Exception ("'" + applicationConf.getPath() + "' exists, delete application first"); 121 } 122 } 123 124 132 public Boolean delete(String name, Boolean backup) throws Exception { 133 Preferences serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class); 135 File root = new File (serverPrefs.get(ServerConfiguration.WEBAPP_ROOT, System.getProperty("user.home"))); 136 File app = new File (root, name); 137 if (app.exists()) { 138 try { 139 ApplicationLoader.unloadApplication(root.getPath(), name); 140 } catch (Exception 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 (app.delete()); 147 } 148 return Boolean.TRUE; 149 } 150 151 private void createBackupJar(String jarName, File file) throws IOException { 152 JarOutputStream jar = new JarOutputStream (new FileOutputStream (jarName), 153 new Manifest ()); 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 jar, File file) throws IOException { 163 JarEntry entry = new JarEntry (file.getPath()); 164 jar.putNextEntry(entry); 165 if (file.isDirectory()) { 166 File [] fileList = file.listFiles(); 167 for (int fileNo = 0; fileNo < fileList.length; fileNo++) { 168 addToJarFile(jar, fileList[fileNo]); 169 } 170 } else { 171 FileInputStream fileStream = new FileInputStream (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 e) { 180 System.err.println("Jar: error adding '" + file.getPath() + "': " + e); 181 } finally { 182 fileStream.close(); 183 } 184 } 185 } 186 } 187 | Popular Tags |