Configuration.java revision 833
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 2007 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.BufferedOutputStream;
639N/Aimport java.io.ByteArrayInputStream;
639N/Aimport java.io.ByteArrayOutputStream;
234N/Aimport java.io.File;
234N/Aimport java.io.FileInputStream;
234N/Aimport java.io.FileOutputStream;
234N/Aimport java.io.IOException;
639N/Aimport java.io.InputStream;
639N/Aimport java.io.OutputStream;
58N/Aimport java.util.ArrayList;
667N/Aimport java.util.Date;
58N/Aimport java.util.List;
664N/Aimport org.opensolaris.opengrok.history.RepositoryInfo;
112N/Aimport org.opensolaris.opengrok.index.IgnoredNames;
58N/A
58N/A/**
77N/A * Placeholder class for all configuration variables. Due to the multithreaded
77N/A * nature of the web application, each thread will use the same instance of the
77N/A * configuration object for each page request. Class and methods should have
77N/A * package scope, but that didn't work with the XMLDecoder/XMLEncoder.
58N/A */
418N/Apublic final class Configuration {
58N/A private String ctags;
773N/A
773N/A /** Should the history log be cached? */
58N/A private boolean historyCache;
773N/A /**
773N/A * The maximum time in milliseconds {@code HistoryCache.get()} can take
773N/A * before its result is cached.
773N/A */
58N/A private int historyCacheTime;
773N/A
773N/A /** Should the history cache be stored in a database? */
773N/A private boolean historyCacheInDB;
773N/A
58N/A private List<Project> projects;
58N/A private String sourceRoot;
58N/A private String dataRoot;
664N/A private List<RepositoryInfo> repositories;
58N/A private String urlPrefix;
65N/A private boolean generateHtml;
77N/A private Project defaultProject;
99N/A private int indexWordLimit;
99N/A private boolean verbose;
125N/A private boolean allowLeadingWildcard;
112N/A private IgnoredNames ignoredNames;
129N/A private String userPage;
129N/A private String bugPage;
129N/A private String bugPattern;
318N/A private String reviewPage;
318N/A private String reviewPattern;
144N/A private String webappLAF;
173N/A private boolean remoteScmSupported;
253N/A private boolean optimizeDatabase;
296N/A private boolean useLuceneLocking;
335N/A private boolean compressXref;
480N/A private boolean indexVersionedFilesOnly;
816N/A private int hitsPerPage;
816N/A private int cachePages;
833N/A private String databaseDriver;
833N/A private String databaseUrl;
816N/A
58N/A /** Creates a new instance of Configuration */
58N/A public Configuration() {
816N/A //defaults for an opengrok instance configuration
58N/A setHistoryCache(true);
58N/A setHistoryCacheTime(30);
773N/A setHistoryCacheInDB(false);
58N/A setProjects(new ArrayList<Project>());
664N/A setRepositories(new ArrayList<RepositoryInfo>());
58N/A setUrlPrefix("/source/s?");
99N/A setCtags("ctags");
99N/A setIndexWordLimit(60000);
99N/A setVerbose(false);
101N/A setGenerateHtml(true);
106N/A setQuickContextScan(true);
112N/A setIgnoredNames(new IgnoredNames());
129N/A setUserPage("http://www.opensolaris.org/viewProfile.jspa?username=");
129N/A setBugPage("http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=");
129N/A setBugPattern("\\b([12456789][0-9]{6})\\b");
318N/A setReviewPage("http://www.opensolaris.org/os/community/arc/caselog/");
318N/A setReviewPattern("\\b(\\d{4}/\\d{3})\\b"); // in form e.g. PSARC 2008/305
144N/A setWebappLAF("default");
173N/A setRemoteScmSupported(false);
253N/A setOptimizeDatabase(true);
296N/A setUsingLuceneLocking(false);
335N/A setCompressXref(true);
480N/A setIndexVersionedFilesOnly(false);
816N/A setHitsPerPage(25);
816N/A setCachePages(5);
833N/A setDatabaseDriver("org.apache.derby.jdbc.EmbeddedDriver");
58N/A }
58N/A
58N/A public String getCtags() {
58N/A return ctags;
58N/A }
58N/A
58N/A public void setCtags(String ctags) {
58N/A this.ctags = ctags;
58N/A }
816N/A
816N/A public int getCachePages() {
816N/A return cachePages;
816N/A }
816N/A
816N/A public void setCachePages(int cachePages) {
816N/A this.cachePages = cachePages;
816N/A }
816N/A
816N/A public int getHitsPerPage() {
816N/A return hitsPerPage;
816N/A }
816N/A
816N/A public void setHitsPerPage(int hitsPerPage) {
816N/A this.hitsPerPage = hitsPerPage;
816N/A }
773N/A
773N/A /**
773N/A * Should the history log be cached?
773N/A * @return {@code true} if a {@code HistoryCache} implementation should
773N/A * be used, {@code false} otherwise
773N/A */
58N/A public boolean isHistoryCache() {
58N/A return historyCache;
58N/A }
773N/A
773N/A /**
773N/A * Set whether history should be cached.
773N/A * @param historyCache if {@code true} enable history cache
773N/A */
58N/A public void setHistoryCache(boolean historyCache) {
58N/A this.historyCache = historyCache;
58N/A }
773N/A
773N/A /**
773N/A * How long can a history request take before it's cached? If the time
773N/A * is exceeded, the result is cached. This setting only affects
773N/A * {@code FileHistoryCache}.
773N/A *
773N/A * @return the maximum time in milliseconds a history request can take
773N/A * before it's cached
773N/A */
58N/A public int getHistoryCacheTime() {
58N/A return historyCacheTime;
58N/A }
773N/A
773N/A /**
773N/A * Set the maximum time a history request can take before it's cached.
773N/A * This setting is only respected if {@code FileHistoryCache} is used.
773N/A *
773N/A * @param historyCacheTime maximum time in milliseconds
773N/A */
58N/A public void setHistoryCacheTime(int historyCacheTime) {
58N/A this.historyCacheTime = historyCacheTime;
58N/A }
773N/A
773N/A /**
773N/A * Should the history cache be stored in a database? If yes,
773N/A * {@code JDBCHistoryCache} will be used to cache the history; otherwise,
773N/A * {@code FileHistoryCache} is used.
773N/A *
773N/A * @return whether the history cache should be stored in a database
773N/A */
773N/A public boolean isHistoryCacheInDB() {
773N/A return historyCacheInDB;
773N/A }
773N/A
773N/A /**
773N/A * Set whether the history cache should be stored in a database, and
773N/A * {@code JDBCHistoryCache} should be used instead of {@code
773N/A * FileHistoryCache}.
773N/A *
773N/A * @param historyCacheInDB whether the history cached should be stored in
773N/A * a database
773N/A */
773N/A public void setHistoryCacheInDB(boolean historyCacheInDB) {
773N/A this.historyCacheInDB = historyCacheInDB;
773N/A }
58N/A
58N/A public List<Project> getProjects() {
58N/A return projects;
58N/A }
58N/A
58N/A public void setProjects(List<Project> projects) {
58N/A this.projects = projects;
58N/A }
58N/A
58N/A public String getSourceRoot() {
58N/A return sourceRoot;
58N/A }
58N/A
58N/A public void setSourceRoot(String sourceRoot) {
58N/A this.sourceRoot = sourceRoot;
58N/A }
58N/A
58N/A public String getDataRoot() {
58N/A return dataRoot;
58N/A }
58N/A
58N/A public void setDataRoot(String dataRoot) {
58N/A this.dataRoot = dataRoot;
58N/A }
58N/A
664N/A public List<RepositoryInfo> getRepositories() {
58N/A return repositories;
58N/A }
58N/A
664N/A public void setRepositories(List<RepositoryInfo> repositories) {
58N/A this.repositories = repositories;
58N/A }
58N/A
58N/A public String getUrlPrefix() {
58N/A return urlPrefix;
58N/A }
58N/A
58N/A public void setUrlPrefix(String urlPrefix) {
58N/A this.urlPrefix = urlPrefix;
58N/A }
77N/A
65N/A public void setGenerateHtml(boolean generateHtml) {
65N/A this.generateHtml = generateHtml;
65N/A }
65N/A
65N/A public boolean isGenerateHtml() {
65N/A return generateHtml;
65N/A }
77N/A
77N/A public void setDefaultProject(Project defaultProject) {
77N/A this.defaultProject = defaultProject;
77N/A }
77N/A
77N/A public Project getDefaultProject() {
77N/A return defaultProject;
77N/A }
99N/A
99N/A public int getIndexWordLimit() {
99N/A return indexWordLimit;
99N/A }
99N/A
99N/A public void setIndexWordLimit(int indexWordLimit) {
99N/A this.indexWordLimit = indexWordLimit;
99N/A }
99N/A
99N/A public boolean isVerbose() {
99N/A return verbose;
99N/A }
99N/A
99N/A public void setVerbose(boolean verbose) {
99N/A this.verbose = verbose;
99N/A }
125N/A
125N/A public void setAllowLeadingWildcard(boolean allowLeadingWildcard) {
125N/A this.allowLeadingWildcard = allowLeadingWildcard;
125N/A }
125N/A
125N/A public boolean isAllowLeadingWildcard() {
125N/A return allowLeadingWildcard;
125N/A }
106N/A
106N/A private boolean quickContextScan;
106N/A
106N/A public boolean isQuickContextScan() {
106N/A return quickContextScan;
106N/A }
106N/A
106N/A public void setQuickContextScan(boolean quickContextScan) {
106N/A this.quickContextScan = quickContextScan;
106N/A }
112N/A
112N/A public void setIgnoredNames(IgnoredNames ignoredNames) {
112N/A this.ignoredNames = ignoredNames;
112N/A }
112N/A
112N/A public IgnoredNames getIgnoredNames() {
112N/A return ignoredNames;
112N/A }
129N/A
129N/A public void setUserPage(String userPage) {
129N/A this.userPage = userPage;
129N/A }
129N/A
129N/A public String getUserPage() {
129N/A return userPage;
129N/A }
129N/A
129N/A public void setBugPage(String bugPage) {
129N/A this.bugPage = bugPage;
129N/A }
129N/A
129N/A public String getBugPage() {
129N/A return bugPage;
129N/A }
129N/A
129N/A public void setBugPattern(String bugPattern) {
129N/A this.bugPattern = bugPattern;
129N/A }
129N/A
129N/A public String getBugPattern() {
129N/A return bugPattern;
129N/A }
318N/A
318N/A public String getReviewPage() {
318N/A return reviewPage;
318N/A }
318N/A
318N/A public void setReviewPage(String reviewPage) {
318N/A this.reviewPage = reviewPage;
318N/A }
318N/A
318N/A public String getReviewPattern() {
318N/A return reviewPattern;
318N/A }
318N/A
318N/A public void setReviewPattern(String reviewPattern) {
318N/A this.reviewPattern = reviewPattern;
318N/A }
144N/A
144N/A public String getWebappLAF() {
144N/A return webappLAF;
144N/A }
144N/A
144N/A public void setWebappLAF(String webappLAF) {
144N/A this.webappLAF = webappLAF;
144N/A }
173N/A
173N/A public boolean isRemoteScmSupported() {
173N/A return remoteScmSupported;
173N/A }
173N/A
173N/A public void setRemoteScmSupported(boolean remoteScmSupported) {
173N/A this.remoteScmSupported = remoteScmSupported;
173N/A }
234N/A
253N/A public boolean isOptimizeDatabase() {
253N/A return optimizeDatabase;
253N/A }
253N/A
253N/A public void setOptimizeDatabase(boolean optimizeDatabase) {
253N/A this.optimizeDatabase = optimizeDatabase;
253N/A }
253N/A
296N/A public boolean isUsingLuceneLocking() {
296N/A return useLuceneLocking;
296N/A }
296N/A
296N/A public void setUsingLuceneLocking(boolean useLuceneLocking) {
296N/A this.useLuceneLocking = useLuceneLocking;
296N/A }
335N/A
335N/A public void setCompressXref(boolean compressXref) {
335N/A this.compressXref = compressXref;
335N/A }
335N/A
335N/A public boolean isCompressXref() {
335N/A return compressXref;
335N/A }
335N/A
480N/A public boolean isIndexVersionedFilesOnly() {
480N/A return indexVersionedFilesOnly;
480N/A }
480N/A
480N/A public void setIndexVersionedFilesOnly(boolean indexVersionedFilesOnly) {
480N/A this.indexVersionedFilesOnly = indexVersionedFilesOnly;
480N/A }
667N/A
667N/A public Date getDateForLastIndexRun() {
667N/A File timestamp = new File(getDataRoot(), "timestamp");
667N/A return new Date(timestamp.lastModified());
667N/A }
833N/A
833N/A public String getDatabaseDriver() {
833N/A return databaseDriver;
833N/A }
833N/A
833N/A public void setDatabaseDriver(String databaseDriver) {
833N/A this.databaseDriver = databaseDriver;
833N/A }
833N/A
833N/A public String getDatabaseUrl() {
833N/A if (databaseUrl == null) {
833N/A return "jdbc:derby:" + getDataRoot() +
833N/A File.separator + "cachedb;create=true";
833N/A }
833N/A return databaseUrl;
833N/A }
833N/A
833N/A public void setDatabaseUrl(String databaseUrl) {
833N/A this.databaseUrl = databaseUrl;
833N/A }
833N/A
234N/A /**
234N/A * Write the current configuration to a file
234N/A * @param file the file to write the configuration into
234N/A * @throws IOException if an error occurs
234N/A */
234N/A public void write(File file) throws IOException {
509N/A final FileOutputStream out = new FileOutputStream(file);
640N/A try {
640N/A this.encodeObject(out);
640N/A } finally {
640N/A out.close();
640N/A }
639N/A }
639N/A
639N/A public String getXMLRepresentationAsString() {
639N/A ByteArrayOutputStream bos = new ByteArrayOutputStream();
639N/A this.encodeObject(bos);
639N/A return bos.toString();
639N/A }
639N/A
639N/A private void encodeObject(OutputStream out) {
640N/A XMLEncoder e = new XMLEncoder(new BufferedOutputStream(out));
640N/A e.writeObject(this);
640N/A e.close();
234N/A }
234N/A
234N/A public static Configuration read(File file) throws IOException {
639N/A final FileInputStream in = new FileInputStream(file);
640N/A try {
640N/A return decodeObject(in);
640N/A } finally {
640N/A in.close();
640N/A }
639N/A }
639N/A
639N/A
639N/A
639N/A public static Configuration makeXMLStringAsConfiguration(String xmlconfig) throws IOException {
639N/A final Configuration ret;
639N/A final ByteArrayInputStream in = new ByteArrayInputStream(xmlconfig.getBytes());
639N/A ret = decodeObject(in);
639N/A return ret;
639N/A }
639N/A
639N/A private static Configuration decodeObject(InputStream in) throws IOException {
640N/A XMLDecoder d = new XMLDecoder(new BufferedInputStream(in));
640N/A final Object ret = d.readObject();
640N/A d.close();
509N/A
234N/A if (!(ret instanceof Configuration)) {
234N/A throw new IOException("Not a valid config file");
640N/A }
234N/A return (Configuration)ret;
234N/A }
639N/A
58N/A}