Skip to content

Instantly share code, notes, and snippets.

@ibaca
Forked from messo/BeanLocator.java
Created March 7, 2012 19:43
Show Gist options
  • Save ibaca/1995494 to your computer and use it in GitHub Desktop.
Save ibaca/1995494 to your computer and use it in GitHub Desktop.
My EJB Bean
public class BeanLocator implements ServiceLocator {
@Override
public Object getInstance(Class<?> clazz) {
System.out.println(clazz);
if (clazz.equals(GroupManager.class)) {
return lookupBean(clazz, "GroupManager");
} else {
return null;
}
}
private static <T> T lookupBean(Class<T> clazz, String name) {
try {
return (T) InitialContext.doLookup("java:global/kfc-frontend/" + name);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
@Entity
@Table(name = "groups")
@NamedQueries({
@NamedQuery(name = Group.retrieveAll, query = "SELECT g FROM Group g"),
@NamedQuery(name = Group.retrieveByToken, query = "SELECT g FROM Group g "
+ "WHERE g.token = :token") })
public class Group {
public static final String retrieveAll = "retrieveAll";
public static final String retrieveByToken = "retrieveByToken";
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "token", unique = true)
private String token;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "organizer")
private List<Program> programs;
@OneToMany(mappedBy = "producer")
private List<Food> foods;
@Version
@Column(name = "version")
private Integer version;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public List<Program> getPrograms() {
return programs;
}
public void setPrograms(List<Program> programs) {
this.programs = programs;
}
public List<Food> getFoods() {
return foods;
}
public void setFoods(List<Food> foods) {
this.foods = foods;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
}
@Stateless
@LocalBean
public class GroupManager extends Locator<Group, Long> {
@PersistenceContext
private EntityManager em;
public Group findGroup(Long id) {
return null;
}
public Group findByToken(String token) {
return em.createNamedQuery(Group.retrieveByToken, Group.class)
.setParameter("token", token).getSingleResult();
}
public List<Group> findGroups() {
return em.createNamedQuery(Group.retrieveAll, Group.class)
.getResultList();
}
@Override
public Group create(Class<? extends Group> clazz) {
return new Group();
}
@Override
public Group find(Class<? extends Group> clazz, Long id) {
System.out.println(em);
System.out.println(clazz);
System.out.println(id);
return em.find(clazz, id);
}
@Override
public Class<Group> getDomainType() {
return Group.class;
}
@Override
public Long getId(Group domainObject) {
return domainObject.getId();
}
@Override
public Class<Long> getIdType() {
return Long.class;
}
@Override
public Object getVersion(Group domainObject) {
return domainObject.getVersion();
}
}
@ProxyFor(value = Group.class, locator = GroupManager.class)
public interface GroupProxy extends EntityProxy {
String getToken();
void setToken(String token);
String getName();
void setName(String name);
List<ProgramProxy> getPrograms();
EntityProxyId<GroupProxy> stableId();
}
@Service(value = GroupManager.class, locator = BeanLocator.class)
public interface GroupRequest extends RequestContext {
Request<GroupProxy> findGroup(Long id);
Request<GroupProxy> findByToken(String groupToken);
Request<List<GroupProxy>> findGroups();
}
@WebServlet("/Test")
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
GroupManager groupManager;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.println(groupManager.find(Group.class, 3L).getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment