001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.core;
017:
018: import org.apache.solr.util.NamedList;
019: import org.apache.solr.search.SolrIndexSearcher;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.List;
024: import java.util.ArrayList;
025: import java.util.logging.Level;
026:
027: /**
028: * @author yonik
029: */
030: class RunExecutableListener extends AbstractSolrEventListener {
031: protected String[] cmd;
032: protected File dir;
033: protected String[] envp;
034: protected boolean wait = true;
035:
036: public void init(NamedList args) {
037: super .init(args);
038:
039: List cmdlist = new ArrayList();
040: cmdlist.add(args.get("exe"));
041: List lst = (List) args.get("args");
042: if (lst != null)
043: cmdlist.addAll(lst);
044: cmd = (String[]) cmdlist.toArray(new String[cmdlist.size()]);
045:
046: lst = (List) args.get("env");
047: if (lst != null) {
048: envp = (String[]) lst.toArray(new String[lst.size()]);
049: }
050:
051: String str = (String) args.get("dir");
052: if (str == null || str.equals("") || str.equals(".")
053: || str.equals("./")) {
054: dir = null;
055: } else {
056: dir = new File(str);
057: }
058:
059: if ("false".equals(args.get("wait"))
060: || Boolean.FALSE.equals(args.get("wait")))
061: wait = false;
062: }
063:
064: protected int exec(String callback) {
065: int ret = 0;
066:
067: try {
068: boolean doLog = log.isLoggable(Level.FINE);
069: if (doLog) {
070: log.fine("About to exec " + cmd[0]);
071: }
072: Process proc = Runtime.getRuntime().exec(cmd, envp, dir);
073:
074: if (wait) {
075: try {
076: ret = proc.waitFor();
077: } catch (InterruptedException e) {
078: SolrException.log(log, e);
079: }
080: }
081:
082: if (wait && doLog) {
083: log.fine("Executable " + cmd[0] + " returned " + ret);
084: }
085:
086: } catch (IOException e) {
087: // don't throw exception, just log it...
088: SolrException.log(log, e);
089: }
090:
091: return ret;
092: }
093:
094: public void postCommit() {
095: // anything generic need to be passed to the external program?
096: // the directory of the index? the command that caused it to be
097: // invoked? the version of the index?
098: exec("postCommit");
099: }
100:
101: public void newSearcher(SolrIndexSearcher newSearcher,
102: SolrIndexSearcher currentSearcher) {
103: exec("newSearcher");
104: }
105:
106: }
|