RuntimeEnvironment.java revision 125
30N/A/*
30N/A * CDDL HEADER START
30N/A *
30N/A * The contents of this file are subject to the terms of the
30N/A * Common Development and Distribution License (the "License").
30N/A * You may not use this file except in compliance with the License.
30N/A *
30N/A * See LICENSE.txt included in this distribution for the specific
30N/A * language governing permissions and limitations under the License.
30N/A *
30N/A * When distributing Covered Code, include this CDDL HEADER in each
30N/A * file and include the License file at LICENSE.txt.
30N/A * If applicable, add the following below this CDDL HEADER, with the
30N/A * fields enclosed by brackets "[]" replaced with your own identifying
30N/A * information: Portions Copyright [yyyy] [name of copyright owner]
30N/A *
30N/A * CDDL HEADER END
30N/A */
30N/A
30N/A/*
30N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
30N/A * Use is subject to license terms.
30N/A */
30N/Apackage org.opensolaris.opengrok.configuration;
30N/A
58N/Aimport java.beans.XMLDecoder;
58N/Aimport java.beans.XMLEncoder;
58N/Aimport java.io.BufferedInputStream;
58N/Aimport java.io.BufferedOutputStream;
99N/Aimport java.io.BufferedReader;
30N/Aimport java.io.File;
58N/Aimport java.io.FileInputStream;
58N/Aimport java.io.FileOutputStream;
30N/Aimport java.io.IOException;
99N/Aimport java.io.InputStreamReader;
58N/Aimport java.net.InetAddress;
58N/Aimport java.net.ServerSocket;
58N/Aimport java.net.Socket;
58N/Aimport java.net.SocketAddress;
58N/Aimport java.net.UnknownHostException;
58N/Aimport java.util.Collections;
58N/Aimport java.util.Date;
58N/Aimport java.util.HashMap;
58N/Aimport java.util.List;
58N/Aimport java.util.Map;
58N/Aimport org.opensolaris.opengrok.history.ExternalRepository;
112N/Aimport org.opensolaris.opengrok.index.IgnoredNames;
30N/A
30N/A/**
30N/A * The RuntimeEnvironment class is used as a placeholder for the current
58N/A * configuration this execution context (classloader) is using.
30N/A */
30N/Apublic class RuntimeEnvironment {
58N/A private Configuration configuration;
58N/A private Map<Thread, Configuration> threadmap;
30N/A
30N/A private static RuntimeEnvironment instance = new RuntimeEnvironment();
30N/A
77N/A /**
77N/A * Get the one and only instance of the RuntimeEnvironment
77N/A * @return the one and only instance of the RuntimeEnvironment
77N/A */
30N/A public static RuntimeEnvironment getInstance() {
30N/A return instance;
30N/A }
30N/A
30N/A /**
77N/A * Creates a new instance of RuntimeEnvironment. Private to ensure a
77N/A * singleton pattern.
30N/A */
30N/A private RuntimeEnvironment() {
58N/A configuration = new Configuration();
58N/A threadmap = Collections.synchronizedMap(new HashMap<Thread, Configuration>());
58N/A }
58N/A
58N/A private Configuration getConfiguration() {
58N/A Configuration ret = configuration;
77N/A Configuration config = threadmap.get(Thread.currentThread());
77N/A
77N/A if (config != null) {
77N/A ret = config;
58N/A }
58N/A
58N/A return ret;
58N/A }
58N/A
77N/A /**
77N/A * Get the path to the where the index database is stored
77N/A * @return the path to the index database
77N/A */
58N/A public String getDataRootPath() {
58N/A return getConfiguration().getDataRoot();
58N/A }
58N/A
77N/A /**
77N/A * Get a file representing the index database
77N/A * @return the index database
77N/A */
30N/A public File getDataRootFile() {
58N/A File ret = null;
58N/A String file = getDataRootPath();
58N/A if (file != null) {
58N/A ret = new File(file);
58N/A }
58N/A
58N/A return ret;
30N/A }
58N/A
77N/A /**
77N/A * Set the path to where the index database is stored
77N/A * @param data the index database
77N/A * @throws IOException if the path cannot be resolved
77N/A */
77N/A public void setDataRoot(File data) throws IOException {
77N/A getConfiguration().setDataRoot(data.getCanonicalPath());
77N/A }
77N/A
77N/A /**
77N/A * Set the path to where the index database is stored
77N/A * @param dataRoot the index database
77N/A */
77N/A public void setDataRoot(String dataRoot) {
77N/A getConfiguration().setDataRoot(dataRoot);
77N/A }
77N/A
77N/A /**
77N/A * Get the path to where the sources are located
77N/A * @return path to where the sources are located
77N/A */
77N/A public String getSourceRootPath() {
77N/A return getConfiguration().getSourceRoot();
77N/A }
77N/A
77N/A /**
77N/A * Get a file representing the directory where the sources are located
77N/A * @return A file representing the directory where the sources are located
77N/A */
30N/A public File getSourceRootFile() {
58N/A File ret = null;
58N/A String file = getSourceRootPath();
58N/A if (file != null) {
58N/A ret = new File(file);
58N/A }
58N/A
58N/A return ret;
58N/A }
58N/A
77N/A /**
77N/A * Specify the source root
77N/A * @param source the location of the sources
77N/A * @throws IOException if the name cannot be resolved
77N/A */
125N/A public void setSourceRootFile(File source) throws IOException {
58N/A getConfiguration().setSourceRoot(source.getCanonicalPath());
58N/A }
58N/A
77N/A /**
77N/A * Specify the source root
77N/A * @param sourceRoot the location of the sources
77N/A */
58N/A public void setSourceRoot(String sourceRoot) {
58N/A getConfiguration().setSourceRoot(sourceRoot);
58N/A }
58N/A
77N/A /**
77N/A * Do we have projects?
77N/A * @return true if we have projects
77N/A */
58N/A public boolean hasProjects() {
58N/A List<Project> proj = getProjects();
58N/A return (proj != null && proj.size() > 0);
58N/A }
58N/A
77N/A /**
77N/A * Get all of the projects
77N/A * @return a list containing all of the projects (may be null)
77N/A */
58N/A public List<Project> getProjects() {
58N/A return getConfiguration().getProjects();
58N/A }
58N/A
77N/A /**
77N/A * Set the list of the projects
77N/A * @param projects the list of projects to use
77N/A */
58N/A public void setProjects(List<Project> projects) {
58N/A getConfiguration().setProjects(projects);
58N/A }
58N/A
77N/A /**
77N/A * Register this thread in the thread/configuration map (so that all
77N/A * subsequent calls to the RuntimeEnvironment from this thread will use
77N/A * the same configuration
77N/A */
58N/A public void register() {
58N/A threadmap.put(Thread.currentThread(), configuration);
58N/A }
58N/A
77N/A /**
77N/A * Get the context name of the web application
77N/A * @return the web applications context name
77N/A */
58N/A public String getUrlPrefix() {
58N/A return getConfiguration().getUrlPrefix();
58N/A }
58N/A
77N/A /**
77N/A * Set the web context name
77N/A * @param urlPrefix the web applications context name
77N/A */
58N/A public void setUrlPrefix(String urlPrefix) {
58N/A getConfiguration().setUrlPrefix(urlPrefix);
58N/A }
58N/A
77N/A /**
77N/A * Get the name of the ctags program in use
77N/A * @return the name of the ctags program in use
77N/A */
58N/A public String getCtags() {
58N/A return getConfiguration().getCtags();
58N/A }
58N/A
77N/A /**
77N/A * Specify the CTags program to use
77N/A * @param ctags the ctags program to use
77N/A */
58N/A public void setCtags(String ctags) {
58N/A getConfiguration().setCtags(ctags);
58N/A }
99N/A
99N/A /**
99N/A * Validate that I have a Exuberant ctags program I may use
99N/A * @return true if success, false otherwise
99N/A */
99N/A public boolean validateExuberantCtags() {
99N/A String ctags = getCtags();
99N/A
99N/A //Check if exub ctags is available
99N/A Process ctagsProcess = null;
99N/A try {
99N/A ctagsProcess = Runtime.getRuntime().exec(new String[] {ctags, "--version" });
99N/A } catch (Exception e) {
99N/A }
99N/A try {
99N/A BufferedReader cin = new BufferedReader(new InputStreamReader(ctagsProcess.getInputStream()));
99N/A String ctagOut;
99N/A if (!((ctagOut = cin.readLine()) != null && ctagOut.startsWith("Exuberant Ctags"))) {
99N/A System.err.println("Error: No Exuberant Ctags found in PATH!\n" +
99N/A "(tried running " + ctags + ")\n" +
99N/A "Please use option -c to specify path to a good Exuberant Ctags program");
99N/A return false;
99N/A }
99N/A } catch (Exception e) {
99N/A System.err.println("Error: executing " + ctags + "! " +e.getLocalizedMessage() +
99N/A "\nPlease use option -c to specify path to a good Exuberant Ctags program");
99N/A return false;
99N/A }
99N/A
99N/A // reap the child process..
99N/A try {
99N/A int ret;
99N/A if ((ret = ctagsProcess.exitValue()) != 0) {
99N/A System.err.println("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
77N/A /**
77N/A * Get the max time a SMC operation may use to avoid beeing cached
77N/A * @return the max time
77N/A */
58N/A public int getHistoryReaderTimeLimit() {
58N/A return getConfiguration().getHistoryCacheTime();
58N/A }
58N/A
77N/A /**
77N/A * Specify the maximum time a SCM operation should take before it will
77N/A * be cached (in ms)
77N/A * @param historyReaderTimeLimit the max time in ms before it is cached
77N/A */
58N/A public void setHistoryReaderTimeLimit(int historyReaderTimeLimit) {
58N/A getConfiguration().setHistoryCacheTime(historyReaderTimeLimit);
30N/A }
58N/A
77N/A /**
77N/A * Is history cache currently enabled?
77N/A * @return true if history cache is enabled
77N/A */
58N/A public boolean useHistoryCache() {
58N/A return getConfiguration().isHistoryCache();
58N/A }
58N/A
77N/A /**
77N/A * Specify if we should use history cache or not
77N/A * @param useHistoryCache set false if you do not want to use history cache
77N/A */
58N/A public void setUseHistoryCache(boolean useHistoryCache) {
58N/A getConfiguration().setHistoryCache(useHistoryCache);
58N/A }
77N/A
77N/A /**
77N/A * Should we generate HTML or not during the indexing phase
77N/A * @return true if HTML should be generated during the indexing phase
77N/A */
65N/A public boolean isGenerateHtml() {
65N/A return getConfiguration().isGenerateHtml();
65N/A }
65N/A
77N/A /**
77N/A * Specify if we should generate HTML or not during the indexing phase
77N/A * @param generateHtml set this to true to pregenerate HTML
77N/A */
65N/A public void setGenerateHtml(boolean generateHtml) {
65N/A getConfiguration().setGenerateHtml(generateHtml);
65N/A }
58N/A
106N/A public boolean isQuickContextScan() {
106N/A return getConfiguration().isQuickContextScan();
106N/A }
106N/A
106N/A public void setQuickContextScan(boolean quickContextScan) {
106N/A getConfiguration().setQuickContextScan(quickContextScan);
106N/A }
106N/A
77N/A /**
77N/A * Get the map of external SCM repositories available
77N/A * @return A map containing all available SCMs
77N/A */
58N/A public Map<String, ExternalRepository> getRepositories() {
58N/A return getConfiguration().getRepositories();
58N/A }
58N/A
77N/A /**
77N/A * Set the map of external SCM repositories
77N/A * @param repositories the repositories to use
77N/A */
58N/A public void setRepositories(Map<String, ExternalRepository> repositories) {
58N/A getConfiguration().setRepositories(repositories);
58N/A }
58N/A
77N/A /**
77N/A * Set the project that is specified to be the default project to use. The
77N/A * default project is the project you will search (from the web application)
77N/A * if the page request didn't contain the cookie..
77N/A * @param defaultProject The default project to use
77N/A */
77N/A public void setDefaultProject(Project defaultProject) {
77N/A getConfiguration().setDefaultProject(defaultProject);
58N/A }
58N/A
77N/A /**
77N/A * Get the project that is specified to be the default project to use. The
77N/A * default project is the project you will search (from the web application)
77N/A * if the page request didn't contain the cookie..
77N/A * @return the default project (may be null if not specified)
77N/A */
77N/A public Project getDefaultProject() {
77N/A return getConfiguration().getDefaultProject();
77N/A }
77N/A
77N/A /**
99N/A * Chandan wrote the following answer on the opengrok-discuss list:
99N/A * "Traditionally search engines (specially spiders) think that large files
99N/A * are junk. Large files tend to be multimedia files etc., which text
99N/A * search spiders do not want to chew. So they ignore the contents of
99N/A * the file after a cutoff length. Lucene does this by number of words,
99N/A * which is by default is 10,000."
99N/A * By default OpenGrok will increase this limit to 60000, but it may be
99N/A * overridden in the configuration file
99N/A * @return The maximum words to index
99N/A */
99N/A public int getIndexWordLimit() {
99N/A return getConfiguration().getIndexWordLimit();
99N/A }
99N/A
124N/A /**
124N/A * Set the number of words in a file Lucene will index.
124N/A * See getIndexWordLimit for a better description.
124N/A * @param indexWordLimit the number of words to index in a single file
124N/A */
99N/A public void setIndexWordLimit(int indexWordLimit) {
99N/A getConfiguration().setIndexWordLimit(indexWordLimit);
99N/A }
99N/A
124N/A /**
124N/A * Is the verbosity flag turned on?
124N/A * @return true if we can print extra information
124N/A */
99N/A public boolean isVerbose() {
99N/A return getConfiguration().isVerbose();
99N/A }
124N/A
124N/A /**
124N/A * Set the verbosity flag (to add extra debug information in output)
124N/A * @param verbose new value
124N/A */
99N/A public void setVerbose(boolean verbose) {
99N/A getConfiguration().setVerbose(verbose);
99N/A }
124N/A
125N/A /**
125N/A * Specify if a search may start with a wildcard. Note that queries
125N/A * that start with a wildcard will give a significant impact on the
125N/A * search performace.
125N/A * @param allowLeadingWildcard set to true to activate (disabled by default)
125N/A */
125N/A public void setAllowLeadingWildcard(boolean allowLeadingWildcard) {
125N/A getConfiguration().setAllowLeadingWildcard(allowLeadingWildcard);
125N/A }
125N/A
125N/A /**
125N/A * Is leading wildcards allowed?
125N/A * @return true if a search may start with a wildcard
125N/A */
125N/A public boolean isAllowLeadingWildcard() {
125N/A return getConfiguration().isAllowLeadingWildcard();
125N/A }
124N/A
112N/A public IgnoredNames getIgnoredNames() {
112N/A return getConfiguration().getIgnoredNames();
112N/A }
112N/A
112N/A public void setIgnoredNames(IgnoredNames ignoredNames) {
112N/A getConfiguration().setIgnoredNames(ignoredNames);
112N/A }
99N/A
99N/A /**
77N/A * Read an configuration file and set it as the current configuration.
77N/A * @param file the file to read
77N/A * @throws IOException if an error occurs
77N/A */
58N/A public void readConfiguration(File file) throws IOException {
58N/A XMLDecoder d = new XMLDecoder(
58N/A new BufferedInputStream(new FileInputStream(file)));
58N/A Object obj = d.readObject();
58N/A d.close();
58N/A
58N/A if (obj instanceof Configuration) {
58N/A configuration = (Configuration)obj;
58N/A System.out.println("Config file " + file.getName() + " successfully read");
58N/A } else {
58N/A throw new IOException("Not a valid config file");
58N/A }
58N/A }
58N/A
77N/A /**
77N/A * Write the current configuration to a file
77N/A * @param file the file to write the configuration into
77N/A * @throws IOException if an error occurs
77N/A */
58N/A public void writeConfiguration(File file) throws IOException {
58N/A XMLEncoder e = new XMLEncoder(
58N/A new BufferedOutputStream(new FileOutputStream(file)));
58N/A e.writeObject(getConfiguration());
58N/A e.close();
58N/A }
58N/A
77N/A /**
77N/A * Write the current configuration to a socket
77N/A * @param host the host address to receive the configuration
77N/A * @param port the port to use on the host
77N/A * @throws IOException if an error occurs
77N/A */
58N/A public void writeConfiguration(InetAddress host, int port) throws IOException {
58N/A Socket sock = new Socket(host, port);
58N/A XMLEncoder e = new XMLEncoder(sock.getOutputStream());
58N/A e.writeObject(getConfiguration());
58N/A e.close();
58N/A try {
58N/A sock.close();
58N/A } catch (Exception ex) {
58N/A ;
58N/A }
58N/A }
58N/A
58N/A private Thread configurationListenerThread;
58N/A private ServerSocket configServerSocket;
58N/A
77N/A /**
77N/A * Try to stop the configuration listener thread
77N/A */
58N/A public void stopConfigurationListenerThread() {
58N/A try {
58N/A configServerSocket.close();
77N/A } catch (Exception e) {}
30N/A }
77N/A
77N/A /**
77N/A * Start a thread to listen on a socket to receive new configurations
77N/A * to use.
77N/A * @param endpoint The socket address to listen on
77N/A * @return true if the endpoint was available (and the thread was started)
77N/A */
58N/A public boolean startConfigurationListenerThread(SocketAddress endpoint) {
58N/A boolean ret = false;
58N/A
58N/A try {
58N/A configServerSocket = new ServerSocket();
58N/A configServerSocket.bind(endpoint);
58N/A ret = true;
58N/A final ServerSocket sock = configServerSocket;
58N/A Thread t = new Thread(new Runnable() {
58N/A public void run() {
58N/A Socket s = null;
58N/A while (!sock.isClosed()) {
58N/A try {
58N/A System.out.flush();
58N/A s = sock.accept();
58N/A System.out.println((new Date()).toString() + " OpenGrok: Got request from " + s.getInetAddress().getHostAddress());
58N/A String line;
58N/A BufferedInputStream in = new BufferedInputStream(s.getInputStream());
58N/A
58N/A XMLDecoder d = new XMLDecoder(new BufferedInputStream(in));
58N/A Object obj = d.readObject();
58N/A d.close();
58N/A
58N/A if (obj instanceof Configuration) {
58N/A configuration = (Configuration)obj;
58N/A System.out.println("Configuration updated: " + configuration.getSourceRoot());
58N/A System.out.flush();
58N/A }
58N/A } catch (IOException e) {
58N/A e.printStackTrace();
58N/A } finally {
58N/A try { s.close(); } catch (Exception ex) { }
58N/A }
58N/A }
58N/A }
58N/A });
58N/A t.start();
58N/A } catch (UnknownHostException ex) {
58N/A ex.printStackTrace();
58N/A } catch (IOException ex) {
58N/A ex.printStackTrace();
58N/A }
58N/A
58N/A if (!ret && configServerSocket != null) {
58N/A try {
58N/A configServerSocket.close();
58N/A } catch (IOException ex) {
58N/A ;
58N/A }
58N/A }
58N/A
58N/A return ret;
30N/A }
30N/A}