http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/ejb-examples.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/ejb-examples.adoc b/src/main/jbake/content/examples/ejb-examples.adoc deleted file mode 100755 index 64262f1..0000000 --- a/src/main/jbake/content/examples/ejb-examples.adoc +++ /dev/null @@ -1,1188 +0,0 @@ -= EJB Examples -:jbake-date: 2016-09-06 -:jbake-type: page -:jbake-tomeepdf: -:jbake-status: published - -Example ejb-examples can be browsed at https://github.com/apache/tomee/tree/master/examples/ejb-examples - - -*Help us document this example! Click the blue pencil icon in the upper right to edit this page.* - -== AnnotatedEJB - - -[source,java] ----- -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 + "]"; - } -} ----- - - -== AnnotatedEJBLocal - - -[source,java] ----- -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); -} ----- - - -== AnnotatedEJBRemote - - -[source,java] ----- -package org.superbiz.servlet; - -import javax.ejb.Remote; - -@Remote -public interface AnnotatedEJBRemote { - String getName(); - - void setName(String name); -} ----- - - -== AnnotatedServlet - - -[source,java] ----- -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; - } - } -} ----- - - -== ClientHandler - - -[source,java] ----- -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; - } -} ----- - - -== HelloEjb - - -[source,java] ----- -package org.superbiz.servlet; - -import javax.jws.WebService; - -@WebService(targetNamespace = "http://examples.org/wsdl") -public interface HelloEjb { - String hello(String name); -} ----- - - -== HelloEjbService - - -[source,java] ----- -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!"; - } -} ----- - - -== HelloPojo - - -[source,java] ----- -package org.superbiz.servlet; - -import javax.jws.WebService; - -@WebService(targetNamespace = "http://examples.org/wsdl") -public interface HelloPojo { - String hello(String name); -} ----- - - -== HelloPojoService - - -[source,java] ----- -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!"; - } -} ----- - - -== JndiServlet - - -[source,java] ----- -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<String, Object> bindings = new TreeMap<String, Object>(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<String, Object> entry : bindings.entrySet()) { - if (entry.getValue() != null) { - out.println(" " + entry.getKey() + "=" + entry.getValue()); - } else { - out.println(" " + entry.getKey()); - } - } - } - - private void addBindings(String path, Map<String, Object> 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, "<file>"); - } 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()); - } - } -} ----- - - -== JpaBean - - -[source,java] ----- -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 + "]"; - } -} ----- - - -== JpaServlet - - -[source,java] ----- -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(); - } -} ----- - - -== ResourceBean - - -[source,java] ----- -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 + "]"; - } -} ----- - - -== RunAsServlet - - -[source,java] ----- -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()=<null>"); - } - 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()=<null>"); - } - 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(); - } -} ----- - - -== SecureEJB - - -[source,java] ----- -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() + "]"; - } -} ----- - - -== SecureEJBLocal - - -[source,java] ----- -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(); -} ----- - - -== SecureServlet - - -[source,java] ----- -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()=<null>"); - } - 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()=<null>"); - } - 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(); - } -} ----- - - -== ServerHandler - - -[source,java] ----- -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; - } -} ----- - - -== WebserviceClient - - -[source,java] ----- -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: - * <p/> - * jaxb-api-2.0.jar - * jaxb-impl-2.0.3.jar - * <p/> - * saaj-api-1.3.jar - * saaj-impl-1.3.jar - * <p/> - * <p/> - * 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 - * <p/> - * 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 - * <p/> - * 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(); - } -} ----- - - -== WebserviceServlet - - -[source,java] ----- -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(); - } - } -} ----- - - - -== persistence.xml - - -[source,xml] ----- -<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> - <persistence-unit transaction-type="RESOURCE_LOCAL" name="jpa-example"> - <jta-data-source>java:openejb/Connector/Default JDBC Database</jta-data-source> - <non-jta-data-source>java:openejb/Connector/Default Unmanaged JDBC Database</non-jta-data-source> - <class>org.superbiz.servlet.JpaBean</class> - - <properties> - <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> - </properties> - </persistence-unit> -</persistence> ----- - - -== client-handlers.xml - - -[source,xml] ----- -<jws:handler-chains xmlns:jws="http://java.sun.com/xml/ns/javaee"> - <jws:handler-chain> - <jws:handler> - <jws:handler-name>ClientHandler</jws:handler-name> - <jws:handler-class>org.superbiz.servlet.ClientHandler</jws:handler-class> - </jws:handler> - </jws:handler-chain> -</jws:handler-chains> ----- - - - -== server-handlers.xml - - -[source,xml] ----- -<jws:handler-chains xmlns:jws="http://java.sun.com/xml/ns/javaee"> - <jws:handler-chain> - <jws:handler> - <jws:handler-name>ServerHandler</jws:handler-name> - <jws:handler-class>org.superbiz.servlet.ServerHandler</jws:handler-class> - </jws:handler> - </jws:handler-chain> -</jws:handler-chains> ----- - - - -== context.xml - - -[source,xml] ----- -<Context> - <!-- This only works if the context is installed under the correct name --> - <Realm className="org.apache.catalina.realm.MemoryRealm" - pathname="webapps/ejb-examples-1.0-SNAPSHOT/WEB-INF/tomcat-users.xml"/> - - <Environment - name="context.xml/environment" - value="ContextString" - type="java.lang.String"/> - <Resource - name="context.xml/resource" - auth="Container" - type="org.superbiz.servlet.ResourceBean" - factory="org.apache.naming.factory.BeanFactory" - value="ContextResource"/> - <ResourceLink - name="context.xml/resource-link" - global="server.xml/environment" - type="java.lang.String"/> - - <!-- web.xml resources --> - <Resource - name="web.xml/resource-env-ref" - auth="Container" - type="org.superbiz.servlet.ResourceBean" - factory="org.apache.naming.factory.BeanFactory" - value="ContextResourceEnvRef"/> - <Resource - name="web.xml/resource-ref" - auth="Container" - type="org.superbiz.servlet.ResourceBean" - factory="org.apache.naming.factory.BeanFactory" - value="ContextResourceRef"/> - <ResourceLink - name="web.xml/resource-link" - global="server.xml/environment" - type="java.lang.String"/> -</Context> ----- - - - -== jetty-web.xml - - -[source,xml] ----- -<Configure class="org.eclipse.jetty.webapp.WebAppContext"> - <Get name="securityHandler"> - <Set name="loginService"> - <New class="org.eclipse.jetty.security.HashLoginService"> - <Set name="name">Test Realm</Set> - <Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties - </Set> - </New> - </Set> - </Get> -</Configure> ----- - - -== tomcat-users.xml - - -[source,xml] ----- -<tomcat-users> - <user name="manager" password="manager" roles="manager,user"/> - <user name="user" password="user" roles="user"/> -</tomcat-users> ----- - - - -== web.xml - - -[source,xml] ----- -<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"> - - <display-name>OpenEJB Servlet Examples</display-name> - - <servlet> - <servlet-name>AnnotatedServlet</servlet-name> - <servlet-class>org.superbiz.servlet.AnnotatedServlet</servlet-class> - </servlet> - - <servlet-mapping> - <servlet-name>AnnotatedServlet</servlet-name> - <url-pattern>/annotated/*</url-pattern> - </servlet-mapping> - - <servlet> - <servlet-name>JpaServlet</servlet-name> - <servlet-class>org.superbiz.servlet.JpaServlet</servlet-class> - </servlet> - - <servlet-mapping> - <servlet-name>JpaServlet</servlet-name> - <url-pattern>/jpa/*</url-pattern> - </servlet-mapping> - - <servlet> - <servlet-name>JndiServlet</servlet-name> - <servlet-class>org.superbiz.servlet.JndiServlet</servlet-class> - </servlet> - - <servlet-mapping> - <servlet-name>JndiServlet</servlet-name> - <url-pattern>/jndi/*</url-pattern> - </servlet-mapping> - - <servlet> - <servlet-name>RunAsServlet</servlet-name> - <servlet-class>org.superbiz.servlet.RunAsServlet</servlet-class> - <run-as> - <role-name>fake</role-name> - </run-as> - </servlet> - - <servlet-mapping> - <servlet-name>RunAsServlet</servlet-name> - <url-pattern>/runas/*</url-pattern> - </servlet-mapping> - - <servlet> - <servlet-name>SecureServlet</servlet-name> - <servlet-class>org.superbiz.servlet.SecureServlet</servlet-class> - </servlet> - - <servlet-mapping> - <servlet-name>SecureServlet</servlet-name> - <url-pattern>/secure/*</url-pattern> - </servlet-mapping> - - <security-constraint> - <web-resource-collection> - <web-resource-name>Secure Area</web-resource-name> - <url-pattern>/secure/*</url-pattern> - <url-pattern>/runas/*</url-pattern> - </web-resource-collection> - <auth-constraint> - <role-name>user</role-name> - </auth-constraint> - </security-constraint> - - <servlet> - <servlet-name>WebserviceServlet</servlet-name> - <servlet-class>org.superbiz.servlet.WebserviceServlet</servlet-class> - </servlet> - - <servlet-mapping> - <servlet-name>WebserviceServlet</servlet-name> - <url-pattern>/webservice/*</url-pattern> - </servlet-mapping> - - - <servlet> - <servlet-name>HelloPojoService</servlet-name> - <servlet-class>org.superbiz.servlet.HelloPojoService</servlet-class> - </servlet> - - <servlet-mapping> - <servlet-name>HelloPojoService</servlet-name> - <url-pattern>/hello</url-pattern> - </servlet-mapping> - - <login-config> - <auth-method>BASIC</auth-method> - </login-config> - - <security-role> - <role-name>manager</role-name> - </security-role> - - <security-role> - <role-name>user</role-name> - </security-role> - - <env-entry> - <env-entry-name>web.xml/env-entry</env-entry-name> - <env-entry-type>java.lang.String</env-entry-type> - <env-entry-value>WebValue</env-entry-value> - </env-entry> - - <resource-ref> - <res-ref-name>web.xml/Data Source</res-ref-name> - <res-type>javax.sql.DataSource</res-type> - <res-auth>Container</res-auth> - </resource-ref> - - <resource-env-ref> - <resource-env-ref-name>web.xml/Queue</resource-env-ref-name> - <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type> - </resource-env-ref> - - <ejb-ref> - <ejb-ref-name>web.xml/EjbRemote</ejb-ref-name> - <ejb-ref-type>Session</ejb-ref-type> - <remote>org.superbiz.servlet.AnnotatedEJBRemote</remote> - </ejb-ref> - - <ejb-local-ref> - <ejb-ref-name>web.xml/EjLocal</ejb-ref-name> - <ejb-ref-type>Session</ejb-ref-type> - <local>org.superbiz.servlet.AnnotatedEJBLocal</local> - </ejb-local-ref> - - <persistence-unit-ref> - <persistence-unit-ref-name>web.xml/PersistenceUnit</persistence-unit-ref-name> - <persistence-unit-name>jpa-example</persistence-unit-name> - </persistence-unit-ref> - - <persistence-context-ref> - <persistence-context-ref-name>web.xml/PersistenceContext</persistence-context-ref-name> - <persistence-unit-name>jpa-example</persistence-unit-name> - <persistence-context-type>Transactional</persistence-context-type> - </persistence-context-ref> -</web-app> ----- - -
http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/ejb-webservice.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/ejb-webservice.adoc b/src/main/jbake/content/examples/ejb-webservice.adoc deleted file mode 100755 index 0d21d16..0000000 --- a/src/main/jbake/content/examples/ejb-webservice.adoc +++ /dev/null @@ -1,52 +0,0 @@ -= EJB Webservice -:jbake-date: 2016-09-06 -:jbake-type: page -:jbake-tomeepdf: -:jbake-status: published - -Example ejb-webservice can be browsed at https://github.com/apache/tomee/tree/master/examples/ejb-webservice - - -*Help us document this example! Click the blue pencil icon in the upper right to edit this page.* - -== Calculator - - -[source,java] ----- -package org.superbiz.ws; - -import javax.ejb.Stateless; -import javax.jws.WebService; - -@Stateless -@WebService(portName = "CalculatorPort", - serviceName = "CalculatorWebService", - targetNamespace = "http://superbiz.org/wsdl") -public class Calculator { - public int sum(int add1, int add2) { - return add1 + add2; - } - - public int multiply(int mul1, int mul2) { - return mul1 * mul2; - } -} ----- - - -== web.xml - - -[source,xml] ----- -<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"> - -</web-app> ----- - - http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/groovy-cdi.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/groovy-cdi.adoc b/src/main/jbake/content/examples/groovy-cdi.adoc deleted file mode 100755 index 0e0a6a0..0000000 --- a/src/main/jbake/content/examples/groovy-cdi.adoc +++ /dev/null @@ -1,9 +0,0 @@ -= groovy-cdi -:jbake-date: 2016-09-06 -:jbake-type: page -:jbake-tomeepdf: -:jbake-status: published - -Example groovy-cdi can be browsed at https://github.com/apache/tomee/tree/master/examples/groovy-cdi - -No README.md yet, be the first to contribute one! http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/groovy-jpa.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/groovy-jpa.adoc b/src/main/jbake/content/examples/groovy-jpa.adoc deleted file mode 100755 index 91f9218..0000000 --- a/src/main/jbake/content/examples/groovy-jpa.adoc +++ /dev/null @@ -1,9 +0,0 @@ -= groovy-jpa -:jbake-date: 2016-09-06 -:jbake-type: page -:jbake-tomeepdf: -:jbake-status: published - -Example groovy-jpa can be browsed at https://github.com/apache/tomee/tree/master/examples/groovy-jpa - -No README.md yet, be the first to contribute one! http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/groovy-spock.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/groovy-spock.adoc b/src/main/jbake/content/examples/groovy-spock.adoc deleted file mode 100755 index 203cddc..0000000 --- a/src/main/jbake/content/examples/groovy-spock.adoc +++ /dev/null @@ -1,9 +0,0 @@ -= groovy-spock -:jbake-date: 2016-09-06 -:jbake-type: page -:jbake-tomeepdf: -:jbake-status: published - -Example groovy-spock can be browsed at https://github.com/apache/tomee/tree/master/examples/groovy-spock - -No README.md yet, be the first to contribute one! http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/helloworld-weblogic.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/helloworld-weblogic.adoc b/src/main/jbake/content/examples/helloworld-weblogic.adoc deleted file mode 100755 index dd8279e..0000000 --- a/src/main/jbake/content/examples/helloworld-weblogic.adoc +++ /dev/null @@ -1,169 +0,0 @@ -= Helloworld Weblogic -:jbake-date: 2016-09-06 -:jbake-type: page -:jbake-tomeepdf: -:jbake-status: published - -Example helloworld-weblogic can be browsed at https://github.com/apache/tomee/tree/master/examples/helloworld-weblogic - - -*Help us document this example! Click the blue pencil icon in the upper right to edit this page.* - -== HelloBean - - -[source,java] ----- -package org.superbiz.hello; - -import javax.ejb.LocalHome; -import javax.ejb.Stateless; - -/** - * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 (Thu, 27 Dec 2007) $ - */ -@Stateless -@LocalHome(HelloEjbLocalHome.class) -public class HelloBean { - - public String sayHello() { - return "Hello, World!"; - } -} ----- - - -== HelloEjbLocal - - -[source,java] ----- -package org.superbiz.hello; - -import javax.ejb.EJBLocalObject; - -/** - * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 (Thu, 27 Dec 2007) $ - */ -public interface HelloEjbLocal extends EJBLocalObject { - - String sayHello(); -} ----- - - -== HelloEjbLocalHome - - -[source,java] ----- -package org.superbiz.hello; - -import javax.ejb.CreateException; -import javax.ejb.EJBLocalHome; - -/** - * @version $Revision: 1090810 $ $Date: 2011-04-10 07:49:26 -0700 (Sun, 10 Apr 2011) $ - */ -public interface HelloEjbLocalHome extends EJBLocalHome { - HelloEjbLocal create() throws CreateException; -} ----- - - -== ejb-jar.xml - - -[source,xml] ----- -<ejb-jar/> ----- - - -== weblogic-ejb-jar.xml - - -[source,xml] ----- -<weblogic-ejb-jar> - <weblogic-enterprise-bean> - <ejb-name>HelloBean</ejb-name> - <local-jndi-name>MyHello</local-jndi-name> - </weblogic-enterprise-bean> -</weblogic-ejb-jar> ----- - - - - -== HelloTest - - -[source,java] ----- -package org.superbiz.hello; - -import junit.framework.TestCase; - -import javax.naming.Context; -import javax.naming.InitialContext; -import java.util.Properties; - -/** - * @version $Revision: 1090810 $ $Date: 2011-04-10 07:49:26 -0700 (Sun, 10 Apr 2011) $ - */ -public class HelloTest extends TestCase { - - public void test() throws Exception { - Properties properties = new Properties(); - properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory"); - InitialContext initialContext = new InitialContext(properties); - - HelloEjbLocalHome localHome = (HelloEjbLocalHome) initialContext.lookup("MyHello"); - HelloEjbLocal helloEjb = localHome.create(); - - String message = helloEjb.sayHello(); - - assertEquals(message, "Hello, World!"); - } -} ----- - - -= Running - - - -[source] ----- -------------------------------------------------------- - T E S T S -------------------------------------------------------- -Running org.superbiz.hello.HelloTest -Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 -http://tomee.apache.org/ -INFO - openejb.home = /Users/dblevins/examples/helloworld-weblogic -INFO - openejb.base = /Users/dblevins/examples/helloworld-weblogic -INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) -INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) -INFO - Found EjbModule in classpath: /Users/dblevins/examples/helloworld-weblogic/target/classes -INFO - Beginning load: /Users/dblevins/examples/helloworld-weblogic/target/classes -INFO - Configuring enterprise application: /Users/dblevins/examples/helloworld-weblogic/classpath.ear -INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) -INFO - Auto-creating a container for bean HelloBean: Container(type=STATELESS, id=Default Stateless Container) -INFO - Enterprise application "/Users/dblevins/examples/helloworld-weblogic/classpath.ear" loaded. -INFO - Assembling app: /Users/dblevins/examples/helloworld-weblogic/classpath.ear -INFO - Jndi(name=MyHello) --> Ejb(deployment-id=HelloBean) -INFO - Jndi(name=global/classpath.ear/helloworld-weblogic/HelloBean!org.superbiz.hello.HelloEjbLocalHome) --> Ejb(deployment-id=HelloBean) -INFO - Jndi(name=global/classpath.ear/helloworld-weblogic/HelloBean) --> Ejb(deployment-id=HelloBean) -INFO - Created Ejb(deployment-id=HelloBean, ejb-name=HelloBean, container=Default Stateless Container) -INFO - Started Ejb(deployment-id=HelloBean, ejb-name=HelloBean, container=Default Stateless Container) -INFO - Deployed Application(path=/Users/dblevins/examples/helloworld-weblogic/classpath.ear) -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.414 sec - -Results : - -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ----- - - http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/index-ng.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/index-ng.adoc b/src/main/jbake/content/examples/index-ng.adoc deleted file mode 100755 index 822efdd..0000000 --- a/src/main/jbake/content/examples/index-ng.adoc +++ /dev/null @@ -1,5 +0,0 @@ -= Examples -:jbake-date: 2016-08-30 -:jbake-type: examples -:jbake-status: published -:jbake-tomeepdf: http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/index.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/index.adoc b/src/main/jbake/content/examples/index.adoc deleted file mode 100755 index 822efdd..0000000 --- a/src/main/jbake/content/examples/index.adoc +++ /dev/null @@ -1,5 +0,0 @@ -= Examples -:jbake-date: 2016-08-30 -:jbake-type: examples -:jbake-status: published -:jbake-tomeepdf: http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/injection-of-connectionfactory.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/injection-of-connectionfactory.adoc b/src/main/jbake/content/examples/injection-of-connectionfactory.adoc deleted file mode 100755 index c861c7b..0000000 --- a/src/main/jbake/content/examples/injection-of-connectionfactory.adoc +++ /dev/null @@ -1,180 +0,0 @@ -= Injection Of Connectionfactory -:jbake-date: 2016-09-06 -:jbake-type: page -:jbake-tomeepdf: -:jbake-status: published - -Example injection-of-connectionfactory can be browsed at https://github.com/apache/tomee/tree/master/examples/injection-of-connectionfactory - - -*Help us document this example! Click the blue pencil icon in the upper right to edit this page.* - -== Messages - - -[source,java] ----- -package org.superbiz.injection.jms; - -import javax.annotation.Resource; -import javax.ejb.Stateless; -import javax.jms.Connection; -import javax.jms.ConnectionFactory; -import javax.jms.DeliveryMode; -import javax.jms.JMSException; -import javax.jms.MessageConsumer; -import javax.jms.MessageProducer; -import javax.jms.Queue; -import javax.jms.Session; -import javax.jms.TextMessage; - -@Stateless -public class Messages { - - @Resource - private ConnectionFactory connectionFactory; - - @Resource - private Queue chatQueue; - - - public void sendMessage(String text) throws JMSException { - - Connection connection = null; - Session session = null; - - try { - connection = connectionFactory.createConnection(); - connection.start(); - - // Create a Session - session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - - // Create a MessageProducer from the Session to the Topic or Queue - MessageProducer producer = session.createProducer(chatQueue); - producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - - // Create a message - TextMessage message = session.createTextMessage(text); - - // Tell the producer to send the message - producer.send(message); - } finally { - // Clean up - if (session != null) session.close(); - if (connection != null) connection.close(); - } - } - - public String receiveMessage() throws JMSException { - - Connection connection = null; - Session session = null; - MessageConsumer consumer = null; - try { - connection = connectionFactory.createConnection(); - connection.start(); - - // Create a Session - session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - - // Create a MessageConsumer from the Session to the Topic or Queue - consumer = session.createConsumer(chatQueue); - - // Wait for a message - TextMessage message = (TextMessage) consumer.receive(1000); - - return message.getText(); - } finally { - if (consumer != null) consumer.close(); - if (session != null) session.close(); - if (connection != null) connection.close(); - } - } -} ----- - - -== MessagingBeanTest - - -[source,java] ----- -package org.superbiz.injection.jms; - -import junit.framework.TestCase; - -import javax.ejb.embeddable.EJBContainer; -import javax.naming.Context; - -public class MessagingBeanTest extends TestCase { - - public void test() throws Exception { - - final Context context = EJBContainer.createEJBContainer().getContext(); - - Messages messages = (Messages) context.lookup("java:global/injection-of-connectionfactory/Messages"); - - messages.sendMessage("Hello World!"); - messages.sendMessage("How are you?"); - messages.sendMessage("Still spinning?"); - - assertEquals(messages.receiveMessage(), "Hello World!"); - assertEquals(messages.receiveMessage(), "How are you?"); - assertEquals(messages.receiveMessage(), "Still spinning?"); - } -} ----- - - -= Running - - - -[source] ----- -------------------------------------------------------- - T E S T S -------------------------------------------------------- -Running org.superbiz.injection.jms.MessagingBeanTest -Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 -http://tomee.apache.org/ -INFO - openejb.home = /Users/dblevins/examples/injection-of-connectionfactory -INFO - openejb.base = /Users/dblevins/examples/injection-of-connectionfactory -INFO - Using 'javax.ejb.embeddable.EJBContainer=true' -INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) -INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) -INFO - Found EjbModule in classpath: /Users/dblevins/examples/injection-of-connectionfactory/target/classes -INFO - Beginning load: /Users/dblevins/examples/injection-of-connectionfactory/target/classes -INFO - Configuring enterprise application: /Users/dblevins/examples/injection-of-connectionfactory -WARN - Method 'lookup' is not available for 'javax.annotation.Resource'. Probably using an older Runtime. -INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) -INFO - Auto-creating a container for bean Messages: Container(type=STATELESS, id=Default Stateless Container) -INFO - Configuring Service(id=Default JMS Connection Factory, type=Resource, provider-id=Default JMS Connection Factory) -INFO - Auto-creating a Resource with id 'Default JMS Connection Factory' of type 'javax.jms.ConnectionFactory for 'Messages'. -INFO - Configuring Service(id=Default JMS Resource Adapter, type=Resource, provider-id=Default JMS Resource Adapter) -INFO - Auto-linking resource-ref 'java:comp/env/org.superbiz.injection.jms.Messages/connectionFactory' in bean Messages to Resource(id=Default JMS Connection Factory) -INFO - Configuring Service(id=org.superbiz.injection.jms.Messages/chatQueue, type=Resource, provider-id=Default Queue) -INFO - Auto-creating a Resource with id 'org.superbiz.injection.jms.Messages/chatQueue' of type 'javax.jms.Queue for 'Messages'. -INFO - Auto-linking resource-env-ref 'java:comp/env/org.superbiz.injection.jms.Messages/chatQueue' in bean Messages to Resource(id=org.superbiz.injection.jms.Messages/chatQueue) -INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) -INFO - Auto-creating a container for bean org.superbiz.injection.jms.MessagingBeanTest: Container(type=MANAGED, id=Default Managed Container) -INFO - Enterprise application "/Users/dblevins/examples/injection-of-connectionfactory" loaded. -INFO - Assembling app: /Users/dblevins/examples/injection-of-connectionfactory -INFO - Jndi(name="java:global/injection-of-connectionfactory/Messages!org.superbiz.injection.jms.Messages") -INFO - Jndi(name="java:global/injection-of-connectionfactory/Messages") -INFO - Jndi(name="java:global/EjbModule1634151355/org.superbiz.injection.jms.MessagingBeanTest!org.superbiz.injection.jms.MessagingBeanTest") -INFO - Jndi(name="java:global/EjbModule1634151355/org.superbiz.injection.jms.MessagingBeanTest") -INFO - Created Ejb(deployment-id=Messages, ejb-name=Messages, container=Default Stateless Container) -INFO - Created Ejb(deployment-id=org.superbiz.injection.jms.MessagingBeanTest, ejb-name=org.superbiz.injection.jms.MessagingBeanTest, container=Default Managed Container) -INFO - Started Ejb(deployment-id=Messages, ejb-name=Messages, container=Default Stateless Container) -INFO - Started Ejb(deployment-id=org.superbiz.injection.jms.MessagingBeanTest, ejb-name=org.superbiz.injection.jms.MessagingBeanTest, container=Default Managed Container) -INFO - Deployed Application(path=/Users/dblevins/examples/injection-of-connectionfactory) -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.562 sec - -Results : - -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ----- - - http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/injection-of-datasource.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/injection-of-datasource.adoc b/src/main/jbake/content/examples/injection-of-datasource.adoc deleted file mode 100755 index 4637a56..0000000 --- a/src/main/jbake/content/examples/injection-of-datasource.adoc +++ /dev/null @@ -1,248 +0,0 @@ -= Injection Of Datasource -:jbake-date: 2016-09-06 -:jbake-type: page -:jbake-tomeepdf: -:jbake-status: published - -Example injection-of-datasource can be browsed at https://github.com/apache/tomee/tree/master/examples/injection-of-datasource - - -*Help us document this example! Click the blue pencil icon in the upper right to edit this page.* - -== Movie - - -[source,java] ----- -package org.superbiz.injection; - -/** - * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 (Thu, 27 Dec 2007) $ - */ -public class Movie { - private String director; - private String title; - private int year; - - public Movie() { - } - - public Movie(String director, String title, int year) { - this.director = director; - this.title = title; - this.year = year; - } - - public String getDirector() { - return director; - } - - public void setDirector(String director) { - this.director = director; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public int getYear() { - return year; - } - - public void setYear(int year) { - this.year = year; - } - -} ----- - - -== Movies - - -[source,java] ----- -package org.superbiz.injection; - -import javax.annotation.PostConstruct; -import javax.annotation.Resource; -import javax.ejb.Stateful; -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.util.ArrayList; -import java.util.List; - -@Stateful -public class Movies { - - /** - * The field name "movieDatabase" matches the DataSource we - * configure in the TestCase via : - * p.put("movieDatabase", "new://Resource?type=DataSource"); - * <p/> - * This would also match an equivalent delcaration in an openejb.xml: - * <Resource id="movieDatabase" type="DataSource"/> - * <p/> - * If you'd like the freedom to change the field name without - * impact on your configuration you can set the "name" attribute - * of the @Resource annotation to "movieDatabase" instead. - */ - @Resource - private DataSource movieDatabase; - - @PostConstruct - private void construct() throws Exception { - Connection connection = movieDatabase.getConnection(); - try { - PreparedStatement stmt = connection.prepareStatement("CREATE TABLE movie ( director VARCHAR(255), title VARCHAR(255), year integer)"); - stmt.execute(); - } finally { - connection.close(); - } - } - - public void addMovie(Movie movie) throws Exception { - Connection conn = movieDatabase.getConnection(); - try { - PreparedStatement sql = conn.prepareStatement("INSERT into movie (director, title, year) values (?, ?, ?)"); - sql.setString(1, movie.getDirector()); - sql.setString(2, movie.getTitle()); - sql.setInt(3, movie.getYear()); - sql.execute(); - } finally { - conn.close(); - } - } - - public void deleteMovie(Movie movie) throws Exception { - Connection conn = movieDatabase.getConnection(); - try { - PreparedStatement sql = conn.prepareStatement("DELETE from movie where director = ? AND title = ? AND year = ?"); - sql.setString(1, movie.getDirector()); - sql.setString(2, movie.getTitle()); - sql.setInt(3, movie.getYear()); - sql.execute(); - } finally { - conn.close(); - } - } - - public List<Movie> getMovies() throws Exception { - ArrayList<Movie> movies = new ArrayList<Movie>(); - Connection conn = movieDatabase.getConnection(); - try { - PreparedStatement sql = conn.prepareStatement("SELECT director, title, year from movie"); - ResultSet set = sql.executeQuery(); - while (set.next()) { - Movie movie = new Movie(); - movie.setDirector(set.getString("director")); - movie.setTitle(set.getString("title")); - movie.setYear(set.getInt("year")); - movies.add(movie); - } - } finally { - conn.close(); - } - return movies; - } -} ----- - - -== MoviesTest - - -[source,java] ----- -package org.superbiz.injection; - -import junit.framework.TestCase; - -import javax.ejb.embeddable.EJBContainer; -import javax.naming.Context; -import java.util.List; -import java.util.Properties; - -//START SNIPPET: code -public class MoviesTest extends TestCase { - - public void test() throws Exception { - - Properties p = new Properties(); - p.put("movieDatabase", "new://Resource?type=DataSource"); - p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver"); - p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb"); - - Context context = EJBContainer.createEJBContainer(p).getContext(); - - Movies movies = (Movies) context.lookup("java:global/injection-of-datasource/Movies"); - - movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992)); - movies.addMovie(new Movie("Joel Coen", "Fargo", 1996)); - movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998)); - - List<Movie> list = movies.getMovies(); - assertEquals("List.size()", 3, list.size()); - - for (Movie movie : list) { - movies.deleteMovie(movie); - } - - assertEquals("Movies.getMovies()", 0, movies.getMovies().size()); - } -} ----- - - -= Running - - - -[source] ----- -------------------------------------------------------- - T E S T S -------------------------------------------------------- -Running org.superbiz.injection.MoviesTest -Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 -http://tomee.apache.org/ -INFO - openejb.home = /Users/dblevins/examples/injection-of-datasource -INFO - openejb.base = /Users/dblevins/examples/injection-of-datasource -INFO - Using 'javax.ejb.embeddable.EJBContainer=true' -INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) -INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) -INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database) -INFO - Found EjbModule in classpath: /Users/dblevins/examples/injection-of-datasource/target/classes -INFO - Beginning load: /Users/dblevins/examples/injection-of-datasource/target/classes -INFO - Configuring enterprise application: /Users/dblevins/examples/injection-of-datasource -WARN - Method 'lookup' is not available for 'javax.annotation.Resource'. Probably using an older Runtime. -INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container) -INFO - Auto-creating a container for bean Movies: Container(type=STATEFUL, id=Default Stateful Container) -INFO - Auto-linking resource-ref 'java:comp/env/org.superbiz.injection.Movies/movieDatabase' in bean Movies to Resource(id=movieDatabase) -INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) -INFO - Auto-creating a container for bean org.superbiz.injection.MoviesTest: Container(type=MANAGED, id=Default Managed Container) -INFO - Enterprise application "/Users/dblevins/examples/injection-of-datasource" loaded. -INFO - Assembling app: /Users/dblevins/examples/injection-of-datasource -INFO - Jndi(name="java:global/injection-of-datasource/Movies!org.superbiz.injection.Movies") -INFO - Jndi(name="java:global/injection-of-datasource/Movies") -INFO - Jndi(name="java:global/EjbModule1508028338/org.superbiz.injection.MoviesTest!org.superbiz.injection.MoviesTest") -INFO - Jndi(name="java:global/EjbModule1508028338/org.superbiz.injection.MoviesTest") -INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container) -INFO - Created Ejb(deployment-id=org.superbiz.injection.MoviesTest, ejb-name=org.superbiz.injection.MoviesTest, container=Default Managed Container) -INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container) -INFO - Started Ejb(deployment-id=org.superbiz.injection.MoviesTest, ejb-name=org.superbiz.injection.MoviesTest, container=Default Managed Container) -INFO - Deployed Application(path=/Users/dblevins/examples/injection-of-datasource) -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.276 sec - -Results : - -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ----- - - http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/injection-of-ejbs.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/injection-of-ejbs.adoc b/src/main/jbake/content/examples/injection-of-ejbs.adoc deleted file mode 100755 index 4eb9b59..0000000 --- a/src/main/jbake/content/examples/injection-of-ejbs.adoc +++ /dev/null @@ -1,234 +0,0 @@ -= Injection Of Ejbs -:jbake-date: 2016-09-06 -:jbake-type: page -:jbake-tomeepdf: -:jbake-status: published - -Example injection-of-ejbs can be browsed at https://github.com/apache/tomee/tree/master/examples/injection-of-ejbs - - -This example shows how to use the @EJB annotation on a bean class to refer to other beans. - -This functionality is often referred as dependency injection (see -http://www.martinfowler.com/articles/injection.html), and has been recently introduced in -Java EE 5. - -In this particular example, we will create two session stateless beans - - * a DataStore session bean - * a DataReader session bean - -The DataReader bean uses the DataStore to retrieve some informations, and -we will see how we can, inside the DataReader bean, get a reference to the -DataStore bean using the @EJB annotation, thus avoiding the use of the -JNDI API. - -== DataReader - - -[source,java] ----- -package org.superbiz.injection; - -import javax.ejb.EJB; -import javax.ejb.Stateless; - -/** - * This is an EJB 3.1 style pojo stateless session bean - * Every stateless session bean implementation must be annotated - * using the annotation @Stateless - * This EJB has 2 business interfaces: DataReaderRemote, a remote business - * interface, and DataReaderLocal, a local business interface - * <p/> - * The instance variables 'dataStoreRemote' is annotated with the @EJB annotation: - * this means that the application server, at runtime, will inject in this instance - * variable a reference to the EJB DataStoreRemote - * <p/> - * The instance variables 'dataStoreLocal' is annotated with the @EJB annotation: - * this means that the application server, at runtime, will inject in this instance - * variable a reference to the EJB DataStoreLocal - */ -//START SNIPPET: code -@Stateless -public class DataReader { - - @EJB - private DataStoreRemote dataStoreRemote; - @EJB - private DataStoreLocal dataStoreLocal; - @EJB - private DataStore dataStore; - - public String readDataFromLocalStore() { - return "LOCAL:" + dataStoreLocal.getData(); - } - - public String readDataFromLocalBeanStore() { - return "LOCALBEAN:" + dataStore.getData(); - } - - public String readDataFromRemoteStore() { - return "REMOTE:" + dataStoreRemote.getData(); - } -} ----- - - -== DataStore - - -[source,java] ----- -package org.superbiz.injection; - -import javax.ejb.LocalBean; -import javax.ejb.Stateless; - -/** - * This is an EJB 3 style pojo stateless session bean - * Every stateless session bean implementation must be annotated - * using the annotation @Stateless - * This EJB has 2 business interfaces: DataStoreRemote, a remote business - * interface, and DataStoreLocal, a local business interface - */ -//START SNIPPET: code -@Stateless -@LocalBean -public class DataStore implements DataStoreLocal, DataStoreRemote { - - public String getData() { - return "42"; - } -} ----- - - -== DataStoreLocal - - -[source,java] ----- -package org.superbiz.injection; - -import javax.ejb.Local; - -/** - * This is an EJB 3 local business interface - * A local business interface may be annotated with the @Local - * annotation, but it's optional. A business interface which is - * not annotated with @Local or @Remote is assumed to be Local - */ -//START SNIPPET: code -@Local -public interface DataStoreLocal { - - public String getData(); -} ----- - - -== DataStoreRemote - - -[source,java] ----- -package org.superbiz.injection; - -import javax.ejb.Remote; - -/** - * This is an EJB 3 remote business interface - * A remote business interface must be annotated with the @Remote - * annotation - */ -//START SNIPPET: code -@Remote -public interface DataStoreRemote { - - public String getData(); -} ----- - - -== EjbDependencyTest - - -[source,java] ----- -package org.superbiz.injection; - -import junit.framework.TestCase; - -import javax.ejb.embeddable.EJBContainer; -import javax.naming.Context; - -/** - * A test case for DataReaderImpl ejb, testing both the remote and local interface - */ -//START SNIPPET: code -public class EjbDependencyTest extends TestCase { - - public void test() throws Exception { - final Context context = EJBContainer.createEJBContainer().getContext(); - - DataReader dataReader = (DataReader) context.lookup("java:global/injection-of-ejbs/DataReader"); - - assertNotNull(dataReader); - - assertEquals("LOCAL:42", dataReader.readDataFromLocalStore()); - assertEquals("REMOTE:42", dataReader.readDataFromRemoteStore()); - assertEquals("LOCALBEAN:42", dataReader.readDataFromLocalBeanStore()); - } -} ----- - - -= Running - - - -[source] ----- -------------------------------------------------------- - T E S T S -------------------------------------------------------- -Running org.superbiz.injection.EjbDependencyTest -Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 -http://tomee.apache.org/ -INFO - openejb.home = /Users/dblevins/examples/injection-of-ejbs -INFO - openejb.base = /Users/dblevins/examples/injection-of-ejbs -INFO - Using 'javax.ejb.embeddable.EJBContainer=true' -INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) -INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) -INFO - Found EjbModule in classpath: /Users/dblevins/examples/injection-of-ejbs/target/classes -INFO - Beginning load: /Users/dblevins/examples/injection-of-ejbs/target/classes -INFO - Configuring enterprise application: /Users/dblevins/examples/injection-of-ejbs -INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) -INFO - Auto-creating a container for bean DataReader: Container(type=STATELESS, id=Default Stateless Container) -INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) -INFO - Auto-creating a container for bean org.superbiz.injection.EjbDependencyTest: Container(type=MANAGED, id=Default Managed Container) -INFO - Enterprise application "/Users/dblevins/examples/injection-of-ejbs" loaded. -INFO - Assembling app: /Users/dblevins/examples/injection-of-ejbs -INFO - Jndi(name="java:global/injection-of-ejbs/DataReader!org.superbiz.injection.DataReader") -INFO - Jndi(name="java:global/injection-of-ejbs/DataReader") -INFO - Jndi(name="java:global/injection-of-ejbs/DataStore!org.superbiz.injection.DataStore") -INFO - Jndi(name="java:global/injection-of-ejbs/DataStore!org.superbiz.injection.DataStoreLocal") -INFO - Jndi(name="java:global/injection-of-ejbs/DataStore!org.superbiz.injection.DataStoreRemote") -INFO - Jndi(name="java:global/injection-of-ejbs/DataStore") -INFO - Jndi(name="java:global/EjbModule355598874/org.superbiz.injection.EjbDependencyTest!org.superbiz.injection.EjbDependencyTest") -INFO - Jndi(name="java:global/EjbModule355598874/org.superbiz.injection.EjbDependencyTest") -INFO - Created Ejb(deployment-id=DataReader, ejb-name=DataReader, container=Default Stateless Container) -INFO - Created Ejb(deployment-id=DataStore, ejb-name=DataStore, container=Default Stateless Container) -INFO - Created Ejb(deployment-id=org.superbiz.injection.EjbDependencyTest, ejb-name=org.superbiz.injection.EjbDependencyTest, container=Default Managed Container) -INFO - Started Ejb(deployment-id=DataReader, ejb-name=DataReader, container=Default Stateless Container) -INFO - Started Ejb(deployment-id=DataStore, ejb-name=DataStore, container=Default Stateless Container) -INFO - Started Ejb(deployment-id=org.superbiz.injection.EjbDependencyTest, ejb-name=org.superbiz.injection.EjbDependencyTest, container=Default Managed Container) -INFO - Deployed Application(path=/Users/dblevins/examples/injection-of-ejbs) -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.225 sec - -Results : - -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ----- - - http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/injection-of-entitymanager.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/injection-of-entitymanager.adoc b/src/main/jbake/content/examples/injection-of-entitymanager.adoc deleted file mode 100755 index e80e407..0000000 --- a/src/main/jbake/content/examples/injection-of-entitymanager.adoc +++ /dev/null @@ -1,249 +0,0 @@ -= Injection Of Entitymanager -:jbake-date: 2016-09-06 -:jbake-type: page -:jbake-tomeepdf: -:jbake-status: published - -Example injection-of-entitymanager can be browsed at https://github.com/apache/tomee/tree/master/examples/injection-of-entitymanager - - -This example shows use of `@PersistenceContext` to have an `EntityManager` with an -`EXTENDED` persistence context injected into a `@Stateful bean`. A JPA -`@Entity` bean is used with the `EntityManager` to create, persist and merge -data to a database. - -== Creating the JPA Entity - -The entity itself is simply a pojo annotated with `@Entity`. We create one called `Movie` which we can use to hold movie records. - - -[source,java] ----- -package org.superbiz.injection.jpa; - -import javax.persistence.Entity; - -@Entity -public class Movie { - - @Id @GeneratedValue - private long id; - - private String director; - private String title; - private int year; - - public Movie() { - } - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - - public Movie(String director, String title, int year) { - this.director = director; - this.title = title; - this.year = year; - } - - public String getDirector() { - return director; - } - - public void setDirector(String director) { - this.director = director; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public int getYear() { - return year; - } - - public void setYear(int year) { - this.year = year; - } -} ----- - - -== Configure the EntityManager via a persistence.xml file - -The above `Movie` entity can be created, removed, updated or deleted via an `EntityManager` object. The `EntityManager` itself is -configured via a `META-INF/persistence.xml` file that is placed in the same jar as the `Movie` entity. - - -[source,xml] ----- -<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> - - <persistence-unit name="movie-unit"> - <jta-data-source>movieDatabase</jta-data-source> - <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source> - <class>org.superbiz.injection.jpa.Movie</class> - - <properties> - <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> - </properties> - </persistence-unit> -</persistence> ----- - - -Notice that the `Movie` entity is listed via a `<class>` element. This is not required, but can help when testing or when the -`Movie` class is located in a different jar than the jar containing the `persistence.xml` file. - -== Injection via @PersistenceContext - -The `EntityManager` itself is created by the container using the information in the `persistence.xml`, so to use it at -runtime, we simply need to request it be injected into one of our components. We do this via `@PersistenceContext` - -The `@PersistenceContext` annotation can be used on any CDI bean, EJB, Servlet, Servlet Listener, Servlet Filter, or JSF ManagedBean. If you don't use an EJB you will need to use a `UserTransaction` begin and commit transactions manually. A transaction is required for any of the create, update or delete methods of the EntityManager to work. - - -[source,java] ----- -package org.superbiz.injection.jpa; - -import javax.ejb.Stateful; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.PersistenceContextType; -import javax.persistence.Query; -import java.util.List; - -@Stateful -public class Movies { - - @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.EXTENDED) - private EntityManager entityManager; - - public void addMovie(Movie movie) throws Exception { - entityManager.persist(movie); - } - - public void deleteMovie(Movie movie) throws Exception { - entityManager.remove(movie); - } - - public List<Movie> getMovies() throws Exception { - Query query = entityManager.createQuery("SELECT m from Movie as m"); - return query.getResultList(); - } -} ----- - - -This particular `EntityManager` is injected as an `EXTENDED` persistence context, which simply means that the `EntityManager` -is created when the `@Stateful` bean is created and destroyed when the `@Stateful` bean is destroyed. Simply put, the -data in the `EntityManager` is cached for the lifetime of the `@Stateful` bean. - -The use of `EXTENDED` persistence contexts is **only** available to `@Stateful` beans. See the link:../../jpa-concepts.html[JPA Concepts] page for an high level explanation of what a "persistence context" really is and how it is significant to JPA. - -== MoviesTest - -Testing JPA is quite easy, we can simply use the `EJBContainer` API to create a container in our test case. - - -[source,java] ----- -package org.superbiz.injection.jpa; - -import junit.framework.TestCase; - -import javax.ejb.embeddable.EJBContainer; -import javax.naming.Context; -import java.util.List; -import java.util.Properties; - -//START SNIPPET: code -public class MoviesTest extends TestCase { - - public void test() throws Exception { - - final Properties p = new Properties(); - p.put("movieDatabase", "new://Resource?type=DataSource"); - p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver"); - p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb"); - - final Context context = EJBContainer.createEJBContainer(p).getContext(); - - Movies movies = (Movies) context.lookup("java:global/injection-of-entitymanager/Movies"); - - movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992)); - movies.addMovie(new Movie("Joel Coen", "Fargo", 1996)); - movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998)); - - List<Movie> list = movies.getMovies(); - assertEquals("List.size()", 3, list.size()); - - for (Movie movie : list) { - movies.deleteMovie(movie); - } - - assertEquals("Movies.getMovies()", 0, movies.getMovies().size()); - } -} ----- - - -= Running - -When we run our test case we should see output similar to the following. - - -[source] ----- -------------------------------------------------------- - T E S T S -------------------------------------------------------- -Running org.superbiz.injection.jpa.MoviesTest -Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 -http://tomee.apache.org/ -INFO - openejb.home = /Users/dblevins/examples/injection-of-entitymanager -INFO - openejb.base = /Users/dblevins/examples/injection-of-entitymanager -INFO - Using 'javax.ejb.embeddable.EJBContainer=true' -INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) -INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) -INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database) -INFO - Found EjbModule in classpath: /Users/dblevins/examples/injection-of-entitymanager/target/classes -INFO - Beginning load: /Users/dblevins/examples/injection-of-entitymanager/target/classes -INFO - Configuring enterprise application: /Users/dblevins/examples/injection-of-entitymanager -INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container) -INFO - Auto-creating a container for bean Movies: Container(type=STATEFUL, id=Default Stateful Container) -INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) -INFO - Auto-creating a container for bean org.superbiz.injection.jpa.MoviesTest: Container(type=MANAGED, id=Default Managed Container) -INFO - Configuring PersistenceUnit(name=movie-unit) -INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'. -INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase) -INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged' -INFO - Enterprise application "/Users/dblevins/examples/injection-of-entitymanager" loaded. -INFO - Assembling app: /Users/dblevins/examples/injection-of-entitymanager -INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 462ms -INFO - Jndi(name="java:global/injection-of-entitymanager/Movies!org.superbiz.injection.jpa.Movies") -INFO - Jndi(name="java:global/injection-of-entitymanager/Movies") -INFO - Jndi(name="java:global/EjbModule1461341140/org.superbiz.injection.jpa.MoviesTest!org.superbiz.injection.jpa.MoviesTest") -INFO - Jndi(name="java:global/EjbModule1461341140/org.superbiz.injection.jpa.MoviesTest") -INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container) -INFO - Created Ejb(deployment-id=org.superbiz.injection.jpa.MoviesTest, ejb-name=org.superbiz.injection.jpa.MoviesTest, container=Default Managed Container) -INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container) -INFO - Started Ejb(deployment-id=org.superbiz.injection.jpa.MoviesTest, ejb-name=org.superbiz.injection.jpa.MoviesTest, container=Default Managed Container) -INFO - Deployed Application(path=/Users/dblevins/examples/injection-of-entitymanager) -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.301 sec - -Results : - -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ----- - - http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/efed31f4/src/main/jbake/content/examples/injection-of-env-entry.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/injection-of-env-entry.adoc b/src/main/jbake/content/examples/injection-of-env-entry.adoc deleted file mode 100755 index add6d0e..0000000 --- a/src/main/jbake/content/examples/injection-of-env-entry.adoc +++ /dev/null @@ -1,271 +0,0 @@ -= Using EnvEntries -:jbake-date: 2016-09-06 -:jbake-type: page -:jbake-tomeepdf: -:jbake-status: published - -Example injection-of-env-entry can be browsed at https://github.com/apache/tomee/tree/master/examples/injection-of-env-entry - - -The `@Resource` annotation can be used to inject several things including -DataSources, Topics, Queues, etc. Most of these are container supplied objects. - -It is possible, however, to supply your own values to be injected via an `<env-entry>` -in your `ejb-jar.xml` or `web.xml` deployment descriptor. Java EE 6 supported `<env-entry>` types -are limited to the following: - - - java.lang.String - - java.lang.Integer - - java.lang.Short - - java.lang.Float - - java.lang.Double - - java.lang.Byte - - java.lang.Character - - java.lang.Boolean - - java.lang.Class - - java.lang.Enum (any enum) - -See also the link:../custom-injection[Custom Injection] exmaple for a TomEE and OpenEJB feature that will let you -use more than just the above types as well as declare `<env-entry>` items with a plain properties file. - -= Using @Resource for basic properties - -The use of the `@Resource` annotation isn't limited to setters. For -example, this annotation could have been used on the corresponding *field* -like so: - - -[source,java] ----- -@Resource -private int maxLineItems; - -A fuller example might look like this: - -package org.superbiz.injection.enventry; - -import javax.annotation.Resource; -import javax.ejb.Singleton; -import java.util.Date; - -@Singleton -public class Configuration { - - @Resource - private String color; - - @Resource - private Shape shape; - - @Resource - private Class strategy; - - @Resource(name = "date") - private long date; - - public String getColor() { - return color; - } - - public Shape getShape() { - return shape; - } - - public Class getStrategy() { - return strategy; - } - - public Date getDate() { - return new Date(date); - } -} ----- - - -Here we have an `@Singleton` bean called `Confuration` that has the following properties (`<env-entry>` items) - -- String color -- Shape shape -- Class strategy -- long date - -== Supplying @Resource values for <env-entry> items in ejb-jar.xml - -The values for our `color`, `shape`, `strategy` and `date` properties are supplied via `<env-entry>` elements in the `ejb-jar.xml` file or the -`web.xml` file like so: - - - -[source,xml] ----- -<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.0" metadata-complete="false"> - <enterprise-beans> - <session> - <ejb-name>Configuration</ejb-name> - <env-entry> - <env-entry-name>org.superbiz.injection.enventry.Configuration/color</env-entry-name> - <env-entry-type>java.lang.String</env-entry-type> - <env-entry-value>orange</env-entry-value> - </env-entry> - <env-entry> - <env-entry-name>org.superbiz.injection.enventry.Configuration/shape</env-entry-name> - <env-entry-type>org.superbiz.injection.enventry.Shape</env-entry-type> - <env-entry-value>TRIANGLE</env-entry-value> - </env-entry> - <env-entry> - <env-entry-name>org.superbiz.injection.enventry.Configuration/strategy</env-entry-name> - <env-entry-type>java.lang.Class</env-entry-type> - <env-entry-value>org.superbiz.injection.enventry.Widget</env-entry-value> - </env-entry> - <env-entry> - <description>The name was explicitly set in the annotation so the classname prefix isn't required</description> - <env-entry-name>date</env-entry-name> - <env-entry-type>java.lang.Long</env-entry-type> - <env-entry-value>123456789</env-entry-value> - </env-entry> - </session> - </enterprise-beans> -</ejb-jar> ----- - - - -=== Using the @Resource 'name' attribute - -Note that `date` was referenced by `name` as: - - -[source,java] ----- -@Resource(name = "date") -private long date; - -When the `@Resource(name)` is used, you do not need to specify the full class name of the bean and can do it briefly like so: - - <env-entry> - <description>The name was explicitly set in the annotation so the classname prefix isn't required</description> - <env-entry-name>date</env-entry-name> - <env-entry-type>java.lang.Long</env-entry-type> - <env-entry-value>123456789</env-entry-value> - </env-entry> - -Conversly, `color` was not referenced by `name` - -@Resource -private String color; - -When something is not referenced by `name` in the `@Resource` annotation a default name is created. The format is essentially this: - -bean.getClass() + "/" + field.getName() - -So the default `name` of the above `color` property ends up being `org.superbiz.injection.enventry.Configuration/color`. This is the name -we must use when we attempt to decalre a value for it in xml. - - <env-entry> - <env-entry-name>org.superbiz.injection.enventry.Configuration/color</env-entry-name> - <env-entry-type>java.lang.String</env-entry-type> - <env-entry-value>orange</env-entry-value> - </env-entry> - -### @Resource and Enum (Enumerations) - -The `shape` field is actually a custom Java Enum type - -package org.superbiz.injection.enventry; - -public enum Shape { - - CIRCLE, - TRIANGLE, - SQUARE -} ----- - - -As of Java EE 6, java.lang.Enum types are allowed as `<env-entry>` items. Declaring one in xml is done using the actual enum's class name like so: - - <env-entry> - <env-entry-name>org.superbiz.injection.enventry.Configuration/shape</env-entry-name> - <env-entry-type>org.superbiz.injection.enventry.Shape</env-entry-type> - <env-entry-value>TRIANGLE</env-entry-value> - </env-entry> - -Do not use `<env-entry-type>java.lang.Enum</env-entry-type>` or it will not work! - -== ConfigurationTest - - -[source,java] ----- -package org.superbiz.injection.enventry; - -import junit.framework.TestCase; - -import javax.ejb.embeddable.EJBContainer; -import javax.naming.Context; -import java.util.Date; - -public class ConfigurationTest extends TestCase { - - - public void test() throws Exception { - final Context context = EJBContainer.createEJBContainer().getContext(); - - final Configuration configuration = (Configuration) context.lookup("java:global/injection-of-env-entry/Configuration"); - - assertEquals("orange", configuration.getColor()); - - assertEquals(Shape.TRIANGLE, configuration.getShape()); - - assertEquals(Widget.class, configuration.getStrategy()); - - assertEquals(new Date(123456789), configuration.getDate()); - } -} ----- - - -= Running - - - -[source] ----- -------------------------------------------------------- - T E S T S -------------------------------------------------------- -Running org.superbiz.injection.enventry.ConfigurationTest -Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 -http://tomee.apache.org/ -INFO - openejb.home = /Users/dblevins/examples/injection-of-env-entry -INFO - openejb.base = /Users/dblevins/examples/injection-of-env-entry -INFO - Using 'javax.ejb.embeddable.EJBContainer=true' -INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) -INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) -INFO - Found EjbModule in classpath: /Users/dblevins/examples/injection-of-env-entry/target/classes -INFO - Beginning load: /Users/dblevins/examples/injection-of-env-entry/target/classes -INFO - Configuring enterprise application: /Users/dblevins/examples/injection-of-env-entry -WARN - Method 'lookup' is not available for 'javax.annotation.Resource'. Probably using an older Runtime. -INFO - Configuring Service(id=Default Singleton Container, type=Container, provider-id=Default Singleton Container) -INFO - Auto-creating a container for bean Configuration: Container(type=SINGLETON, id=Default Singleton Container) -INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) -INFO - Auto-creating a container for bean org.superbiz.injection.enventry.ConfigurationTest: Container(type=MANAGED, id=Default Managed Container) -INFO - Enterprise application "/Users/dblevins/examples/injection-of-env-entry" loaded. -INFO - Assembling app: /Users/dblevins/examples/injection-of-env-entry -INFO - Jndi(name="java:global/injection-of-env-entry/Configuration!org.superbiz.injection.enventry.Configuration") -INFO - Jndi(name="java:global/injection-of-env-entry/Configuration") -INFO - Jndi(name="java:global/EjbModule1355224018/org.superbiz.injection.enventry.ConfigurationTest!org.superbiz.injection.enventry.ConfigurationTest") -INFO - Jndi(name="java:global/EjbModule1355224018/org.superbiz.injection.enventry.ConfigurationTest") -INFO - Created Ejb(deployment-id=org.superbiz.injection.enventry.ConfigurationTest, ejb-name=org.superbiz.injection.enventry.ConfigurationTest, container=Default Managed Container) -INFO - Created Ejb(deployment-id=Configuration, ejb-name=Configuration, container=Default Singleton Container) -INFO - Started Ejb(deployment-id=org.superbiz.injection.enventry.ConfigurationTest, ejb-name=org.superbiz.injection.enventry.ConfigurationTest, container=Default Managed Container) -INFO - Started Ejb(deployment-id=Configuration, ejb-name=Configuration, container=Default Singleton Container) -INFO - Deployed Application(path=/Users/dblevins/examples/injection-of-env-entry) -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.664 sec - -Results : - -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ----- - -
