2
* Copyright 2009 CrossWire Bible Society (http://www.crosswire.org)
3
* CrossWire Bible Society
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.
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.
18
package org.crosswire.sword.orb;
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;
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";
48
static java.util.Properties p = new java.util.Properties();
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
54
static org.omg.CORBA.ORB orb = org.omg.CORBA_2_3.ORB.init(new String[]{}, p);
55
static Hashtable clients = new Hashtable();
57
String remoteAddr = null;
58
String localeName = null;
59
long lastAccessed = 0;
60
int intervalCount = 0;
61
long intervalStamp = 0;
62
long blacklistTill = 0;
64
private SWMgr attach() {
66
// assert IOR has been set
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");
80
// e.printStackTrace();
82
System.out.println("failed in attach");
87
public SwordOrb(String remoteAddr) {
88
this.remoteAddr = remoteAddr;
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
96
System.out.println("calling finalize.");
97
getSWMgrInstance().terminate();
99
catch (Exception e) {} // we know this doesn't return property cuz we killed the orb! :)
104
public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {}
106
public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
108
// throw new Exception("value unbound; showing stacktrace");
109
Vector orbs = (Vector)clients.get(remoteAddr);
115
System.out.println("calling valueUnbound. size before: " + size + "; size after: "+orbs.size());
116
getSWMgrInstance().terminate();
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! :)
122
private static void loadConfig(HttpServletRequest request) {
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);
132
catch (Exception e) { e.printStackTrace(); }
133
ORBEXE = config.getProperty("orbexe", "swordorbserver");
136
private void startOrb() {
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);
145
line = input.readLine();
146
// retVal = p.waitFor();
148
System.out.println("Launched ORB, IOR: " + ior);
150
catch (Exception e) {e.printStackTrace();}
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));
157
if (++intervalCount > MAX_ACCESS_COUNT_PER_INTERVAL) {
158
if (System.currentTimeMillis() < intervalStamp + MAX_ACCESS_COUNT_INTERVAL) {
160
blacklistTill = System.currentTimeMillis() + BLACKLIST_DURATION;
162
intervalStamp = System.currentTimeMillis();
167
public SWMgr getSWMgrInstance() throws Exception {
168
lastAccessed = System.currentTimeMillis();
172
System.out.println("trying to attach to running ORB");
176
// e.printStackTrace();
179
if (retVal == null) {
181
System.out.println("no ORB running; trying to launch");
183
System.out.println("trying to attach to newly launched ORB");
185
if (retVal != null) {
186
if (localeName != null) {
187
retVal.setDefaultLocale(localeName);
191
catch(org.omg.CORBA.SystemException e) {
198
public static void setSessionLocale(String localeName, HttpServletRequest request) throws Exception {
199
request.getSession().setAttribute("SwordOrbLocale", localeName);
200
SWMgr mgr = getSWMgrInstance(request);
202
mgr.setDefaultLocale(localeName);
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();
212
System.out.println("No ORB found in session; constructing a new instance");
214
Vector orbs = (Vector)clients.get(remoteAddr);
217
clients.put(remoteAddr, orbs);
219
if (orbs.size() < MAX_REMOTE_ADDR_CONNECTIONS) {
220
orb = new SwordOrb(remoteAddr);
223
String locName = (String)session.getAttribute("SwordOrbLocale");
225
orb.localeName = locName;
227
session.setAttribute("SwordOrb", orb);
229
else throw new Exception("Max Remote Addr Connections from: ["+remoteAddr+"]");
232
System.out.println("ORB found in session");
237
public static SWMgr getSWMgrInstance(HttpServletRequest request) throws Exception {
238
SwordOrb orb = getSessionOrb(request);
239
SWMgr mgr = orb.getSWMgrInstance();
244
public static void main(String args[]) throws Exception {
245
SWMgr mgr = new SwordOrb("127.0.0.1").getSWMgrInstance();
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);
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());
260
module = mgr.getModuleByName("WHNU");
261
module.setKeyText("rev.22.21");
262
System.out.println(new String(module.getRawEntry().getBytes("iso8859-1"), "UTF-8"));
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);