dblevins 2005/08/28 04:55:51
Added: examples/moviefun/src/java/org/acme/movie JndiContext.java
JndiContextBean.java Movie.java MovieBean.java
MovieEntity.java MovieHome.java
Log:
Collapsed ear example (aka. one openejb per webapp)
Revision Changes Path
1.1
openejb1/examples/moviefun/src/java/org/acme/movie/JndiContext.java
Index: JndiContext.java
===================================================================
package org.acme.movie;
import javax.ejb.SessionBean;
import javax.ejb.EJBException;
import javax.ejb.SessionContext;
import javax.ejb.EJBLocalHome;
import javax.ejb.CreateException;
import javax.ejb.EJBLocalObject;
import javax.ejb.RemoveException;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingEnumeration;
import javax.naming.NameParser;
import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.Properties;
/**
*
* @author David Blevins <[EMAIL PROTECTED]>
*/
public interface JndiContext extends Context {
public static final String LOCAL_NAME = "ContextEJBLocal";
/**
* Nested EJBLocalHome interface
*/
public interface LocalHome extends EJBLocalHome {
JndiContext.Local create() throws CreateException;
}
/**
* Nested EJBLocalObject Interface
*/
public interface Local extends EJBLocalObject, JndiContext {
}
/**
* Static LocalObject Facade
*/
public static final Local LOCAL = new Local(){
private Local object;
private Local object(){
if (object == null){
object = getObject();
}
return object;
}
private Local getObject() {
try {
Properties p = new Properties(System.getProperties());
p.put("java.naming.factory.initial",
"org.openejb.client.LocalInitialContextFactory");
InitialContext initialContext = new InitialContext(p);
LocalHome home = (LocalHome)
initialContext.lookup(LOCAL_NAME);
return home.create();
} catch (NamingException e) {
throw (IllegalStateException) new
IllegalStateException(LOCAL_NAME + " cannot be retrieved from
JNDI.").initCause(e);
} catch (CreateException e) {
throw (IllegalStateException) new
IllegalStateException("Cannot create session bean "+LOCAL_NAME).initCause(e);
}
}
public EJBLocalHome getEJBLocalHome() throws EJBException {
return getObject().getEJBLocalHome();
}
public java.lang.Object getPrimaryKey() throws EJBException {
return getObject().getPrimaryKey();
}
public boolean isIdentical(EJBLocalObject ejbLocalObject) throws
EJBException {
return getObject().isIdentical(ejbLocalObject);
}
public void remove() throws RemoveException, EJBException {
getObject().remove();
}
public java.lang.Object lookup(Name name) throws NamingException {
return getObject().lookup(name);
}
public java.lang.Object lookup(String name) throws NamingException {
return getObject().lookup(name);
}
public void bind(Name name, java.lang.Object obj) throws
NamingException {
getObject().bind(name, obj);
}
public void bind(String name, java.lang.Object obj) throws
NamingException {
getObject().bind(name, obj);
}
public void rebind(Name name, java.lang.Object obj) throws
NamingException {
getObject().rebind(name, obj);
}
public void rebind(String name, java.lang.Object obj) throws
NamingException {
getObject().rebind(name, obj);
}
public void unbind(Name name) throws NamingException {
getObject().unbind(name);
}
public void unbind(String name) throws NamingException {
getObject().unbind(name);
}
public void rename(Name oldName, Name newName) throws NamingException
{
getObject().rename(oldName, newName);
}
public void rename(String oldName, String newName) throws
NamingException {
getObject().rename(oldName, newName);
}
public NamingEnumeration list(Name name) throws NamingException {
return getObject().list(name);
}
public NamingEnumeration list(String name) throws NamingException {
return getObject().list(name);
}
public NamingEnumeration listBindings(Name name) throws
NamingException {
return getObject().listBindings(name);
}
public NamingEnumeration listBindings(String name) throws
NamingException {
return getObject().listBindings(name);
}
public void destroySubcontext(Name name) throws NamingException {
getObject().destroySubcontext(name);
}
public void destroySubcontext(String name) throws NamingException {
getObject().destroySubcontext(name);
}
public Context createSubcontext(Name name) throws NamingException {
return getObject().createSubcontext(name);
}
public Context createSubcontext(String name) throws NamingException {
return getObject().createSubcontext(name);
}
public java.lang.Object lookupLink(Name name) throws NamingException {
return getObject().lookupLink(name);
}
public java.lang.Object lookupLink(String name) throws
NamingException {
return getObject().lookupLink(name);
}
public NameParser getNameParser(Name name) throws NamingException {
return getObject().getNameParser(name);
}
public NameParser getNameParser(String name) throws NamingException {
return getObject().getNameParser(name);
}
public Name composeName(Name name, Name prefix) throws
NamingException {
return getObject().composeName(name, prefix);
}
public String composeName(String name, String prefix)
throws NamingException {
return getObject().composeName(name, prefix);
}
public java.lang.Object addToEnvironment(String propName,
java.lang.Object propVal)
throws NamingException {
return getObject().addToEnvironment(propName, propVal);
}
public java.lang.Object removeFromEnvironment(String propName)
throws NamingException {
return getObject().removeFromEnvironment(propName);
}
public Hashtable getEnvironment() throws NamingException {
return getObject().getEnvironment();
}
public void close() throws NamingException {
getObject().close();
}
public String getNameInNamespace() throws NamingException {
return getObject().getNameInNamespace();
}
};
}
1.1
openejb1/examples/moviefun/src/java/org/acme/movie/JndiContextBean.java
Index: JndiContextBean.java
===================================================================
package org.acme.movie;
import javax.ejb.SessionBean;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionContext;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.Context;
import javax.naming.NameParser;
import javax.naming.InitialContext;
import java.util.Hashtable;
import java.rmi.RemoteException;
/**
* Nested EJB implementation class
* Just delegates to this bean's java:comp/env namespace
*
* @author David Blevins <[EMAIL PROTECTED]>
*/
public class JndiContextBean implements SessionBean, JndiContext {
public java.lang.Object lookup(Name name) throws NamingException {
return context.lookup(name);
}
public java.lang.Object lookup(String name) throws NamingException {
return context.lookup(name);
}
public void bind(Name name, java.lang.Object obj) throws NamingException {
context.bind(name, obj);
}
public void bind(String name, java.lang.Object obj) throws
NamingException {
context.bind(name, obj);
}
public void rebind(Name name, java.lang.Object obj) throws
NamingException {
context.rebind(name, obj);
}
public void rebind(String name, java.lang.Object obj) throws
NamingException {
context.rebind(name, obj);
}
public void unbind(Name name) throws NamingException {
context.unbind(name);
}
public void unbind(String name) throws NamingException {
context.unbind(name);
}
public void rename(Name oldName, Name newName) throws NamingException {
context.rename(oldName, newName);
}
public void rename(String oldName, String newName) throws NamingException
{
context.rename(oldName, newName);
}
public NamingEnumeration list(Name name) throws NamingException {
return context.list(name);
}
public NamingEnumeration list(String name) throws NamingException {
return context.list(name);
}
public NamingEnumeration listBindings(Name name) throws NamingException {
return context.listBindings(name);
}
public NamingEnumeration listBindings(String name) throws NamingException
{
return context.listBindings(name);
}
public void destroySubcontext(Name name) throws NamingException {
context.destroySubcontext(name);
}
public void destroySubcontext(String name) throws NamingException {
context.destroySubcontext(name);
}
public Context createSubcontext(Name name) throws NamingException {
return context.createSubcontext(name);
}
public Context createSubcontext(String name) throws NamingException {
return context.createSubcontext(name);
}
public java.lang.Object lookupLink(Name name) throws NamingException {
return context.lookupLink(name);
}
public java.lang.Object lookupLink(String name) throws NamingException {
return context.lookupLink(name);
}
public NameParser getNameParser(Name name) throws NamingException {
return context.getNameParser(name);
}
public NameParser getNameParser(String name) throws NamingException {
return context.getNameParser(name);
}
public Name composeName(Name name, Name prefix) throws NamingException {
return context.composeName(name, prefix);
}
public String composeName(String name, String prefix)
throws NamingException {
return context.composeName(name, prefix);
}
public java.lang.Object addToEnvironment(String propName,
java.lang.Object propVal)
throws NamingException {
return context.addToEnvironment(propName, propVal);
}
public java.lang.Object removeFromEnvironment(String propName)
throws NamingException {
return context.removeFromEnvironment(propName);
}
public Hashtable getEnvironment() throws NamingException {
return context.getEnvironment();
}
public void close() throws NamingException {
context.close();
}
public String getNameInNamespace() throws NamingException {
return context.getNameInNamespace();
}
private Context context;
public void ejbCreate() throws CreateException{
try {
context = new InitialContext();
context = (Context) context.lookup("java:comp/env");
} catch (NamingException e) {
throw (CreateException)new CreateException().initCause(e);
}
}
public void ejbActivate() throws EJBException, RemoteException {
}
public void ejbPassivate() throws EJBException, RemoteException {
}
public void ejbRemove() throws EJBException, RemoteException {
}
public void setSessionContext(SessionContext sessionContext) throws
EJBException, RemoteException {
}
}
1.1
openejb1/examples/moviefun/src/java/org/acme/movie/Movie.java
Index: Movie.java
===================================================================
package org.acme.movie;
import java.util.Date;
/**
* Business interface for the Movie EJB
*
* @author David Blevins <[EMAIL PROTECTED]>
*/
public interface Movie {
Integer getMovieId();
String getTitle();
void setTitle(String title);
String getDirector();
void setDirector(String director);
String getGenre();
void setGenre(String genre);
int getRating();
void setRating(int rating);
Date getReleaseDate();
void setReleaseDate(Date releaseDate);
}
1.1
openejb1/examples/moviefun/src/java/org/acme/movie/MovieBean.java
Index: MovieBean.java
===================================================================
package org.acme.movie;
import javax.ejb.CreateException;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import java.util.Date;
/**
* Bean class for the Movie EJB
*
* @author David Blevins <[EMAIL PROTECTED]>
*/
public class MovieBean implements javax.ejb.EntityBean, Movie {
private EntityContext entityContext;
public Integer movieId;
public String title;
public String director;
public String genre;
public int rating;
public Date releaseDate;
public Integer ejbCreate(String title, String director, String genre, int
rating, Date releaseDate) throws CreateException {
this.title = title;
this.director = director;
this.genre = genre;
this.rating = rating;
this.releaseDate = releaseDate;
return null;
}
public Integer getMovieId() {
return movieId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
public Date getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
public void ejbPostCreate(String title, String director, String genre,
int rating, Date releaseDate) throws CreateException {
}
public void ejbActivate() {
}
public void ejbLoad() {
}
public void ejbPassivate() {
}
public void ejbRemove() throws RemoveException {
}
public void ejbStore() {
}
public void setEntityContext(EntityContext entityContext) {
this.entityContext = entityContext;
}
public void unsetEntityContext() {
this.entityContext = null;
}
}
1.1
openejb1/examples/moviefun/src/java/org/acme/movie/MovieEntity.java
Index: MovieEntity.java
===================================================================
package org.acme.movie;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Date;
import java.util.Collection;
import java.util.Properties;
import java.text.SimpleDateFormat;
/**
* This is actually our EJBs Local interface.
*
* Since local interfaces don't declare crazy EJB specific Exceptions
* in the throws clause we will choose to put our method declarations
* in an interface called Movie and make both the Local interface and
* Bean class implement it.
*
* @author David Blevins <[EMAIL PROTECTED]>
*/
public interface MovieEntity extends javax.ejb.EJBLocalObject, Movie {
/** This EJB's JNDI name */
public static final String NAME = "MovieEJBLocal";
public static final SimpleDateFormat DATE_FORMAT = new
SimpleDateFormat("yyyy.MM.dd");
/**
* This is another fun trick. I prefer this to the standard service
* locator pattern. It makes for easy and neat looking entity creation
* and location, such as:
*
* MovieEntity movie = MovieEntity.Home.create(...);
*
* A home interface is essentially static, so why not really
* make it static.
*
* The easiest way to implement the MovieHome interface for this class
* declare the variable "private MovieHome home;" then use IntelliJ or
* Eclipse's "Delegate Methods" support.
*/
public static final MovieHome Home = new MovieHome(){
private MovieHome home;
private MovieHome home(){
if (home == null){
home = lookup();
}
return home;
}
private MovieHome lookup() {
try {
Properties p = new Properties(System.getProperties());
p.put("java.naming.factory.initial",
"org.openejb.client.LocalInitialContextFactory");
InitialContext initialContext = new InitialContext(p);
return (MovieHome) initialContext.lookup(NAME);
} catch (NamingException e) {
throw (IllegalStateException) new IllegalStateException(NAME
+ " cannot be retrieved from JNDI.").initCause(e);
}
}
public MovieEntity create(String title, String director, String
genre, int rating, Date releaseDate) throws CreateException {
return home().create(title, director, genre, rating, releaseDate);
}
public Collection findByTitle(String title) throws FinderException {
return home().findByTitle(title);
}
public Collection findByDirector(String director) throws
FinderException {
return home().findByDirector(director);
}
public Collection findByGenre(String genre) throws FinderException {
return home().findByGenre(genre);
}
public MovieEntity findByPrimaryKey(Integer key) throws
FinderException {
return home().findByPrimaryKey(key);
}
public Collection findAllMovies() throws FinderException {
return home().findAllMovies();
}
public void remove(Object o) throws RemoveException, EJBException {
home().remove(o);
}
};
}
1.1
openejb1/examples/moviefun/src/java/org/acme/movie/MovieHome.java
Index: MovieHome.java
===================================================================
package org.acme.movie;
import javax.ejb.FinderException;
import javax.ejb.CreateException;
import java.util.Collection;
import java.util.Date;
/**
* EJB Local Home interface for the Movie EJB
*
* @author David Blevins <[EMAIL PROTECTED]>
*/
public interface MovieHome extends javax.ejb.EJBLocalHome {
public MovieEntity create(String title, String director, String genre,
int rating, Date releaseDate) throws CreateException;
public Collection findByTitle(String title) throws FinderException;
public Collection findByDirector(String director) throws FinderException;
public Collection findByGenre(String genre) throws FinderException;
public MovieEntity findByPrimaryKey(Integer key) throws FinderException;
public Collection findAllMovies() throws FinderException;
}