/* * Copyright 2008 NeoConsult A/S. All rights reserved. * NeoConsult A/S PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.neoconsult.example; import javax.jws.WebService; // snip - lot's of imports here /** * * @author Nikolaj Andresen - NeoConsult A/S */ @WebService(endpointInterface = "com.neoconsult.example.FailedInjectionInterface") public class FailedInjectionWebService implements FailedInjectionInterface { // logging private static final Logger LOG = Logger.getLogger(FailedInjectionWebService.class.getName()); // persistence configuration private static final String PERSISTENCE_UNIT_NAME = "FailedInjectionPU"; @PersistenceUnit(unitName=PERSISTENCE_UNIT_NAME) private EntityManagerFactory emf = null; //////////////////////////////////////////////////////////////////////////// // The following two methods are GlassFish work-arounds, since resource // injection doesn't work properly on web service instances on the official // URL (the one specified in web.xml). //////////////////////////////////////////////////////////////////////////// private boolean initialized = false; @PostConstruct public void init() { if (emf == null) { LOG.warning("Entity Manager Factory failed to be injected - creating one programmatically."); emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); initialized = true; } } @PreDestroy public void destroy() { if (initialized) { LOG.warning("We initialized ourselves - we must clean up also."); emf.close(); } } //////////////////////////////////////////////////////////////////////////// // Web service methods //////////////////////////////////////////////////////////////////////////// public Customer findCustomer(String id) { Customer result = null; EntityManager em = null; try { em = emf.createEntityManager(); if (id != null && id.trim().matches("^\\d+$")) { // customer id search result = em.find(Customer.class, Long.parseLong(id)); } } finally { em.close(); } return result; } }