RuntimeEnvironment.java revision 58
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;
58N/Aimport java.io.BufferedReader;
30N/Aimport java.io.File;
58N/Aimport java.io.FileInputStream;
58N/Aimport java.io.FileOutputStream;
30N/Aimport java.io.IOException;
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;
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 boolean threadLocalConfig;
30N/A
58N/A private Configuration configuration;
58N/A private Map<Thread, Configuration> threadmap;
30N/A
30N/A private static RuntimeEnvironment instance = new RuntimeEnvironment();
30N/A
30N/A public static RuntimeEnvironment getInstance() {
30N/A return instance;
30N/A }
30N/A
30N/A /**
30N/A * Creates a new instance of RuntimeEnvironment
30N/A */
30N/A private RuntimeEnvironment() {
58N/A threadLocalConfig = false;
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;
58N/A
58N/A if (threadLocalConfig) {
58N/A Configuration config = threadmap.get(Thread.currentThread());
58N/A
58N/A if (config != null) {
58N/A ret = config;
58N/A }
58N/A }
58N/A
58N/A return ret;
58N/A }
58N/A
58N/A public String getDataRootPath() {
58N/A return getConfiguration().getDataRoot();
58N/A }
58N/A
58N/A public String getSourceRootPath() {
58N/A return getConfiguration().getSourceRoot();
58N/A }
30N/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
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
58N/A public void setDataRoot(File data) throws IOException {
58N/A getConfiguration().setDataRoot(data.getCanonicalPath());
58N/A }
58N/A
58N/A public void setDataRoot(String dataRoot) {
58N/A getConfiguration().setDataRoot(dataRoot);
58N/A }
58N/A
58N/A public void setSourceRoot(File source) throws IOException {
58N/A getConfiguration().setSourceRoot(source.getCanonicalPath());
58N/A }
58N/A
58N/A public void setSourceRoot(String sourceRoot) {
58N/A getConfiguration().setSourceRoot(sourceRoot);
58N/A }
58N/A
58N/A public boolean hasProjects() {
58N/A List<Project> proj = getProjects();
58N/A return (proj != null && proj.size() > 0);
58N/A }
58N/A
58N/A public List<Project> getProjects() {
58N/A return getConfiguration().getProjects();
58N/A }
58N/A
58N/A public void setProjects(List<Project> projects) {
58N/A getConfiguration().setProjects(projects);
58N/A }
58N/A
58N/A public void register() {
58N/A threadmap.put(Thread.currentThread(), configuration);
58N/A }
58N/A
58N/A public String getUrlPrefix() {
58N/A return getConfiguration().getUrlPrefix();
58N/A }
58N/A
58N/A public void setUrlPrefix(String urlPrefix) {
58N/A getConfiguration().setUrlPrefix(urlPrefix);
58N/A }
58N/A
58N/A public String getCtags() {
58N/A return getConfiguration().getCtags();
58N/A }
58N/A
58N/A public void setCtags(String ctags) {
58N/A getConfiguration().setCtags(ctags);
58N/A }
58N/A
58N/A public int getHistoryReaderTimeLimit() {
58N/A return getConfiguration().getHistoryCacheTime();
58N/A }
58N/A
58N/A public void setHistoryReaderTimeLimit(int historyReaderTimeLimit) {
58N/A getConfiguration().setHistoryCacheTime(historyReaderTimeLimit);
30N/A }
58N/A
58N/A public boolean useHistoryCache() {
58N/A return getConfiguration().isHistoryCache();
58N/A }
58N/A
58N/A public void setUseHistoryCache(boolean useHistoryCache) {
58N/A getConfiguration().setHistoryCache(useHistoryCache);
58N/A }
58N/A
58N/A public void setThreadLocalConfiguration(boolean tls) {
58N/A threadLocalConfig = tls;
58N/A }
58N/A
58N/A public Map<String, ExternalRepository> getRepositories() {
58N/A return getConfiguration().getRepositories();
58N/A }
58N/A
58N/A public void setRepositories(Map<String, ExternalRepository> repositories) {
58N/A getConfiguration().setRepositories(repositories);
58N/A }
58N/A
58N/A public Map<Thread, Configuration> getThreadmap() {
58N/A return threadmap;
58N/A }
58N/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
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
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
58N/A public void stopConfigurationListenerThread() {
58N/A try {
58N/A configServerSocket.close();
58N/A } catch (Exception e) {}
30N/A }
30N/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 threadLocalConfig = 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}