View Javadoc

1   
2   package example.filestorage;
3   
4   import java.sql.*;
5   
6   import javax.naming.*;
7   import javax.sql.DataSource;
8   import java.util.Properties;
9   import java.io.InputStream;
10  
11  class DBUtil
12  {
13  	private static DataSource fileStorageDataSource;
14  
15  	private static boolean useDataSource = true;
16  	private static String jdbcURL = null;
17  	private static String jdbcDriver = null;
18  	private static boolean closeQuietly = true;
19  	
20  	static
21  	{
22  		InputStream inStream = DBUtil.class.getResourceAsStream("/db.properties");
23  		if (inStream != null)
24  		{
25  			Properties props = new Properties();
26  			try
27  			{
28  				props.load(inStream);
29  				useDataSource = Boolean.valueOf(props.getProperty("use.datasource")).booleanValue();
30  				jdbcURL = props.getProperty("jdbc.url");
31  				jdbcDriver = props.getProperty("jdbc.driver");
32  				if (jdbcDriver != null)
33  				{
34  					try
35  					{
36  						Class.forName(jdbcDriver);
37  					}
38  					catch (ClassNotFoundException ex)
39  					{
40  						throw new DataAccessException(ex);
41  					}
42  				}
43  			}
44  			catch (java.io.IOException ex)
45  			{
46  				throw new DataAccessException(ex);
47  			}
48  			finally
49  			{
50  				closeQuietly(inStream);
51  			}
52  		}
53  		
54  	}
55  		
56  	private static void closeQuietly(InputStream in)
57  	{
58  		if (in != null)
59  		{
60  		
61  			try
62  			{
63  				in.close();
64  			}
65  			catch (java.io.IOException ignored)
66  			{
67  				// ignored
68  			}
69  		}
70  	}
71  	public static void setDataSource(javax.sql.DataSource ds)
72  	{
73  		fileStorageDataSource = ds;
74  	}
75  	
76  	private static DataSource lookupDataSource()
77  	{
78    	    DataSource ds = null;
79  		Object obj = lookup(Constants.DATASOURCE_NAME);
80  		ds = (DataSource) narrow(obj, DataSource.class); 
81  		return ds;
82  	}
83  	
84  	public static synchronized Connection getConnection()
85  	{
86  		Connection conn = null;
87  
88  		if (useDataSource)
89  		{
90  			if (fileStorageDataSource == null)
91  			{
92  				fileStorageDataSource = lookupDataSource();
93  			}
94  	
95  			try
96  			{
97  				conn = fileStorageDataSource.getConnection();
98  			}
99  			catch (SQLException ex)
100 			{
101 				throw new DataAccessException(ex);
102 			}
103 		}
104 		else
105 		{
106 			try
107 			{
108 				conn = DriverManager.getConnection(jdbcURL);
109 			}
110 			catch (SQLException ex)
111 			{
112 				throw new DataAccessException(ex);
113 			}
114 		}
115 
116 		return conn;
117 	}
118 	
119 	public static void setCloseQuietly(boolean value)
120 	{
121 		closeQuietly = value;
122 	}
123 	
124 	public static void close(Connection conn, Statement s, ResultSet rs)
125 	{
126 		Throwable caught = null;
127 		
128 		if (s != null)
129 		{
130 			try
131 			{
132 				s.close();
133 			}
134 			catch (SQLException e)
135 			{
136 				caught = e;
137 			}
138 		}
139 		
140 		if (rs != null)
141 		{
142 			try
143 			{
144 				rs.close();
145 			}
146 			catch (SQLException e)
147 			{
148 				caught = e;
149 			}
150 		}
151 		
152 		if (conn != null)
153 		{
154 			try
155 			{
156 				conn.close();
157 			}
158 			catch (SQLException e)
159 			{
160 				caught = e;
161 			}
162 		}
163 	
164 		if ( closeQuietly == false)
165 		{
166 			if (caught != null)
167 			{
168 				throw new DataAccessException(caught);
169 			}
170 		}
171 		
172 	}
173 	
174 	static private Object lookup(final String name)
175 	{
176 		Context initContext = getInitialContext();
177 		
178 		Object result = null;
179 		
180 		try
181 		{
182 			Context envContext = (Context) initContext.lookup("java:comp/env");
183 			result = envContext.lookup(name);
184 		}
185 		catch (NamingException ex)
186 		{
187 			throw new DataAccessException(ex);
188 		}
189 		
190 		return result;
191 	}
192 	
193 	static private Context getInitialContext()
194 	{
195 		Context ctx = null;
196 
197 		try
198 		{
199 			ctx = new InitialContext();
200 		}
201 		catch (NamingException ex)
202 		{
203 			throw new DataAccessException(ex);
204 		}		
205 		return ctx; 
206 	}
207 	
208 	static private Object narrow(final Object obj, final Class clazz)
209 	{
210 		return javax.rmi.PortableRemoteObject.narrow(obj, clazz);
211 	}
212 
213 
214 }