Configuration.java revision 875
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
1272N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/Apackage org.opensolaris.opengrok.configuration;
143N/A
143N/Aimport java.beans.XMLDecoder;
143N/Aimport java.beans.XMLEncoder;
921N/Aimport java.io.BufferedInputStream;
143N/Aimport java.io.BufferedOutputStream;
143N/Aimport java.io.ByteArrayInputStream;
143N/Aimport java.io.ByteArrayOutputStream;
202N/Aimport java.io.File;
421N/Aimport java.io.FileInputStream;
200N/Aimport java.io.FileOutputStream;
143N/Aimport java.io.IOException;
143N/Aimport java.io.InputStream;
424N/Aimport java.io.OutputStream;
143N/Aimport java.util.ArrayList;
143N/Aimport java.util.Date;
99N/Aimport java.util.List;
424N/Aimport org.opensolaris.opengrok.history.RepositoryInfo;
0N/Aimport org.opensolaris.opengrok.index.IgnoredNames;
202N/A
202N/A/**
202N/A * Placeholder class for all configuration variables. Due to the multithreaded
202N/A * nature of the web application, each thread will use the same instance of the
202N/A * configuration object for each page request. Class and methods should have
670N/A * package scope, but that didn't work with the XMLDecoder/XMLEncoder.
202N/A */
202N/Apublic final class Configuration {
202N/A private String ctags;
202N/A
202N/A /** Should the history log be cached? */
202N/A private boolean historyCache;
825N/A /**
202N/A * The maximum time in milliseconds {@code HistoryCache.get()} can take
1272N/A * before its result is cached.
202N/A */
1073N/A private int historyCacheTime;
1153N/A
202N/A /** Should the history cache be stored in a database? */
202N/A private boolean historyCacheInDB;
1028N/A
202N/A private List<Project> projects;
202N/A private String sourceRoot;
244N/A private String dataRoot;
1275N/A private List<RepositoryInfo> repositories;
1275N/A private String urlPrefix;
58N/A private boolean generateHtml;
143N/A private Project defaultProject;
615N/A private int indexWordLimit;
143N/A private boolean verbose;
143N/A private boolean allowLeadingWildcard;
0N/A private IgnoredNames ignoredNames;
0N/A private String userPage;
0N/A private String bugPage;
1272N/A private String bugPattern;
143N/A private String reviewPage;
143N/A private String reviewPattern;
143N/A private String webappLAF;
0N/A private boolean remoteScmSupported;
0N/A private boolean optimizeDatabase;
0N/A private boolean useLuceneLocking;
0N/A private boolean compressXref;
143N/A private boolean indexVersionedFilesOnly;
202N/A private int hitsPerPage;
202N/A private int cachePages;
202N/A private String databaseDriver;
202N/A private String databaseUrl;
483N/A
483N/A /** Creates a new instance of Configuration */
483N/A public Configuration() {
483N/A //defaults for an opengrok instance configuration
202N/A setHistoryCache(true);
202N/A setHistoryCacheTime(30);
202N/A setHistoryCacheInDB(false);
202N/A setProjects(new ArrayList<Project>());
460N/A setRepositories(new ArrayList<RepositoryInfo>());
202N/A setUrlPrefix("/source/s?");
202N/A //setUrlPrefix("../s?"); // TODO generate relative search paths, get rid of -w <webapp> option to indexer !
202N/A setCtags("ctags");
202N/A //below can cause an outofmemory error, since it is defaulting to NO LIMIT
202N/A setIndexWordLimit(Integer.MAX_VALUE);
202N/A setVerbose(false);
202N/A setGenerateHtml(true);
202N/A setQuickContextScan(true);
202N/A setIgnoredNames(new IgnoredNames());
202N/A setUserPage("http://www.opensolaris.org/viewProfile.jspa?username=");
202N/A setBugPage("http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=");
210N/A setBugPattern("\\b([12456789][0-9]{6})\\b");
210N/A setReviewPage("http://arc.opensolaris.org/caselog/PSARC/");
210N/A setReviewPattern("\\b(\\d{4}/\\d{3})\\b"); // in form e.g. PSARC 2008/305
210N/A setWebappLAF("default");
0N/A setRemoteScmSupported(false);
0N/A setOptimizeDatabase(true);
0N/A setUsingLuceneLocking(false);
36N/A setCompressXref(true);
202N/A setIndexVersionedFilesOnly(false);
202N/A setHitsPerPage(25);
202N/A setCachePages(5);
202N/A setDatabaseDriver("org.apache.derby.jdbc.ClientDriver");
202N/A }
202N/A
202N/A public String getCtags() {
202N/A return ctags;
202N/A }
257N/A
257N/A public void setCtags(String ctags) {
202N/A this.ctags = ctags;
202N/A }
1275N/A
1275N/A public int getCachePages() {
670N/A return cachePages;
202N/A }
257N/A
202N/A public void setCachePages(int cachePages) {
202N/A this.cachePages = cachePages;
1272N/A }
1028N/A
1072N/A public int getHitsPerPage() {
1153N/A return hitsPerPage;
202N/A }
244N/A
202N/A public void setHitsPerPage(int hitsPerPage) {
825N/A this.hitsPerPage = hitsPerPage;
202N/A }
202N/A
202N/A /**
210N/A * Should the history log be cached?
0N/A * @return {@code true} if a {@code HistoryCache} implementation should
0N/A * be used, {@code false} otherwise
126N/A */
143N/A public boolean isHistoryCache() {
210N/A return historyCache;
210N/A }
210N/A
483N/A /**
483N/A * Set whether history should be cached.
483N/A * @param historyCache if {@code true} enable history cache
483N/A */
483N/A public void setHistoryCache(boolean historyCache) {
210N/A this.historyCache = historyCache;
210N/A }
210N/A
210N/A /**
210N/A * How long can a history request take before it's cached? If the time
210N/A * is exceeded, the result is cached. This setting only affects
257N/A * {@code FileHistoryCache}.
257N/A *
257N/A * @return the maximum time in milliseconds a history request can take
210N/A * before it's cached
210N/A */
210N/A public int getHistoryCacheTime() {
210N/A return historyCacheTime;
210N/A }
210N/A
143N/A /**
143N/A * Set the maximum time a history request can take before it's cached.
143N/A * This setting is only respected if {@code FileHistoryCache} is used.
202N/A *
202N/A * @param historyCacheTime maximum time in milliseconds
143N/A */
143N/A public void setHistoryCacheTime(int historyCacheTime) {
143N/A this.historyCacheTime = historyCacheTime;
202N/A }
202N/A
202N/A /**
202N/A * Should the history cache be stored in a database? If yes,
202N/A * {@code JDBCHistoryCache} will be used to cache the history; otherwise,
202N/A * {@code FileHistoryCache} is used.
126N/A *
126N/A * @return whether the history cache should be stored in a database
143N/A */
202N/A public boolean isHistoryCacheInDB() {
0N/A return historyCacheInDB;
0N/A }
0N/A
202N/A /**
0N/A * Set whether the history cache should be stored in a database, and
143N/A * {@code JDBCHistoryCache} should be used instead of {@code
143N/A * FileHistoryCache}.
143N/A *
143N/A * @param historyCacheInDB whether the history cached should be stored in
1190N/A * a database
143N/A */
143N/A public void setHistoryCacheInDB(boolean historyCacheInDB) {
143N/A this.historyCacheInDB = historyCacheInDB;
143N/A }
419N/A
0N/A public List<Project> getProjects() {
143N/A return projects;
202N/A }
202N/A
202N/A public void setProjects(List<Project> projects) {
0N/A this.projects = projects;
202N/A }
0N/A
143N/A public String getSourceRoot() {
143N/A return sourceRoot;
143N/A }
143N/A
143N/A public void setSourceRoot(String sourceRoot) {
143N/A this.sourceRoot = sourceRoot;
143N/A }
143N/A
143N/A public String getDataRoot() {
143N/A return dataRoot;
224N/A }
224N/A
0N/A public void setDataRoot(String dataRoot) {
1190N/A this.dataRoot = dataRoot;
1185N/A }
1190N/A
1185N/A public List<RepositoryInfo> getRepositories() {
1190N/A return repositories;
1185N/A }
143N/A
143N/A public void setRepositories(List<RepositoryInfo> repositories) {
143N/A this.repositories = repositories;
0N/A }
99N/A
0N/A public String getUrlPrefix() {
0N/A return urlPrefix;
615N/A }
424N/A
0N/A public void setUrlPrefix(String urlPrefix) {
816N/A this.urlPrefix = urlPrefix;
143N/A }
816N/A
123N/A public void setGenerateHtml(boolean generateHtml) {
123N/A this.generateHtml = generateHtml;
816N/A }
58N/A
143N/A public boolean isGenerateHtml() {
224N/A return generateHtml;
0N/A }
508N/A
1185N/A public void setDefaultProject(Project defaultProject) {
1190N/A this.defaultProject = defaultProject;
1185N/A }
0N/A
508N/A public Project getDefaultProject() {
0N/A return defaultProject;
143N/A }
0N/A
0N/A public int getIndexWordLimit() {
143N/A return indexWordLimit;
0N/A }
143N/A
143N/A public void setIndexWordLimit(int indexWordLimit) {
143N/A this.indexWordLimit = indexWordLimit;
143N/A }
143N/A
216N/A public boolean isVerbose() {
216N/A return verbose;
143N/A }
143N/A
143N/A public void setVerbose(boolean verbose) {
143N/A this.verbose = verbose;
216N/A }
216N/A
395N/A public void setAllowLeadingWildcard(boolean allowLeadingWildcard) {
216N/A this.allowLeadingWildcard = allowLeadingWildcard;
395N/A }
216N/A
0N/A public boolean isAllowLeadingWildcard() {
143N/A return allowLeadingWildcard;
143N/A }
143N/A
143N/A private boolean quickContextScan;
202N/A
143N/A public boolean isQuickContextScan() {
143N/A return quickContextScan;
1127N/A }
143N/A
271N/A public void setQuickContextScan(boolean quickContextScan) {
143N/A this.quickContextScan = quickContextScan;
143N/A }
143N/A
921N/A public void setIgnoredNames(IgnoredNames ignoredNames) {
1127N/A this.ignoredNames = ignoredNames;
1127N/A }
202N/A
202N/A public IgnoredNames getIgnoredNames() {
922N/A return ignoredNames;
922N/A }
922N/A
922N/A public void setUserPage(String userPage) {
922N/A this.userPage = userPage;
922N/A }
1127N/A
0N/A public String getUserPage() {
143N/A return userPage;
0N/A }
143N/A
143N/A public void setBugPage(String bugPage) {
143N/A this.bugPage = bugPage;
0N/A }
0N/A
143N/A public String getBugPage() {
202N/A return bugPage;
143N/A }
143N/A
143N/A public void setBugPattern(String bugPattern) {
143N/A this.bugPattern = bugPattern;
143N/A }
143N/A
143N/A public String getBugPattern() {
143N/A return bugPattern;
143N/A }
0N/A
202N/A public String getReviewPage() {
0N/A return reviewPage;
143N/A }
143N/A
143N/A public void setReviewPage(String reviewPage) {
202N/A this.reviewPage = reviewPage;
143N/A }
143N/A
202N/A public String getReviewPattern() {
202N/A return reviewPattern;
202N/A }
0N/A
202N/A public void setReviewPattern(String reviewPattern) {
0N/A this.reviewPattern = reviewPattern;
143N/A }
0N/A
210N/A public String getWebappLAF() {
210N/A return webappLAF;
210N/A }
210N/A
210N/A public void setWebappLAF(String webappLAF) {
210N/A this.webappLAF = webappLAF;
210N/A }
210N/A
210N/A public boolean isRemoteScmSupported() {
210N/A return remoteScmSupported;
210N/A }
210N/A
210N/A public void setRemoteScmSupported(boolean remoteScmSupported) {
210N/A this.remoteScmSupported = remoteScmSupported;
210N/A }
210N/A
210N/A public boolean isOptimizeDatabase() {
210N/A return optimizeDatabase;
210N/A }
210N/A
210N/A public void setOptimizeDatabase(boolean optimizeDatabase) {
210N/A this.optimizeDatabase = optimizeDatabase;
210N/A }
210N/A
210N/A public boolean isUsingLuceneLocking() {
210N/A return useLuceneLocking;
210N/A }
210N/A
210N/A public void setUsingLuceneLocking(boolean useLuceneLocking) {
210N/A this.useLuceneLocking = useLuceneLocking;
210N/A }
1185N/A
210N/A public void setCompressXref(boolean compressXref) {
210N/A this.compressXref = compressXref;
210N/A }
210N/A
210N/A public boolean isCompressXref() {
210N/A return compressXref;
210N/A }
210N/A
210N/A public boolean isIndexVersionedFilesOnly() {
210N/A return indexVersionedFilesOnly;
210N/A }
210N/A
210N/A public void setIndexVersionedFilesOnly(boolean indexVersionedFilesOnly) {
210N/A this.indexVersionedFilesOnly = indexVersionedFilesOnly;
143N/A }
143N/A
143N/A public Date getDateForLastIndexRun() {
143N/A File timestamp = new File(getDataRoot(), "timestamp");
0N/A return new Date(timestamp.lastModified());
143N/A }
143N/A
143N/A public String getDatabaseDriver() {
143N/A return databaseDriver;
202N/A }
143N/A
0N/A public void setDatabaseDriver(String databaseDriver) {
202N/A this.databaseDriver = databaseDriver;
202N/A }
202N/A
202N/A public String getDatabaseUrl() {
1072N/A if (databaseUrl == null) {
1072N/A return "jdbc:derby://localhost/cachedb;create=true";
1072N/A }
1072N/A return databaseUrl;
202N/A }
202N/A
0N/A public void setDatabaseUrl(String databaseUrl) {
202N/A this.databaseUrl = databaseUrl;
0N/A }
143N/A
143N/A /**
143N/A * Write the current configuration to a file
143N/A * @param file the file to write the configuration into
143N/A * @throws IOException if an error occurs
202N/A */
143N/A public void write(File file) throws IOException {
202N/A final FileOutputStream out = new FileOutputStream(file);
440N/A try {
0N/A this.encodeObject(out);
1190N/A } finally {
460N/A out.close();
460N/A }
0N/A }
440N/A
143N/A public String getXMLRepresentationAsString() {
202N/A ByteArrayOutputStream bos = new ByteArrayOutputStream();
1072N/A this.encodeObject(bos);
202N/A return bos.toString();
202N/A }
0N/A
0N/A private void encodeObject(OutputStream out) {
483N/A XMLEncoder e = new XMLEncoder(new BufferedOutputStream(out));
1072N/A e.writeObject(this);
0N/A e.close();
143N/A }
143N/A
143N/A public static Configuration read(File file) throws IOException {
143N/A final FileInputStream in = new FileInputStream(file);
143N/A try {
202N/A return decodeObject(in);
143N/A } finally {
143N/A in.close();
143N/A }
202N/A }
0N/A
0N/A
0N/A
0N/A public static Configuration makeXMLStringAsConfiguration(String xmlconfig) throws IOException {
143N/A final Configuration ret;
0N/A final ByteArrayInputStream in = new ByteArrayInputStream(xmlconfig.getBytes());
143N/A ret = decodeObject(in);
143N/A return ret;
202N/A }
202N/A
202N/A private static Configuration decodeObject(InputStream in) throws IOException {
202N/A XMLDecoder d = new XMLDecoder(new BufferedInputStream(in));
202N/A final Object ret = d.readObject();
202N/A d.close();
257N/A
202N/A if (!(ret instanceof Configuration)) {
202N/A throw new IOException("Not a valid config file");
0N/A }
0N/A return (Configuration)ret;
202N/A }
202N/A
0N/A}
143N/A