01: /**
02: * Title: OpenUSS - Open Source University Support System
03: * Description: Utility for OpenUSS
04: * Copyright: Copyright (c) B. Lofi Dewanto
05: * Company: University of Muenster
06: * @author B. Lofi Dewanto
07: * @version 1.0
08: */package org.openuss.utility;
09:
10: import java.io.*;
11:
12: import java.rmi.RemoteException;
13:
14: import java.sql.*;
15:
16: import java.util.*;
17:
18: //import javax.ejb.*;
19:
20: import javax.naming.*;
21:
22: import javax.rmi.*;
23:
24: import javax.sql.*;
25:
26: //import javax.transaction.UserTransaction;
27:
28: /**
29: * Factory for all connections use in OpenUSS.
30: *
31: * @author B. Lofi Dewanto
32: * @version 1.0
33: */
34: public class ConnectionFactory {
35: // Context
36: private static Context initialContext = null;
37:
38: /**
39: * @return the connection from the dataSource
40: * @param env. for Jdbc
41: * @exception EJBException Thrown by the method if the dataSource is not found
42: * in the naming.
43: * @exception SQLException may be thrown by dataSource.getConnection()
44: */
45: public static Connection getConnection(String envJdbc)
46: throws Exception {//EJBException, SQLException
47: // Do we need to create the initial context?
48: if (initialContext == null) {
49: // Create the initial context
50: try {
51: initialContext = new InitialContext();
52: } catch (Exception e) {
53: System.out
54: .println("Error getting initial context from JNDI: "
55: + e.toString());
56: }
57: }
58:
59: // Get the datasource
60: DataSource dataSource = null;
61:
62: try {
63: // Finds DataSource from JNDI
64: dataSource = (DataSource) initialContext.lookup(envJdbc);
65:
66: // Return the result
67: return dataSource.getConnection();
68: } catch (Exception e) {
69: System.out.println("Cannot lookup data source "
70: + e.toString());
71: throw new Exception("Cannot lookup data source ");//javax.ejb.EJBException
72: }
73: }
74: }
|