2 %% Copyright (C) 2006 Damien Katz
4 %% This program is free software; you can redistribute it and/or
5 %% modify it under the terms of the GNU General Public License
6 %% as published by the Free Software Foundation; either version 2
7 %% of the License, or (at your option) any later version.
9 %% This program is distributed in the hope that it will be useful,
10 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
11 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 %% GNU General Public License for more details.
14 %% You should have received a copy of the GNU General Public License
15 %% along with this program; if not, write to the Free Software
16 %% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 -module(couch_lucene
).
19 -behaviour(gen_server
).
21 -include("couch_db.hrl").
23 -export([start_link
/1, timeout
/0]).
24 -export([init
/1, terminate
/2, handle_call
/3, handle_cast
/2, handle_info
/2,code_change
/3,stop
/0]).
26 -define(ERR_HANDLE
, {Port
, {exit_status
, Status
}} -> {stop
, {unknown_error
, Status
}, {unknown_error
, Status
}, Port
}).
28 start_link(LuceneExec
) ->
29 gen_server:start_link({local
, couch_lucene
}, couch_lucene
, LuceneExec
, []).
32 exit(whereis(couch_lucene
), close
).
35 % in Msecs. maybe need to make this a configurable value
40 case erlang:system_info(os_type
) of
42 lists:flatten(io_lib:format("~s launched_from_erlang", [LuceneExec
]));
44 lists:flatten(io_lib:format("cmd /c\"~s\" launched_from_erlang", [LuceneExec
]))
46 Port
= open_port({spawn, Cmd
}, [nouse_stdio
]),
49 terminate(_Reason
, _Server
) ->
52 handle_call(get_exe_cmd
, _From
, {Cmd
, Port
}) ->
56 handle_cast(_Whatever
, {Exe
, Port
}) ->
57 {noreply
, {Exe
, Port
}}.
59 handle_info({Port
, {exit_status
, Status
}}, {Exe
, Port
}) ->
60 {stop
, {lucene_server_exited
, Status
}, {Exe
, Port
}};
62 handle_info(_Whatever
, {Exe
, Port
}) ->
63 {noreply
, {Exe
, Port
}}.
65 code_change(_OldVsn
, State
, _Extra
) ->