KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > hessian > BasicService


1 package dinamica.hessian;
2
3 import javax.sql.*;
4 import com.caucho.hessian.server.HessianServlet;
5 import dinamica.*;
6 import java.io.*;
7 import javax.servlet.ServletContext JavaDoc;
8
9 /**
10  * BasicService<br>
11  * Factorizes utility methods that are common
12  * to many database oriented web services. This
13  * type of services should inherit from this class
14  * and implement the corresponding interfaces that
15  * represent the web services.
16  * <br><br>
17  * Creation date: 01/06/2004<br>
18  * @author Martin Cordova dinamica@martincordova.com
19  * <br>(c) 2004 Martin Cordova y Asociados
20  * <br>http://www.martincordova.com
21  */

22 public class BasicService extends HessianServlet
23 {
24
25     /**
26      * Load a text resource relative to the class location
27      * @param path Pathname of the resource, if it is a filename
28      * then the resource location is assumed in the same path as the class
29      * otherwise may be a sybdirectory relative to the class directory.
30      * @return A String containing the resource
31      * @throws Throwable
32      */

33     protected String JavaDoc getResource(String JavaDoc path) throws Throwable JavaDoc
34     {
35
36         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(5000);
37         byte[] data = new byte[5000];
38
39         InputStream in = null;
40         
41         in = this.getClass().getResourceAsStream(path);
42         
43         try
44         {
45             if (in!=null)
46             {
47                 while (true)
48                 {
49                     int len = in.read(data);
50                     if (len!=-1)
51                     {
52                         buf.append(new String JavaDoc(data,0,len));
53                     }
54                     else
55                     {
56                         break;
57                     }
58                 }
59                 return buf.toString();
60             }
61             else
62             {
63                 throw new Throwable JavaDoc("Invalid path to resource: " + path);
64             }
65             
66         }
67         catch (Throwable JavaDoc e)
68         {
69             throw e;
70         }
71         finally
72         {
73             if (in!=null)
74             {
75                 try{
76                     in.close();
77                 } catch (Exception JavaDoc e){}
78             }
79         }
80                 
81     }
82
83     /**
84      * Return DataSource object using JNDI prefix
85      * configured in web.xml context parameter. This is an
86      * utility method to help simplify Transaction code. A DataSource
87      * can be obtained with a single line of code:<br><br>
88      * <pre>
89      * javax.sql.DataSource ds = getDataSource("jdbc/customersDB");
90      * setConnection(ds.getConnection);
91      * ....
92      * </pre>
93      * <br>
94      * Remember that when you use your own datasource, you
95      * must close the connection in your Transaction code! consult
96      * the reference guide ("Sample code" section) for more information.
97      *
98      * @param name Name of the datasource (Example: jdbc/customersdb)
99      * @return DataSource object
100      * @throws Throwable If DataSource cannot be obtained
101      */

102     protected DataSource getDataSource(String JavaDoc name) throws Throwable JavaDoc
103     {
104
105       //get datasource config from web.xml
106
ServletContext JavaDoc ctx = getServletContext();
107       String JavaDoc jndiPrefix = ctx.getInitParameter("jndi-prefix");
108       
109       if (jndiPrefix==null)
110         jndiPrefix="";
111       
112       DataSource ds = Jndi.getDataSource(jndiPrefix + name);
113       if (ds==null)
114         throw new Throwable JavaDoc("Can't get datasource: " + name);
115         
116       return ds;
117             
118     }
119
120     /**
121      * Return the default application DataSource object
122      * as configured in web.xml context parameters. This is a
123      * utility method to help simplify WebService code. A DataSource
124      * can be obtained with a single line of code:<br><br>
125      * <pre>
126      * javax.sql.DataSource ds = getDataSource();
127      * Db db = new Db(ds.getConnection());
128      * ....
129      * </pre>
130      * <br>
131      * Remember that when you use your own datasource, you
132      * must close the connection in your Transaction code! consult
133      * the reference guide ("Sample code" section) for more information.
134      *
135      * @return DataSource object
136      * @throws Throwable If DataSource cannot be obtained
137      */

138     protected DataSource getDataSource() throws Throwable JavaDoc
139     {
140
141       //get datasource config from web.xml
142
String JavaDoc jndiPrefix = null;
143       String JavaDoc name = null;
144       ServletContext JavaDoc ctx = getServletContext();
145       
146       name = ctx.getInitParameter("def-datasource");
147       jndiPrefix = ctx.getInitParameter("jndi-prefix");
148       
149       if (jndiPrefix==null)
150         jndiPrefix="";
151       
152       DataSource ds = Jndi.getDataSource(jndiPrefix + name);
153       if (ds==null)
154         throw new Throwable JavaDoc("Can't get datasource: " + name);
155         
156       return ds;
157             
158     }
159
160 }
161
Popular Tags