RuntimeEnvironment.java revision 77
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/*
1291N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
1356N/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.BufferedOutputStream;
1287N/Aimport java.io.File;
639N/Aimport java.io.FileInputStream;
639N/Aimport java.io.FileOutputStream;
234N/Aimport java.io.IOException;
234N/Aimport java.net.InetAddress;
234N/Aimport java.net.ServerSocket;
1289N/Aimport java.net.Socket;
234N/Aimport java.net.SocketAddress;
639N/Aimport java.net.UnknownHostException;
639N/Aimport java.util.Collections;
58N/Aimport java.util.Date;
1185N/Aimport java.util.HashMap;
667N/Aimport java.util.List;
1185N/Aimport java.util.Map;
1016N/Aimport org.opensolaris.opengrok.history.ExternalRepository;
58N/A
1185N/A/**
1016N/A * The RuntimeEnvironment class is used as a placeholder for the current
1436N/A * configuration this execution context (classloader) is using.
1185N/A */
664N/Apublic class RuntimeEnvironment {
1026N/A private Configuration configuration;
112N/A private Map<Thread, Configuration> threadmap;
1195N/A
1419N/A private static RuntimeEnvironment instance = new RuntimeEnvironment();
58N/A
58N/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 */
77N/A public static RuntimeEnvironment getInstance() {
58N/A return instance;
418N/A }
1462N/A
1462N/A /**
1327N/A * Creates a new instance of RuntimeEnvironment. Private to ensure a
1327N/A * singleton pattern.
1462N/A */
1327N/A private RuntimeEnvironment() {
1327N/A configuration = new Configuration();
1327N/A threadmap = Collections.synchronizedMap(new HashMap<Thread, Configuration>());
1356N/A }
1356N/A
1356N/A private Configuration getConfiguration() {
58N/A Configuration ret = configuration;
1436N/A Configuration config = threadmap.get(Thread.currentThread());
1436N/A
1436N/A if (config != null) {
58N/A ret = config;
773N/A }
773N/A
773N/A return ret;
773N/A }
58N/A
1436N/A /**
1436N/A * Get the path to the where the index database is stored
1436N/A * @return the path to the index database
773N/A */
773N/A public String getDataRootPath() {
58N/A return getConfiguration().getDataRoot();
58N/A }
58N/A
664N/A /**
58N/A * Get a file representing the index database
65N/A * @return the index database
1436N/A */
1436N/A public File getDataRootFile() {
1436N/A File ret = null;
1436N/A String file = getDataRootPath();
1436N/A if (file != null) {
77N/A ret = new File(file);
99N/A }
99N/A
1115N/A return ret;
1115N/A }
125N/A
112N/A /**
1026N/A * Set the path to where the index database is stored
129N/A * @param data the index database
1100N/A * @throws IOException if the path cannot be resolved
129N/A */
129N/A public void setDataRoot(File data) throws IOException {
318N/A getConfiguration().setDataRoot(data.getCanonicalPath());
318N/A }
144N/A
173N/A /**
253N/A * Set the path to where the index database is stored
296N/A * @param dataRoot the index database
335N/A */
480N/A public void setDataRoot(String dataRoot) {
816N/A getConfiguration().setDataRoot(dataRoot);
816N/A }
833N/A
833N/A /**
1416N/A * Get the path to where the sources are located
1185N/A * @return path to where the sources are located
1016N/A */
1123N/A public String getSourceRootPath() {
1125N/A return getConfiguration().getSourceRoot();
1218N/A }
1185N/A
1326N/A /**
993N/A * Get a file representing the directory where the sources are located
1185N/A * @return A file representing the directory where the sources are located
1185N/A */
1190N/A public File getSourceRootFile() {
1436N/A File ret = null;
1185N/A String file = getSourceRootPath();
1185N/A if (file != null) {
1252N/A ret = new File(file);
1185N/A }
1185N/A
1185N/A return ret;
1185N/A }
1185N/A
1185N/A /**
1185N/A * Specify the source root
1185N/A * @param source the location of the sources
1436N/A * @throws IOException if the name cannot be resolved
1185N/A */
1185N/A public void setSourceRoot(File source) throws IOException {
1252N/A getConfiguration().setSourceRoot(source.getCanonicalPath());
1185N/A }
1185N/A
1185N/A /**
1185N/A * Specify the source root
1185N/A * @param sourceRoot the location of the sources
1461N/A */
1461N/A public void setSourceRoot(String sourceRoot) {
1461N/A getConfiguration().setSourceRoot(sourceRoot);
1461N/A }
1461N/A
1185N/A /**
993N/A * Do we have projects?
993N/A * @return true if we have projects
993N/A */
1461N/A public boolean hasProjects() {
1461N/A List<Project> proj = getProjects();
1461N/A return (proj != null && proj.size() > 0);
1461N/A }
1461N/A
1185N/A /**
993N/A * Get all of the projects
993N/A * @return a list containing all of the projects (may be null)
937N/A */
1436N/A public List<Project> getProjects() {
1436N/A return getConfiguration().getProjects();
1436N/A }
58N/A
816N/A /**
58N/A * Set the list of the projects
58N/A * @param projects the list of projects to use
773N/A */
58N/A public void setProjects(List<Project> projects) {
664N/A getConfiguration().setProjects(projects);
1419N/A }
1419N/A
1419N/A /**
1327N/A * Register this thread in the thread/configuration map (so that all
870N/A * subsequent calls to the RuntimeEnvironment from this thread will use
870N/A * the same configuration
99N/A */
1115N/A public void register() {
101N/A threadmap.put(Thread.currentThread(), configuration);
106N/A }
112N/A
1026N/A /**
129N/A * Get the context name of the web application
129N/A * @return the web applications context name
129N/A */
875N/A public String getUrlPrefix() {
318N/A return getConfiguration().getUrlPrefix();
1356N/A }
173N/A
253N/A /**
296N/A * Set the web context name
335N/A * @param urlPrefix the web applications context name
480N/A */
816N/A public void setUrlPrefix(String urlPrefix) {
816N/A getConfiguration().setUrlPrefix(urlPrefix);
993N/A }
1016N/A
1315N/A /**
1185N/A * Get the name of the ctags program in use
58N/A * @return the name of the ctags program in use
937N/A */
1461N/A public String getCtags() {
1461N/A return getConfiguration().getCtags();
1461N/A }
1461N/A
1461N/A /**
1461N/A * Specify the CTags program to use
1185N/A * @param ctags the ctags program to use
1185N/A */
1185N/A public void setCtags(String ctags) {
1190N/A getConfiguration().setCtags(ctags);
1461N/A }
1461N/A
1461N/A /**
1461N/A * Get the max time a SMC operation may use to avoid beeing cached
1461N/A * @return the max time
1461N/A */
1461N/A public int getHistoryReaderTimeLimit() {
1461N/A return getConfiguration().getHistoryCacheTime();
1461N/A }
1185N/A
1185N/A /**
1185N/A * Specify the maximum time a SCM operation should take before it will
1185N/A * be cached (in ms)
1185N/A * @param historyReaderTimeLimit the max time in ms before it is cached
1185N/A */
1185N/A public void setHistoryReaderTimeLimit(int historyReaderTimeLimit) {
1185N/A getConfiguration().setHistoryCacheTime(historyReaderTimeLimit);
1185N/A }
1185N/A
1461N/A /**
1461N/A * Is history cache currently enabled?
1461N/A * @return true if history cache is enabled
1461N/A */
1461N/A public boolean useHistoryCache() {
1461N/A return getConfiguration().isHistoryCache();
1185N/A }
1185N/A
1185N/A /**
1190N/A * Specify if we should use history cache or not
1461N/A * @param useHistoryCache set false if you do not want to use history cache
1461N/A */
1461N/A public void setUseHistoryCache(boolean useHistoryCache) {
1461N/A getConfiguration().setHistoryCache(useHistoryCache);
1461N/A }
1461N/A
1185N/A /**
1185N/A * Should we generate HTML or not during the indexing phase
1185N/A * @return true if HTML should be generated during the indexing phase
1185N/A */
1190N/A public boolean isGenerateHtml() {
1461N/A return getConfiguration().isGenerateHtml();
1461N/A }
1461N/A
1461N/A /**
58N/A * Specify if we should generate HTML or not during the indexing phase
58N/A * @param generateHtml set this to true to pregenerate HTML
58N/A */
937N/A public void setGenerateHtml(boolean generateHtml) {
1461N/A getConfiguration().setGenerateHtml(generateHtml);
1461N/A }
1461N/A
1461N/A /**
58N/A * Get the map of external SCM repositories available
58N/A * @return A map containing all available SCMs
58N/A */
937N/A public Map<String, ExternalRepository> getRepositories() {
1461N/A return getConfiguration().getRepositories();
1461N/A }
1461N/A
1461N/A /**
1461N/A * Set the map of external SCM repositories
1461N/A * @param repositories the repositories to use
816N/A */
816N/A public void setRepositories(Map<String, ExternalRepository> repositories) {
816N/A getConfiguration().setRepositories(repositories);
816N/A }
1461N/A
1461N/A /**
1461N/A * Set the project that is specified to be the default project to use. The
1461N/A * default project is the project you will search (from the web application)
1461N/A * if the page request didn't contain the cookie..
1461N/A * @param defaultProject The default project to use
816N/A */
816N/A public void setDefaultProject(Project defaultProject) {
816N/A getConfiguration().setDefaultProject(defaultProject);
816N/A }
1461N/A
1461N/A /**
1461N/A * Get the project that is specified to be the default project to use. The
1461N/A * default project is the project you will search (from the web application)
816N/A * if the page request didn't contain the cookie..
816N/A * @return the default project (may be null if not specified)
816N/A */
816N/A public Project getDefaultProject() {
1461N/A return getConfiguration().getDefaultProject();
1461N/A }
1461N/A
1461N/A /**
816N/A * Read an configuration file and set it as the current configuration.
816N/A * @param file the file to read
816N/A * @throws IOException if an error occurs
773N/A */
773N/A public void readConfiguration(File file) throws IOException {
773N/A XMLDecoder d = new XMLDecoder(
1436N/A new BufferedInputStream(new FileInputStream(file)));
1436N/A Object obj = d.readObject();
1436N/A d.close();
773N/A
58N/A if (obj instanceof Configuration) {
58N/A configuration = (Configuration)obj;
58N/A System.out.println("Config file " + file.getName() + " successfully read");
773N/A } else {
773N/A throw new IOException("Not a valid config file");
773N/A }
1436N/A }
773N/A
773N/A /**
58N/A * Write the current configuration to a file
58N/A * @param file the file to write the configuration into
58N/A * @throws IOException if an error occurs
773N/A */
773N/A public void writeConfiguration(File file) throws IOException {
1436N/A XMLEncoder e = new XMLEncoder(
1436N/A new BufferedOutputStream(new FileOutputStream(file)));
773N/A e.writeObject(getConfiguration());
773N/A e.close();
773N/A }
773N/A
773N/A /**
58N/A * Write the current configuration to a socket
58N/A * @param host the host address to receive the configuration
58N/A * @param port the port to use on the host
773N/A * @throws IOException if an error occurs
773N/A */
1436N/A public void writeConfiguration(InetAddress host, int port) throws IOException {
1436N/A Socket sock = new Socket(host, port);
773N/A XMLEncoder e = new XMLEncoder(sock.getOutputStream());
773N/A e.writeObject(getConfiguration());
773N/A e.close();
58N/A try {
58N/A sock.close();
58N/A } catch (Exception ex) {
773N/A ;
773N/A }
773N/A }
773N/A
773N/A private Thread configurationListenerThread;
773N/A private ServerSocket configServerSocket;
773N/A
773N/A /**
773N/A * Try to stop the configuration listener thread
773N/A */
773N/A public void stopConfigurationListenerThread() {
773N/A try {
773N/A configServerSocket.close();
773N/A } catch (Exception e) {}
773N/A }
773N/A
773N/A /**
1436N/A * Start a thread to listen on a socket to receive new configurations
1436N/A * to use.
773N/A * @param endpoint The socket address to listen on
773N/A * @return true if the endpoint was available (and the thread was started)
773N/A */
773N/A public boolean startConfigurationListenerThread(SocketAddress endpoint) {
937N/A boolean ret = false;
1461N/A
1461N/A try {
1461N/A configServerSocket = new ServerSocket();
1461N/A configServerSocket.bind(endpoint);
58N/A ret = true;
58N/A final ServerSocket sock = configServerSocket;
58N/A Thread t = new Thread(new Runnable() {
937N/A public void run() {
1461N/A Socket s = null;
1461N/A while (!sock.isClosed()) {
1461N/A try {
1461N/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;
937N/A BufferedInputStream in = new BufferedInputStream(s.getInputStream());
1461N/A
1461N/A XMLDecoder d = new XMLDecoder(new BufferedInputStream(in));
1461N/A Object obj = d.readObject();
1461N/A d.close();
58N/A
58N/A if (obj instanceof Configuration) {
58N/A configuration = (Configuration)obj;
937N/A System.out.println("Configuration updated: " + configuration.getSourceRoot());
1461N/A System.out.flush();
1461N/A }
1461N/A } catch (IOException e) {
1461N/A e.printStackTrace();
58N/A } finally {
58N/A try { s.close(); } catch (Exception ex) { }
58N/A }
937N/A }
1461N/A }
1461N/A });
1461N/A t.start();
1461N/A } catch (UnknownHostException ex) {
58N/A ex.printStackTrace();
58N/A } catch (IOException ex) {
58N/A ex.printStackTrace();
937N/A }
1461N/A
1461N/A if (!ret && configServerSocket != null) {
1461N/A try {
1461N/A configServerSocket.close();
58N/A } catch (IOException ex) {
58N/A ;
58N/A }
937N/A }
1461N/A
1461N/A return ret;
1461N/A }
1461N/A}
664N/A