RuntimeEnvironment.java revision 504
58N/A/*
58N/A * CDDL HEADER START
58N/A *
58N/A * The contents of this file are subject to the terms of the
58N/A * Common Development and Distribution License (the "License").
58N/A * You may not use this file except in compliance with the License.
58N/A *
58N/A * See LICENSE.txt included in this distribution for the specific
58N/A * language governing permissions and limitations under the License.
58N/A *
58N/A * When distributing Covered Code, include this CDDL HEADER in each
58N/A * file and include the License file at LICENSE.txt.
58N/A * If applicable, add the following below this CDDL HEADER, with the
58N/A * fields enclosed by brackets "[]" replaced with your own identifying
58N/A * information: Portions Copyright [yyyy] [name of copyright owner]
58N/A *
58N/A * CDDL HEADER END
58N/A */
58N/A
58N/A/*
77N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
58N/A * Use is subject to license terms.
58N/A */
58N/Apackage org.opensolaris.opengrok.configuration;
58N/A
234N/Aimport java.beans.XMLDecoder;
234N/Aimport java.beans.XMLEncoder;
234N/Aimport java.io.BufferedInputStream;
234N/Aimport java.io.BufferedReader;
639N/Aimport java.io.File;
639N/Aimport java.io.IOException;
234N/Aimport java.io.InputStreamReader;
234N/Aimport java.net.InetAddress;
234N/Aimport java.net.ServerSocket;
234N/Aimport java.net.Socket;
639N/Aimport java.net.SocketAddress;
639N/Aimport java.net.UnknownHostException;
58N/Aimport java.util.List;
667N/Aimport java.util.Map;
58N/Aimport java.util.logging.Level;
664N/Aimport java.util.logging.Logger;
112N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
58N/Aimport org.opensolaris.opengrok.history.Repository;
58N/Aimport org.opensolaris.opengrok.index.IgnoredNames;
77N/A
77N/A/**
77N/A * The RuntimeEnvironment class is used as a placeholder for the current
77N/A * configuration this execution context (classloader) is using.
58N/A */
418N/Apublic final class RuntimeEnvironment {
58N/A private Configuration configuration;
773N/A private final ThreadLocal<Configuration> threadConfig;
773N/A
58N/A private static final Logger log = Logger.getLogger(RuntimeEnvironment.class.getName());
773N/A
773N/A private static RuntimeEnvironment instance = new RuntimeEnvironment();
773N/A
773N/A /**
58N/A * Get the one and only instance of the RuntimeEnvironment
773N/A * @return the one and only instance of the RuntimeEnvironment
773N/A */
773N/A public static RuntimeEnvironment getInstance() {
773N/A return instance;
58N/A }
58N/A
58N/A /**
664N/A * Creates a new instance of RuntimeEnvironment. Private to ensure a
58N/A * singleton pattern.
65N/A */
77N/A private RuntimeEnvironment() {
99N/A configuration = new Configuration();
99N/A threadConfig = new ThreadLocal<Configuration>() {
125N/A @Override protected Configuration initialValue() {
112N/A return configuration;
129N/A }
129N/A };
129N/A }
318N/A
318N/A private String getCanonicalPath(String s) {
144N/A try {
173N/A File file = new File(s);
253N/A if (!file.exists()) {
296N/A return s;
335N/A }
480N/A return file.getCanonicalPath();
816N/A } catch (IOException ex) {
816N/A OpenGrokLogger.getLogger().log(Level.SEVERE, "Failed to get canonical path", ex);
833N/A return s;
833N/A }
816N/A }
58N/A
58N/A /**
816N/A * Get the path to the where the index database is stored
58N/A * @return the path to the index database
58N/A */
773N/A public String getDataRootPath() {
58N/A return threadConfig.get().getDataRoot();
664N/A }
58N/A
850N/A /**
99N/A * Get a file representing the index database
99N/A * @return the index database
99N/A */
101N/A public File getDataRootFile() {
106N/A File ret = null;
112N/A String file = getDataRootPath();
129N/A if (file != null) {
129N/A ret = new File(file);
129N/A }
318N/A
318N/A return ret;
144N/A }
173N/A
253N/A /**
296N/A * Set the path to where the index database is stored
335N/A * @param dataRoot the index database
480N/A */
816N/A public void setDataRoot(String dataRoot) {
816N/A final File file = new File(dataRoot);
843N/A if (!file.exists() && !file.mkdirs()) {
58N/A OpenGrokLogger.getLogger().log(
58N/A Level.SEVERE, "Failed to create dataroot: " + dataRoot);
58N/A }
58N/A threadConfig.get().setDataRoot(getCanonicalPath(dataRoot));
58N/A }
58N/A
58N/A /**
58N/A * Get the path to where the sources are located
58N/A * @return path to where the sources are located
816N/A */
816N/A public String getSourceRootPath() {
816N/A return threadConfig.get().getSourceRoot();
816N/A }
816N/A
816N/A /**
816N/A * Get a file representing the directory where the sources are located
816N/A * @return A file representing the directory where the sources are located
816N/A */
816N/A public File getSourceRootFile() {
816N/A File ret = null;
816N/A String file = getSourceRootPath();
816N/A if (file != null) {
816N/A ret = new File(file);
816N/A }
816N/A
773N/A return ret;
773N/A }
773N/A
773N/A /**
773N/A * Specify the source root
773N/A * @param sourceRoot the location of the sources
58N/A */
58N/A public void setSourceRoot(String sourceRoot) {
58N/A threadConfig.get().setSourceRoot(getCanonicalPath(sourceRoot));
773N/A }
773N/A
773N/A /**
773N/A * Do we have projects?
773N/A * @return true if we have projects
58N/A */
58N/A public boolean hasProjects() {
58N/A List<Project> proj = getProjects();
773N/A return (proj != null && !proj.isEmpty());
773N/A }
773N/A
773N/A /**
773N/A * Get all of the projects
773N/A * @return a list containing all of the projects (may be null)
773N/A */
773N/A public List<Project> getProjects() {
773N/A return threadConfig.get().getProjects();
58N/A }
58N/A
58N/A /**
773N/A * Set the list of the projects
773N/A * @param projects the list of projects to use
773N/A */
773N/A public void setProjects(List<Project> projects) {
773N/A threadConfig.get().setProjects(projects);
773N/A }
773N/A
58N/A /**
58N/A * Register this thread in the thread/configuration map (so that all
58N/A * subsequent calls to the RuntimeEnvironment from this thread will use
773N/A * the same configuration
773N/A */
773N/A public void register() {
773N/A threadConfig.set(configuration);
773N/A }
773N/A
773N/A /**
773N/A * Get the context name of the web application
773N/A * @return the web applications context name
773N/A */
773N/A public String getUrlPrefix() {
773N/A return threadConfig.get().getUrlPrefix();
773N/A }
773N/A
773N/A /**
773N/A * Set the web context name
773N/A * @param urlPrefix the web applications context name
773N/A */
773N/A public void setUrlPrefix(String urlPrefix) {
773N/A threadConfig.get().setUrlPrefix(urlPrefix);
773N/A }
773N/A
773N/A /**
58N/A * Get the name of the ctags program in use
58N/A * @return the name of the ctags program in use
58N/A */
58N/A public String getCtags() {
58N/A return threadConfig.get().getCtags();
58N/A }
58N/A
58N/A /**
58N/A * Specify the CTags program to use
58N/A * @param ctags the ctags program to use
58N/A */
58N/A public void setCtags(String ctags) {
58N/A threadConfig.get().setCtags(ctags);
58N/A }
58N/A
58N/A /**
58N/A * Validate that I have a Exuberant ctags program I may use
58N/A * @return true if success, false otherwise
58N/A */
58N/A public boolean validateExuberantCtags() {
58N/A String ctags = getCtags();
58N/A
58N/A //Check if exub ctags is available
58N/A Process ctagsProcess = null;
58N/A try {
664N/A ctagsProcess = Runtime.getRuntime().exec(new String[] {ctags, "--version" });
58N/A } catch (Exception e) {
58N/A }
58N/A if (ctagsProcess == null) {
664N/A log.severe("Error: executing " + ctags + "! " +
58N/A "\nPlease use option -c to specify path to a good Exuberant Ctags program");
58N/A return false;
58N/A }
58N/A try {
58N/A BufferedReader cin = new BufferedReader(new InputStreamReader(ctagsProcess.getInputStream()));
58N/A try {
58N/A String ctagOut;
58N/A if (!((ctagOut = cin.readLine()) != null && ctagOut.startsWith("Exuberant Ctags"))) {
58N/A log.severe("Error: No Exuberant Ctags found in PATH!\n" +
58N/A "(tried running " + ctags + ")\n" +
77N/A "Please use option -c to specify path to a good Exuberant Ctags program");
65N/A return false;
65N/A }
65N/A } finally {
65N/A cin.close();
65N/A }
65N/A } catch (Exception e) {
65N/A log.severe("Error: executing " + ctags + "! " +e.getLocalizedMessage() +
77N/A "\nPlease use option -c to specify path to a good Exuberant Ctags program");
77N/A return false;
77N/A }
77N/A
77N/A // reap the child process..
77N/A try {
77N/A int ret;
77N/A if ((ret = ctagsProcess.exitValue()) != 0) {
99N/A log.severe("Error: ctags returned " + ret);
99N/A return false;
99N/A }
99N/A } catch (IllegalThreadStateException exp) {
99N/A // the process is still running??? just kill it..
99N/A ctagsProcess.destroy();
99N/A return true;
99N/A }
99N/A return true;
99N/A }
99N/A
99N/A /**
99N/A * Get the max time a SMC operation may use to avoid beeing cached
99N/A * @return the max time
99N/A */
99N/A public int getHistoryReaderTimeLimit() {
125N/A return threadConfig.get().getHistoryCacheTime();
125N/A }
125N/A
125N/A /**
125N/A * Specify the maximum time a SCM operation should take before it will
125N/A * be cached (in ms)
125N/A * @param historyReaderTimeLimit the max time in ms before it is cached
125N/A */
106N/A public void setHistoryReaderTimeLimit(int historyReaderTimeLimit) {
106N/A threadConfig.get().setHistoryCacheTime(historyReaderTimeLimit);
106N/A }
106N/A
106N/A /**
106N/A * Is history cache currently enabled?
106N/A * @return true if history cache is enabled
106N/A */
106N/A public boolean useHistoryCache() {
106N/A return threadConfig.get().isHistoryCache();
112N/A }
112N/A
112N/A /**
112N/A * Specify if we should use history cache or not
112N/A * @param useHistoryCache set false if you do not want to use history cache
112N/A */
112N/A public void setUseHistoryCache(boolean useHistoryCache) {
112N/A threadConfig.get().setHistoryCache(useHistoryCache);
129N/A }
129N/A
129N/A /**
129N/A * Should we generate HTML or not during the indexing phase
129N/A * @return true if HTML should be generated during the indexing phase
129N/A */
129N/A public boolean isGenerateHtml() {
129N/A return threadConfig.get().isGenerateHtml();
129N/A }
129N/A
129N/A /**
129N/A * Specify if we should generate HTML or not during the indexing phase
129N/A * @param generateHtml set this to true to pregenerate HTML
129N/A */
129N/A public void setGenerateHtml(boolean generateHtml) {
129N/A threadConfig.get().setGenerateHtml(generateHtml);
129N/A }
129N/A
129N/A /**
129N/A * Set if we should compress the xref files or not
129N/A * @param compressXref set to true if the generated html files should be
129N/A * compressed
129N/A */
129N/A public void setCompressXref(boolean compressXref) {
318N/A threadConfig.get().setCompressXref(compressXref);
318N/A }
318N/A
318N/A /**
318N/A * Are we using copressed HTML files?
318N/A * @return true if the html-files should be compressed. false otherwise
318N/A */
318N/A public boolean isCompressXref() {
318N/A return threadConfig.get().isCompressXref();
318N/A }
318N/A
318N/A public boolean isQuickContextScan() {
318N/A return threadConfig.get().isQuickContextScan();
318N/A }
318N/A
318N/A public void setQuickContextScan(boolean quickContextScan) {
144N/A threadConfig.get().setQuickContextScan(quickContextScan);
144N/A }
144N/A
144N/A /**
144N/A * Get the map of external SCM repositories available
144N/A * @return A map containing all available SCMs
144N/A */
144N/A public Map<String, Repository> getRepositories() {
173N/A return threadConfig.get().getRepositories();
173N/A }
173N/A
173N/A /**
173N/A * Set the map of external SCM repositories
173N/A * @param repositories the repositories to use
173N/A */
173N/A public void setRepositories(Map<String, Repository> repositories) {
234N/A threadConfig.get().setRepositories(repositories);
253N/A }
253N/A
253N/A /**
253N/A * Set the project that is specified to be the default project to use. The
253N/A * default project is the project you will search (from the web application)
253N/A * if the page request didn't contain the cookie..
253N/A * @param defaultProject The default project to use
253N/A */
296N/A public void setDefaultProject(Project defaultProject) {
296N/A threadConfig.get().setDefaultProject(defaultProject);
296N/A }
296N/A
296N/A /**
296N/A * Get the project that is specified to be the default project to use. The
296N/A * default project is the project you will search (from the web application)
335N/A * if the page request didn't contain the cookie..
335N/A * @return the default project (may be null if not specified)
335N/A */
335N/A public Project getDefaultProject() {
335N/A return threadConfig.get().getDefaultProject();
335N/A }
335N/A
335N/A /**
335N/A * Chandan wrote the following answer on the opengrok-discuss list:
480N/A * "Traditionally search engines (specially spiders) think that large files
480N/A * are junk. Large files tend to be multimedia files etc., which text
480N/A * search spiders do not want to chew. So they ignore the contents of
480N/A * the file after a cutoff length. Lucene does this by number of words,
480N/A * which is by default is 10,000."
480N/A * By default OpenGrok will increase this limit to 60000, but it may be
480N/A * overridden in the configuration file
667N/A * @return The maximum words to index
667N/A */
667N/A public int getIndexWordLimit() {
667N/A return threadConfig.get().getIndexWordLimit();
667N/A }
833N/A
833N/A /**
833N/A * Set the number of words in a file Lucene will index.
833N/A * See getIndexWordLimit for a better description.
833N/A * @param indexWordLimit the number of words to index in a single file
833N/A */
833N/A public void setIndexWordLimit(int indexWordLimit) {
833N/A threadConfig.get().setIndexWordLimit(indexWordLimit);
833N/A }
833N/A
833N/A /**
843N/A * Is the verbosity flag turned on?
833N/A * @return true if we can print extra information
833N/A */
833N/A public boolean isVerbose() {
833N/A return threadConfig.get().isVerbose();
833N/A }
833N/A
833N/A /**
833N/A * Set the verbosity flag (to add extra debug information in output)
234N/A * @param verbose new value
234N/A */
234N/A public void setVerbose(boolean verbose) {
234N/A threadConfig.get().setVerbose(verbose);
234N/A }
234N/A
509N/A /**
640N/A * Specify if a search may start with a wildcard. Note that queries
640N/A * that start with a wildcard will give a significant impact on the
640N/A * search performace.
640N/A * @param allowLeadingWildcard set to true to activate (disabled by default)
640N/A */
639N/A public void setAllowLeadingWildcard(boolean allowLeadingWildcard) {
639N/A threadConfig.get().setAllowLeadingWildcard(allowLeadingWildcard);
639N/A }
639N/A
639N/A /**
639N/A * Is leading wildcards allowed?
639N/A * @return true if a search may start with a wildcard
639N/A */
639N/A public boolean isAllowLeadingWildcard() {
640N/A return threadConfig.get().isAllowLeadingWildcard();
640N/A }
640N/A
234N/A public IgnoredNames getIgnoredNames() {
234N/A return threadConfig.get().getIgnoredNames();
234N/A }
639N/A
640N/A public void setIgnoredNames(IgnoredNames ignoredNames) {
640N/A threadConfig.get().setIgnoredNames(ignoredNames);
640N/A }
640N/A
640N/A /**
639N/A * Returns the user page for the history listing
639N/A * @return the URL string fragment preceeding the username
639N/A */
639N/A public String getUserPage() {
639N/A return threadConfig.get().getUserPage();
639N/A }
639N/A
639N/A /**
639N/A * Sets the user page for the history listing
639N/A * @param userPage the URL fragment preceeding the username from history
639N/A */
639N/A public void setUserPage(String userPage) {
640N/A threadConfig.get().setUserPage(userPage);
640N/A }
640N/A
509N/A /**
234N/A * Returns the bug page for the history listing
234N/A * @return the URL string fragment preceeding the bug ID
640N/A */
234N/A public String getBugPage() {
234N/A return threadConfig.get().getBugPage();
639N/A }
58N/A
/**
* Sets the bug page for the history listing
* @param bugPage the URL fragment preceeding the bug ID
*/
public void setBugPage(String bugPage) {
threadConfig.get().setBugPage(bugPage);
}
/**
* Returns the bug regex for the history listing
* @return the regex that is looked for in history comments
*/
public String getBugPattern() {
return threadConfig.get().getBugPattern();
}
/**
* Sets the bug regex for the history listing
* @param bugPattern the regex to search history comments
*/
public void setBugPattern(String bugPattern) {
threadConfig.get().setBugPattern(bugPattern);
}
/**
* Returns the review(ARC) page for the history listing
* @return the URL string fragment preceeding the review page ID
*/
public String getReviewPage() {
return threadConfig.get().getReviewPage();
}
/**
* Sets the review(ARC) page for the history listing
* @param reviewPage the URL fragment preceeding the review page ID
*/
public void setReviewPage(String reviewPage) {
threadConfig.get().setReviewPage(reviewPage);
}
/**
* Returns the review(ARC) regex for the history listing
* @return the regex that is looked for in history comments
*/
public String getReviewPattern() {
return threadConfig.get().getReviewPattern();
}
/**
* Sets the review(ARC) regex for the history listing
* @param reviewPattern the regex to search history comments
*/
public void setReviewPattern(String reviewPattern) {
threadConfig.get().setReviewPattern(reviewPattern);
}
public String getWebappLAF() {
return threadConfig.get().getWebappLAF();
}
public void setWebappLAF(String laf) {
threadConfig.get().setWebappLAF(laf);
}
public boolean isRemoteScmSupported() {
return threadConfig.get().isRemoteScmSupported();
}
public void setRemoteScmSupported(boolean supported) {
threadConfig.get().setRemoteScmSupported(supported);
}
public boolean isOptimizeDatabase() {
return threadConfig.get().isOptimizeDatabase();
}
public void setOptimizeDatabase(boolean optimizeDatabase) {
threadConfig.get().setOptimizeDatabase(optimizeDatabase);
}
public boolean isUsingLuceneLocking() {
return threadConfig.get().isUsingLuceneLocking();
}
public void setUsingLuceneLocking(boolean useLuceneLocking) {
threadConfig.get().setUsingLuceneLocking(useLuceneLocking);
}
public boolean isIndexVersionedFilesOnly() {
return threadConfig.get().isIndexVersionedFilesOnly();
}
public void setIndexVersionedFilesOnly(boolean indexVersionedFilesOnly) {
threadConfig.get().setIndexVersionedFilesOnly(indexVersionedFilesOnly);
}
/**
* Read an configuration file and set it as the current configuration.
* @param file the file to read
* @throws IOException if an error occurs
*/
public void readConfiguration(File file) throws IOException {
configuration = Configuration.read(file);
register();
}
/**
* Write the current configuration to a file
* @param file the file to write the configuration into
* @throws IOException if an error occurs
*/
public void writeConfiguration(File file) throws IOException {
threadConfig.get().write(file);
}
/**
* Write the current configuration to a socket
* @param host the host address to receive the configuration
* @param port the port to use on the host
* @throws IOException if an error occurs
*/
public void writeConfiguration(InetAddress host, int port) throws IOException {
Socket sock = new Socket(host, port);
XMLEncoder e = new XMLEncoder(sock.getOutputStream());
e.writeObject(threadConfig.get());
e.close();
try {
sock.close();
} catch (Exception ex) {
log.log(Level.INFO, "Couldn't close socket after writing configuration.", ex);
}
}
protected void writeConfiguration() throws IOException {
writeConfiguration(configServerSocket.getInetAddress(), configServerSocket.getLocalPort());
}
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
register();
}
private ServerSocket configServerSocket;
/**
* Try to stop the configuration listener thread
*/
public void stopConfigurationListenerThread() {
try {
configServerSocket.close();
} catch (Exception e) { log.log(Level.FINE, "Stopping config listener thread: ", e); }
}
/**
* Start a thread to listen on a socket to receive new configurations
* to use.
* @param endpoint The socket address to listen on
* @return true if the endpoint was available (and the thread was started)
*/
public boolean startConfigurationListenerThread(SocketAddress endpoint) {
boolean ret = false;
try {
configServerSocket = new ServerSocket();
configServerSocket.bind(endpoint);
ret = true;
final ServerSocket sock = configServerSocket;
Thread t = new Thread(new Runnable() {
public void run() {
while (!sock.isClosed()) {
Socket s = null;
try {
s = sock.accept();
log.info(" OpenGrok: Got request from " + s.getInetAddress().getHostAddress());
BufferedInputStream in = new BufferedInputStream(s.getInputStream());
XMLDecoder d = new XMLDecoder(new BufferedInputStream(in));
Object obj = d.readObject();
d.close();
if (obj instanceof Configuration) {
configuration = (Configuration)obj;
log.info("Configuration updated: " + configuration.getSourceRoot());
}
} catch (IOException e) {
log.log(Level.WARNING, "Error reading config file: ",e);
} finally {
if (s != null) {
try {
s.close();
} catch (IOException ex) {
log.log(Level.WARNING, "Interrupt closing config listener reader socket: ", ex);
}
}
}
}
}
});
t.start();
} catch (UnknownHostException ex) {
log.log(Level.FINE,"Problem resolving sender: ",ex);
} catch (IOException ex) {
log.log(Level.FINE,"I/O error when waiting for config: ",ex);
}
if (!ret && configServerSocket != null) {
try {
configServerSocket.close();
} catch (IOException ex) {
log.log(Level.FINE,"I/O problem closing reader config socket: ",ex);
}
}
return ret;
}
}