~ubuntu-branches/debian/squeeze/sword/squeeze

« back to all changes in this revision

Viewing changes to bindings/corba/java/src/org/crosswire/sword/orb/SwordOrb.java

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Glassey
  • Date: 2004-01-15 15:50:07 UTC
  • Revision ID: james.westby@ubuntu.com-20040115155007-n9mz4x0zxrs1isd3
Tags: upstream-1.5.7
ImportĀ upstreamĀ versionĀ 1.5.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.crosswire.sword.orb;
 
2
 
 
3
import java.io.InputStream;
 
4
import java.io.InputStreamReader;
 
5
import java.io.BufferedReader;
 
6
import java.io.StringWriter;
 
7
import javax.servlet.http.HttpSession;
 
8
import javax.servlet.http.HttpSessionBindingListener;
 
9
import javax.servlet.http.HttpSessionBindingEvent;
 
10
 
 
11
public class SwordOrb extends Object implements HttpSessionBindingListener {
 
12
        public static final String BIBLES = "Biblical Texts";
 
13
        public static final String COMMENTARIES = "Commentaries";
 
14
        public static final String LEXDICTS = "Lexicons / Dictionaries";
 
15
        public static final String GENBOOKS = "Generic Books";
 
16
        public static final String DAILYDEVOS = "Daily Devotional";
 
17
        static org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(new String[]{}, null);
 
18
        String ior = null;
 
19
 
 
20
        private SWMgr attach() {
 
21
                SWMgr retVal = null;
 
22
                try {
 
23
System.out.println("attaching...");
 
24
                        org.omg.CORBA.Object obj = orb.string_to_object(ior);
 
25
                        retVal = SWMgrHelper.narrow(obj);
 
26
System.out.println("calling testConnection");
 
27
                        retVal.testConnection();
 
28
System.out.println("testConnection successful");
 
29
                }
 
30
                catch(org.omg.CORBA.SystemException e) {
 
31
                        e.printStackTrace();
 
32
                        retVal = null;
 
33
System.out.println("failed in attach");
 
34
                }
 
35
                return retVal;
 
36
        }
 
37
 
 
38
        public SwordOrb() {
 
39
        }
 
40
 
 
41
 
 
42
//      this doesn't seem to work.  Never seems to get called for me
 
43
        public void finalize () throws Throwable {
 
44
                // shut down external process
 
45
                try {
 
46
                        getSWMgrInstance().terminate();
 
47
                }
 
48
                catch (Exception e) {}  // we know this doesn't return property cuz we killed the orb! :)
 
49
 
 
50
        }
 
51
 
 
52
 
 
53
        public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {}
 
54
 
 
55
        public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
 
56
                try {
 
57
//                      throw new Exception("value unbound; showing stacktrace");
 
58
                        getSWMgrInstance().terminate();
 
59
                }
 
60
                catch (Exception e) {}  // we know this doesn't return properly cuz we killed the orb! :)
 
61
//              catch (Exception e) {e.printStackTrace();}      // we know this doesn't return properly cuz we killed the orb! :)
 
62
        }
 
63
 
 
64
 
 
65
        private void startOrb() {
 
66
                try {
 
67
                        // start external process
 
68
                        java.lang.Process p = Runtime.getRuntime().exec("swordorbserver");
 
69
                        InputStream is = p.getInputStream();
 
70
                        InputStreamReader isr = new InputStreamReader(is);
 
71
                        BufferedReader input = new BufferedReader(isr);
 
72
 
 
73
                        String line;
 
74
                        line = input.readLine();
 
75
//              retVal = p.waitFor();
 
76
                        ior = line;
 
77
System.out.println("Launched ORB, IOR: " + ior);
 
78
                }
 
79
                catch (Exception e) {e.printStackTrace();}
 
80
        }
 
81
 
 
82
 
 
83
        public SWMgr getSWMgrInstance() {
 
84
                SWMgr retVal = null;
 
85
                try {
 
86
System.out.println("trying to attach to running ORB");
 
87
                        retVal = attach();
 
88
                }
 
89
                catch(org.omg.CORBA.SystemException e) {
 
90
                        e.printStackTrace();
 
91
                        retVal = null;
 
92
                }
 
93
                if (retVal == null) {
 
94
                        try {
 
95
System.out.println("no ORB running; trying to launch");
 
96
                                startOrb();
 
97
System.out.println("trying to attach to newly launched ORB");
 
98
                                retVal = attach();
 
99
                        }
 
100
                        catch(org.omg.CORBA.SystemException e) {
 
101
                                e.printStackTrace();
 
102
                        }
 
103
                }
 
104
                return retVal;
 
105
        }
 
106
 
 
107
 
 
108
        public static SWMgr getSWMgrInstance(HttpSession session) {
 
109
                SwordOrb orb = (SwordOrb)session.getAttribute("SwordOrb");
 
110
                if (orb == null) {
 
111
System.out.println("No ORB found in session; constructing a new instance");
 
112
                        orb = new SwordOrb();
 
113
                        session.setAttribute("SwordOrb", orb);
 
114
                }
 
115
                else {
 
116
System.out.println("ORB found in session");
 
117
                }
 
118
                SWMgr mgr = orb.getSWMgrInstance();
 
119
                return mgr;
 
120
        }
 
121
 
 
122
 
 
123
        public static void main(String args[]) throws Exception {
 
124
                SWMgr mgr = new SwordOrb().getSWMgrInstance();
 
125
 
 
126
                System.out.println("PrefixPath: " + mgr.getPrefixPath());
 
127
                System.out.println("ConfigPath: " + mgr.getConfigPath());
 
128
                ModInfo[] modInfoList = mgr.getModInfoList();
 
129
                System.out.println("sequence size: " + modInfoList.length);
 
130
                SWModule module;
 
131
                for (int i = 0; i < modInfoList.length; i++) {
 
132
                        System.out.println(modInfoList[i].name + ": " + modInfoList[i].category + ": " + modInfoList[i].language);
 
133
                        module = mgr.getModuleByName(modInfoList[i].name);
 
134
                        module.setKeyText("jas1:19");
 
135
                        System.out.println(module.getRenderText());
 
136
                }
 
137
                module = mgr.getModuleByName("WEB");
 
138
                SearchHit[] searchResults = module.search("God love world", SearchType.MULTIWORD, 0, "");
 
139
                for (int i = 0; i < searchResults.length; i++)
 
140
                        System.out.println(searchResults[i].key);
 
141
 
 
142
        }
 
143
}