1 package net.sf.cantina.application;
2
3 import net.sf.cantina.DataSource;
4 import net.sf.cantina.SearchEngine;
5 import net.sf.cantina.datasource.UserDefinedDataSource;
6 import net.sf.cantina.search.LuceneSearchEngine;
7 import org.apache.log4j.Logger;
8
9
10 /***
11 * Puts the pieces together and tear them apart with init() and shut()
12 * @author Stephane JAIS
13 */
14 public class Application
15 {
16 private static final Logger logger = Logger.getLogger(Application.class);
17
18
19 protected static boolean isOn = false;
20
21 protected static DataSource datasource;
22
23 /***
24 * Should be called at least once before running any code
25 * in net.sf.cantina.
26 * @throws Exception
27 */
28
29 public static void init()
30 throws Exception
31 {
32 if (isOn)
33 return;
34 isOn = true;
35
36 initDataSource();
37 initSearchEngine();
38 }
39
40 public static void shut()
41 throws Exception
42 {
43 if (!isOn)
44 return;
45 isOn = false;
46
47 shutDataSource();
48 shutSearchEngine();
49 }
50
51
52
53 protected static void initDataSource()
54 throws Exception
55 {
56 DataSource.instance = new UserDefinedDataSource();
57 }
58
59 protected static void shutDataSource()
60 {
61 if (DataSource.getInstance() != null)
62 {
63 DataSource.getInstance().release();
64 }
65 }
66
67
68
69 protected static void initSearchEngine()
70 throws Exception
71 {
72 String indexPath = Config.getString("search.index.path");
73 SearchEngine.instance = new LuceneSearchEngine(indexPath);
74 if (Config.getBoolean("search.index.remove"))
75 ((LuceneSearchEngine)SearchEngine.getInstance()).deleteIndex();
76 if (!LuceneSearchEngine.indexExists(indexPath))
77 SearchEngine.getInstance().indexAllDocuments(DataSource.getInstance());
78 }
79
80 protected static void shutSearchEngine()
81 throws Exception
82 {
83 }
84
85 }
86