~ubuntu-branches/ubuntu/lucid/sword/lucid

« 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): Jonathan Marsden, Jonathan Marsden, Dmitrijs Ledkovs, Closed Bugs
  • Date: 2009-05-30 11:55:55 UTC
  • mfrom: (1.1.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090530115555-1tdz23ekn0pk1va1
Tags: 1.6.0+dfsg-1
[ Jonathan Marsden ]
* New upstream release. (Closes: #507960) (LP: #320558)
* debian/patches/02_libver.diff:
  - Bump SONAME to 8 -- SWORD 1.6 is not backward compatible with 1.5.11.
* debian/patches/series:
  - Remove 10_diatheke.diff -- included in upstream source.
* debian/patches/:
  - Remove several old unused .diff files.
  - Add 11_regex_only_when_needed.diff to conditionally include regex lib.
  - Add 12_fix_compiler_warnings.diff to remove all compiler warnings.
  - Add 13_fix_osis2mod_compression_default.diff from upstream svn.
  - Add 14_closing_section_not_chapter.diff from upstream svn.
* debian/libsword7.*: 
  - Rename to libsword8.*
  - Change libsword7 to libsword8 within files.
* debian/rules: 
  - SONAME bump to 8.
  - Set library version check to >= 1.6
* debian/control:
  - Change libsword7 to libsword8.
  - Add libsword7 to Conflicts.
  - Fix case of sword to SWORD in package descriptions.
  - Bump Standards-Version to 3.8.1 (no changes needed).
  - Fix section for libsword-dbg to avoid lintian warning.
* debian/rules:
  - Add DFSG get-orig-source target.
* debian/copyright:
  - Fix various mistakes in initial attempt to document copyrights.

[ Dmitrijs Ledkovs ]
* debian/rules: Added utils.mk to use missing-files target and call it on
  each build.
* debian/libsword-dev.install: Added libsword.la, previously missing.
* debian/libsword7.install: Added missing libicu translit files.
* debian/control:
  - Updated all uses of SWORD version to 1.6
  - Added libsword-dbg package
* debian/watch: Fixed a small mistake which was resulting in extra "."
  in final version name.
* debian/rules: simplified manpage processing.
* debian/libsword8.lintian-overrides: added override for module
  installation directory.
* debian/copyright: Updated with information about everyfile.
  Closes: #513448 LP: #322638
* debian/diatheke.examples: moved examples here from the diatheke.install
* debian/rules:
  - enabled shell script based testsuite
  - added commented out cppunit testsuite
* debian/patches/40_missing_includes.diff: 
  - added several missing stdio.h includes to prevent FTBFS of testsuite.

[ Closed Bugs ]
* FTBFS on intrepid (LP: #305172)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2009 CrossWire Bible Society (http://www.crosswire.org)
 
3
 *      CrossWire Bible Society
 
4
 *      P. O. Box 2528
 
5
 *      Tempe, AZ  85280-2528
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify it
 
8
 * under the terms of the GNU General Public License as published by the
 
9
 * Free Software Foundation version 2.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful, but
 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * General Public License for more details.
 
15
 *
 
16
 */
 
17
 
 
18
package org.crosswire.sword.orb;
 
19
 
 
20
import java.io.File;
 
21
import java.io.FileInputStream;
 
22
import java.io.InputStream;
 
23
import java.io.InputStreamReader;
 
24
import java.io.BufferedReader;
 
25
import java.io.StringWriter;
 
26
import javax.servlet.http.HttpSession;
 
27
import javax.servlet.http.HttpServletRequest;
 
28
import javax.servlet.http.HttpSessionBindingListener;
 
29
import javax.servlet.http.HttpSessionBindingEvent;
 
30
import java.util.Vector;
 
31
import java.util.Hashtable;
 
32
import java.util.Date;
 
33
import java.util.Properties;
 
34
 
 
35
public class SwordOrb extends Object implements HttpSessionBindingListener {
 
36
        public static Properties config = null;
 
37
        public static String ORBEXE = "swordorbserver";
 
38
        public static final int MAX_REMOTE_ADDR_CONNECTIONS = 20;
 
39
        public static final int MAX_ACCESS_COUNT_PER_INTERVAL = 10;
 
40
        public static final long MAX_ACCESS_COUNT_INTERVAL = 10 * 1000; // milliseconds
 
41
        public static final long BLACKLIST_DURATION = 10 * 60 * 1000;   // milliseconds
 
42
        public static final String BIBLES = "Biblical Texts";
 
43
        public static final String COMMENTARIES = "Commentaries";
 
44
        public static final String LEXDICTS = "Lexicons / Dictionaries";
 
45
        public static final String GENBOOKS = "Generic Books";
 
46
        public static final String DAILYDEVOS = "Daily Devotional";
 
47
 
 
48
        static java.util.Properties p = new java.util.Properties();
 
49
        static {
 
50
                p.setProperty("com.sun.CORBA.codeset.charsets", "0x05010001, 0x00010109");    // UTF-8, UTF-16
 
51
                p.setProperty("com.sun.CORBA.codeset.wcharsets", "0x00010109, 0x05010001");    // UTF-16, UTF-8
 
52
        }
 
53
 
 
54
        static org.omg.CORBA.ORB orb = org.omg.CORBA_2_3.ORB.init(new String[]{}, p);
 
55
        static Hashtable clients = new Hashtable();
 
56
        String ior = null;
 
57
        String remoteAddr = null;
 
58
        String localeName = null;
 
59
        long   lastAccessed = 0;
 
60
        int    intervalCount = 0;
 
61
        long   intervalStamp = 0;
 
62
        long   blacklistTill = 0;
 
63
 
 
64
        private SWMgr attach() {
 
65
 
 
66
                // assert IOR has been set
 
67
                if (ior == null)
 
68
                        return null;
 
69
 
 
70
                SWMgr retVal = null;
 
71
                try {
 
72
System.out.println("attaching...");
 
73
                        org.omg.CORBA.Object obj = orb.string_to_object(ior);
 
74
                        retVal = SWMgrHelper.narrow(obj);
 
75
System.out.println("calling testConnection");
 
76
                        retVal.testConnection();
 
77
System.out.println("testConnection successful");
 
78
                }
 
79
                catch(Throwable e) {
 
80
//                      e.printStackTrace();
 
81
                        retVal = null;
 
82
System.out.println("failed in attach");
 
83
                }
 
84
                return retVal;
 
85
        }
 
86
 
 
87
        public SwordOrb(String remoteAddr) {
 
88
                this.remoteAddr = remoteAddr;
 
89
        }
 
90
 
 
91
 
 
92
//      this doesn't seem to work.  Never seems to get called for me
 
93
        public void finalize () throws Throwable {
 
94
                // shut down external process
 
95
                try {
 
96
System.out.println("calling finalize.");
 
97
                        getSWMgrInstance().terminate();
 
98
                }
 
99
                catch (Exception e) {}  // we know this doesn't return property cuz we killed the orb! :)
 
100
 
 
101
        }
 
102
 
 
103
 
 
104
        public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {}
 
105
 
 
106
        public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
 
107
                try {
 
108
//                      throw new Exception("value unbound; showing stacktrace");
 
109
                        Vector orbs = (Vector)clients.get(remoteAddr);
 
110
int size = -1;
 
111
                        if (orbs != null) {
 
112
size = orbs.size();
 
113
                                orbs.remove(this);
 
114
                        }
 
115
System.out.println("calling valueUnbound. size before: " + size + "; size after: "+orbs.size());
 
116
                        getSWMgrInstance().terminate();
 
117
                }
 
118
                catch (Exception e) {}  // we know this doesn't return properly cuz we killed the orb! :)
 
119
//              catch (Exception e) {e.printStackTrace();}      // we know this doesn't return properly cuz we killed the orb! :)
 
120
        }
 
121
 
 
122
        private static void loadConfig(HttpServletRequest request) {
 
123
                try {
 
124
                        config = new Properties();
 
125
                        File propName = new File(request.getSession().getServletContext().getRealPath("/WEB-INF/swordweb.properties"));
 
126
                        if (propName.exists()) {
 
127
                                FileInputStream propFile = new FileInputStream(propName);
 
128
                                config.load(propFile);
 
129
                                propFile.close();
 
130
                        }
 
131
                }
 
132
                catch (Exception e) { e.printStackTrace(); }
 
133
                ORBEXE = config.getProperty("orbexe", "swordorbserver");
 
134
        }
 
135
 
 
136
        private void startOrb() {
 
137
                try {
 
138
                        // start external process
 
139
                        java.lang.Process p = Runtime.getRuntime().exec(ORBEXE);
 
140
                        InputStream is = p.getInputStream();
 
141
                        InputStreamReader isr = new InputStreamReader(is);
 
142
                        BufferedReader input = new BufferedReader(isr);
 
143
 
 
144
                        String line;
 
145
                        line = input.readLine();
 
146
//              retVal = p.waitFor();
 
147
                        ior = line;
 
148
System.out.println("Launched ORB, IOR: " + ior);
 
149
                }
 
150
                catch (Exception e) {e.printStackTrace();}
 
151
        }
 
152
 
 
153
        void checkAccessAbuse() throws Exception {
 
154
                if ((blacklistTill > 0) && (System.currentTimeMillis() < blacklistTill)) {
 
155
                        throw new Exception("You're an abuser and have been blacklisted till " + new Date(blacklistTill));
 
156
                }
 
157
                if (++intervalCount > MAX_ACCESS_COUNT_PER_INTERVAL) {
 
158
                        if (System.currentTimeMillis() < intervalStamp + MAX_ACCESS_COUNT_INTERVAL) {
 
159
                                // abuser
 
160
                                blacklistTill = System.currentTimeMillis() + BLACKLIST_DURATION;
 
161
                        }
 
162
                        intervalStamp = System.currentTimeMillis();
 
163
                        intervalCount = 0;
 
164
                }
 
165
        }
 
166
 
 
167
        public SWMgr getSWMgrInstance() throws Exception {
 
168
                lastAccessed = System.currentTimeMillis();
 
169
                checkAccessAbuse();
 
170
                SWMgr retVal = null;
 
171
                try {
 
172
System.out.println("trying to attach to running ORB");
 
173
                        retVal = attach();
 
174
                }
 
175
                catch(Exception e) {
 
176
//                      e.printStackTrace();
 
177
                        retVal = null;
 
178
                }
 
179
                if (retVal == null) {
 
180
                        try {
 
181
System.out.println("no ORB running; trying to launch");
 
182
                                startOrb();
 
183
System.out.println("trying to attach to newly launched ORB");
 
184
                                retVal = attach();
 
185
                                if (retVal != null) {
 
186
                                        if (localeName != null) {
 
187
                                                retVal.setDefaultLocale(localeName);
 
188
                                        }
 
189
                                }
 
190
                        }
 
191
                        catch(org.omg.CORBA.SystemException e) {
 
192
                                e.printStackTrace();
 
193
                        }
 
194
                }
 
195
                return retVal;
 
196
        }
 
197
 
 
198
        public static void setSessionLocale(String localeName, HttpServletRequest request) throws Exception {
 
199
                request.getSession().setAttribute("SwordOrbLocale", localeName);
 
200
                SWMgr mgr = getSWMgrInstance(request);
 
201
                if (mgr != null) {
 
202
                        mgr.setDefaultLocale(localeName);
 
203
                }
 
204
        }
 
205
 
 
206
        public static SwordOrb getSessionOrb(HttpServletRequest request) throws Exception {
 
207
                if (config == null) loadConfig(request);
 
208
                HttpSession session = request.getSession();
 
209
                SwordOrb orb = (SwordOrb)session.getAttribute("SwordOrb");
 
210
                String remoteAddr = request.getRemoteAddr();
 
211
                if (orb == null) {
 
212
System.out.println("No ORB found in session; constructing a new instance");
 
213
 
 
214
                        Vector orbs = (Vector)clients.get(remoteAddr);
 
215
                        if (orbs == null) {
 
216
                                orbs = new Vector();
 
217
                                clients.put(remoteAddr, orbs);
 
218
                        }
 
219
                        if (orbs.size() < MAX_REMOTE_ADDR_CONNECTIONS) {
 
220
                                orb = new SwordOrb(remoteAddr);
 
221
                                orbs.add(orb);
 
222
 
 
223
                                String locName = (String)session.getAttribute("SwordOrbLocale");
 
224
                                if (locName != null)
 
225
                                        orb.localeName = locName;
 
226
 
 
227
                                session.setAttribute("SwordOrb", orb);
 
228
                        }
 
229
                        else throw new Exception("Max Remote Addr Connections from: ["+remoteAddr+"]");
 
230
                }
 
231
                else {
 
232
System.out.println("ORB found in session");
 
233
                }
 
234
                return orb;
 
235
        }
 
236
 
 
237
        public static SWMgr getSWMgrInstance(HttpServletRequest request) throws Exception {
 
238
                SwordOrb orb = getSessionOrb(request);
 
239
                SWMgr mgr = orb.getSWMgrInstance();
 
240
                return mgr;
 
241
        }
 
242
 
 
243
 
 
244
        public static void main(String args[]) throws Exception {
 
245
                SWMgr mgr = new SwordOrb("127.0.0.1").getSWMgrInstance();
 
246
 
 
247
                System.out.println("PrefixPath: " + mgr.getPrefixPath());
 
248
                System.out.println("ConfigPath: " + mgr.getConfigPath());
 
249
                ModInfo[] modInfoList = mgr.getModInfoList();
 
250
                System.out.println("sequence size: " + modInfoList.length);
 
251
                SWModule module;
 
252
/*
 
253
                for (int i = 0; i < modInfoList.length; i++) {
 
254
                        System.out.println(modInfoList[i].name + ": " + modInfoList[i].category + ": " + modInfoList[i].language);
 
255
                        module = mgr.getModuleByName(modInfoList[i].name);
 
256
                        module.setKeyText("jas1:19");
 
257
                        System.out.println(module.getRenderText());
 
258
                }
 
259
*/
 
260
                module = mgr.getModuleByName("WHNU");
 
261
                module.setKeyText("rev.22.21");
 
262
                System.out.println(new String(module.getRawEntry().getBytes("iso8859-1"), "UTF-8"));
 
263
/*
 
264
                boolean lucene = module.hasSearchFramework();
 
265
                SearchHit[] searchResults = module.search("God love world", (lucene)?SearchType.LUCENE:SearchType.MULTIWORD, 0, "");
 
266
                for (int i = 0; i < searchResults.length; i++)
 
267
                        System.out.println(searchResults[i].key);
 
268
 
 
269
*/
 
270
        }
 
271
}