001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: //Copyright (C) 1999 - 2004, Salmon LLC
004: //
005: //This program is free software; you can redistribute it and/or
006: //modify it under the terms of the GNU General Public License version 2
007: //as published by the Free Software Foundation;
008: //
009: //This program 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 General Public License for more details.
013: //
014: //You should have received a copy of the GNU General Public License
015: //along with this program; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: //For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.util;
021:
022: import java.io.File;
023:
024: /**
025: * Creates an application context object that can be used by other framwork
026: * classes to determine information on the web application
027: */
028: public class ApplicationContext {
029: private static ThreadLocal _context = new ContextThreadLocal();
030: private String _appID;
031: private String _appDocumentRoot;
032: private String _appPropsPath;
033: private MessageLog _logger;
034:
035: private static class ContextThread extends Thread {
036: ApplicationContext _parentThreadContext;
037:
038: private ContextThread(ApplicationContext parentContext,
039: Runnable o) {
040: super (o);
041: _parentThreadContext = parentContext;
042: }
043:
044: private ApplicationContext getContext() {
045: return _parentThreadContext;
046: }
047: }
048:
049: private static class ContextThreadLocal extends ThreadLocal {
050: protected Object initialValue() {
051: Thread t = Thread.currentThread();
052: if (t instanceof ContextThread)
053: return ((ContextThread) t).getContext();
054: else
055: return super .initialValue();
056: }
057: }
058:
059: /**
060: * Creates a new thread that shares the same application context as the current thread
061: */
062: public static Thread createThreadWithContext(Runnable r) {
063: return new ContextThread(getContext(), r);
064: }
065:
066: /**
067: * Creates a new thread with a clone of the application context as the current thread
068: */
069: public static Thread createThreadWithContextClone(Runnable r) {
070: ApplicationContext from = getContext();
071: if (from != null) {
072: ApplicationContext to = new ApplicationContext();
073: to.setAppDocumentRoot(from.getAppDocumentRoot());
074: to.setAppID(from.getAppID());
075: to.setLogger(from.getLogger());
076: return new ContextThread(to, r);
077: } else
078: return new ContextThread(null, r);
079:
080: }
081:
082: public static void setContext(ApplicationContext cont) {
083: _context.set(cont);
084: }
085:
086: public static ApplicationContext getContext() {
087: return (ApplicationContext) _context.get();
088: }
089:
090: /**
091: * @return Returns the appID.
092: */
093: public String getAppID() {
094: if (_appID == null)
095: return "";
096: else
097: return _appID;
098: }
099:
100: /**
101: * @param appID
102: * The appID to set.
103: */
104: public void setAppID(String appID) {
105: _appID = appID;
106: }
107:
108: /**
109: * @return Returns the contextRoot.
110: */
111: public String getAppDocumentRoot() {
112: return _appDocumentRoot;
113: }
114:
115: /**
116: * @return Returns the logger.
117: */
118: public MessageLog getLogger() {
119: return _logger;
120: }
121:
122: /**
123: * @param Framework method, don't call directly
124: */
125: public void setLogger(MessageLog logger) {
126: _logger = logger;
127: }
128:
129: /**
130: * @param Framework method, don't call directly
131: */
132: public void setAppDocumentRoot(String contextRoot) {
133: _appDocumentRoot = contextRoot;
134: _appPropsPath = contextRoot;
135: if (!_appPropsPath.endsWith(File.separator))
136: _appPropsPath += File.separator;
137: _appPropsPath += "WEB-INF" + File.separator + "properties";
138: }
139:
140: /**
141: * Returns the directory where framework properties are stored
142: * @return
143: */
144: public String getAppPropertiesPath() {
145: return _appPropsPath;
146: }
147:
148: public String toString() {
149: return "AppID:" + _appID + " DocumentRoot:" + _appDocumentRoot;
150: }
151:
152: }
|