1 package net.sf.maia.web;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7
8 import javax.servlet.ServletConfig;
9 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServlet;
11
12 import net.sf.maia.core.storage.PageStore;
13 import net.sf.maia.core.storage.StorageException;
14
15 import org.springframework.context.ApplicationContext;
16 import org.springframework.context.support.ClassPathXmlApplicationContext;
17
18 /***
19 *
20 * @author Magnus Grimsell
21 */
22 public class InstallServlet extends HttpServlet
23 {
24 private static boolean sInitialized = false;
25
26 public void init(ServletConfig config) throws ServletException
27 {
28 synchronized(this)
29 {
30 File maiaDir = new File(Configuration.getMaiaDir());
31
32 if(!maiaDir.exists())
33 {
34 try
35 {
36 System.out.println("installing...");
37
38 File repoDir = new File(Configuration.getMaiaStorageDir());
39 repoDir.mkdirs();
40 File nodeTypesDir = new File(repoDir, "repository/nodetypes");
41 nodeTypesDir.mkdirs();
42 ClassLoader cl = this.getClass().getClassLoader();
43 InputStream in = cl.getResourceAsStream("storage/repository.xml");
44 FileOutputStream out =
45 new FileOutputStream(new File(maiaDir, "repository.xml"));
46 copy(in, out);
47 in = cl.getResourceAsStream("storage/custom_nodetypes.xml");
48 out = new FileOutputStream(new File(nodeTypesDir, "custom_nodetypes.xml"));
49 copy(in, out);
50
51
52 ApplicationContext ctx = new ClassPathXmlApplicationContext("storage.xml");
53 PageStore store = (PageStore)ctx.getBean("pageStore");
54 InputStream base = cl.getResourceAsStream("storage/base.xml");
55 store.importXML(base);
56 base.close();
57 }
58 catch(IOException e)
59 {
60 throw new StorageException(e);
61 }
62 }
63 sInitialized = true;
64 }
65 }
66
67 /***
68 * @param in
69 * @param out
70 * @throws IOException
71 */
72 private void copy(InputStream in, FileOutputStream out) throws IOException
73 {
74 byte[] b = new byte[1024];
75 int read = 0;
76 while((read = in.read(b)) != -1)
77 {
78 out.write(b, 0, read);
79 }
80 in.close();
81 out.close();
82 }
83 }