1 23 package org.hammurapi; 24 25 import java.io.IOException ; 26 import java.io.Reader ; 27 import java.sql.SQLException ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 35 import javax.sql.DataSource ; 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 50 public class SessionImpl implements Session { 51 52 private DispatchingVisitor visitor; 53 54 private Map contexts=new HashMap (); 55 56 private Collection inspectors; 57 58 private Storage storage; 59 60 private boolean inspectorsSet; 61 62 65 public Storage getStorage() { 66 return storage==null ? ResultsFactory.getInstance().getStorage() : storage; 67 } 68 71 void setStorage(Storage storage) { 72 this.storage = storage; 73 } 74 79 public void setInspectors(InspectorSet inspectorSet) throws ConfigurationException, HammurapiException { 80 this.inspectorsSet=true; 81 this.inspectors = new ArrayList (inspectorSet.getInspectors()); 82 inspectorSet.initInspectors(); 83 } 84 85 public InspectorContext getContext(String 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 inspectorName, InspectorContext context) { 94 contexts.put(inspectorName, context); 95 } 96 97 private Map attributes=new HashMap (); 98 99 public void setAttribute(Object key, Object value) { 100 attributes.put(key, value); 101 } 102 103 public Object getAttribute(Object key) { 104 return attributes.get(key); 105 } 106 107 public Object removeAttribute(Object 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 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 [] classPath; 131 132 void setClassPath(String [] 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 ) null); 145 processor=new SQLProcessor(datasource, null); 146 scheduleInitDb=true; 147 } catch (ClassNotFoundException e) { 148 throw new HammurapiRuntimeException(e); 149 } catch (IOException e) { 150 throw new HammurapiRuntimeException(e); 151 } 152 } 153 if (scheduleInitDb) { 154 scheduleInitDb=false; 155 initDb(); 156 } 157 } catch (SQLException e) { 158 throw new HammurapiRuntimeException(e); 159 } 160 161 return processor; 162 } 163 164 167 private void initDb() throws SQLException { 168 if (inspectorsSet) { 169 if (inspectors!=null) { 170 Iterator it=inspectors.iterator(); 171 while (it.hasNext()) { 172 Object next = it.next(); 173 if (next instanceof Inspector) { 174 ((Inspector) next).initDb(processor, dbProperties); 175 } 176 } 177 } 178 } else { 179 throw new IllegalStateException ("getProcessor() called before inspectors were set"); 180 } 181 } 182 183 void setDatasource(DataSource 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 { 192 if (datasource != null) { 193 datasource.shutdown(); 194 } 195 } 196 197 public String [] getClassPath() { 198 return classPath; 199 } 200 201 private Properties dbProperties=new Properties (); 202 203 void setDbProperty(String name, String value) { 204 dbProperties.setProperty(name, value); 205 } 206 } 207 | Popular Tags |