Guest User

Untitled

a guest
Aug 28th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. Java EE 6: How to add web module on top of application client
  2. public static void main(String[] args) {
  3. EntityManagerFactory emf = Persistence.createEntityManagerFactory("CoreInPU");
  4. EntityManager em = emf.createEntityManager();
  5. EntityDAO entityDAOClient = new EntityDAOClient(em);
  6. Main pgm = new Main();
  7. try {
  8. process(entityDAOClient);
  9. } catch (Exception e) {
  10. logger.fatal("", e);
  11. }finally{
  12. em.close();
  13. emf.close();
  14. }
  15. }
  16.  
  17. public void process(EntityDAO entityDAO){
  18. validatePDF(List<pdfFiles>);
  19. processPDF(List<pdfFiles>, entityDAO);
  20. createPrintJob(List<pdfFiles>, entityDAO);
  21. }
  22.  
  23. public void processPDF(List<pdfFiles>, EntityDAO entityDAO){
  24. for(File file : pdfFiles){
  25. entityDAO.create(file);
  26. }
  27. }
  28.  
  29. public interface EntityDAO {
  30. public <T> T create(T t);
  31. public <T> T find(Class<T> type, Object id);
  32. public List findWithNamedQuery(String queryName);
  33. public List findWithNamedQuery(String queryName, int resultLimit);
  34.  
  35. }
  36.  
  37. public class EntityDAOClient implements EntityDAO {
  38.  
  39. private EntityManager em;
  40.  
  41. private static Logger logger = Logger.getLogger(EntityDAOClient.class);
  42.  
  43. public EntityDAOClient(EntityManager em) {
  44. this.em = em;
  45. }
  46.  
  47. @Override
  48. public <T> T create(T t){
  49. em.getTransaction().begin();
  50. em.persist(t);
  51. em.getTransaction().commit();
  52. return t;
  53. }
  54.  
  55. @Override
  56. public <T> T find(Class<T> type, Object id){
  57. em.getTransaction().begin();
  58. T t = em.find(type, id);
  59. em.getTransaction().commit();
  60. return t;
  61. }
  62. ...
  63. }
  64.  
  65. <persistence-unit name="CoreInPU" transaction-type="RESOURCE_LOCAL">
  66. <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
  67. <class>com.wf.docsys.core.entity.Acknowledgement</class>
  68. <class>com.wf.docsys.core.entity.PackageLog</class>
  69. <properties>
  70. <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/core"/>
  71. <property name="javax.persistence.jdbc.password" value="root"/>
  72. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  73. <property name="javax.persistence.jdbc.user" value="xxxx"/>
  74. <property name="eclipselink.ddl-generation" value="create-tables"/>
  75. </properties>
  76. </persistence-unit>
  77.  
  78. @Stateless
  79. @LocalBean
  80. public class CoreEJB implements EntityDAO{
  81.  
  82. @PersistenceContext(unitName = "CoreInWeb-ejbPU")
  83. private EntityManager em;
  84.  
  85. //@Override
  86. public <T> T create(T t) {
  87. em.persist(t);
  88. return t;
  89. }
  90.  
  91. //@Override
  92. public <T> T find(Class<T> type, Object id) {
  93. return em.find(type, id);
  94. }
  95.  
  96. ...
  97. }
  98.  
  99. @ManagedBean
  100. @RequestScoped
  101. public class Bean {
  102.  
  103. @EJB
  104. private CoreEJB coreEJB;
  105.  
  106.  
  107. public Bean() {
  108.  
  109. }
  110.  
  111. public void runAppClientMainProcess() {
  112. //The web interface can also kick off the same process as the app client
  113. process(coreEJB);
  114. }
  115.  
  116. // ...
  117. }
  118.  
  119. @Stateless
  120. public class SomeEJB {
  121.  
  122. @PersistenceUnit(unitName="someWebPU")
  123. private EntityManagerFactory emf;
  124.  
  125. @PostConstruct
  126. public void init() {
  127. EntityUtil.newInstance(emf);
  128. }
  129.  
  130. public void create(Some some) {
  131. EntityUtil.create(some);
  132. }
  133.  
  134. // ...
  135. }
  136.  
  137. public interface SomeDAO {
  138.  
  139. public void save(Some some);
  140. // ...
  141. }
  142.  
  143. public class SomeDAOClient implements SomeDAO {
  144.  
  145. private EntityManager em;
  146.  
  147. public SomeDAO(EntityManager em) {
  148. this.em = em;
  149. }
  150.  
  151. public void save(Some some) {
  152. em.getTransaction().begin();
  153. em.persist(some);
  154. em.getTransaction().commit();
  155. }
  156.  
  157. // ...
  158. }
  159.  
  160. public static void main(String[] args) throws Exception {
  161. EntityManagerFactory emf = Persistence.createEntityManagerFactory("someClientPU");
  162. EntityManager em = emf.createEntityManager();
  163. SomeDAO someDAO = new SomeDAOClient(em);
  164.  
  165. try {
  166. Some some = new Some();
  167. some.setFoo("foo");
  168. someDAO.save(some);
  169. } finally {
  170. em.close();
  171. emf.close();
  172. }
  173. }
  174.  
  175. @Stateless
  176. public class SomeDAOEJB implements SomeDAO {
  177.  
  178. @PersistenceContext(unitName="someWebPU")
  179. private EntityManager em;
  180.  
  181. public void save(Some some) {
  182. em.persist(some);
  183. }
  184.  
  185. // ...
  186. }
  187.  
  188. @ManagedBean
  189. @RequestScoped
  190. public class Bean {
  191.  
  192. @EJB
  193. private SomeDAO someDAO;
  194. private Some some;
  195.  
  196. public Bean() {
  197. some = new Some();
  198. }
  199.  
  200. public void save() {
  201. someDAO.save(some);
  202. }
  203.  
  204. // ...
  205. }
  206.  
  207. //This class works as is in both javaEE and client.
  208. //There is no need of managedbean in CDI.
  209. //This class can be accessed in jsf using EL #businessProcessor
  210. @Singleton @Named
  211. public class BusinessProcessor
  212. {
  213. private @Inject EntityDAO entityDAO; // CDI will inject an implementation of EntityDao.
  214. //In our case there is only one impl for client as well as JavaEE
  215.  
  216. // CDI will invoke below method on startup. Not used in JavaEE.
  217. // In JavaEE JSF will execute process() directly using Expression Language
  218. public void init(@Observes ContainerInitialized event, @Parameters List parameters){
  219. process(entityDAO);
  220. }
  221.  
  222. public void process(@Inject EntityDAO entityDAO){
  223.     validatePDF(List);
  224.     processPDF(List, entityDAO);
  225.     createPrintJob(List, entityDAO);
  226. }
  227.  
  228. public void processPDF(List, EntityDAO entityDAO){
  229.     for(File file : pdfFiles){
  230.         entityDAO.create(file);
  231.     }
  232. }
  233. }
  234.  
  235. // this class is same for both JavaEE and Client
  236. public class EntityDAOClient implements EntityDAO {
  237.  
  238. private @Inject EntityManager em;
  239.  
  240.     private static Logger logger = Logger.getLogger(EntityDAOClient.class);
  241.    
  242.     @Override
  243.     public T create(T t){
  244.         em.getTransaction().begin();
  245.         em.persist(t);
  246.         em.getTransaction().commit();
  247.         return t;
  248.     }
  249.  
  250.     @Override
  251.     public T find(Class type, Object id){
  252.         em.getTransaction().begin();
  253.         T t = em.find(type, id);
  254.         em.getTransaction().commit();
  255.         return t;
  256.     }
  257.     ...
  258. }
  259.  
  260. java org.jboss.weld.environment.se.StartMain
  261.  
  262. public class Main {
  263.  
  264. @EJB
  265. private static CoreMainEJBRemote coreEJBRemote;
  266.  
  267. private static Logger logger = Logger.getLogger(Main.class);
  268.  
  269. /**
  270. * @param args the command line arguments
  271. */
  272. public static void main(String[] args) {
  273. coreEJBRemote.process(args[0]);
  274. }
  275. }
  276.  
  277. @Stateless
  278. public class CoreMainEJB implements CoreMainEJBRemote, CoreMainEJBLocal {
  279.  
  280. @EJB
  281. private PackageProcessor packageProcessor;
  282.  
  283. @Override
  284. public void process(String configFileName) {
  285. ...
  286. //Process config File
  287. packageProcessor.validatePDF();
  288. packageProcessor.processPDF();
  289. ...
  290. }
  291. }
Add Comment
Please, Sign In to add comment