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