KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > j2ee > ejb > ProtoEJB


1 package com.protomatter.j2ee.ejb;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import javax.ejb.*;
54 import javax.naming.*;
55
56 import java.sql.*;
57 import java.util.*;
58 import com.protomatter.util.*;
59 import com.protomatter.syslog.*;
60
61 /**
62  * Base class for all EJBs.
63  */

64 public abstract class ProtoEJB
65 implements EnterpriseBean, SyslogChannelAware
66 {
67   private String JavaDoc[] channelList = null;
68
69   /**
70    * The JNDI name of the syslog channel list. The value of this
71    * constant is "<TT>Syslog.channelList</TT>".
72    *
73    * @see #initSyslogChannelList()
74    */

75   public final static String JavaDoc COMP_ENV_CHANNEL_LIST = "Syslog.channelList";
76
77   /**
78    * Default constructor.
79    */

80   public ProtoEJB()
81   {
82     super();
83   }
84
85   /**
86    * Get the list of channels to log to.
87    *
88    * @see SyslogChannelAware
89    */

90   public Object JavaDoc getSyslogChannel()
91   {
92     return this.channelList;
93   }
94
95   /**
96    * Set the list of channels to log messages to. The
97    * symbolic names "<TT>DEFAULT_CHANNEL</TT>" and
98    * "<TT>ALL_CHANNEL</TT>" are converted to their
99    * internal meanings.
100    */

101   protected void setChannelList(String JavaDoc[] channelList)
102   {
103     this.channelList = channelList;
104   }
105
106   /**
107    * Set the list of channels to log messages to. The
108    * symbolic names "<TT>DEFAULT_CHANNEL</TT>" and
109    * "<TT>ALL_CHANNEL</TT>" are converted to their
110    * internal meanings.
111    */

112   protected void setChannelList(List channelList)
113   {
114     Object JavaDoc olist[] = channelList.toArray();
115     String JavaDoc list[] = new String JavaDoc[olist.length];
116     for (int i=0; i<olist.length; i++)
117     {
118       list[i] = (String JavaDoc)olist[i];
119       if (list[i].equals("DEFAULT_CHANNEL"))
120         list[i] = Syslog.DEFAULT_CHANNEL;
121       else if (list[i].equals("ALL_CHANNEL"))
122         list[i] = Syslog.ALL_CHANNEL;
123     }
124     setChannelList(list);
125   }
126
127   /**
128    * Get the environment context for this component. This method
129    * looks up the <tt>Context</tt> at the "<tt>java:comp/env</tt>"
130    * location.
131    */

132   protected Context getComponentContext()
133   throws NamingException
134   {
135     Context ctx = new InitialContext();
136     return (Context)ctx.lookup("java:comp/env");
137   }
138
139   /**
140    * Initialize the syslog channel list from the component context.
141    * Looks for the "<tt>Syslog.channelList</tt>" component environment
142    * entry, and parses it as a comma-separated list of channel names.
143    * The sumbolic names "<TT>DEFAULT_CHANNEL</TT>" and "<TT>ALL_CHANNEL</TT>"
144    * are converted to their internal meanings.<P>
145    *
146    * For example, the following snippet from an <tt>ejb-jar.xml</tt>
147    * file:<P>
148    *
149    * <TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
150    * <TR><TD>
151    * <PRE><B>
152    * ...
153    * &lt;env-entry&gt;
154    * &lt;env-entry-name&gt;Syslog.channelList&lt;/env-entry-name&gt;
155    * &lt;env-entry-type&gt;java.lang.String&lt;/env-entry-type&gt;
156    * &lt;env-entry-value&gt;THIS_CHANNEL, THAT_CHANNEL&lt;/env-entry-value&gt;
157    * &lt;/env-entry&gt;
158    * ...
159    * </B></PRE>
160    * </TD></TR></TABLE><P>
161    *
162    * Will set the list of channels to contain "<TT>THIS_CHANNEL</TT>"
163    * and "<TT>THAT_CHANNEL</TT>". Also, whitespace around the edges
164    * of the value is stripped, so you can do pretty formatting in
165    * the XML file, like this:<P>
166    *
167    * <TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
168    * <TR><TD>
169    * <PRE><B>
170    * ...
171    * &lt;env-entry&gt;
172    * &lt;env-entry-name&gt;Syslog.channelList&lt;/env-entry-name&gt;
173    * &lt;env-entry-type&gt;java.lang.String&lt;/env-entry-type&gt;
174    * &lt;env-entry-value&gt;
175    * THIS_CHANNEL,
176    * THAT_CHANNEL
177    * &lt;/env-entry-value&gt;
178    * &lt;/env-entry&gt;
179    * ...
180    * </B></PRE>
181    * </TD></TR></TABLE><P>
182    *
183    * And get the same result as above. Refer to the EJB spec,
184    * version 1.1, for more information about the component
185    * environment.<P>
186    *
187    */

188   protected void initSyslogChannelList()
189   {
190     try
191     {
192       Context env = getComponentContext();
193       String JavaDoc s = (String JavaDoc)env.lookup(COMP_ENV_CHANNEL_LIST);
194       s = (s == null) ? "" : s.trim();
195       if (s.length() != 0)
196       {
197         StringTokenizer st = new StringTokenizer(s, ", ");
198         Vector list = new Vector();
199         while (st.hasMoreTokens())
200           list.add(st.nextToken());
201         setChannelList(list);
202       }
203       else
204       {
205         setChannelList(new String JavaDoc[] { Syslog.DEFAULT_CHANNEL });
206       }
207     }
208     catch (NamingException x)
209     {
210       setChannelList(new String JavaDoc[] { Syslog.DEFAULT_CHANNEL });
211     }
212   }
213
214   /**
215    * Close the given SQL Connection. If there is an exception,
216    * it is logged using Syslog.
217    *
218    * @see DatabaseUtil#close(Connection, Object)
219    */

220   protected boolean close(Connection c)
221   {
222     return DatabaseUtil.close(c, this);
223   }
224
225   /**
226    * Close the given SQL Statement. If there is an exception,
227    * it is logged using Syslog.
228    *
229    * @see DatabaseUtil#close(Statement, Object)
230    */

231   protected boolean close(Statement s)
232   {
233     return DatabaseUtil.close(s, this);
234   }
235
236   /**
237    * Close the given SQL ResultSet. If there is an exception,
238    * it is logged using Syslog.
239    *
240    * @see DatabaseUtil#close(ResultSet, Object)
241    */

242   protected boolean close(ResultSet r)
243   {
244     return DatabaseUtil.close(r, this);
245   }
246 }
247
Popular Tags