blob: f836afdd7086e2729d4fb0bedb15c6eb9749cdc5 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>EJB Examples</title>
<meta name="description" content="Apache TomEE">
<meta name="author" content="Apache TomEE">
<meta name="google-translate-customization" content="f36a520c08f4c9-0a04e86a9c075ce9-g265f3196f697cf8f-10">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate, max-age=0">
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le styles -->
<link href="./../../resources/css/bootstrap.css" rel="stylesheet">
<link href="./../../resources/css/prettify.css" rel="stylesheet">
<!--link href="./../../resources/css/bootstrap-mods.css" rel="stylesheet"-->
<link href="./../../resources/css/main.css" rel="stylesheet">
<link href="./../../resources/font-awesome-4.6.3/css/font-awesome.min.css" rel="stylesheet">
<script type="text/javascript">
var t = encodeURIComponent(document.title.replace(/^\s+|\s+$/g,""));
var u = encodeURIComponent(""+document.URL);
function fbshare () {
window.open(
"http://www.facebook.com/sharer/sharer.php?u="+u,
'Share on Facebook',
'width=640,height=426');
};
function gpshare () {
window.open(
"https://plus.google.com/share?url="+u,
'Share on Google+',
'width=584,height=385');
};
function twshare () {
window.open(
"https://twitter.com/intent/tweet?url="+u+"&text="+t,
'Share on Twitter',
'width=800,height=526');
};
function pinshare () {
window.open("//www.pinterest.com/pin/create/button/?url="+u+"&media=http%3A%2F%2Ftomee.apache.org%2Fresources%2Fimages%2Ffeather-logo.png&description="+t,
'Share on Pinterest',
'width=800,height=526');
};
</script>
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="./../../favicon.ico">
<link rel="apple-touch-icon" href="./../../resources/images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="./../../resources/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="./../../resources/images/apple-touch-icon-114x114.png">
<script src="./../../resources/js/prettify.js" type="text/javascript"></script>
<script src="./../../resources/js/jquery-latest.js"></script>
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
<script src="./../../resources/js/common.js"></script>
<script src="./../../resources/js/prettyprint.js"></script>
<!--script src="//assets.pinterest.com/js/pinit.js" type="text/javascript" async></script//-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2717626-1']);
_gaq.push(['_setDomainName', 'apache.org']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="topbar" data-dropdown="dropdown">
<div class="fill">
<div class="container">
<a class="brand" href="./../../index.html">Apache TomEE</a>
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
Apache
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<!-- <li><a href="./../../misc/whoweare.html">Who we are?</a></li> -->
<!-- <li><a href="./../../misc/heritage.html">Heritage</a></li> -->
<li><a href="http://www.apache.org">Apache Home</a></li>
<!-- <li><a href="./../../misc/resources.html">Resources</a></li> -->
<li><a href="./../../misc/contact.html">Contact</a></li>
<li><a href="./../../misc/legal.html">Legal</a></li>
<li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="http://www.apache.org/foundation/thanks.html">Thanks</a></li>
<li class="divider"/>
<li><a href="http://www.apache.org/security">Security</a></li>
</ul>
</li>
<li><a href="./../../index.html">Home</a></li>
<li><a href="./../../downloads.html">Downloads</a></li>
<li><a href="./../../documentation.html">Documentation</a></li>
<li><a href="./../../examples-trunk/index.html">Examples</a></li>
<li><a href="./../../support.html">Support</a></li>
<li><a href="./../../contribute.html">Contribute</a></li>
<li><a href="./../../security/index.html">Security</a></li>
</ul>
<!-- Google CSE Search Box Begins -->
<FORM class="pull-right" id="searchbox_010475492895890475512:_t4iqjrgx90" action="http://www.google.com/cse">
<INPUT type="hidden" name="cx" value="010475492895890475512:_t4iqjrgx90">
<INPUT type="hidden" name="cof" value="FORID:0">
<INPUT size="18" width="130" style="width:130px" name="q" type="text" placeholder="Search">
</FORM>
<!--<SCRIPT type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_010475492895890475512:_t4iqjrgx90"></SCRIPT>-->
<!-- Google CSE Search Box Ends -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="span8">
<small><a href="./../../index.html">Home</a>&nbsp;&raquo&nbsp;<a href="./../../examples-trunk/">Examples Trunk</a>&nbsp;&raquo&nbsp;<a href="./../../examples-trunk/ejb-examples/">Ejb Examples</a></small><br>
</div>
<div class="span8">
</div>
</div>
&nbsp;
<div class="page-header">
<h1>EJB Examples
<div style="float: right; position: relative; bottom: -10px; ">
<div id="google_translate_element"></div><script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}
</script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<a onclick="javascript:gpshare()" class="gp-share sprite" title="Share on Google+">share [gp]</a>
<a onclick="javascript:fbshare()" class="fb-share sprite" title="Share on Facebook">share [fb]</a>
<a onclick="javascript:twshare()" class="tw-share sprite" title="Share on Twitter">share [tw]</a>
<a onclick="javascript:pinshare()" class="pin-share sprite" title="Share on Pinterest">share [pin]</a>
<a data-toggle="modal" href="#edit" class="edit-page" title="Contribute to this Page">contribute</a>
</div>
</h1>
</div>
<p><em>Help us document this example! Click the blue pencil icon in the upper right to edit this page.</em></p>
<h2>AnnotatedEJB</h2>
<pre><code>package org.superbiz.servlet;
import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.sql.DataSource;
@Stateless
@LocalBean
public class AnnotatedEJB implements AnnotatedEJBLocal, AnnotatedEJBRemote {
@Resource
private DataSource ds;
private String name = "foo";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DataSource getDs() {
return ds;
}
public void setDs(DataSource ds) {
this.ds = ds;
}
public String toString() {
return "AnnotatedEJB[name=" + name + "]";
}
}
</code></pre>
<h2>AnnotatedEJBLocal</h2>
<pre><code>package org.superbiz.servlet;
import javax.ejb.Local;
import javax.sql.DataSource;
@Local
public interface AnnotatedEJBLocal {
String getName();
void setName(String name);
DataSource getDs();
void setDs(DataSource ds);
}
</code></pre>
<h2>AnnotatedEJBRemote</h2>
<pre><code>package org.superbiz.servlet;
import javax.ejb.Remote;
@Remote
public interface AnnotatedEJBRemote {
String getName();
void setName(String name);
}
</code></pre>
<h2>AnnotatedServlet</h2>
<pre><code>package org.superbiz.servlet;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
public class AnnotatedServlet extends HttpServlet {
@EJB
private AnnotatedEJBLocal localEJB;
@EJB
private AnnotatedEJBRemote remoteEJB;
@EJB
private AnnotatedEJB localbeanEJB;
@Resource
private DataSource ds;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
ServletOutputStream out = response.getOutputStream();
out.println("LocalBean EJB");
out.println("@EJB=" + localbeanEJB);
if (localbeanEJB != null) {
out.println("@EJB.getName()=" + localbeanEJB.getName());
out.println("@EJB.getDs()=" + localbeanEJB.getDs());
}
out.println("JNDI=" + lookupField("localbeanEJB"));
out.println();
out.println("Local EJB");
out.println("@EJB=" + localEJB);
if (localEJB != null) {
out.println("@EJB.getName()=" + localEJB.getName());
out.println("@EJB.getDs()=" + localEJB.getDs());
}
out.println("JNDI=" + lookupField("localEJB"));
out.println();
out.println("Remote EJB");
out.println("@EJB=" + remoteEJB);
if (localEJB != null) {
out.println("@EJB.getName()=" + remoteEJB.getName());
}
out.println("JNDI=" + lookupField("remoteEJB"));
out.println();
out.println("DataSource");
out.println("@Resource=" + ds);
out.println("JNDI=" + lookupField("ds"));
}
private Object lookupField(String name) {
try {
return new InitialContext().lookup("java:comp/env/" + getClass().getName() + "/" + name);
} catch (NamingException e) {
return null;
}
}
}
</code></pre>
<h2>ClientHandler</h2>
<pre><code>package org.superbiz.servlet;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.MessageContext;
public class ClientHandler implements Handler {
public boolean handleMessage(MessageContext messageContext) {
WebserviceServlet.write(" ClientHandler handleMessage");
return true;
}
public void close(MessageContext messageContext) {
WebserviceServlet.write(" ClientHandler close");
}
public boolean handleFault(MessageContext messageContext) {
WebserviceServlet.write(" ClientHandler handleFault");
return true;
}
}
</code></pre>
<h2>HelloEjb</h2>
<pre><code>package org.superbiz.servlet;
import javax.jws.WebService;
@WebService(targetNamespace = "http://examples.org/wsdl")
public interface HelloEjb {
String hello(String name);
}
</code></pre>
<h2>HelloEjbService</h2>
<pre><code>package org.superbiz.servlet;
import javax.ejb.Stateless;
import javax.jws.HandlerChain;
import javax.jws.WebService;
@WebService(
portName = "HelloEjbPort",
serviceName = "HelloEjbService",
targetNamespace = "http://examples.org/wsdl",
endpointInterface = "org.superbiz.servlet.HelloEjb"
)
@HandlerChain(file = "server-handlers.xml")
@Stateless
public class HelloEjbService implements HelloEjb {
public String hello(String name) {
WebserviceServlet.write(" HelloEjbService hello(" + name + ")");
if (name == null) name = "World";
return "Hello " + name + " from EJB Webservice!";
}
}
</code></pre>
<h2>HelloPojo</h2>
<pre><code>package org.superbiz.servlet;
import javax.jws.WebService;
@WebService(targetNamespace = "http://examples.org/wsdl")
public interface HelloPojo {
String hello(String name);
}
</code></pre>
<h2>HelloPojoService</h2>
<pre><code>package org.superbiz.servlet;
import javax.jws.HandlerChain;
import javax.jws.WebService;
@WebService(
portName = "HelloPojoPort",
serviceName = "HelloPojoService",
targetNamespace = "http://examples.org/wsdl",
endpointInterface = "org.superbiz.servlet.HelloPojo"
)
@HandlerChain(file = "server-handlers.xml")
public class HelloPojoService implements HelloPojo {
public String hello(String name) {
WebserviceServlet.write(" HelloPojoService hello(" + name + ")");
if (name == null) name = "World";
return "Hello " + name + " from Pojo Webservice!";
}
}
</code></pre>
<h2>JndiServlet</h2>
<pre><code>package org.superbiz.servlet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class JndiServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
ServletOutputStream out = response.getOutputStream();
Map&lt;String, Object&gt; bindings = new TreeMap&lt;String, Object&gt;(String.CASE_INSENSITIVE_ORDER);
try {
Context context = (Context) new InitialContext().lookup("java:comp/");
addBindings("", bindings, context);
} catch (NamingException e) {
throw new ServletException(e);
}
out.println("JNDI Context:");
for (Map.Entry&lt;String, Object&gt; entry : bindings.entrySet()) {
if (entry.getValue() != null) {
out.println(" " + entry.getKey() + "=" + entry.getValue());
} else {
out.println(" " + entry.getKey());
}
}
}
private void addBindings(String path, Map&lt;String, Object&gt; bindings, Context context) {
try {
for (NameClassPair pair : Collections.list(context.list(""))) {
String name = pair.getName();
String className = pair.getClassName();
if ("org.apache.naming.resources.FileDirContext$FileResource".equals(className)) {
bindings.put(path + name, "&lt;file&gt;");
} else {
try {
Object value = context.lookup(name);
if (value instanceof Context) {
Context nextedContext = (Context) value;
bindings.put(path + name, "");
addBindings(path + name + "/", bindings, nextedContext);
} else {
bindings.put(path + name, value);
}
} catch (NamingException e) {
// lookup failed
bindings.put(path + name, "ERROR: " + e.getMessage());
}
}
}
} catch (NamingException e) {
bindings.put(path, "ERROR: list bindings threw an exception: " + e.getMessage());
}
}
}
</code></pre>
<h2>JpaBean</h2>
<pre><code>package org.superbiz.servlet;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class JpaBean {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "[JpaBean id=" + id + ", name=" + name + "]";
}
}
</code></pre>
<h2>JpaServlet</h2>
<pre><code>package org.superbiz.servlet;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class JpaServlet extends HttpServlet {
@PersistenceUnit(name = "jpa-example")
private EntityManagerFactory emf;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
ServletOutputStream out = response.getOutputStream();
out.println("@PersistenceUnit=" + emf);
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
transaction.begin();
JpaBean jpaBean = new JpaBean();
jpaBean.setName("JpaBean");
em.persist(jpaBean);
transaction.commit();
transaction.begin();
Query query = em.createQuery("SELECT j FROM JpaBean j WHERE j.name='JpaBean'");
jpaBean = (JpaBean) query.getSingleResult();
out.println("Loaded " + jpaBean);
em.remove(jpaBean);
transaction.commit();
transaction.begin();
query = em.createQuery("SELECT count(j) FROM JpaBean j WHERE j.name='JpaBean'");
int count = ((Number) query.getSingleResult()).intValue();
if (count == 0) {
out.println("Removed " + jpaBean);
} else {
out.println("ERROR: unable to remove" + jpaBean);
}
transaction.commit();
}
}
</code></pre>
<h2>ResourceBean</h2>
<pre><code>package org.superbiz.servlet;
public class ResourceBean {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String toString() {
return "[ResourceBean " + value + "]";
}
}
</code></pre>
<h2>RunAsServlet</h2>
<pre><code>package org.superbiz.servlet;
import javax.ejb.EJB;
import javax.ejb.EJBAccessException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;
public class RunAsServlet extends HttpServlet {
@EJB
private SecureEJBLocal secureEJBLocal;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
ServletOutputStream out = response.getOutputStream();
out.println("Servlet");
Principal principal = request.getUserPrincipal();
if (principal != null) {
out.println("Servlet.getUserPrincipal()=" + principal + " [" + principal.getName() + "]");
} else {
out.println("Servlet.getUserPrincipal()=&lt;null&gt;");
}
out.println("Servlet.isCallerInRole(\"user\")=" + request.isUserInRole("user"));
out.println("Servlet.isCallerInRole(\"manager\")=" + request.isUserInRole("manager"));
out.println("Servlet.isCallerInRole(\"fake\")=" + request.isUserInRole("fake"));
out.println();
out.println("@EJB=" + secureEJBLocal);
if (secureEJBLocal != null) {
principal = secureEJBLocal.getCallerPrincipal();
if (principal != null) {
out.println("@EJB.getCallerPrincipal()=" + principal + " [" + principal.getName() + "]");
} else {
out.println("@EJB.getCallerPrincipal()=&lt;null&gt;");
}
out.println("@EJB.isCallerInRole(\"user\")=" + secureEJBLocal.isCallerInRole("user"));
out.println("@EJB.isCallerInRole(\"manager\")=" + secureEJBLocal.isCallerInRole("manager"));
out.println("@EJB.isCallerInRole(\"fake\")=" + secureEJBLocal.isCallerInRole("fake"));
try {
secureEJBLocal.allowUserMethod();
out.println("@EJB.allowUserMethod() ALLOWED");
} catch (EJBAccessException e) {
out.println("@EJB.allowUserMethod() DENIED");
}
try {
secureEJBLocal.allowManagerMethod();
out.println("@EJB.allowManagerMethod() ALLOWED");
} catch (EJBAccessException e) {
out.println("@EJB.allowManagerMethod() DENIED");
}
try {
secureEJBLocal.allowFakeMethod();
out.println("@EJB.allowFakeMethod() ALLOWED");
} catch (EJBAccessException e) {
out.println("@EJB.allowFakeMethod() DENIED");
}
try {
secureEJBLocal.denyAllMethod();
out.println("@EJB.denyAllMethod() ALLOWED");
} catch (EJBAccessException e) {
out.println("@EJB.denyAllMethod() DENIED");
}
}
out.println();
}
}
</code></pre>
<h2>SecureEJB</h2>
<pre><code>package org.superbiz.servlet;
import javax.annotation.Resource;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.DenyAll;
import javax.annotation.security.RolesAllowed;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import java.security.Principal;
@Stateless
@DeclareRoles({"user", "manager", "fake"})
public class SecureEJB implements SecureEJBLocal {
@Resource
private SessionContext context;
public Principal getCallerPrincipal() {
return context.getCallerPrincipal();
}
public boolean isCallerInRole(String role) {
return context.isCallerInRole(role);
}
@RolesAllowed("user")
public void allowUserMethod() {
}
@RolesAllowed("manager")
public void allowManagerMethod() {
}
@RolesAllowed("fake")
public void allowFakeMethod() {
}
@DenyAll
public void denyAllMethod() {
}
public String toString() {
return "SecureEJB[userName=" + getCallerPrincipal() + "]";
}
}
</code></pre>
<h2>SecureEJBLocal</h2>
<pre><code>package org.superbiz.servlet;
import javax.ejb.Local;
import java.security.Principal;
@Local
public interface SecureEJBLocal {
Principal getCallerPrincipal();
boolean isCallerInRole(String role);
void allowUserMethod();
void allowManagerMethod();
void allowFakeMethod();
void denyAllMethod();
}
</code></pre>
<h2>SecureServlet</h2>
<pre><code>package org.superbiz.servlet;
import javax.ejb.EJB;
import javax.ejb.EJBAccessException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;
public class SecureServlet extends HttpServlet {
@EJB
private SecureEJBLocal secureEJBLocal;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
ServletOutputStream out = response.getOutputStream();
out.println("Servlet");
Principal principal = request.getUserPrincipal();
if (principal != null) {
out.println("Servlet.getUserPrincipal()=" + principal + " [" + principal.getName() + "]");
} else {
out.println("Servlet.getUserPrincipal()=&lt;null&gt;");
}
out.println("Servlet.isCallerInRole(\"user\")=" + request.isUserInRole("user"));
out.println("Servlet.isCallerInRole(\"manager\")=" + request.isUserInRole("manager"));
out.println("Servlet.isCallerInRole(\"fake\")=" + request.isUserInRole("fake"));
out.println();
out.println("@EJB=" + secureEJBLocal);
if (secureEJBLocal != null) {
principal = secureEJBLocal.getCallerPrincipal();
if (principal != null) {
out.println("@EJB.getCallerPrincipal()=" + principal + " [" + principal.getName() + "]");
} else {
out.println("@EJB.getCallerPrincipal()=&lt;null&gt;");
}
out.println("@EJB.isCallerInRole(\"user\")=" + secureEJBLocal.isCallerInRole("user"));
out.println("@EJB.isCallerInRole(\"manager\")=" + secureEJBLocal.isCallerInRole("manager"));
out.println("@EJB.isCallerInRole(\"fake\")=" + secureEJBLocal.isCallerInRole("fake"));
try {
secureEJBLocal.allowUserMethod();
out.println("@EJB.allowUserMethod() ALLOWED");
} catch (EJBAccessException e) {
out.println("@EJB.allowUserMethod() DENIED");
}
try {
secureEJBLocal.allowManagerMethod();
out.println("@EJB.allowManagerMethod() ALLOWED");
} catch (EJBAccessException e) {
out.println("@EJB.allowManagerMethod() DENIED");
}
try {
secureEJBLocal.allowFakeMethod();
out.println("@EJB.allowFakeMethod() ALLOWED");
} catch (EJBAccessException e) {
out.println("@EJB.allowFakeMethod() DENIED");
}
try {
secureEJBLocal.denyAllMethod();
out.println("@EJB.denyAllMethod() ALLOWED");
} catch (EJBAccessException e) {
out.println("@EJB.denyAllMethod() DENIED");
}
}
out.println();
}
}
</code></pre>
<h2>ServerHandler</h2>
<pre><code>package org.superbiz.servlet;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.MessageContext;
public class ServerHandler implements Handler {
public boolean handleMessage(MessageContext messageContext) {
WebserviceServlet.write(" ServerHandler handleMessage");
return true;
}
public void close(MessageContext messageContext) {
WebserviceServlet.write(" ServerHandler close");
}
public boolean handleFault(MessageContext messageContext) {
WebserviceServlet.write(" ServerHandler handleFault");
return true;
}
}
</code></pre>
<h2>WebserviceClient</h2>
<pre><code>package org.superbiz.servlet;
import javax.xml.ws.Service;
import java.io.PrintStream;
import java.net.URL;
public class WebserviceClient {
/**
* Unfortunately, to run this example with CXF you need to have a HUGE class path. This
* is just what is required to run CXF:
* &lt;p/&gt;
* jaxb-api-2.0.jar
* jaxb-impl-2.0.3.jar
* &lt;p/&gt;
* saaj-api-1.3.jar
* saaj-impl-1.3.jar
* &lt;p/&gt;
* &lt;p/&gt;
* cxf-api-2.0.2-incubator.jar
* cxf-common-utilities-2.0.2-incubator.jar
* cxf-rt-bindings-soap-2.0.2-incubator.jar
* cxf-rt-core-2.0.2-incubator.jar
* cxf-rt-databinding-jaxb-2.0.2-incubator.jar
* cxf-rt-frontend-jaxws-2.0.2-incubator.jar
* cxf-rt-frontend-simple-2.0.2-incubator.jar
* cxf-rt-transports-http-jetty-2.0.2-incubator.jar
* cxf-rt-transports-http-2.0.2-incubator.jar
* cxf-tools-common-2.0.2-incubator.jar
* &lt;p/&gt;
* geronimo-activation_1.1_spec-1.0.jar
* geronimo-annotation_1.0_spec-1.1.jar
* geronimo-ejb_3.0_spec-1.0.jar
* geronimo-jpa_3.0_spec-1.1.jar
* geronimo-servlet_2.5_spec-1.1.jar
* geronimo-stax-api_1.0_spec-1.0.jar
* jaxws-api-2.0.jar
* axis2-jws-api-1.3.jar
* &lt;p/&gt;
* wsdl4j-1.6.1.jar
* xml-resolver-1.2.jar
* XmlSchema-1.3.1.jar
*/
public static void main(String[] args) throws Exception {
PrintStream out = System.out;
Service helloPojoService = Service.create(new URL("http://localhost:8080/ejb-examples/hello?wsdl"), null);
HelloPojo helloPojo = helloPojoService.getPort(HelloPojo.class);
out.println();
out.println("Pojo Webservice");
out.println(" helloPojo.hello(\"Bob\")=" + helloPojo.hello("Bob"));
out.println(" helloPojo.hello(null)=" + helloPojo.hello(null));
out.println();
Service helloEjbService = Service.create(new URL("http://localhost:8080/HelloEjbService?wsdl"), null);
HelloEjb helloEjb = helloEjbService.getPort(HelloEjb.class);
out.println();
out.println("EJB Webservice");
out.println(" helloEjb.hello(\"Bob\")=" + helloEjb.hello("Bob"));
out.println(" helloEjb.hello(null)=" + helloEjb.hello(null));
out.println();
}
}
</code></pre>
<h2>WebserviceServlet</h2>
<pre><code>package org.superbiz.servlet;
import javax.jws.HandlerChain;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceRef;
import java.io.IOException;
public class WebserviceServlet extends HttpServlet {
@WebServiceRef
@HandlerChain(file = "client-handlers.xml")
private HelloPojo helloPojo;
@WebServiceRef
@HandlerChain(file = "client-handlers.xml")
private HelloEjb helloEjb;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
ServletOutputStream out = response.getOutputStream();
OUT = out;
try {
out.println("Pojo Webservice");
out.println(" helloPojo.hello(\"Bob\")=" + helloPojo.hello("Bob"));
out.println();
out.println(" helloPojo.hello(null)=" + helloPojo.hello(null));
out.println();
out.println("EJB Webservice");
out.println(" helloEjb.hello(\"Bob\")=" + helloEjb.hello("Bob"));
out.println();
out.println(" helloEjb.hello(null)=" + helloEjb.hello(null));
out.println();
} finally {
OUT = out;
}
}
private static ServletOutputStream OUT;
public static void write(String message) {
try {
ServletOutputStream out = OUT;
out.println(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
</code></pre>
<h2>persistence.xml</h2>
<pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"&gt;
&lt;persistence-unit transaction-type="RESOURCE_LOCAL" name="jpa-example"&gt;
&lt;jta-data-source&gt;java:openejb/Connector/Default JDBC Database&lt;/jta-data-source&gt;
&lt;non-jta-data-source&gt;java:openejb/Connector/Default Unmanaged JDBC Database&lt;/non-jta-data-source&gt;
&lt;class&gt;org.superbiz.servlet.JpaBean&lt;/class&gt;
&lt;properties&gt;
&lt;property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/&gt;
&lt;/properties&gt;
&lt;/persistence-unit&gt;
&lt;/persistence&gt;
</code></pre>
<h2>client-handlers.xml</h2>
<pre><code>&lt;jws:handler-chains xmlns:jws="http://java.sun.com/xml/ns/javaee"&gt;
&lt;jws:handler-chain&gt;
&lt;jws:handler&gt;
&lt;jws:handler-name&gt;ClientHandler&lt;/jws:handler-name&gt;
&lt;jws:handler-class&gt;org.superbiz.servlet.ClientHandler&lt;/jws:handler-class&gt;
&lt;/jws:handler&gt;
&lt;/jws:handler-chain&gt;
&lt;/jws:handler-chains&gt;
</code></pre>
<h2>server-handlers.xml</h2>
<pre><code>&lt;jws:handler-chains xmlns:jws="http://java.sun.com/xml/ns/javaee"&gt;
&lt;jws:handler-chain&gt;
&lt;jws:handler&gt;
&lt;jws:handler-name&gt;ServerHandler&lt;/jws:handler-name&gt;
&lt;jws:handler-class&gt;org.superbiz.servlet.ServerHandler&lt;/jws:handler-class&gt;
&lt;/jws:handler&gt;
&lt;/jws:handler-chain&gt;
&lt;/jws:handler-chains&gt;
</code></pre>
<h2>context.xml</h2>
<pre><code>&lt;Context&gt;
&lt;!-- This only works if the context is installed under the correct name --&gt;
&lt;Realm className="org.apache.catalina.realm.MemoryRealm"
pathname="webapps/ejb-examples-1.0-SNAPSHOT/WEB-INF/tomcat-users.xml"/&gt;
&lt;Environment
name="context.xml/environment"
value="ContextString"
type="java.lang.String"/&gt;
&lt;Resource
name="context.xml/resource"
auth="Container"
type="org.superbiz.servlet.ResourceBean"
factory="org.apache.naming.factory.BeanFactory"
value="ContextResource"/&gt;
&lt;ResourceLink
name="context.xml/resource-link"
global="server.xml/environment"
type="java.lang.String"/&gt;
&lt;!-- web.xml resources --&gt;
&lt;Resource
name="web.xml/resource-env-ref"
auth="Container"
type="org.superbiz.servlet.ResourceBean"
factory="org.apache.naming.factory.BeanFactory"
value="ContextResourceEnvRef"/&gt;
&lt;Resource
name="web.xml/resource-ref"
auth="Container"
type="org.superbiz.servlet.ResourceBean"
factory="org.apache.naming.factory.BeanFactory"
value="ContextResourceRef"/&gt;
&lt;ResourceLink
name="web.xml/resource-link"
global="server.xml/environment"
type="java.lang.String"/&gt;
&lt;/Context&gt;
</code></pre>
<h2>jetty-web.xml</h2>
<pre><code>&lt;Configure class="org.eclipse.jetty.webapp.WebAppContext"&gt;
&lt;Get name="securityHandler"&gt;
&lt;Set name="loginService"&gt;
&lt;New class="org.eclipse.jetty.security.HashLoginService"&gt;
&lt;Set name="name"&gt;Test Realm&lt;/Set&gt;
&lt;Set name="config"&gt;&lt;SystemProperty name="jetty.home" default="."/&gt;/etc/realm.properties
&lt;/Set&gt;
&lt;/New&gt;
&lt;/Set&gt;
&lt;/Get&gt;
&lt;/Configure&gt;
</code></pre>
<h2>tomcat-users.xml</h2>
<pre><code>&lt;tomcat-users&gt;
&lt;user name="manager" password="manager" roles="manager,user"/&gt;
&lt;user name="user" password="user" roles="user"/&gt;
&lt;/tomcat-users&gt;
</code></pre>
<h2>web.xml</h2>
<pre><code>&lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
metadata-complete="false"
version="2.5"&gt;
&lt;display-name&gt;OpenEJB Servlet Examples&lt;/display-name&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;AnnotatedServlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.superbiz.servlet.AnnotatedServlet&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;AnnotatedServlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/annotated/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;JpaServlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.superbiz.servlet.JpaServlet&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;JpaServlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/jpa/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;JndiServlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.superbiz.servlet.JndiServlet&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;JndiServlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/jndi/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;RunAsServlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.superbiz.servlet.RunAsServlet&lt;/servlet-class&gt;
&lt;run-as&gt;
&lt;role-name&gt;fake&lt;/role-name&gt;
&lt;/run-as&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;RunAsServlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/runas/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;SecureServlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.superbiz.servlet.SecureServlet&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;SecureServlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/secure/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;security-constraint&gt;
&lt;web-resource-collection&gt;
&lt;web-resource-name&gt;Secure Area&lt;/web-resource-name&gt;
&lt;url-pattern&gt;/secure/*&lt;/url-pattern&gt;
&lt;url-pattern&gt;/runas/*&lt;/url-pattern&gt;
&lt;/web-resource-collection&gt;
&lt;auth-constraint&gt;
&lt;role-name&gt;user&lt;/role-name&gt;
&lt;/auth-constraint&gt;
&lt;/security-constraint&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;WebserviceServlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.superbiz.servlet.WebserviceServlet&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;WebserviceServlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/webservice/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;HelloPojoService&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.superbiz.servlet.HelloPojoService&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;HelloPojoService&lt;/servlet-name&gt;
&lt;url-pattern&gt;/hello&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;login-config&gt;
&lt;auth-method&gt;BASIC&lt;/auth-method&gt;
&lt;/login-config&gt;
&lt;security-role&gt;
&lt;role-name&gt;manager&lt;/role-name&gt;
&lt;/security-role&gt;
&lt;security-role&gt;
&lt;role-name&gt;user&lt;/role-name&gt;
&lt;/security-role&gt;
&lt;env-entry&gt;
&lt;env-entry-name&gt;web.xml/env-entry&lt;/env-entry-name&gt;
&lt;env-entry-type&gt;java.lang.String&lt;/env-entry-type&gt;
&lt;env-entry-value&gt;WebValue&lt;/env-entry-value&gt;
&lt;/env-entry&gt;
&lt;resource-ref&gt;
&lt;res-ref-name&gt;web.xml/Data Source&lt;/res-ref-name&gt;
&lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
&lt;res-auth&gt;Container&lt;/res-auth&gt;
&lt;/resource-ref&gt;
&lt;resource-env-ref&gt;
&lt;resource-env-ref-name&gt;web.xml/Queue&lt;/resource-env-ref-name&gt;
&lt;resource-env-ref-type&gt;javax.jms.Queue&lt;/resource-env-ref-type&gt;
&lt;/resource-env-ref&gt;
&lt;ejb-ref&gt;
&lt;ejb-ref-name&gt;web.xml/EjbRemote&lt;/ejb-ref-name&gt;
&lt;ejb-ref-type&gt;Session&lt;/ejb-ref-type&gt;
&lt;remote&gt;org.superbiz.servlet.AnnotatedEJBRemote&lt;/remote&gt;
&lt;/ejb-ref&gt;
&lt;ejb-local-ref&gt;
&lt;ejb-ref-name&gt;web.xml/EjLocal&lt;/ejb-ref-name&gt;
&lt;ejb-ref-type&gt;Session&lt;/ejb-ref-type&gt;
&lt;local&gt;org.superbiz.servlet.AnnotatedEJBLocal&lt;/local&gt;
&lt;/ejb-local-ref&gt;
&lt;persistence-unit-ref&gt;
&lt;persistence-unit-ref-name&gt;web.xml/PersistenceUnit&lt;/persistence-unit-ref-name&gt;
&lt;persistence-unit-name&gt;jpa-example&lt;/persistence-unit-name&gt;
&lt;/persistence-unit-ref&gt;
&lt;persistence-context-ref&gt;
&lt;persistence-context-ref-name&gt;web.xml/PersistenceContext&lt;/persistence-context-ref-name&gt;
&lt;persistence-unit-name&gt;jpa-example&lt;/persistence-unit-name&gt;
&lt;persistence-context-type&gt;Transactional&lt;/persistence-context-type&gt;
&lt;/persistence-context-ref&gt;
&lt;/web-app&gt;
</code></pre>
<div class="page-header">&nbsp;</div>
<h4>APIs Used</h4>
<ul><li><a href="http://docs.oracle.com/javaee/6/api/javax/annotation/Resource.html">javax.annotation.Resource</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/annotation/security/DeclareRoles.html">javax.annotation.security.DeclareRoles</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/annotation/security/DenyAll.html">javax.annotation.security.DenyAll</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/annotation/security/RolesAllowed.html">javax.annotation.security.RolesAllowed</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/ejb/EJB.html">javax.ejb.EJB</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/ejb/EJBAccessException.html">javax.ejb.EJBAccessException</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/ejb/Local.html">javax.ejb.Local</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/ejb/LocalBean.html">javax.ejb.LocalBean</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/ejb/Remote.html">javax.ejb.Remote</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/ejb/SessionContext.html">javax.ejb.SessionContext</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/ejb/Stateless.html">javax.ejb.Stateless</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/jws/HandlerChain.html">javax.jws.HandlerChain</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/jws/WebService.html">javax.jws.WebService</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/naming/Context.html">javax.naming.Context</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/naming/InitialContext.html">javax.naming.InitialContext</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/naming/NameClassPair.html">javax.naming.NameClassPair</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/naming/NamingException.html">javax.naming.NamingException</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/persistence/Column.html">javax.persistence.Column</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/persistence/Entity.html">javax.persistence.Entity</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html">javax.persistence.EntityManager</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManagerFactory.html">javax.persistence.EntityManagerFactory</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/persistence/EntityTransaction.html">javax.persistence.EntityTransaction</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/persistence/GeneratedValue.html">javax.persistence.GeneratedValue</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/persistence/GenerationType.html">javax.persistence.GenerationType</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/persistence/Id.html">javax.persistence.Id</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/persistence/PersistenceUnit.html">javax.persistence.PersistenceUnit</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/persistence/Query.html">javax.persistence.Query</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletException.html">javax.servlet.ServletException</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletOutputStream.html">javax.servlet.ServletOutputStream</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html">javax.servlet.http.HttpServlet</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html">javax.servlet.http.HttpServletRequest</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html">javax.servlet.http.HttpServletResponse</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/sql/DataSource.html">javax.sql.DataSource</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/xml/ws/Service.html">javax.xml.ws.Service</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/xml/ws/WebServiceRef.html">javax.xml.ws.WebServiceRef</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/xml/ws/handler/Handler.html">javax.xml.ws.handler.Handler</a></li>
<li><a href="http://docs.oracle.com/javaee/6/api/javax/xml/ws/handler/MessageContext.html">javax.xml.ws.handler.MessageContext</a></li>
</ul>
<h3>Source</h3>
<ul>
<li>Apache <a href="http://svn.apache.org/repos/asf/tomee/tomee/trunk/examples/ejb-examples">ejb-examples</a></li>
<li>Github <a href="https://github.com/apache/tomee/tree/trunk/examples/ejb-examples">ejb-examples</a></li>
</ul>
<pre>
svn co http://svn.apache.org/repos/asf/tomee/tomee/trunk/examples/ejb-examples
cd ejb-examples
mvn clean install
</pre>
<div id="edit" class="modal hide fade in" style="display: none; ">
<div class="modal-header">
<a class="close" data-dismiss="modal">x</a>
<h3>Thank you for contributing to the documentation!</h3>
</div>
<div class="modal-body">
<h4>Any help with the documentation is greatly appreciated.</h4>
<p>All edits are reviewed before going live, so feel free to do much more than fix typos or links. If you see a page that could benefit from an entire rewrite, we'd be thrilled to review it. Don't be surprised if we like it so much we ask you for help with other pages :)</p>
<small>NOTICE: unless indicated otherwise on the pages in question, all editable content available from apache.org is presumed to be licensed under the Apache License (AL) version 2.0 and hence all submissions to apache.org treated as formal Contributions under the license terms.</small>
<!--[if gt IE 6]>
<h4>Internet Explorer Users</h4>
<p>If you are not an Apache committer, click the Yes link and enter a <i>anonymous</i> for the username and leave the password empty</p>
<![endif]-->
</div>
<div class="modal-footer">
Do you have an Apache ID?
<a href="javascript:void(location.href='https://cms.apache.org/redirect?uri='+escape(location.href))" class="btn">Yes</a>
<a href="javascript:void(location.href='https://anonymous:@cms.apache.org/redirect?uri='+escape(location.href))" class="btn">No</a>
</div>
</div>
<script src="./../../resources/js/bootstrap-modal.js"></script>
<footer>
<p>Copyright &copy; 1999-2016 The Apache Software Foundation, Licensed under the Apache License, Version 2.0.
Apache TomEE, TomEE, Apache, the Apache feather logo, and the Apache TomEE project logo are trademarks of The Apache Software Foundation.
All other marks mentioned may be trademarks or registered trademarks of their respective owners.</p>
</footer>
</div> <!-- /container -->
<!-- Javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="./../../resources/js/bootstrap-dropdown.js"></script>
</body>
</html>