001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi;
024:
025: import java.io.IOException;
026: import java.io.Reader;
027: import java.sql.SQLException;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.Properties;
034:
035: import javax.sql.DataSource;
036:
037: import org.hammurapi.results.ResultsFactory;
038:
039: import com.pavelvlasov.config.ConfigurationException;
040: import com.pavelvlasov.persistence.Storage;
041: import com.pavelvlasov.sql.ConnectionPerThreadDataSource;
042: import com.pavelvlasov.sql.SQLProcessor;
043: import com.pavelvlasov.sql.hypersonic.HypersonicTmpDataSource;
044: import com.pavelvlasov.util.DispatchingVisitor;
045:
046: /**
047: * @author Pavel Vlasov
048: * @version $Revision: 1.6 $
049: */
050: public class SessionImpl implements Session {
051:
052: private DispatchingVisitor visitor;
053:
054: private Map contexts = new HashMap();
055:
056: private Collection inspectors;
057:
058: private Storage storage;
059:
060: private boolean inspectorsSet;
061:
062: /**
063: * @return Returns the storage.
064: */
065: public Storage getStorage() {
066: return storage == null ? ResultsFactory.getInstance()
067: .getStorage() : storage;
068: }
069:
070: /**
071: * @param storage The storage to set.
072: */
073: void setStorage(Storage storage) {
074: this .storage = storage;
075: }
076:
077: /**
078: * @param inspectorSet The inspectors to set.
079: * @throws HammurapiException
080: * @throws ConfigurationException
081: */
082: public void setInspectors(InspectorSet inspectorSet)
083: throws ConfigurationException, HammurapiException {
084: this .inspectorsSet = true;
085: this .inspectors = new ArrayList(inspectorSet.getInspectors());
086: inspectorSet.initInspectors();
087: }
088:
089: public InspectorContext getContext(String inspectorName) {
090: InspectorContext ret = (InspectorContext) contexts
091: .get(inspectorName);
092: if (ret == null) {
093: throw new HammurapiRuntimeException("Inspector '"
094: + inspectorName + "' does not exist");
095: }
096: return ret;
097: }
098:
099: void addContext(String inspectorName, InspectorContext context) {
100: contexts.put(inspectorName, context);
101: }
102:
103: private Map attributes = new HashMap();
104:
105: public void setAttribute(Object key, Object value) {
106: attributes.put(key, value);
107: }
108:
109: public Object getAttribute(Object key) {
110: return attributes.get(key);
111: }
112:
113: public Object removeAttribute(Object key) {
114: return attributes.remove(key);
115: }
116:
117: public void disable(Inspector inspector) {
118: if (visitor != null) {
119: visitor.remove(inspector);
120: }
121: }
122:
123: /**
124: * @param visitor The visitor to set.
125: */
126: public void setVisitor(DispatchingVisitor visitor) {
127: this .visitor = visitor;
128: }
129:
130: private SQLProcessor processor;
131:
132: private ConnectionPerThreadDataSource datasource;
133:
134: private boolean scheduleInitDb;
135:
136: private String[] classPath;
137:
138: void setClassPath(String[] classPath) {
139: this .classPath = classPath;
140: }
141:
142: public void scheduleInitDb() {
143: this .scheduleInitDb = true;
144: }
145:
146: public SQLProcessor getProcessor() {
147: try {
148: if (processor == null) {
149: try {
150: datasource = new HypersonicTmpDataSource(
151: (Reader) null);
152: processor = new SQLProcessor(datasource, null);
153: scheduleInitDb = true;
154: } catch (ClassNotFoundException e) {
155: throw new HammurapiRuntimeException(e);
156: } catch (IOException e) {
157: throw new HammurapiRuntimeException(e);
158: }
159: }
160: if (scheduleInitDb) {
161: scheduleInitDb = false;
162: initDb();
163: }
164: } catch (SQLException e) {
165: throw new HammurapiRuntimeException(e);
166: }
167:
168: return processor;
169: }
170:
171: /**
172: * @throws SQLException
173: */
174: private void initDb() throws SQLException {
175: if (inspectorsSet) {
176: if (inspectors != null) {
177: Iterator it = inspectors.iterator();
178: while (it.hasNext()) {
179: Object next = it.next();
180: if (next instanceof Inspector) {
181: ((Inspector) next).initDb(processor,
182: dbProperties);
183: }
184: }
185: }
186: } else {
187: throw new IllegalStateException(
188: "getProcessor() called before inspectors were set");
189: }
190: }
191:
192: void setDatasource(DataSource datasource) {
193: if (processor == null) {
194: processor = new SQLProcessor(datasource, null);
195: } else {
196: throw new HammurapiRuntimeException(
197: "Illegal state: processor is not null");
198: }
199: }
200:
201: void shutdown() throws SQLException {
202: if (datasource != null) {
203: datasource.shutdown();
204: }
205: }
206:
207: public String[] getClassPath() {
208: return classPath;
209: }
210:
211: private Properties dbProperties = new Properties();
212:
213: void setDbProperty(String name, String value) {
214: dbProperties.setProperty(name, value);
215: }
216: }
|