KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > sampleCluster2 > web > AbstractServlet


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AbstractServlet.java,v 1.4 2005/04/28 12:17:19 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.sampleCluster2.web;
26
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.rmi.PortableRemoteObject JavaDoc;
31 import javax.servlet.http.HttpServlet JavaDoc;
32
33 import org.objectweb.sampleCluster2.ejb.MyEjb1;
34 import org.objectweb.sampleCluster2.ejb.MyEjb1Home;
35 import org.objectweb.sampleCluster2.ejb.MyStateful;
36 import org.objectweb.sampleCluster2.ejb.MyStatefulHome;
37
38 import org.objectweb.jonas.common.JProp;
39 import org.objectweb.jonas.common.Log;
40
41 import org.objectweb.util.monolog.api.BasicLevel;
42 import org.objectweb.util.monolog.api.Logger;
43
44 /**
45  * @author goebelg
46  *
47  * Servlet super class
48  */

49 public abstract class AbstractServlet extends HttpServlet JavaDoc {
50
51     /**
52      * Servlet initialization
53      */

54     public void init() {
55
56         if (logger == null) {
57             logger = Log.getLogger("org.objectweb.jonas_tests");
58         }
59         logger.log(BasicLevel.DEBUG, "");
60     }
61
62     /**
63      * Creates a new Statefull session bean.
64      * @return A new session bean
65      */

66     public MyStateful createStatefulEJB() {
67         Context JavaDoc ctx = null;
68         MyStatefulHome home = null;
69         MyStateful bean = null;
70
71         try {
72             ctx = new InitialContext JavaDoc();
73         } catch (NamingException JavaDoc e) {
74             e.printStackTrace();
75         }
76
77         // Lookup bean home
78
String JavaDoc bName = "java:comp/env/ejb/MyStatefulHome";
79         try {
80             home = (MyStatefulHome) PortableRemoteObject.narrow(ctx.lookup(bName), MyStatefulHome.class);
81             bean = home.create();
82         } catch (Exception JavaDoc e) {
83             e.printStackTrace();
84         }
85         return bean;
86     }
87
88     /**
89      * Creates a new stateless session bean.
90      * @return a new stateless session bean
91      */

92     public MyEjb1 theEJB() {
93         Context JavaDoc ctx = null;
94         MyEjb1Home home = null;
95         MyEjb1 bean = null;
96
97         try {
98             ctx = new InitialContext JavaDoc();
99         } catch (NamingException JavaDoc e) {
100             e.printStackTrace();
101         }
102
103         // Lookup bean home
104
String JavaDoc bName = "java:comp/env/ejb/MyEjb1Home";
105         try {
106             home = (MyEjb1Home) PortableRemoteObject.narrow(ctx.lookup(bName), MyEjb1Home.class);
107             bean = home.create();
108         } catch (Exception JavaDoc e) {
109             e.printStackTrace();
110         }
111         return bean;
112     }
113
114     /**
115      * Retrieve the name of the JOnAS node where the servlet is executed.
116      * @return The JOnAS node name where the servlet is executed.
117      */

118     public static String JavaDoc getMyJonasInstanceName() {
119
120         String JavaDoc s = "unknown";
121         try {
122             JProp jp = JProp.getInstance();
123             s = jp.getValue("jonas.name");
124         } catch (Exception JavaDoc e) {
125             e.printStackTrace();
126         }
127         return s;
128     }
129
130     /**
131      * Get the logger
132      * @return the logger
133      */

134     public Logger getLogger() {
135         return logger;
136     }
137
138     /**
139      * The logger
140      */

141     private static Logger logger = null;
142 }
143
144
Popular Tags