001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: AbstractServlet.java 9280 2006-08-01 10:15:24Z japaz $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.sampleCluster2.web;
025:
026: import javax.naming.Context;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029: import javax.rmi.PortableRemoteObject;
030: import javax.servlet.http.HttpServlet;
031:
032: import org.objectweb.sampleCluster2.ejb.MyEjb1;
033: import org.objectweb.sampleCluster2.ejb.MyEjb1Home;
034: import org.objectweb.sampleCluster2.ejb.MyStateful;
035: import org.objectweb.sampleCluster2.ejb.MyStatefulHome;
036:
037: import org.objectweb.jonas.common.JProp;
038: import org.objectweb.jonas.common.Log;
039:
040: import org.objectweb.util.monolog.api.BasicLevel;
041: import org.objectweb.util.monolog.api.Logger;
042:
043: /**
044: * @author goebelg
045: *
046: * Servlet super class
047: */
048: public abstract class AbstractServlet extends HttpServlet {
049:
050: /**
051: * Servlet initialization
052: */
053: public void init() {
054:
055: if (logger == null) {
056: logger = Log.getLogger("org.objectweb.jonas_tests");
057: }
058: logger.log(BasicLevel.DEBUG, "");
059: }
060:
061: /**
062: * Creates a new Statefull session bean.
063: * @return A new session bean
064: */
065: public MyStateful createStatefulEJB() {
066: Context ctx = null;
067: MyStatefulHome home = null;
068: MyStateful bean = null;
069:
070: try {
071: ctx = new InitialContext();
072: } catch (NamingException e) {
073: e.printStackTrace();
074: }
075:
076: // Lookup bean home
077: String bName = "java:comp/env/ejb/MyStatefulHome";
078: try {
079: home = (MyStatefulHome) PortableRemoteObject.narrow(ctx
080: .lookup(bName), MyStatefulHome.class);
081: bean = home.create();
082: bean.initialize();
083: } catch (Exception e) {
084: e.printStackTrace();
085: }
086: return bean;
087: }
088:
089: /**
090: * Creates a new stateless session bean.
091: * @return a new stateless session bean
092: */
093: public MyEjb1 theEJB() {
094: Context ctx = null;
095: MyEjb1Home home = null;
096: MyEjb1 bean = null;
097:
098: try {
099: ctx = new InitialContext();
100: } catch (NamingException e) {
101: e.printStackTrace();
102: }
103:
104: // Lookup bean home
105: String bName = "java:comp/env/ejb/MyEjb1Home";
106: try {
107: home = (MyEjb1Home) PortableRemoteObject.narrow(ctx
108: .lookup(bName), MyEjb1Home.class);
109: bean = home.create();
110: } catch (Exception e) {
111: e.printStackTrace();
112: }
113: return bean;
114: }
115:
116: /**
117: * Retrieve the name of the JOnAS node where the servlet is executed.
118: * @return The JOnAS node name where the servlet is executed.
119: */
120: public static String getMyJonasInstanceName() {
121:
122: String s = "unknown";
123: try {
124: JProp jp = JProp.getInstance();
125: s = jp.getValue("jonas.name");
126: } catch (Exception e) {
127: e.printStackTrace();
128: }
129: return s;
130: }
131:
132: /**
133: * Get the logger
134: * @return the logger
135: */
136: public Logger getLogger() {
137: return logger;
138: }
139:
140: /**
141: * The logger
142: */
143: private static Logger logger = null;
144: }
|