KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > SessionImpl


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi;
24
25 import java.io.IOException JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import javax.sql.DataSource JavaDoc;
36
37 import org.hammurapi.results.ResultsFactory;
38
39 import com.pavelvlasov.config.ConfigurationException;
40 import com.pavelvlasov.persistence.Storage;
41 import com.pavelvlasov.sql.ConnectionPerThreadDataSource;
42 import com.pavelvlasov.sql.SQLProcessor;
43 import com.pavelvlasov.sql.hypersonic.HypersonicTmpDataSource;
44 import com.pavelvlasov.util.DispatchingVisitor;
45
46 /**
47  * @author Pavel Vlasov
48  * @version $Revision: 1.6 $
49  */

50 public class SessionImpl implements Session {
51             
52     private DispatchingVisitor visitor;
53
54     private Map JavaDoc contexts=new HashMap JavaDoc();
55     
56     private Collection JavaDoc inspectors;
57     
58     private Storage storage;
59
60     private boolean inspectorsSet;
61
62     /**
63      * @return Returns the storage.
64      */

65     public Storage getStorage() {
66         return storage==null ? ResultsFactory.getInstance().getStorage() : storage;
67     }
68     /**
69      * @param storage The storage to set.
70      */

71     void setStorage(Storage storage) {
72         this.storage = storage;
73     }
74     /**
75      * @param inspectorSet The inspectors to set.
76      * @throws HammurapiException
77      * @throws ConfigurationException
78      */

79     public void setInspectors(InspectorSet inspectorSet) throws ConfigurationException, HammurapiException {
80         this.inspectorsSet=true;
81         this.inspectors = new ArrayList JavaDoc(inspectorSet.getInspectors());
82         inspectorSet.initInspectors();
83     }
84     
85     public InspectorContext getContext(String JavaDoc inspectorName) {
86         InspectorContext ret = (InspectorContext) contexts.get(inspectorName);
87         if (ret==null) {
88             throw new HammurapiRuntimeException("Inspector '"+inspectorName+"' does not exist");
89         }
90         return ret;
91     }
92     
93     void addContext(String JavaDoc inspectorName, InspectorContext context) {
94         contexts.put(inspectorName, context);
95     }
96
97     private Map JavaDoc attributes=new HashMap JavaDoc();
98     
99     public void setAttribute(Object JavaDoc key, Object JavaDoc value) {
100         attributes.put(key, value);
101     }
102     
103     public Object JavaDoc getAttribute(Object JavaDoc key) {
104         return attributes.get(key);
105     }
106     
107     public Object JavaDoc removeAttribute(Object JavaDoc key) {
108         return attributes.remove(key);
109     }
110
111     public void disable(Inspector inspector) {
112         if (visitor!=null) {
113             visitor.remove(inspector);
114         }
115     }
116     
117     /**
118      * @param visitor The visitor to set.
119      */

120     public void setVisitor(DispatchingVisitor visitor) {
121         this.visitor = visitor;
122     }
123
124     private SQLProcessor processor;
125
126     private ConnectionPerThreadDataSource datasource;
127     
128     private boolean scheduleInitDb;
129
130     private String JavaDoc[] classPath;
131     
132     void setClassPath(String JavaDoc[] classPath) {
133         this.classPath=classPath;
134     }
135     
136     public void scheduleInitDb() {
137         this.scheduleInitDb = true;
138     }
139     
140     public SQLProcessor getProcessor() {
141         try {
142             if (processor==null) {
143                 try {
144                     datasource=new HypersonicTmpDataSource((Reader JavaDoc) null);
145                     processor=new SQLProcessor(datasource, null);
146                     scheduleInitDb=true;
147                 } catch (ClassNotFoundException JavaDoc e) {
148                     throw new HammurapiRuntimeException(e);
149                 } catch (IOException JavaDoc e) {
150                     throw new HammurapiRuntimeException(e);
151                 }
152             }
153             if (scheduleInitDb) {
154                 scheduleInitDb=false;
155                 initDb();
156             }
157         } catch (SQLException JavaDoc e) {
158             throw new HammurapiRuntimeException(e);
159         }
160         
161         return processor;
162     }
163     
164     /**
165      * @throws SQLException
166      */

167     private void initDb() throws SQLException JavaDoc {
168         if (inspectorsSet) {
169             if (inspectors!=null) {
170                 Iterator JavaDoc it=inspectors.iterator();
171                 while (it.hasNext()) {
172                     Object JavaDoc next = it.next();
173                     if (next instanceof Inspector) {
174                         ((Inspector) next).initDb(processor, dbProperties);
175                     }
176                 }
177             }
178         } else {
179             throw new IllegalStateException JavaDoc("getProcessor() called before inspectors were set");
180         }
181     }
182
183     void setDatasource(DataSource JavaDoc datasource) {
184         if (processor==null) {
185             processor=new SQLProcessor(datasource,null);
186         } else {
187             throw new HammurapiRuntimeException("Illegal state: processor is not null");
188         }
189     }
190     
191     void shutdown() throws SQLException JavaDoc {
192         if (datasource != null) {
193             datasource.shutdown();
194         }
195     }
196
197     public String JavaDoc[] getClassPath() {
198         return classPath;
199     }
200     
201     private Properties JavaDoc dbProperties=new Properties JavaDoc();
202     
203     void setDbProperty(String JavaDoc name, String JavaDoc value) {
204         dbProperties.setProperty(name, value);
205     }
206 }
207
Popular Tags