001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixxml.config;
020:
021: import java.net.MalformedURLException;
022: import java.net.URL;
023:
024: import javax.servlet.ServletContext;
025:
026: import de.schlund.pfixxml.resources.DocrootResourceProvider;
027: import de.schlund.pfixxml.resources.internal.DocrootResourceByServletContextProvider;
028: import de.schlund.pfixxml.resources.internal.DocrootResourceOnFileSystemProvider;
029:
030: /**
031: * Provides access to global (shared between all application within an environment)
032: * Pustefix settings. Settings can be changed through
033: * {@link de.schlund.pfixxml.config.GlobalConfigurator}.
034: *
035: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
036: */
037: public class GlobalConfig {
038: private static String docroot = null;
039: private static URL docrootURL = null;
040: private static ServletContext servletContext = null;
041: private static DocrootResourceProvider docrootResourceProvider = null;
042:
043: private final static String WAR_DOCROOT = "/WEB-INF/pfixroot";
044:
045: /**
046: * Returns the absolute to the Pustefix docroot (usually the "projects" directory)
047: *
048: * @return Absolute path to Pustefix docroot or <code>null</code> if Pustefix
049: * docroot is not available (e.g. in packed WAR mode)
050: */
051: public static String getDocroot() {
052: return docroot;
053: }
054:
055: static void setDocroot(String path) {
056: if (docroot != null || servletContext != null) {
057: throw new IllegalStateException(
058: "Docroot or servlet context may only be set once!");
059: }
060: docroot = path;
061: setDocrootResourceProvider(new DocrootResourceOnFileSystemProvider(
062: docroot));
063: try {
064: docrootURL = new URL("file", null, -1, docroot);
065: } catch (MalformedURLException e) {
066: throw new RuntimeException(
067: "Cannot create URL for docroot: " + docroot, e);
068: }
069:
070: }
071:
072: /**
073: * Returns an URL that points to the Pustefix docroot.
074: * This can be used to retrieve elements relative to the
075: * docroot.
076: *
077: * @return URL denoting the Pustefix docroot
078: */
079: public static URL getDocrootAsURL() {
080: if (docrootURL != null) {
081: return docrootURL;
082: } else {
083: throw new IllegalStateException(
084: "getDocrootAsURL() can only be used if either docroot or servlet context are set!");
085: }
086: }
087:
088: /**
089: * Returns the servlet context
090: *
091: * @return The servlet context or <code>null</code> if this instance of
092: * Pustefix is shared across different contexts (e.g. in standalone mode)
093: */
094: public static ServletContext getServletContext() {
095: return servletContext;
096: }
097:
098: static void setServletContext(ServletContext context) {
099: if (docroot != null || servletContext != null) {
100: throw new IllegalStateException(
101: "Docroot or servlet context may only be set once!");
102: }
103: servletContext = context;
104: setDocrootResourceProvider(new DocrootResourceByServletContextProvider(
105: servletContext));
106: try {
107: docrootURL = servletContext.getResource(WAR_DOCROOT);
108: } catch (MalformedURLException e) {
109: throw new RuntimeException(
110: "Unexpected error while creating URL for docroot!",
111: e);
112: }
113:
114: }
115:
116: /**
117: * Returns a provider that is used to get DocrootResources.
118: *
119: * @return Provider for DocrootResource
120: */
121: public static DocrootResourceProvider getDocrootResourceProvider() {
122: return docrootResourceProvider;
123: }
124:
125: static void setDocrootResourceProvider(
126: DocrootResourceProvider provider) {
127: if (docrootResourceProvider != null) {
128: throw new IllegalStateException(
129: "The provider can only be set once!");
130: }
131: docrootResourceProvider = provider;
132: }
133:
134: }
|